#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 998244353;
const int nax = 1e6+5;
const int MAX_VAL = 1e6;
double PI=3.14159265359;
int arx[8]={1,1,0,-1,-1,-1, 0, 1};
int ary[8]={0,1,1, 1, 0,-1,-1,-1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
vi tab;
int segtree[nax*4][2];
int lazy[nax*4];
void conq(int v) {
segtree[v*2+1][0] += lazy[v];
segtree[v*2+1][1] += lazy[v];
lazy[v*2+1] += lazy[v];
segtree[v*2+2][0] += lazy[v];
segtree[v*2+2][1] += lazy[v];
lazy[v*2+2] += lazy[v];
lazy[v] = 0;
//cout<<"3asba"<<endl;
}
void build(int pos,int l,int r){
if (l>r) return;
if (l==r){
segtree[pos][0]=tab[l];
segtree[pos][1]=tab[l];
return;
}
int mid=(r+l)/2;
build(pos*2+1,l,mid);
build(pos*2+2,mid+1,r);
segtree[pos][0]=max(segtree[pos*2+1][0],segtree[pos*2+2][0]);
segtree[pos][1]=min(segtree[pos*2+1][1],segtree[pos*2+2][1]);
return;
}
void update(int pos,int l,int r,int left,int right){
if (l>r||r<left||l>right) return;
if (l==left&&right==r){
lazy[pos]++;
segtree[pos][0]++;
segtree[pos][1]++;
return;
}else{
conq(pos);
int mid=(r+l)/2;
update(pos*2+1,l,mid,left,min(mid,right));
update(pos*2+2,mid+1,r,max(left,mid+1),right);
segtree[pos][0]=max(segtree[pos*2+1][0],segtree[pos*2+2][0]);
segtree[pos][1]=min(segtree[pos*2+1][1],segtree[pos*2+2][1]);
}
}
int query(int pos,int l,int r,int index){
if (l>r) return -1e9;
if (l==r) return l;
int mid=(r+l)/2;
conq(pos);
if (segtree[pos*2+1][0]>=index) return query(pos*2+1,l,mid,index);
else return query(pos*2+2,mid+1,r,index);
}
int query1(int pos,int l,int r,int index){
if (l>r) return -1e9;
if (l==r) return l;
int mid=(r+l)/2;
conq(pos);
if (segtree[pos*2+2][1]<=index) return query1(pos*2+2,mid+1,r,index);
else return query1(pos*2+1,l,mid,index);;
}
int query2(int pos,int l,int r,int index){
if (l>r) return -1e9;
if (l==r) return segtree[pos][0];
int mid=(r+l)/2;
conq(pos);
if (mid>=index) return query2(pos*2+1,l,mid,index);
else return query2(pos*2+2,mid+1,r,index);
}
int main(){
optimise;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
//setIO("cowland");
int n,k;
cin>>n>>k;
tab.assign(n,0);
for (int i = 0; i < n; ++i)
{
cin>>tab[i];
}
sort(tab.begin(),tab.end());
build(0,0,n-1);
while(k--){
char c;
cin>>c;
int x,y;
cin>>x>>y;
if (c=='F'){
if(y>segtree[0][0]) continue;
int cur=query(0,0,n-1,y);
if (cur+x-1>=n-1){
update(0,0,n-1,cur,n-1);
continue;
}else{
int z=query2(0,0,n-1,cur+x-1);
if (z>y){
int h=query1(0,0,n-1,z-1);
//cout << "kk "<<y<<" "<<segtree[0][1]<<endl;
if(h>cur){
update(0,0,n-1,cur,h);
x-=(h-cur+1);
}else if(query2(0,0,n-1,cur)<z){
update(0,0,n-1,cur,h);
x-=(h-cur+1);
}
}
int h=query1(0,0,n-1,z);
//cout <<h-x+1<<endl;
update(0,0,n-1,h-x+1,h);
}
}else{
int l=query(0,0,n-1,x);
int r=query1(0,0,n-1,y);
//cout<< l<<" "<<r<<endl;
if (segtree[0][0]<x||segtree[0][1]>y){
cout << 0<<endl;
continue;
}
cout << r-l+1<<endl;
}
}
}
Compilation message
grow.cpp: In function 'void setIO(std::string)':
grow.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
5068 KB |
Output is correct |
2 |
Correct |
145 ms |
5416 KB |
Output is correct |
3 |
Correct |
38 ms |
5380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
6 ms |
340 KB |
Output is correct |
3 |
Correct |
6 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
340 KB |
Output is correct |
5 |
Correct |
112 ms |
1496 KB |
Output is correct |
6 |
Correct |
149 ms |
1784 KB |
Output is correct |
7 |
Correct |
7 ms |
596 KB |
Output is correct |
8 |
Correct |
110 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
1808 KB |
Output is correct |
2 |
Correct |
132 ms |
1888 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
125 ms |
1432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
1976 KB |
Output is correct |
2 |
Correct |
151 ms |
1824 KB |
Output is correct |
3 |
Correct |
9 ms |
596 KB |
Output is correct |
4 |
Correct |
146 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
3304 KB |
Output is correct |
2 |
Correct |
145 ms |
5076 KB |
Output is correct |
3 |
Correct |
34 ms |
1492 KB |
Output is correct |
4 |
Correct |
31 ms |
4968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
4740 KB |
Output is correct |
2 |
Correct |
147 ms |
5108 KB |
Output is correct |
3 |
Correct |
38 ms |
5260 KB |
Output is correct |
4 |
Correct |
35 ms |
1568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
4904 KB |
Output is correct |
2 |
Correct |
110 ms |
5072 KB |
Output is correct |
3 |
Correct |
37 ms |
5380 KB |
Output is correct |
4 |
Correct |
41 ms |
1552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
5580 KB |
Output is correct |
2 |
Correct |
140 ms |
4988 KB |
Output is correct |
3 |
Correct |
39 ms |
4428 KB |
Output is correct |
4 |
Correct |
108 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
5196 KB |
Output is correct |
2 |
Correct |
148 ms |
5416 KB |
Output is correct |
3 |
Correct |
159 ms |
5572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
5988 KB |
Output is correct |