//thatsramen
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define pi pair<int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)
using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;
//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ld PI = 3.14159265359;
//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set
void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
unsyncIO(); setPrec();
if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}
// #define TEST_CASES
struct SegmentTree {
struct Node {
ll val, modi;
};
vector<Node> tre;
const ll NO_OPERATION = 0;
const ll NEUTRAL = 0;
int sz;
SegmentTree(vector<int> &a) {
int n = (int) a.size();
sz = 1;
while (sz < n) sz *= 2;
tre.assign(2 * sz, {NEUTRAL, NO_OPERATION});
build(a, 1, 0, sz);
}
SegmentTree(int n) {
sz = 1;
while (sz < n) sz *= 2;
tre.assign(2 * sz, {NEUTRAL, NO_OPERATION});
}
ll op_modify(ll x, ll y) {
return x + y;
}
ll op_modify2(ll x, ll y, int lx, int rx) {
return x + (rx - lx) * y;
}
ll op_calc(ll x, ll y) {
return x + y;
}
void propagate(int x, int lx, int rx) {
if (rx - lx == 1 || tre[x].modi == NO_OPERATION) return;
int m = (lx + rx) / 2;
tre[2 * x].modi = op_modify(tre[2 * x].modi, tre[x].modi);
tre[2 * x].val = op_modify2(tre[2 * x].val, tre[x].modi, lx, m);
tre[2 * x + 1].modi = op_modify(tre[2 * x + 1].modi, tre[x].modi);
tre[2 * x + 1].val = op_modify2(tre[2 * x + 1].val, tre[x].modi, m, rx);
tre[x].modi = NO_OPERATION;
}
void build(vector<int> &a, int x, int lx, int rx) {
if (rx - lx == 1) {
if (lx < a.size()) {
tre[x].val = a[lx];
}
return;
}
int m = (lx + rx) / 2;
build(a, 2 * x, lx, m);
build(a, 2 * x + 1, m, rx);
tre[x].val = op_calc(tre[2 * x].val, tre[2 * x + 1].val);
}
void set(int l, int r, int v, int x, int lx, int rx) {
propagate(x, lx, rx);
if (rx <= l || lx >= r) {
return;
}
if (l <= lx && r >= rx) {
if (tre[x].modi == NO_OPERATION) {
tre[x].modi = v;
} else {
tre[x].modi = op_modify(tre[x].modi, v);
}
tre[x].val = op_modify2(tre[x].val, v, lx, rx);
return;
}
int m = (lx + rx) / 2;
set(l, r, v, 2 * x, lx, m);
set(l, r, v, 2 * x + 1, m, rx);
tre[x].val = op_calc(tre[2 * x].val, tre[2 * x + 1].val);
}
void set(int l, int r, int v) {
set(l, r, v, 1, 0, sz);
}
ll get(int l, int r, int x, int lx, int rx) {
propagate(x, lx, rx);
if (rx <= l || lx >= r) {
return NEUTRAL;
}
if (l <= lx && r >= rx) {
return tre[x].val;
}
int m = (lx + rx) / 2;
ll s1 = get(l, r, 2 * x, lx, m);
ll s2 = get(l, r, 2 * x + 1, m, rx);
return op_calc(s1, s2);
}
ll get(int l, int r) {
return get(l, r, 1, 0, sz);
}
};
void solve() {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(all(a));
SegmentTree st(a);
dbg(a);
for (int it = 0; it < q; it++) {
char op; cin >> op;
if (op == 'F') {
int x, h;
cin >> x >> h;
int l = -1;
{
int lo = 0, hi = n - 1;
while (lo <= hi) {
int mi = (lo + hi) / 2;
if (st.get(mi, mi + 1) >= h) {
l = mi;
hi = mi - 1;
} else {
lo = mi + 1;
}
}
}
if (l == -1) continue;
int r = min(l + x, n) - 1;
if (r == n - 1) {
st.set(l, r + 1, 1);
} else {
int val = st.get(r, r + 1);
int left, right;
{
int lo = 0, hi = n - 1;
while (lo <= hi) {
int mi = (lo + hi) / 2;
if (st.get(mi, mi + 1) <= val) {
right = mi;
lo = mi + 1;
} else {
hi = mi - 1;
}
}
}
{
int lo = 0, hi = n - 1;
while (lo <= hi) {
int mi = (lo + hi) / 2;
if (st.get(mi, mi + 1) >= val) {
left = mi;
hi = mi - 1;
} else {
lo = mi + 1;
}
}
}
int need = r - left + 1;
st.set(right - need + 1, right + 1, 1);
st.set(l, left, 1);
}
} else {
int l, r;
cin >> l >> r;
int left = -1, right = -1;
{
int lo = 0, hi = n - 1;
while (lo <= hi) {
int mi = (lo + hi) / 2;
if (st.get(mi, mi + 1) <= r) {
right = mi;
lo = mi + 1;
} else {
hi = mi - 1;
}
}
}
{
int lo = 0, hi = n - 1;
while (lo <= hi) {
int mi = (lo + hi) / 2;
if (st.get(mi, mi + 1) >= l) {
left = mi;
hi = mi - 1;
} else {
lo = mi + 1;
}
}
}
int ans = 0;
if (left != -1 && right != -1) {
ans = right - left + 1;
}
cout << ans << '\n';
}
}
}
int main() {
setIO();
int tt = 1;
#ifdef TEST_CASES
cin >> tt;
#endif
while (tt--)
solve();
return 0;
}
Compilation message
grow.cpp: In member function 'void SegmentTree::build(std::vector<int>&, int, int, int)':
grow.cpp:102:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | if (lx < a.size()) {
| ~~~^~~~~~~~~~
grow.cpp: In function 'void setIn(std::string)':
grow.cpp:44:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | void setIn(string s) {freopen(s.c_str(), "r", stdin);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
grow.cpp: In function 'void setOut(std::string)':
grow.cpp:45:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | void setOut(string s) {freopen(s.c_str(), "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp: In function 'void solve()':
grow.cpp:216:30: warning: 'right' may be used uninitialized in this function [-Wmaybe-uninitialized]
216 | st.set(right - need + 1, right + 1, 1);
| ~~~~~~^~~~~~
grow.cpp:134:12: warning: 'left' may be used uninitialized in this function [-Wmaybe-uninitialized]
134 | set(l, r, v, 1, 0, sz);
| ~~~^~~~~~~~~~~~~~~~~~~
grow.cpp:190:21: note: 'left' was declared here
190 | int left, right;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
711 ms |
6488 KB |
Output is correct |
2 |
Correct |
912 ms |
7040 KB |
Output is correct |
3 |
Correct |
459 ms |
7044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
340 KB |
Output is correct |
2 |
Correct |
16 ms |
440 KB |
Output is correct |
3 |
Correct |
12 ms |
448 KB |
Output is correct |
4 |
Correct |
11 ms |
428 KB |
Output is correct |
5 |
Correct |
271 ms |
1648 KB |
Output is correct |
6 |
Correct |
342 ms |
1888 KB |
Output is correct |
7 |
Correct |
32 ms |
716 KB |
Output is correct |
8 |
Correct |
227 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
319 ms |
1936 KB |
Output is correct |
2 |
Correct |
359 ms |
2168 KB |
Output is correct |
3 |
Correct |
27 ms |
596 KB |
Output is correct |
4 |
Correct |
242 ms |
1580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
2160 KB |
Output is correct |
2 |
Correct |
377 ms |
1944 KB |
Output is correct |
3 |
Correct |
62 ms |
780 KB |
Output is correct |
4 |
Correct |
381 ms |
2072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
630 ms |
4024 KB |
Output is correct |
2 |
Correct |
846 ms |
6384 KB |
Output is correct |
3 |
Correct |
128 ms |
1996 KB |
Output is correct |
4 |
Correct |
378 ms |
6596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
780 ms |
6140 KB |
Output is correct |
2 |
Correct |
916 ms |
6544 KB |
Output is correct |
3 |
Correct |
435 ms |
6956 KB |
Output is correct |
4 |
Correct |
125 ms |
1996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
708 ms |
6252 KB |
Output is correct |
2 |
Correct |
700 ms |
6576 KB |
Output is correct |
3 |
Correct |
444 ms |
7004 KB |
Output is correct |
4 |
Correct |
122 ms |
2036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
901 ms |
7124 KB |
Output is correct |
2 |
Correct |
844 ms |
6492 KB |
Output is correct |
3 |
Correct |
328 ms |
6604 KB |
Output is correct |
4 |
Correct |
705 ms |
6484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
813 ms |
6828 KB |
Output is correct |
2 |
Correct |
846 ms |
7072 KB |
Output is correct |
3 |
Execution timed out |
1061 ms |
7272 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
782 ms |
7736 KB |
Output is correct |