# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
93391 | RezwanArefin01 | Cake (CEOI14_cake) | C++17 | 249 ms | 9592 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
///usr/bin/g++ -O2 $0 -o ${0%.cpp} && echo "----------" && ./${0%.cpp}; exit;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int N = 2e5 + 10;
struct tree {
vector<int> bit;
int N, lg;
void init(int n) {
N = n; lg = log2(N) + 1;
bit = vector<int> (n + 1, 0);
}
void update(int x, int v) {
for(; x <= N; x += x & -x)
bit[x] = max(bit[x], v);
}
int query(int x) { int ret = 0;
for(; x > 0; x -= x & -x)
ret = max(ret, bit[x]);
return ret;
}
int get(int X) {
int pos = 0, cur = 0;
for(int i = lg; i >= 0; i--) if(pos + (1 << i) <= N) {
if(max(cur, bit[pos + (1 << i)]) <= X) {
cur = max(cur, bit[pos + (1 << i)]);
pos |= 1 << i;
}
} return pos;
}
} t1, t2;
int n, a, d[N], p[20];
int main() {
scanf("%d %d", &n, &a);
for(int i = 1; i <= n; i++) {
scanf("%d", &d[i]);
if(d[i] > n - 10)
p[n - d[i] + 1] = i;
}
int n1 = a, n2 = n - a + 1;
t1.init(n1); t2.init(n2);
auto upd = [&](int i, int x) {
if(i <= a) t1.update(a - i + 1, x);
if(i >= a) t2.update(i - a + 1, x);
};
for(int i = 1; i <= n; i++) upd(i, d[i]);
int q; scanf("%d", &q); while(q--) {
char c; int x, e;
scanf(" %c", &c);
if(c == 'E') {
scanf("%d %d", &x, &e);
d[x] = d[p[e]];
for(int i = 10 ; i > e; i--)
p[i] = p[i - 1];
p[e] = x;
for(int i = e; i >= 1; i--)
upd(p[i], ++d[p[i]]);
} else {
scanf("%d", &x);
int X;
if(x == a) { puts("0"); continue; }
if(x < a) X = t2.get(t1.query(a - x + 1));
else X = t1.get(t2.query(x - a + 1));
printf("%d\n", abs(x - a) + X - 1);
}
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |