#include <bits/stdc++.h>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e18 + 7;
const ll weirdMod = 998244353;
struct node {
int mn, mx;
} t[400040];
int lazy[400040], n, m;
void prop(int v, int tl, int tr) {
t[v].mn += lazy[v];
t[v].mx += lazy[v];
if (tl != tr) {
lazy[2 * v] += lazy[v];
lazy[2 * v + 1] += lazy[v];
} lazy[v] = 0;
}
void set_val(int v, int tl, int tr, int pos, int val) {
if (tl == tr) {
t[v].mx = t[v].mn = val;
return;
} int tm = (tl + tr) / 2;
if (pos <= tm)
set_val(2 * v, tl, tm, pos, val);
else set_val(2 * v + 1, tm + 1, tr, pos, val);
t[v].mn = min(t[2 * v].mn, t[2 * v + 1].mn);
t[v].mx = max(t[2 * v].mx, t[2 * v + 1].mx);
}
int get_val(int v, int tl, int tr, int pos) {
if (lazy[v])
prop(v, tl, tr);
if (tl == tr)
return t[v].mn;
int tm = (tl + tr) / 2;
if (pos <= tm)
return get_val(2 * v, tl, tm, pos);
else return get_val(2 * v + 1, tm + 1, tr, pos);
t[v].mn = min(t[2 * v].mn, t[2 * v + 1].mn);
t[v].mx = max(t[2 * v].mx, t[2 * v + 1].mx);
}
void add_one(int v, int tl, int tr, int l, int r) {
if (lazy[v])
prop(v, tl, tr);
if (tr < l || r < tl)
return;
if (l <= tl && tr <= r) {
t[v].mn++; t[v].mx++;
if (tl != tr) {
lazy[2 * v]++;
lazy[2 * v + 1]++;
} return;
} int tm = (tl + tr) / 2;
add_one(2 * v, tl, tm, l, min(r, tm));
add_one(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
t[v].mn = min(t[2 * v].mn, t[2 * v + 1].mn);
t[v].mx = max(t[2 * v].mx, t[2 * v + 1].mx);
}
int find_first(int v, int tl, int tr, int val) {
if (lazy[v]) prop(v, tl, tr);
if (t[v].mx < val) return tr + 1;
if (tl == tr) return tl;
int tm = (tl + tr) / 2;
int pos = find_first(2 * v, tl, tm, val);
if (pos == tm + 1)
return find_first(2 * v + 1, tm + 1, tr, val);
else return pos;
}
int find_last(int v, int tl, int tr, int val) {
if (lazy[v]) prop(v, tl, tr);
if (val < t[v].mn) return tl - 1;
if (tl == tr) return tl;
int tm = (tl + tr) / 2;
int pos = find_last(2 * v + 1, tm + 1, tr, val);
if (pos == tm)
return find_last(2 * v, tl, tm, val);
else return pos;
}
void upd_query(int c, int h) {
int pos = find_first(1, 1, n, h);
if (pos > n) return;
int num = get_val(1, 1, n, min(n, pos + c - 1));
int l = find_first(1, 1, n, num), r = find_last(1, 1, n, num); c -= l - pos;
add_one(1, 1, n, pos, l - 1); add_one(1, 1, n, max(l, r - c + 1), r);
}
void ans_query(int l, int r) {
l = find_first(1, 1, n, l);
r = find_last(1, 1, n, r);
cout << r - l + 1 << '\n';
}
void solve() {
cin >> n >> m;
vector<int> vec(n);
for (auto &v : vec) cin >> v;
sort(vec.begin(), vec.end());
for (int i = 1; i <= n; i++)
set_val(1, 1, n, i, vec[i - 1]);
char v; int l, r;
for (int i = 1; i <= m; i++) {
cin >> v >> l >> r;
if (v == 'F') upd_query(l, r);
else ans_query(l, r);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
} return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
3872 KB |
Output is correct |
2 |
Correct |
107 ms |
5408 KB |
Output is correct |
3 |
Correct |
94 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
38 ms |
684 KB |
Output is correct |
6 |
Correct |
47 ms |
868 KB |
Output is correct |
7 |
Correct |
5 ms |
596 KB |
Output is correct |
8 |
Correct |
21 ms |
1264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
792 KB |
Output is correct |
2 |
Correct |
49 ms |
1940 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
25 ms |
1392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
908 KB |
Output is correct |
2 |
Correct |
49 ms |
1856 KB |
Output is correct |
3 |
Correct |
9 ms |
596 KB |
Output is correct |
4 |
Correct |
45 ms |
1840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
2200 KB |
Output is correct |
2 |
Correct |
99 ms |
4984 KB |
Output is correct |
3 |
Correct |
17 ms |
1556 KB |
Output is correct |
4 |
Correct |
76 ms |
5084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
3748 KB |
Output is correct |
2 |
Correct |
103 ms |
5136 KB |
Output is correct |
3 |
Correct |
88 ms |
5316 KB |
Output is correct |
4 |
Correct |
14 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
3848 KB |
Output is correct |
2 |
Correct |
68 ms |
5012 KB |
Output is correct |
3 |
Correct |
80 ms |
5376 KB |
Output is correct |
4 |
Correct |
14 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
3864 KB |
Output is correct |
2 |
Correct |
101 ms |
3820 KB |
Output is correct |
3 |
Correct |
33 ms |
4516 KB |
Output is correct |
4 |
Correct |
55 ms |
4888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
3904 KB |
Output is correct |
2 |
Correct |
98 ms |
5444 KB |
Output is correct |
3 |
Correct |
136 ms |
5568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
4204 KB |
Output is correct |