#include <bits/stdc++.h>
using namespace std;
#define DEBUG(a) cout << #a << ": " << a << '\n';
using ll = long long;
struct lazy_segtree {
using T = ll;
using U = ll;
T value_identity = 0ll;
U operation_identity = 0ll;
U compose(U a, U b) {
return a + b;
}
T apply(T a, U b, int n) {
return a + b * n;
}
T combine(T a, T b) {
return a + b;
}
int size;
vector<T> tree;
vector<U> operations;
lazy_segtree(int n) {
size = 1;
while (size < n) size *= 2;
tree.assign(2 * size, value_identity);
operations.assign(2 * size, operation_identity);
}
void propagate(int x, int lx, int rx) {
if (rx - lx == 1) {
return;
}
int m = (lx + rx) / 2;
operations[2 * x + 1] = compose(operations[2 * x + 1], operations[x]);
operations[2 * x + 2] = compose(operations[2 * x + 2], operations[x]);
tree[2 * x + 1] = apply(tree[2 * x + 1], operations[x], m - lx);
tree[2 * x + 2] = apply(tree[2 * x + 2], operations[x], rx - m);
operations[x] = operation_identity;
}
void modify(int l, int r, int v, int x, int lx, int rx) {
propagate(x, lx, rx);
if (lx >= r || rx <= l) return;
if (l <= lx && rx <= r) {
operations[x] = compose(operations[x], v);
tree[x] = apply(tree[x], v, rx - lx);
return;
}
int m = (lx + rx) / 2;
modify(l, r, v, 2 * x + 1, lx, m);
modify(l, r, v, 2 * x + 2, m, rx);
tree[x] = combine(tree[2 * x + 1], tree[2 * x + 2]);
}
void modify(int l, int r, int v) {
modify(l, r, v, 0, 0, size);
}
T query(int l, int r, int x, int lx, int rx) {
propagate(x, lx, rx);
if (lx >= r || rx <= l) return value_identity;
if (l <= lx && rx <= r) {
return tree[x];
}
int m = (lx + rx) / 2;
T a = query(l, r, 2 * x + 1, lx, m);
T b = query(l, r, 2 * x + 2, m, rx);
return combine(a, b);
}
T query(int l, int r) {
return query(l, r, 0, 0, size);
}
T query(int i, int x, int lx, int rx) {
propagate(x, lx, rx);
if (rx - lx == 1) {
return tree[x];
}
int m = (lx + rx) / 2;
if (i < m) {
return query(i, 2 * x + 1, lx, m);
} else {
return query(i, 2 * x + 2, m, rx);
}
}
T query(int i) {
return query(i, 0, 0, size);
}
void build(vector<T>& v, int x, int lx, int rx) {
if (rx - lx == 1) {
if (lx < v.size()) {
tree[x] = v[lx];
}
return;
}
int m = (lx + rx) / 2;
build(v, 2 * x + 1, lx, m);
build(v, 2 * x + 2, m, rx);
tree[x] = combine(tree[2 * x + 1], tree[2 * x + 2]);
}
void build(vector<T>& v) {
build(v, 0, 0, size);
}
};
// O(log^2n)
int lower_bnd(lazy_segtree& st, int x, int n) {
int l = -1, r = n;
while (r > l + 1) {
int mid = (r + l) / 2;
if (st.query(mid) >= x) {
r = mid;
} else {
l = mid;
}
}
return r;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<ll> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
lazy_segtree st(n);
sort(v.begin(), v.end());
st.build(v);
for (int i = 0; i < m; ++i) {
char q;
cin >> q;
if (q == 'F') {
int c, h;
cin >> c >> h;
int first = lower_bnd(st, h, n);
//DEBUG(first);
if (first == n) continue;
int last = min(first + c - 1, n - 1);
//DEBUG(last);
if (last < n - 1 && st.query(last) == st.query(last + 1)) {
int last_v = st.query(last);
int last_of_last = lower_bnd(st, last_v + 1, n);
//DEBUG(last_of_last);
int first_of_last = lower_bnd(st, last_v, n);
//DEBUG(first_of_last);
st.modify(first, first_of_last, 1);
int x = c - abs(first_of_last - first);
//DEBUG(x);
st.modify(last_of_last - x, last_of_last, 1);
} else {
st.modify(first, last + 1, 1);
}
} else {
int mn, mx;
cin >> mn >> mx;
int l = lower_bnd(st, mn, n);
int r = lower_bnd(st, mx + 1, n);
//DEBUG(l);
// DEBUG(r);
cout << r - l << '\n';
}
}
}
Compilation message
grow.cpp: In member function 'void lazy_segtree::build(std::vector<long long int>&, int, int, int)':
grow.cpp:102:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | if (lx < v.size()) {
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
319 ms |
6480 KB |
Output is correct |
2 |
Correct |
338 ms |
7136 KB |
Output is correct |
3 |
Correct |
223 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
142 ms |
1736 KB |
Output is correct |
6 |
Correct |
190 ms |
2052 KB |
Output is correct |
7 |
Correct |
12 ms |
600 KB |
Output is correct |
8 |
Correct |
152 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
2004 KB |
Output is correct |
2 |
Correct |
182 ms |
1976 KB |
Output is correct |
3 |
Correct |
4 ms |
600 KB |
Output is correct |
4 |
Correct |
165 ms |
1668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
2164 KB |
Output is correct |
2 |
Correct |
201 ms |
2084 KB |
Output is correct |
3 |
Correct |
18 ms |
860 KB |
Output is correct |
4 |
Correct |
193 ms |
2188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
4036 KB |
Output is correct |
2 |
Correct |
387 ms |
6512 KB |
Output is correct |
3 |
Correct |
49 ms |
1884 KB |
Output is correct |
4 |
Correct |
186 ms |
6532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
6484 KB |
Output is correct |
2 |
Correct |
376 ms |
6484 KB |
Output is correct |
3 |
Correct |
226 ms |
6736 KB |
Output is correct |
4 |
Correct |
49 ms |
1984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
366 ms |
6220 KB |
Output is correct |
2 |
Correct |
327 ms |
6744 KB |
Output is correct |
3 |
Correct |
239 ms |
6736 KB |
Output is correct |
4 |
Correct |
48 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
6844 KB |
Output is correct |
2 |
Correct |
388 ms |
6292 KB |
Output is correct |
3 |
Correct |
48 ms |
6064 KB |
Output is correct |
4 |
Correct |
492 ms |
6440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
6740 KB |
Output is correct |
2 |
Correct |
338 ms |
6936 KB |
Output is correct |
3 |
Correct |
393 ms |
6992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
324 ms |
7508 KB |
Output is correct |