#include <bits/stdc++.h>
using namespace std;
template<int sz> struct DS{
int seg[sz * 2];
void upd(int i, int v){
seg[i += sz] = v;
for (i /= 2; i; i /= 2) seg[i] = max(seg[i * 2], seg[i * 2 + 1]);
}
int qry(int l, int r){
int ans = 0;
for (l += sz, r += sz; l <= r; r /= 2, l /= 2){
if (l % 2 == 1) ans = max(ans, seg[l++]);
if (r % 2 == 0) ans = max(seg[r--], ans);
}
return ans;
}
int walk(int v){
int i = 1;
while (i < sz) i = (seg[i * 2] < v) ? i * 2 + 1 : i * 2;
return i - sz - 1;
}
};
const int mN = 2.5e5 + 5, mQ = 5e5 + 5;
int n, a, q; pair<int, int> tmp[mN]; DS<(1 << 19)> L, R;
deque<int> w10; int net = mN;
void ins(int pos, int v){
if (pos < a) L.upd(a - pos, v);
if (pos > a) R.upd(pos - a, v);
}
int main(){
cin >> n >> a;
for (int i = 1; i <= n; i++) cin >> tmp[i].first, tmp[i].second = i;
sort(tmp + 1, tmp + n + 1);
for (int i = 1; i <= n; i++){
int pos = tmp[i].second, r = i;
if (i >= n - 9) r += mQ * 2, w10.push_back(pos);
ins(pos, r);
}
cin >> q; L.upd(a, 1e9); R.upd(n - a + 1, 1e9);
while (q--){
int pos; char type; cin >> type >> pos;
if (type == 'E'){
int e; cin >> e;
auto it = find(w10.begin(), w10.end(), pos);
if (it != w10.end()) w10.erase(it);
w10.insert(w10.end() - e + 1, pos);
if (w10.size() > 10) ins(w10[0], ++net), w10.pop_front();
for (int i = 0; i < w10.size(); i++) ins(w10[i], mQ * 2 + i);
}
else{
int ans = pos;
if (pos < a) ans = R.walk(L.qry(1, a - pos)) + a;
if (pos > a) ans = a - L.walk(R.qry(1, pos - a));
cout<<abs(pos - ans)<<"\n";
}
}
}
Compilation message
cake.cpp: In function 'int main()':
cake.cpp:54:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < w10.size(); i++) ins(w10[i], mQ * 2 + i);
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
7 ms |
420 KB |
Output is correct |
5 |
Correct |
17 ms |
564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
510 ms |
460 KB |
Output is correct |
2 |
Correct |
505 ms |
460 KB |
Output is correct |
3 |
Correct |
514 ms |
544 KB |
Output is correct |
4 |
Correct |
661 ms |
460 KB |
Output is correct |
5 |
Correct |
550 ms |
716 KB |
Output is correct |
6 |
Correct |
580 ms |
716 KB |
Output is correct |
7 |
Correct |
515 ms |
716 KB |
Output is correct |
8 |
Correct |
708 ms |
668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
2500 KB |
Output is correct |
2 |
Correct |
206 ms |
2344 KB |
Output is correct |
3 |
Correct |
203 ms |
2356 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
266 ms |
4852 KB |
Output is correct |
6 |
Correct |
266 ms |
4872 KB |
Output is correct |
7 |
Correct |
249 ms |
4724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
560 KB |
Output is correct |
2 |
Correct |
71 ms |
504 KB |
Output is correct |
3 |
Correct |
122 ms |
1336 KB |
Output is correct |
4 |
Correct |
122 ms |
1364 KB |
Output is correct |
5 |
Correct |
197 ms |
736 KB |
Output is correct |
6 |
Correct |
237 ms |
1800 KB |
Output is correct |
7 |
Correct |
267 ms |
968 KB |
Output is correct |
8 |
Correct |
244 ms |
1932 KB |
Output is correct |
9 |
Correct |
825 ms |
5960 KB |
Output is correct |
10 |
Correct |
663 ms |
1520 KB |
Output is correct |
11 |
Correct |
686 ms |
2112 KB |
Output is correct |
12 |
Correct |
797 ms |
5332 KB |
Output is correct |
13 |
Correct |
787 ms |
6040 KB |
Output is correct |