From b04f707972b104d98464f92be57045581050c365 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 2 Jun 2022 08:08:22 +0800 Subject: [PATCH] =?UTF-8?q?P1494=20[=E5=9B=BD=E5=AE=B6=E9=9B=86=E8=AE=AD?= =?UTF-8?q?=E9=98=9F]=20=E5=B0=8F=20Z=20=E7=9A=84=E8=A2=9C=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/76778088 --- Luogu/P1494/P1494.cpp | 77 ++++++++++++++++++++++++++++++++++++ Luogu/P1494/data/P1494_7.in | 3 ++ Luogu/P1494/data/P1494_7.out | 3 ++ 3 files changed, 83 insertions(+) create mode 100644 Luogu/P1494/P1494.cpp create mode 100644 Luogu/P1494/data/P1494_7.in create mode 100644 Luogu/P1494/data/P1494_7.out diff --git a/Luogu/P1494/P1494.cpp b/Luogu/P1494/P1494.cpp new file mode 100644 index 00000000..93ff48de --- /dev/null +++ b/Luogu/P1494/P1494.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 50005; + +int n, m, a[N], c[N], t; +std::pair now, ans[N]; +std::vector> q; + +void add(int x) { + if (++c[x] > 1) { + now.first += c[x] * (c[x] - 1) - (c[x] - 1) * (c[x] - 2); + } +} + +void del(int x) { + if (--c[x]) { + now.first += c[x] * (c[x] - 1) - (c[x] + 1) * c[x]; + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + t = sqrt(n); + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + for (int i = 1, l, r; i <= m; i++) { + cin >> l >> r; + + q.push_back(std::make_tuple(i, l, r)); + } + + std::sort(q.begin(), q.end(), [&](auto a, auto b) { + int p = std::get<1>(a) / t, + q = std::get<1>(b) / t; + + return p == q ? std::get<2>(a) < std::get<2>(b) : p < q; + }); + + int i = 0, j = 1, x, l, r; + for (auto e : q) { + std::tie(x, l, r) = e; + + if (l == r) { + ans[x] = std::make_pair(0, 1); + } else { + while (i < r) add(a[++i]); + while (i > r) del(a[i--]); + while (j < l) del(a[j++]); + while (j > l) add(a[--j]); + + now.second = 1ll * (r - l + 1) * (r - l); + int g = std::__gcd(now.first, now.second); + ans[x] = std::make_pair(now.first / g, now.second / g); + } + } + + for (int i = 1; i <= m; i++) { + cout << ans[i].first << '/' << ans[i].second << endl; + } + + return 0; +} diff --git a/Luogu/P1494/data/P1494_7.in b/Luogu/P1494/data/P1494_7.in new file mode 100644 index 00000000..af5ffd9d --- /dev/null +++ b/Luogu/P1494/data/P1494_7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4d181cea3303fc0f9de0265a36c5212c320f19cd429c6a73dd691ef4dfc3fcc8 +size 926771 diff --git a/Luogu/P1494/data/P1494_7.out b/Luogu/P1494/data/P1494_7.out new file mode 100644 index 00000000..6c0b3abb --- /dev/null +++ b/Luogu/P1494/data/P1494_7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c1bc439ee745eec48236714f53edc802cc9bedb27c4862f52dba6f7e3ce1bafd +size 663310