이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1e5 + 5;
int a[MAXN];
struct SegTree {
int Size;
vector<int> tree, lazy;
SegTree(int N) {
Size = 1;
while (Size < N)
Size <<= 1;
tree.resize(Size << 1);
lazy.resize(Size << 1);
}
void build(int x, int lx, int rx) {
if (lx == rx) {
tree[x] = a[lx];
return;
}
int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
build(lSon, lx, mid);
build(rSon, mid + 1, rx);
tree[x] = max(tree[lSon], tree[rSon]);
}
void push(int x) {
if (lazy[x] == 0)
return;
for (int i = 0; i < 2; ++i) {
int son = x << 1 | i;
tree[son] += lazy[x];
lazy[son] += lazy[x];
}
lazy[x] = 0;
}
void update(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
++tree[x], ++lazy[x];
return;
}
push(x);
int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
if (st <= mid)
update(lSon, lx, mid, st, dr);
if (mid < dr)
update(rSon, mid + 1, rx, st, dr);
tree[x] = max(tree[lSon], tree[rSon]);
}
int lower(int x, int lx, int rx, int val) {
if (lx == rx)
return lx;
push(x);
int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
if (tree[lSon] >= val)
return lower(lSon, lx, mid, val);
return lower(rSon, mid + 1, rx, val);
}
int upper(int x, int lx, int rx, int val) {
if (lx == rx)
return lx;
push(x);
int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
if (tree[lSon] > val)
return upper(lSon, lx, mid, val);
return upper(rSon, mid + 1, rx, val);
}
int get_val(int x, int lx, int rx, int poz) {
if (lx == rx)
return tree[x];
push(x);
int mid = (lx + rx) >> 1;
if (poz <= mid)
return get_val(x << 1, lx, mid, poz);
return get_val(x << 1 | 1, mid + 1, rx, poz);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int N, Q;
cin >> N >> Q;
for (int i = 1; i <= N; ++i)
cin >> a[i];
sort(a + 1, a + N + 1);
a[++N] = INF;
SegTree tree(N);
tree.build(1, 1, N);
for (int q = 0; q < Q; ++q) {
char t;
int x, y;
cin >> t >> x >> y;
if (t == 'F') {
int first_poz = tree.lower(1, 1, N, y);
if (first_poz == N)
continue;
int last_poz = first_poz + x - 1;
if (last_poz >= N) {
tree.update(1, 1, N, first_poz, N - 1);
continue;
}
int val = tree.get_val(1, 1, N, last_poz);
int last_poz_update = tree.lower(1, 1, N, val) - 1;
if (first_poz <= last_poz_update)
tree.update(1, 1, N, first_poz, last_poz_update);
int rem = last_poz - last_poz_update;
if (rem == 0)
continue;
last_poz_update = tree.upper(1, 1, N, val) - 1;
tree.update(1, 1, N, last_poz_update - rem + 1, last_poz_update);
} else cout << tree.upper(1, 1, N, y) - tree.lower(1, 1, N, x) << '\n';
}
return 0;
}
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |