#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#include <stack>
#define pb push_back
#define ff first
#define ss second
using namespace std;
const int MX = 1e6 + 5;
int n, q, l, r, cur;
int p[MX], ans[MX], fix[MX], nxt[MX];
pair<pair<int, int>, int> t[MX];
set<pair<pair<int, int>, int> > s;
void pre() {
while((*s.rbegin()).ff.ss + r <= n / 2) {
for(int i = 0; i < (*s.rbegin()).ff.ss; i++) {
fix[n - r - (*s.rbegin()).ff.ss + i] = p[(*s.rbegin()).ss + i];
}
r += (*s.rbegin()).ff.ss;
s.erase(*s.rbegin());
}
}
void go() {
if(r == n / 2) return;
pair<pair<int, int>, int> x = *s.rbegin();
s.erase(*s.rbegin());
s.insert({{x.ff.ff, x.ff.ss + r - n / 2}, x.ss});
int ind = x.ss + x.ff.ss + r - n / 2;
while(ind < x.ss + x.ff.ss) {
s.insert({{p[ind], min(nxt[ind], x.ss + x.ff.ss) - ind}, ind});
ind = nxt[ind];
}
pre();
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
memset(fix, -1, sizeof(fix));
cin >> n >> q;
stack<pair<int, int > > st;
for(int i = 0; i <= n; i++) {
if (i != n) {
cin >> p[i];
}
else {
p[i] = INT_MAX;
}
while(!st.empty() && st.top().ff < p[i]) {
nxt[st.top().ss] = i;
st.pop();
}
st.push({p[i], i});
}
while(l < n) {
s.insert({{p[l], nxt[l] - l}, l});
l = nxt[l];
}
pre();
for(int i = 0; i < q; i++) {
cin >> t[i].ff.ff >> t[i].ff.ss;
t[i].ff.ss--;
t[i].ss = i;
if (t[i].ff.ff > n) t[i].ff.ff = n;
}
sort(t, t + q);
for(int i = 0; i <= n; i++) {
if(t[cur].ff.ff > i) {
go();
continue;
}
int cnt = 0;
auto itr = s.begin();
while(cur < q && t[cur].ff.ff == i) {
if(fix[t[cur].ff.ss] != -1) {
ans[t[cur].ss] = fix[t[cur].ff.ss];
cur++;
continue;
}
while(itr != s.end() && cnt + (*itr).ff.ss <= t[cur].ff.ss) {
cnt += (*itr).ff.ss;
itr++;
}
ans[t[cur].ss] = p[(*itr).ss + t[cur].ff.ss - cnt];
cur++;
}
go();
}
for(int i = 0; i < q; i++) {
cout << ans[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
267 ms |
24204 KB |
Output is correct |
2 |
Correct |
269 ms |
27744 KB |
Output is correct |
3 |
Correct |
272 ms |
27036 KB |
Output is correct |
4 |
Correct |
279 ms |
26872 KB |
Output is correct |
5 |
Correct |
269 ms |
27600 KB |
Output is correct |
6 |
Correct |
255 ms |
26764 KB |
Output is correct |
7 |
Correct |
268 ms |
27724 KB |
Output is correct |
8 |
Correct |
271 ms |
27008 KB |
Output is correct |
9 |
Correct |
254 ms |
26780 KB |
Output is correct |
10 |
Correct |
260 ms |
27096 KB |
Output is correct |
11 |
Correct |
263 ms |
27056 KB |
Output is correct |
12 |
Correct |
256 ms |
26836 KB |
Output is correct |
13 |
Correct |
265 ms |
27224 KB |
Output is correct |
14 |
Correct |
265 ms |
27344 KB |
Output is correct |
15 |
Correct |
269 ms |
27716 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
242 ms |
27196 KB |
Output is correct |
18 |
Correct |
258 ms |
26980 KB |
Output is correct |
19 |
Correct |
2 ms |
4252 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
34656 KB |
Output is correct |
2 |
Correct |
291 ms |
37384 KB |
Output is correct |
3 |
Correct |
283 ms |
29096 KB |
Output is correct |
4 |
Correct |
233 ms |
26228 KB |
Output is correct |
5 |
Correct |
248 ms |
27104 KB |
Output is correct |
6 |
Correct |
229 ms |
26044 KB |
Output is correct |
7 |
Correct |
249 ms |
29524 KB |
Output is correct |
8 |
Correct |
249 ms |
41912 KB |
Output is correct |
9 |
Correct |
230 ms |
37068 KB |
Output is correct |
10 |
Correct |
243 ms |
39052 KB |
Output is correct |
11 |
Correct |
213 ms |
32716 KB |
Output is correct |
12 |
Correct |
221 ms |
33036 KB |
Output is correct |
13 |
Correct |
228 ms |
38056 KB |
Output is correct |
14 |
Correct |
252 ms |
33700 KB |
Output is correct |
15 |
Correct |
244 ms |
39720 KB |
Output is correct |
16 |
Correct |
18 ms |
7076 KB |
Output is correct |
17 |
Correct |
259 ms |
48136 KB |
Output is correct |
18 |
Correct |
230 ms |
31908 KB |
Output is correct |
19 |
Correct |
52 ms |
13008 KB |
Output is correct |
20 |
Correct |
59 ms |
14592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3042 ms |
9612 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
267 ms |
24204 KB |
Output is correct |
2 |
Correct |
269 ms |
27744 KB |
Output is correct |
3 |
Correct |
272 ms |
27036 KB |
Output is correct |
4 |
Correct |
279 ms |
26872 KB |
Output is correct |
5 |
Correct |
269 ms |
27600 KB |
Output is correct |
6 |
Correct |
255 ms |
26764 KB |
Output is correct |
7 |
Correct |
268 ms |
27724 KB |
Output is correct |
8 |
Correct |
271 ms |
27008 KB |
Output is correct |
9 |
Correct |
254 ms |
26780 KB |
Output is correct |
10 |
Correct |
260 ms |
27096 KB |
Output is correct |
11 |
Correct |
263 ms |
27056 KB |
Output is correct |
12 |
Correct |
256 ms |
26836 KB |
Output is correct |
13 |
Correct |
265 ms |
27224 KB |
Output is correct |
14 |
Correct |
265 ms |
27344 KB |
Output is correct |
15 |
Correct |
269 ms |
27716 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
242 ms |
27196 KB |
Output is correct |
18 |
Correct |
258 ms |
26980 KB |
Output is correct |
19 |
Correct |
2 ms |
4252 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
21 |
Correct |
305 ms |
34656 KB |
Output is correct |
22 |
Correct |
291 ms |
37384 KB |
Output is correct |
23 |
Correct |
283 ms |
29096 KB |
Output is correct |
24 |
Correct |
233 ms |
26228 KB |
Output is correct |
25 |
Correct |
248 ms |
27104 KB |
Output is correct |
26 |
Correct |
229 ms |
26044 KB |
Output is correct |
27 |
Correct |
249 ms |
29524 KB |
Output is correct |
28 |
Correct |
249 ms |
41912 KB |
Output is correct |
29 |
Correct |
230 ms |
37068 KB |
Output is correct |
30 |
Correct |
243 ms |
39052 KB |
Output is correct |
31 |
Correct |
213 ms |
32716 KB |
Output is correct |
32 |
Correct |
221 ms |
33036 KB |
Output is correct |
33 |
Correct |
228 ms |
38056 KB |
Output is correct |
34 |
Correct |
252 ms |
33700 KB |
Output is correct |
35 |
Correct |
244 ms |
39720 KB |
Output is correct |
36 |
Correct |
18 ms |
7076 KB |
Output is correct |
37 |
Correct |
259 ms |
48136 KB |
Output is correct |
38 |
Correct |
230 ms |
31908 KB |
Output is correct |
39 |
Correct |
52 ms |
13008 KB |
Output is correct |
40 |
Correct |
59 ms |
14592 KB |
Output is correct |
41 |
Execution timed out |
3042 ms |
9612 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |