#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define ll long long
#define pii pair <int,int>
#define left (h<<1),l,((l + r)>>1)
#define right ((h<<1)|1),((l+r)>>1) + 1,r
#define int ll // ?
using namespace std;
const int N = 1e5 + 5,inf = 1e9;
int lz[4*N],a[N];
struct {
int mx;
int mn;
} t[4*N];
void push(int h){
if (lz[h] == 0) return;
lz[h*2] += lz[h];
lz[h*2 + 1] += lz[h];
t[h*2].mx += lz[h];
t[h*2].mn += lz[h];
t[h*2 + 1].mn += lz[h];
t[h*2 + 1].mx += lz[h];
lz[h] =0;
}
void build(int h,int l,int r){
if (l == r){
t[h].mn = t[h].mx = a[l];
return;
}
build(left);
build(right);
t[h].mx = max(t[h*2].mx,t[h*2 + 1].mx);
t[h].mn = min(t[h*2].mn,t[h*2 + 1].mn);
}
int get(int h,int l,int r,int idx){
if (l == r) return t[h].mx;
push(h);
int mid = (l + r)/2;
if (idx > mid) return get(right,idx);
return get(left,idx);
t[h].mx = max(t[h*2].mx,t[h*2 + 1].mx);
t[h].mn = min(t[h*2].mn,t[h*2 + 1].mn);
}
void upd(int h,int l,int r,int L,int R,int val){
if (r < L || R < l) return;
if (L <= l && r <= R){
t[h].mn += val;
t[h].mx += val;
lz[h] += val;
return;
}
push(h);
upd(left,L,R,val);
upd(right,L,R,val);
t[h].mx = max(t[h*2].mx,t[h*2 + 1].mx);
t[h].mn = min(t[h*2].mn,t[h*2 + 1].mn);
}
int find(int h,int l,int r,int H){
if (t[h].mx < H) return r + 1;
if (l == r) return l;
push(h);
if (t[h*2].mx >= H) return find(left,H);
return find(right,H);
}
int findlast(int h,int l,int r,int H){
if (t[h].mn > H) return l - 1;
if (l == r) return l;
push(h);
if (t[h*2 + 1].mn <= H) return findlast(right,H);
return findlast(left,H);
}
signed main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,Q;
cin>>n>>Q;
for (int i = 1; i <= n; i++)
cin>>a[i];
sort(a+1,a+n+1);
build(1,1,n);
while (Q--){
char c;
cin>>c;
if (c == 'C'){
int le,ri;
cin>>le>>ri;
if (t[1].mx < le || t[1].mn > ri) {
cout<<0<<endl;
continue;
}
int l = find(1,1,n,le);
int r = findlast(1,1,n,ri);
if (l > r) cout<<0<<endl;
else cout<<r - l + 1<<"\n";
}else{
int x,h;
cin>>x>>h;
int l = find(1,1,n,h),r = min(n,l + x - 1);
if (t[1].mx < h) continue;
int val = get(1,1,n,r);
int l1 = findlast(1,1,n,val - 1);
upd(1,1,n,l,l1,1);
x -= (l1 - l + 1);
int rr = findlast(1,1,n,val);
upd(1,1,n,max(l1 + 1,rr - x + 1),rr,1);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
7316 KB |
Output is correct |
2 |
Correct |
99 ms |
8860 KB |
Output is correct |
3 |
Correct |
74 ms |
8720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
34 ms |
1736 KB |
Output is correct |
6 |
Correct |
44 ms |
2072 KB |
Output is correct |
7 |
Correct |
5 ms |
724 KB |
Output is correct |
8 |
Correct |
67 ms |
1392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
1020 KB |
Output is correct |
2 |
Correct |
41 ms |
2108 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
64 ms |
1672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
1316 KB |
Output is correct |
2 |
Correct |
47 ms |
2032 KB |
Output is correct |
3 |
Correct |
7 ms |
852 KB |
Output is correct |
4 |
Correct |
40 ms |
2184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
4004 KB |
Output is correct |
2 |
Correct |
90 ms |
8340 KB |
Output is correct |
3 |
Correct |
13 ms |
2384 KB |
Output is correct |
4 |
Correct |
63 ms |
8652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
7092 KB |
Output is correct |
2 |
Correct |
90 ms |
8544 KB |
Output is correct |
3 |
Correct |
71 ms |
8464 KB |
Output is correct |
4 |
Correct |
12 ms |
2416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
7244 KB |
Output is correct |
2 |
Correct |
70 ms |
8628 KB |
Output is correct |
3 |
Correct |
76 ms |
8808 KB |
Output is correct |
4 |
Correct |
12 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
7300 KB |
Output is correct |
2 |
Correct |
89 ms |
8392 KB |
Output is correct |
3 |
Correct |
25 ms |
7968 KB |
Output is correct |
4 |
Correct |
86 ms |
8208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
7372 KB |
Output is correct |
2 |
Correct |
87 ms |
8832 KB |
Output is correct |
3 |
Correct |
129 ms |
9036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
7764 KB |
Output is correct |