#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf 1000000000
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1 << j))
#define onbit(i, j) (i | (1 << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
int n, q;
int a[maxn];
int st[maxn], lazy[maxn];
void down(int id)
{
st[id << 1] += lazy[id];
st[id << 1 | 1] += lazy[id];
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
lazy[id] = 0;
}
void up(int id, int l, int r, int u, int v, int val)
{
if (l > v or r < u)
return;
if (u <= l and r <= v)
{
st[id] += val;
lazy[id] += val;
return;
}
down(id);
int mid = (l + r) >> 1;
up(id << 1, l, mid, u, v, val);
up(id << 1 | 1, mid + 1, r, u, v, val);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v)
{
if (l > v or r < u)
return 0;
if (u <= l and r <= v)
return st[id];
down(id);
int mid = (l + r) >> 1;
return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
int get(int idx, int l, int r, int val) {
if(st[idx] < val) return -1;
if(l == r) return l;
down(idx);
int mid = l + r >> 1;
if(st[idx << 1] >= val) return get(idx << 1, l, mid, val);
return get(idx << 1 | 1, mid + 1, r, val);
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
fo(i, 1, n) cin >> a[i];
sort(a + 1, a + n + 1);
// fo(i, 1, n) cout << a[i] << ' ';
// en;
fo(i, 1, n) up(1, 1, n, i, i, a[i]);
// fo(i, 1, n) cout << get(1, 1, n, i, i) << ' ';
while (q--)
{
char t;
int l, r;
cin >> t >> r >> l;
if (t == 'F')
{
int lb = get(1, 1, n, l);
int maxx = get(1, 1, n, lb, lb + r - 1);
int ub = get(1, 1, n, maxx) - 1;
int ub1 = get(1, 1, n, maxx + 1) - 1;
if (ub >= lb)
up(1, 1, n, lb, ub, 1);
int len;
if (ub < lb)
len = r;
else
len = r - (ub - lb + 1);
up(1, 1, n, max(ub + 1,ub1 - len + 1), ub1, 1);
}
else
{
swap(l,r);
int lb = get(1, 1, n, l);
int ub = get(1, 1, n, r + 1);
if (ub == -1)
ub = n + 1;
if (lb != -1)
cout << ub - lb << "\n";
else
cout << 0 << "\n";
}
}
}
Compilation message
grow.cpp: In function 'long long int get(long long int, long long int, long long int, long long int)':
grow.cpp:83:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
83 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: At global scope:
grow.cpp:87:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
87 | main()
| ^~~~
grow.cpp: In function 'int main()':
grow.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
8896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
4712 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
4708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
8792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
8884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
8836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
103 ms |
8900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
8796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
74 ms |
9120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |