#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define pii pair<int, int>
typedef long long ll;
using namespace std;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int OFF = (1 << 20);
int n, q, koji = 0, vri, w;
int a[200005];
int veci[200005];
int gdje[400005];
stack< pair<int, int> > st;
set< pair< pair<int, int>, pair<int, int> > > s;
set< pair< pair<int, int>, pair<int, int> > > :: iterator it;
vector< pair< pair<int, int>, pair<int, int> > > v;
vector< pair<int, int> > kad[200005];
int sol[1000005];
int t[2*OFF+5];
void update(int x, int y) {
x += OFF;
t[x] = y;
x /= 2;
while (x > 0) {
t[x] = t[x*2] + t[x*2+1];
x /= 2;
}
}
void ispis() {
int pot = 2;
for (int i = 1; i < 2*OFF; i++) {
if (i == pot) {
cout << endl;
pot *= 2;
}
cout << t[i] << " ";
}
cout << endl;
}
int query(int x, int y) {
if (x >= OFF) {
int prva = v[x-OFF].Y.X;
return a[prva+y-1];
}
if (t[x*2] >= y) return query(x*2, y);
else return query(x*2+1, y-t[x*2]);
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
veci[i] = n;
}
for (int i = n-1; i >= 0; i--) {
while (st.size() > 0 && st.top().X <= a[i]) {
st.pop();
}
if (st.size() > 0) veci[i] = st.top().Y;
st.push({a[i], i});
}
int pos = 0;
while (pos < n) {
s.insert({{a[pos], koji}, {pos, veci[pos]-1}});
v.push_back({{a[pos], koji}, {pos, veci[pos]-1}});
koji++;
pos = veci[pos];
}
int d = n;
for (int i = 0; i < n; i++) {
while (1) {
it = s.end();
it--;
if (d+(*it).Y.X-(*it).Y.Y-1 < n/2) break;
else {
d = d+(*it).Y.X-(*it).Y.Y-1;
s.erase(it);
}
}
//cout << i << ": " << d << endl;
if (d > n/2) {
it = s.end();
it--;
pos = (*it).Y.X;
int kr = (*it).Y.Y;
d -= (kr-pos+1);
s.erase(it);
int dod = n/2 - d;
s.insert({{a[pos], koji}, {pos, pos+dod-1}});
v.push_back({{a[pos], koji}, {pos, pos+dod-1}});
d += dod;
//cout << "novi: " << pos << " " << pos+dod-1 << endl;
koji++;
pos += dod;
while (pos <= kr) {
s.insert({{a[pos], koji}, {pos, min(veci[pos]-1, kr)}});
v.push_back({{a[pos], koji}, {pos, min(veci[pos]-1, kr)}});
d += min(veci[pos]-1, kr) - pos + 1;
//cout << "novi: " << pos << " " << min(veci[pos]-1, kr) << endl;
koji++;
pos = veci[pos];
}
}
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) {
gdje[v[i].X.Y] = i;
//cout << v[i].Y.X << " " << v[i].Y.Y << " | " << v[i].X.X << endl;
}
// gotov precompute
s.clear();
pos = 0;
koji = 0;
while (pos < n) {
s.insert({{a[pos], koji}, {pos, veci[pos]-1}});
update(gdje[koji], veci[pos]-pos);
//ispis();
koji++;
pos = veci[pos];
}
for (int i = 0; i < q; i++) {
cin >> vri >> w;
kad[min(n, vri)].push_back({w, i});
}
d = n;
for (int i = 0; i <= n; i++) {
//ispis();
for (int j = 0; j < (int)kad[i].size(); j++) {
int trenrj = query(1, kad[i][j].X);
sol[kad[i][j].Y] = trenrj;
}
if (i == n) break;
while (1) {
it = s.end();
it--;
if (d+(*it).Y.X-(*it).Y.Y-1 < n/2) break;
else {
d = d+(*it).Y.X-(*it).Y.Y-1;
//update(gdje[(*it).X.Y], 0);
s.erase(it);
}
}
if (d > n/2) {
it = s.end();
it--;
pos = (*it).Y.X;
int kr = (*it).Y.Y;
d -= (kr-pos+1);
update(gdje[(*it).X.Y], 0);
s.erase(it);
int dod = n/2 - d;
s.insert({{a[pos], koji}, {pos, pos+dod-1}});
update(gdje[koji], dod);
d += dod;
koji++;
pos += dod;
while (pos <= kr) {
s.insert({{a[pos], koji}, {pos, min(veci[pos]-1, kr)}});
update(gdje[koji], min(veci[pos]-1, kr)-pos+1);
d += min(veci[pos]-1, kr) - pos + 1;
koji++;
pos = veci[pos];
}
}
}
for (int i = 0; i < q; i++) cout << sol[i] << "\n";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:130:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
23172 KB |
Output is correct |
2 |
Correct |
254 ms |
21940 KB |
Output is correct |
3 |
Correct |
247 ms |
21680 KB |
Output is correct |
4 |
Correct |
201 ms |
21372 KB |
Output is correct |
5 |
Correct |
227 ms |
23344 KB |
Output is correct |
6 |
Correct |
199 ms |
24156 KB |
Output is correct |
7 |
Correct |
229 ms |
24652 KB |
Output is correct |
8 |
Correct |
204 ms |
22692 KB |
Output is correct |
9 |
Correct |
208 ms |
21784 KB |
Output is correct |
10 |
Correct |
212 ms |
21280 KB |
Output is correct |
11 |
Correct |
204 ms |
21452 KB |
Output is correct |
12 |
Correct |
199 ms |
20056 KB |
Output is correct |
13 |
Correct |
210 ms |
21100 KB |
Output is correct |
14 |
Correct |
215 ms |
23244 KB |
Output is correct |
15 |
Correct |
217 ms |
21292 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
234 ms |
20332 KB |
Output is correct |
18 |
Correct |
193 ms |
20140 KB |
Output is correct |
19 |
Correct |
3 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
436 ms |
46068 KB |
Output is correct |
2 |
Correct |
417 ms |
55112 KB |
Output is correct |
3 |
Correct |
399 ms |
46256 KB |
Output is correct |
4 |
Correct |
289 ms |
35752 KB |
Output is correct |
5 |
Correct |
291 ms |
36816 KB |
Output is correct |
6 |
Correct |
281 ms |
37444 KB |
Output is correct |
7 |
Correct |
322 ms |
36148 KB |
Output is correct |
8 |
Correct |
310 ms |
38516 KB |
Output is correct |
9 |
Correct |
272 ms |
34448 KB |
Output is correct |
10 |
Correct |
278 ms |
33780 KB |
Output is correct |
11 |
Correct |
241 ms |
27540 KB |
Output is correct |
12 |
Correct |
228 ms |
27968 KB |
Output is correct |
13 |
Correct |
264 ms |
33008 KB |
Output is correct |
14 |
Correct |
251 ms |
31768 KB |
Output is correct |
15 |
Correct |
278 ms |
36396 KB |
Output is correct |
16 |
Correct |
23 ms |
7844 KB |
Output is correct |
17 |
Correct |
356 ms |
49760 KB |
Output is correct |
18 |
Correct |
204 ms |
25684 KB |
Output is correct |
19 |
Correct |
68 ms |
12472 KB |
Output is correct |
20 |
Correct |
86 ms |
13656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
17464 KB |
Output is correct |
2 |
Correct |
111 ms |
17048 KB |
Output is correct |
3 |
Correct |
124 ms |
15832 KB |
Output is correct |
4 |
Correct |
52 ms |
9244 KB |
Output is correct |
5 |
Correct |
101 ms |
13856 KB |
Output is correct |
6 |
Correct |
58 ms |
9700 KB |
Output is correct |
7 |
Correct |
77 ms |
12096 KB |
Output is correct |
8 |
Correct |
66 ms |
10756 KB |
Output is correct |
9 |
Correct |
89 ms |
12244 KB |
Output is correct |
10 |
Correct |
38 ms |
8084 KB |
Output is correct |
11 |
Correct |
44 ms |
8436 KB |
Output is correct |
12 |
Correct |
39 ms |
8140 KB |
Output is correct |
13 |
Correct |
45 ms |
8192 KB |
Output is correct |
14 |
Correct |
48 ms |
8268 KB |
Output is correct |
15 |
Correct |
36 ms |
7992 KB |
Output is correct |
16 |
Correct |
13 ms |
6100 KB |
Output is correct |
17 |
Correct |
86 ms |
17964 KB |
Output is correct |
18 |
Correct |
32 ms |
7628 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
23172 KB |
Output is correct |
2 |
Correct |
254 ms |
21940 KB |
Output is correct |
3 |
Correct |
247 ms |
21680 KB |
Output is correct |
4 |
Correct |
201 ms |
21372 KB |
Output is correct |
5 |
Correct |
227 ms |
23344 KB |
Output is correct |
6 |
Correct |
199 ms |
24156 KB |
Output is correct |
7 |
Correct |
229 ms |
24652 KB |
Output is correct |
8 |
Correct |
204 ms |
22692 KB |
Output is correct |
9 |
Correct |
208 ms |
21784 KB |
Output is correct |
10 |
Correct |
212 ms |
21280 KB |
Output is correct |
11 |
Correct |
204 ms |
21452 KB |
Output is correct |
12 |
Correct |
199 ms |
20056 KB |
Output is correct |
13 |
Correct |
210 ms |
21100 KB |
Output is correct |
14 |
Correct |
215 ms |
23244 KB |
Output is correct |
15 |
Correct |
217 ms |
21292 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
234 ms |
20332 KB |
Output is correct |
18 |
Correct |
193 ms |
20140 KB |
Output is correct |
19 |
Correct |
3 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
436 ms |
46068 KB |
Output is correct |
22 |
Correct |
417 ms |
55112 KB |
Output is correct |
23 |
Correct |
399 ms |
46256 KB |
Output is correct |
24 |
Correct |
289 ms |
35752 KB |
Output is correct |
25 |
Correct |
291 ms |
36816 KB |
Output is correct |
26 |
Correct |
281 ms |
37444 KB |
Output is correct |
27 |
Correct |
322 ms |
36148 KB |
Output is correct |
28 |
Correct |
310 ms |
38516 KB |
Output is correct |
29 |
Correct |
272 ms |
34448 KB |
Output is correct |
30 |
Correct |
278 ms |
33780 KB |
Output is correct |
31 |
Correct |
241 ms |
27540 KB |
Output is correct |
32 |
Correct |
228 ms |
27968 KB |
Output is correct |
33 |
Correct |
264 ms |
33008 KB |
Output is correct |
34 |
Correct |
251 ms |
31768 KB |
Output is correct |
35 |
Correct |
278 ms |
36396 KB |
Output is correct |
36 |
Correct |
23 ms |
7844 KB |
Output is correct |
37 |
Correct |
356 ms |
49760 KB |
Output is correct |
38 |
Correct |
204 ms |
25684 KB |
Output is correct |
39 |
Correct |
68 ms |
12472 KB |
Output is correct |
40 |
Correct |
86 ms |
13656 KB |
Output is correct |
41 |
Correct |
123 ms |
17464 KB |
Output is correct |
42 |
Correct |
111 ms |
17048 KB |
Output is correct |
43 |
Correct |
124 ms |
15832 KB |
Output is correct |
44 |
Correct |
52 ms |
9244 KB |
Output is correct |
45 |
Correct |
101 ms |
13856 KB |
Output is correct |
46 |
Correct |
58 ms |
9700 KB |
Output is correct |
47 |
Correct |
77 ms |
12096 KB |
Output is correct |
48 |
Correct |
66 ms |
10756 KB |
Output is correct |
49 |
Correct |
89 ms |
12244 KB |
Output is correct |
50 |
Correct |
38 ms |
8084 KB |
Output is correct |
51 |
Correct |
44 ms |
8436 KB |
Output is correct |
52 |
Correct |
39 ms |
8140 KB |
Output is correct |
53 |
Correct |
45 ms |
8192 KB |
Output is correct |
54 |
Correct |
48 ms |
8268 KB |
Output is correct |
55 |
Correct |
36 ms |
7992 KB |
Output is correct |
56 |
Correct |
13 ms |
6100 KB |
Output is correct |
57 |
Correct |
86 ms |
17964 KB |
Output is correct |
58 |
Correct |
32 ms |
7628 KB |
Output is correct |
59 |
Correct |
2 ms |
5076 KB |
Output is correct |
60 |
Correct |
2 ms |
5076 KB |
Output is correct |
61 |
Correct |
754 ms |
58688 KB |
Output is correct |
62 |
Correct |
598 ms |
58544 KB |
Output is correct |
63 |
Correct |
691 ms |
53300 KB |
Output is correct |
64 |
Correct |
453 ms |
42936 KB |
Output is correct |
65 |
Correct |
483 ms |
46476 KB |
Output is correct |
66 |
Correct |
454 ms |
43584 KB |
Output is correct |
67 |
Correct |
386 ms |
39616 KB |
Output is correct |
68 |
Correct |
388 ms |
40264 KB |
Output is correct |
69 |
Correct |
429 ms |
41712 KB |
Output is correct |
70 |
Correct |
344 ms |
36636 KB |
Output is correct |
71 |
Correct |
293 ms |
37892 KB |
Output is correct |
72 |
Correct |
352 ms |
35012 KB |
Output is correct |
73 |
Correct |
312 ms |
35828 KB |
Output is correct |
74 |
Correct |
357 ms |
38580 KB |
Output is correct |
75 |
Correct |
338 ms |
35840 KB |
Output is correct |
76 |
Correct |
23 ms |
7248 KB |
Output is correct |
77 |
Correct |
460 ms |
48840 KB |
Output is correct |
78 |
Correct |
271 ms |
31136 KB |
Output is correct |
79 |
Correct |
3 ms |
5076 KB |
Output is correct |
80 |
Correct |
3 ms |
5076 KB |
Output is correct |