#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5, S = 320;
int n, q;
int a[N], pos[N], L[S], R[S], cnt[N], cntB[S], fre[N];
long long X[N], res[N];
vector<int> ind[N];
long long qry() {
for (int i = pos[n]; i >= pos[0]; --i) {
if (cntB[i]) {
for (int j = R[i]; j >= L[i]; --j) {
if (cnt[j]) {
return X[j];
}
}
}
}
assert(0);
return -1;
}
void upd(int p, int i) {
cnt[p] += i;
cntB[pos[p]] += i;
}
void add(int i, int j) {
int v = a[i];
upd(ind[v][fre[v]], -1);
fre[v] += j;
upd(ind[v][fre[v]], 1);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> q;
vector<int> comp;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
comp.push_back(a[i]);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 1; i <= n; ++i) {
a[i] = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin();
++fre[a[i]];
}
vector<tuple<long long, int, int>> cands;
for (int i = 0; i < comp.size(); ++i) {
ind[i].resize(fre[i] + 1);
long long val = 0;
for (int j = 1; j <= fre[i]; ++j) {
val += comp[i];
cands.push_back({val, i, j});
}
fre[i] = 0;
}
sort(cands.begin(), cands.end());
for (int i = 0; i < n; ++i) {
auto [x, u, v] = cands[i];
X[i + 1] = x;
ind[u][v] = i + 1;
}
for (int i = 0; i <= n; ++i) {
pos[i] = i / S;
R[pos[i]] = i;
}
for (int i = n; i >= 0; --i) {
L[pos[i]] = i;
}
for (int i = 0; i < comp.size(); ++i) {
upd(ind[i][0], 1);
}
vector<array<int, 3>> queries;
for (int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
queries.push_back({l, r, i});
}
sort(queries.begin(), queries.end(), [&](const auto &a, const auto &b) {
return a[0] / S == b[0] / S ? a[1] < b[1] : a[0] < b[0];
});
int L = 1, R = 0;
for (auto [l, r, id] : queries) {
while (R < r) {
add(++R, 1);
}
while (L > l) {
add(--L, 1);
}
while (R > r) {
add(R--, -1);
}
while (L < l) {
add(L++, -1);
}
res[id] = qry();
}
for (int i = 1; i <= q; ++i) {
cout << res[i] << "\n";
}
return 0;
}
Compilation message
historic.cpp: In function 'int main()':
historic.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < comp.size(); ++i) {
| ~~^~~~~~~~~~~~~
historic.cpp:82:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int i = 0; i < comp.size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
0 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4952 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4740 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4736 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
0 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4952 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4740 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4736 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
5 ms |
5000 KB |
Output is correct |
22 |
Correct |
5 ms |
5136 KB |
Output is correct |
23 |
Correct |
5 ms |
4956 KB |
Output is correct |
24 |
Correct |
5 ms |
4956 KB |
Output is correct |
25 |
Correct |
5 ms |
5212 KB |
Output is correct |
26 |
Correct |
5 ms |
5212 KB |
Output is correct |
27 |
Correct |
5 ms |
5212 KB |
Output is correct |
28 |
Correct |
6 ms |
5512 KB |
Output is correct |
29 |
Correct |
5 ms |
5212 KB |
Output is correct |
30 |
Correct |
5 ms |
5212 KB |
Output is correct |
31 |
Correct |
5 ms |
5208 KB |
Output is correct |
32 |
Correct |
7 ms |
5212 KB |
Output is correct |
33 |
Correct |
6 ms |
5256 KB |
Output is correct |
34 |
Correct |
6 ms |
5212 KB |
Output is correct |
35 |
Correct |
5 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
5384 KB |
Output is correct |
9 |
Correct |
7 ms |
5996 KB |
Output is correct |
10 |
Correct |
7 ms |
6744 KB |
Output is correct |
11 |
Correct |
46 ms |
12444 KB |
Output is correct |
12 |
Correct |
18 ms |
8852 KB |
Output is correct |
13 |
Correct |
31 ms |
9876 KB |
Output is correct |
14 |
Correct |
46 ms |
12492 KB |
Output is correct |
15 |
Correct |
68 ms |
15168 KB |
Output is correct |
16 |
Correct |
37 ms |
14200 KB |
Output is correct |
17 |
Correct |
25 ms |
9616 KB |
Output is correct |
18 |
Correct |
37 ms |
12680 KB |
Output is correct |
19 |
Correct |
41 ms |
16012 KB |
Output is correct |
20 |
Correct |
21 ms |
12744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
0 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4952 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4740 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4736 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
5 ms |
5000 KB |
Output is correct |
22 |
Correct |
5 ms |
5136 KB |
Output is correct |
23 |
Correct |
5 ms |
4956 KB |
Output is correct |
24 |
Correct |
5 ms |
4956 KB |
Output is correct |
25 |
Correct |
5 ms |
5212 KB |
Output is correct |
26 |
Correct |
5 ms |
5212 KB |
Output is correct |
27 |
Correct |
5 ms |
5212 KB |
Output is correct |
28 |
Correct |
6 ms |
5512 KB |
Output is correct |
29 |
Correct |
5 ms |
5212 KB |
Output is correct |
30 |
Correct |
5 ms |
5212 KB |
Output is correct |
31 |
Correct |
5 ms |
5208 KB |
Output is correct |
32 |
Correct |
7 ms |
5212 KB |
Output is correct |
33 |
Correct |
6 ms |
5256 KB |
Output is correct |
34 |
Correct |
6 ms |
5212 KB |
Output is correct |
35 |
Correct |
5 ms |
5212 KB |
Output is correct |
36 |
Correct |
1 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
4700 KB |
Output is correct |
38 |
Correct |
1 ms |
4700 KB |
Output is correct |
39 |
Correct |
1 ms |
4700 KB |
Output is correct |
40 |
Correct |
2 ms |
4700 KB |
Output is correct |
41 |
Correct |
1 ms |
4956 KB |
Output is correct |
42 |
Correct |
2 ms |
4956 KB |
Output is correct |
43 |
Correct |
3 ms |
5384 KB |
Output is correct |
44 |
Correct |
7 ms |
5996 KB |
Output is correct |
45 |
Correct |
7 ms |
6744 KB |
Output is correct |
46 |
Correct |
46 ms |
12444 KB |
Output is correct |
47 |
Correct |
18 ms |
8852 KB |
Output is correct |
48 |
Correct |
31 ms |
9876 KB |
Output is correct |
49 |
Correct |
46 ms |
12492 KB |
Output is correct |
50 |
Correct |
68 ms |
15168 KB |
Output is correct |
51 |
Correct |
37 ms |
14200 KB |
Output is correct |
52 |
Correct |
25 ms |
9616 KB |
Output is correct |
53 |
Correct |
37 ms |
12680 KB |
Output is correct |
54 |
Correct |
41 ms |
16012 KB |
Output is correct |
55 |
Correct |
21 ms |
12744 KB |
Output is correct |
56 |
Correct |
11 ms |
5548 KB |
Output is correct |
57 |
Correct |
25 ms |
6456 KB |
Output is correct |
58 |
Correct |
53 ms |
7448 KB |
Output is correct |
59 |
Correct |
96 ms |
8780 KB |
Output is correct |
60 |
Correct |
132 ms |
9548 KB |
Output is correct |
61 |
Correct |
167 ms |
10108 KB |
Output is correct |
62 |
Correct |
159 ms |
11656 KB |
Output is correct |
63 |
Correct |
173 ms |
12764 KB |
Output is correct |
64 |
Correct |
182 ms |
13444 KB |
Output is correct |
65 |
Correct |
289 ms |
13740 KB |
Output is correct |
66 |
Correct |
217 ms |
13752 KB |
Output is correct |
67 |
Correct |
302 ms |
13964 KB |
Output is correct |
68 |
Correct |
457 ms |
14028 KB |
Output is correct |
69 |
Correct |
460 ms |
14804 KB |
Output is correct |
70 |
Correct |
463 ms |
15564 KB |
Output is correct |
71 |
Correct |
457 ms |
15052 KB |
Output is correct |
72 |
Correct |
460 ms |
15008 KB |
Output is correct |
73 |
Correct |
477 ms |
14912 KB |
Output is correct |
74 |
Correct |
467 ms |
14792 KB |
Output is correct |
75 |
Correct |
488 ms |
14748 KB |
Output is correct |
76 |
Correct |
454 ms |
14792 KB |
Output is correct |
77 |
Correct |
449 ms |
14780 KB |
Output is correct |
78 |
Correct |
471 ms |
14540 KB |
Output is correct |
79 |
Correct |
462 ms |
14624 KB |
Output is correct |
80 |
Correct |
161 ms |
14464 KB |
Output is correct |