From 6735b9e648049ba606a0e3841861522d8f29fb01 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 2 Jun 2022 20:21:31 +0800 Subject: [PATCH] =?UTF-8?q?2492.=20HH=E7=9A=84=E9=A1=B9=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/14548427/ --- AcWing/2492/2492.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 AcWing/2492/2492.cpp diff --git a/AcWing/2492/2492.cpp b/AcWing/2492/2492.cpp new file mode 100644 index 00000000..f96cbc1c --- /dev/null +++ b/AcWing/2492/2492.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 50005, + M = 200005, + S = 1e6 + 5; + +int n, m, a[N], c[S], t, ans[M]; +std::vector> q; + +inline int get(int x) { + return x / t; +} + +inline void add(int x, int& r) { + if (!c[x]++) r++; +} + +inline void del(int x, int& r) { + if (!--c[x]) r--; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + t = sqrt(n); + + cin >> m; + + 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 = get(std::get<1>(a)), + q = get(std::get<1>(b)); + + return p == q ? std::get<2>(a) < std::get<2>(b) : p < q; + }); + + for (int k = 0, i = 0, j = 1, res = 0, id, l, r; k < q.size(); k++) { + std::tie(id, l, r) = q[k]; + + 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; +}