#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44
using namespace std;
const int MAXN = 250000;
int arr[MAXN + 1];
struct Data {
int pos;
bool operator <(const Data &other) const {
return arr[pos] > arr[other.pos];
}
};
multiset <Data> mst;
struct SegTree {
vector < pair <int, int> > aint;
int n;
SegTree(int _n) {
n = _n;
aint.resize(4 * n + 1);
for(int i = 1; i <= 4 * n; i++) {
aint[i] = {0, i};
}
}
inline void refresh(int nod) {
if(aint[2 * nod].first > aint[2 * nod + 1].first) {
aint[nod] = aint[2 * nod];
}
else {
aint[nod] = aint[2 * nod + 1];
}
}
void build(int nod, int left, int right) {
if(left == right) {
aint[nod] = {arr[left], left};
}
else {
int mid = (left + right) / 2;
build(2 * nod, left, mid);
build(2 * nod + 1, mid + 1, right);
refresh(nod);
}
}
void update(int nod, int left, int right, int pos, int val) {
if(left == right) {
aint[nod] = {val, left};
}
else {
int mid = (left + right) / 2;
if(pos <= mid) update(2 * nod, left, mid, pos, val);
else update(2 * nod + 1, mid + 1, right, pos, val);
refresh(nod);
}
}
pair <int, int> query(int nod, int left, int right, int l, int r) {
if(l <= left && right <= r) {
return aint[nod];
}
else {
int mid = (left + right) / 2;
pair <int, int> a, b;
a = b = {0, 0};
if(l <= mid) a = query(2 * nod, left, mid, l, r);
if(mid < r) b = query(2 * nod + 1, mid + 1, right, l, r);
if(a.first < b.first) {
return b;
}
return a;
}
}
int get_left(int nod, int left, int right, int pos, int val) {
if(left > pos) {
return 0;
}
if(right <= pos) {
if(aint[nod].first > val) {
return aint[nod].second;
}
return 0;
}
else {
int mid = (left + right) / 2;
int a = get_left(2 * nod, left, mid, pos, val), b = get_left(2 * nod + 1, mid + 1, right, pos, val);
return max(a, b);
}
}
int get_right(int nod, int left, int right, int pos, int val) {
if(right < pos) {
return n + 1;
}
if(left >= pos) {
if(aint[nod].first > val) {
return aint[nod].second;
}
return n + 1;
}
else {
int mid = (left + right) / 2;
int a = get_right(2 * nod, left, mid, pos, val), b = get_right(2 * nod + 1, mid + 1, right, pos, val);
return min(a, b);
}
}
};
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, n, q, a;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> a;
for(i = 1; i <= n; i++) {
cin >> arr[i];
mst.insert({i});
}
SegTree st(n);
st.build(1, 1, n);
cin >> q;
while(q > 0) {
q--;
int pos, e;
char ch;
cin >> ch;
if(ch == 'F') {
cin >> pos;
int ans = 0;
if(a < pos) {
ans = pos - st.get_left(1, 1, n, a - 1, st.query(1, 1, n, a + 1, pos).first) - 1;
}
else if(a > pos) {
ans = st.get_right(1, 1, n, a + 1, st.query(1, 1, n, pos, a - 1).first) - pos - 1;
}
else if(a == pos) {
ans = 0;
}
cout << ans << "\n";
}
else {
cin >> pos >> e;
auto it = mst.begin();
int last = arr[it -> pos] + 1;
for(i = 1; i < e; i++) {
int cur = it -> pos;
it++;
last = arr[cur];
mst.erase(mst.find({cur}));
arr[cur]++;
mst.insert({cur});
}
mst.erase(mst.find({pos}));
arr[pos] = last;
mst.insert({pos});
it = mst.begin();
for(i = 0; i < e; i++) {
st.update(1, 1, n, it -> pos, arr[it -> pos]);
it++;
}
}
}
//cin.close();
//cout.close();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
543 ms |
5496 KB |
Output isn't correct |
2 |
Correct |
270 ms |
5572 KB |
Output is correct |
3 |
Incorrect |
372 ms |
5628 KB |
Output isn't correct |
4 |
Correct |
169 ms |
5600 KB |
Output is correct |
5 |
Incorrect |
662 ms |
7128 KB |
Output isn't correct |
6 |
Incorrect |
535 ms |
7672 KB |
Output isn't correct |
7 |
Incorrect |
452 ms |
7404 KB |
Output isn't correct |
8 |
Correct |
213 ms |
7692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
134 ms |
10540 KB |
Output isn't correct |
2 |
Incorrect |
75 ms |
10360 KB |
Output isn't correct |
3 |
Incorrect |
70 ms |
10360 KB |
Output isn't correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Incorrect |
277 ms |
24032 KB |
Output isn't correct |
6 |
Incorrect |
291 ms |
24184 KB |
Output isn't correct |
7 |
Correct |
130 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
1016 KB |
Output isn't correct |
2 |
Incorrect |
41 ms |
1272 KB |
Output isn't correct |
3 |
Incorrect |
123 ms |
5724 KB |
Output isn't correct |
4 |
Incorrect |
155 ms |
5624 KB |
Output isn't correct |
5 |
Incorrect |
149 ms |
1912 KB |
Output isn't correct |
6 |
Incorrect |
241 ms |
7860 KB |
Output isn't correct |
7 |
Incorrect |
162 ms |
3344 KB |
Output isn't correct |
8 |
Incorrect |
287 ms |
11128 KB |
Output isn't correct |
9 |
Incorrect |
1574 ms |
29028 KB |
Output isn't correct |
10 |
Incorrect |
491 ms |
5368 KB |
Output isn't correct |
11 |
Incorrect |
749 ms |
7672 KB |
Output isn't correct |
12 |
Incorrect |
1404 ms |
24440 KB |
Output isn't correct |
13 |
Incorrect |
923 ms |
28920 KB |
Output isn't correct |