From 6be7e3a6c4db88da6670a7966ae3b63524c4b18b Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 2 Jun 2022 10:42:28 +0800 Subject: [PATCH] =?UTF-8?q?P2709=20=E5=B0=8FB=E7=9A=84=E8=AF=A2=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/76780844 --- Luogu/P2709/P2709.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Luogu/P2709/P2709.cpp diff --git a/Luogu/P2709/P2709.cpp b/Luogu/P2709/P2709.cpp new file mode 100644 index 00000000..af64baec --- /dev/null +++ b/Luogu/P2709/P2709.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 5e4 + 5; + +int n, m, k, a[N], c[N], t, ans[N]; +std::vector> q; + +inline void add(int x, int &res) { + res += 2 * c[x] + 1; + c[x]++; +} + +inline void del(int x, int &res) { + res -= 2 * c[x] - 1; + c[x]--; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m >> k; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + t = sqrt(n); + + 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 + ? (p & 1) + ? std::get<2>(a) < std::get<2>(b) + : std::get<2>(a) > std::get<2>(b) + : p < q; + }); + + int i = 0, j = 1, res = 0, id, l, r; + for (auto &e : q) { + std::tie(id, l, r) = e; + + while (i < r) add(a[++i], res); + while (i > r) del(a[i--], res); + while (j < l) del(a[j++], res); + while (j > l) add(a[--j], res); + + ans[id] = res; + } + + for (int i = 1; i <= m; i++) { + cout << ans[i] << endl; + } + + return 0; +}