#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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp: In function 'int main()':
grow.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |