#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5 ;
#define ll long long
#define endl '\n'
struct segmentTree {
ll n;
vector<ll> sum, lz, sumB , maxi , mini;
void init(ll _n, ll b[]) {
n = _n;
sum.assign(4 * n + 6, 0);
lz.assign(4 * n + 6, 0);
sumB.assign(4 * n + 6, 0);
maxi.assign(4 * n + 6, -2e9);
mini.assign(4 * n + 6, 2e9);
build(b, 1, 1, n);
}
void build(ll b[], ll i, ll l, ll r) {
if (l == r) {
sumB[i] = b[l];
mini[i] = 0 ;
maxi[i] = 0 ;
return;
}
ll m = (l + r) >> 1;
build(b, i << 1, l, m); build(b, i << 1 | 1, m + 1, r);
sumB[i] = sumB[i << 1] + sumB[i << 1 | 1];
}
void pushdown(ll i) {
for(ll j=i<<1; j<=(i<<1|1); ++j) {
sum[j] += lz[i] * sumB[j];
maxi[j] += lz[i] ;
mini[j] += lz[i] ;
lz[j] += lz[i];
}
lz[i] = 0;
}
void upd(ll i, ll l, ll r, ll u, ll v, ll delta) {
if (l > r || u > v || l > v || r < u) return;
if (l >= u && r <= v) {
sum[i] += delta * sumB[i];
maxi[i] += delta ;
mini[i] += delta ;
lz[i] += delta;
return;
}
pushdown(i);
ll m = (l + r) >> 1;
upd(i << 1, l, m, u, v, delta); upd(i << 1 | 1, m + 1, r, u, v, delta);
sum[i] = sum[i << 1] + sum[i << 1 | 1];
maxi[i] = max(maxi[i<<1] , maxi[i<<1 | 1]) ;
mini[i] = min(mini[i<<1] , mini[i<<1 | 1]) ;
}
void upd(ll u, ll v, ll delta) {
upd(1, 1, n, u, v, delta);
}
ll f(ll x , ll y , ll tp)
{
if(tp==1) return x+y ;
if(tp==2) return min(x , y) ;
return max(x , y) ;
}
ll id(ll tp)
{
if(tp==1) return 0 ;
if(tp==2) return 2e9 ;
return -2e9 ;
}
ll get(ll i, ll l, ll r, ll u, ll v , ll tp) {
if (l > r || u > v || l > v || r < u) return id(tp);
if (l >= u && r <= v) return(tp == 1 ? sum[i] : tp==2 ? mini[i] : maxi[i]);
pushdown(i);
ll m = (l + r) >> 1;
return f(get(i << 1, l, m, u, v , tp) , get(i << 1 | 1, m + 1, r, u, v , tp) , tp);
}
ll get2(ll node , ll l , ll r , ll idx){
if(l==r) return sum[node];
pushdown(node) ;
ll md = (l+r)/2;
if(idx<=md) return get2(2*node,l,md,idx);
return get2(2*node+1,md+1,r,idx);
}
ll get(ll u, ll v , ll tp = 1) {
return get(1, 1, n, u, v , tp);
}
};
ll a[N] , b[N] ;
segmentTree st ;
ll n , q ;
ll getlast(ll x)
{
ll l = 1 , r = n , p = 0 ;
while(l<=r){
ll md = (l+r)/2 ;
ll y = st.get2(1 , 1 , n , md) ;
if(y<=x){
p = md ;
l = md+1 ;
}
else r = md-1 ;
}
return p ;
}
ll getfirst(int node , int l , int r , int x)
{
if(l==r) return l ;
st.pushdown(node) ;
if(st.maxi[node]<x) return n+1 ;
int md = (l+r)/2 ;
if(st.maxi[2*node]>=x) return getfirst(2*node , l , md , x) ;
else return getfirst(2*node+1 , md+1 , r , x) ;
}
ll getfirst(int x)
{
return getfirst(1 , 1 , n , x) ;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0),cout.tie(0) ;
cin>>n>>q ;
for(ll i = 1 ; i<=n ; i++){
cin>>a[i] ;
b[i] = 1 ;
}
st.init(n , b) ;
sort(a+1 , a+n+1) ;
for(ll i = 1 ; i<=n ; i++){
st.upd(i , i ,a[i]) ;
}
// ll m ; cin>>m ;
// while(m--){
// ll a, b ; cin>>a>>b ;
// cout<<getfirst(a)<<' '<<getlast(b)<<endl;
// }
while(q--){
char c ; cin>>c ;
if(c=='F'){
ll c , h ; cin>>c>>h ;
ll l = getfirst(h) ;
if(l==n+1) continue ;
c = min(c , n - l+1) ;
ll r = min(n , l+c-1) ;
if(r==n){
st.upd(l , r , 1) ;
continue ;
}
ll x = st.get(r , r) ;
ll lr = getlast(x-1) ;
ll rr = getlast(x) ;
ll lefties = max(0LL , lr - l+1) ;
ll rl = (rr - (c - lefties) +1) ;
// upd [l , lr] , upd[rl , rr]
if(l<=lr){
st.upd(l , lr , 1) ;
}
if(rl<=rr){
st.upd(rl , rr , 1) ;
}
}
else{
ll a , b ; cin>>a>>b ;
cout<<getlast(b) - getfirst(a)+1<<endl;
}
// for(ll i = 1 ; i<=n ; i++){
// cout<<st.get(i , i)<<' ';
// }
// cout<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
874 ms |
15832 KB |
Output is correct |
2 |
Correct |
836 ms |
15260 KB |
Output is correct |
3 |
Correct |
206 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
596 KB |
Output is correct |
2 |
Correct |
4 ms |
596 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
5 ms |
672 KB |
Output is correct |
5 |
Correct |
155 ms |
1412 KB |
Output is correct |
6 |
Correct |
207 ms |
1532 KB |
Output is correct |
7 |
Correct |
15 ms |
1108 KB |
Output is correct |
8 |
Correct |
314 ms |
1352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
1936 KB |
Output is correct |
2 |
Correct |
238 ms |
2016 KB |
Output is correct |
3 |
Correct |
8 ms |
1492 KB |
Output is correct |
4 |
Correct |
272 ms |
1752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
1940 KB |
Output is correct |
2 |
Correct |
227 ms |
1912 KB |
Output is correct |
3 |
Correct |
34 ms |
1492 KB |
Output is correct |
4 |
Correct |
231 ms |
1804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
577 ms |
11668 KB |
Output is correct |
2 |
Correct |
798 ms |
12880 KB |
Output is correct |
3 |
Correct |
70 ms |
3348 KB |
Output is correct |
4 |
Correct |
166 ms |
12612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
736 ms |
13388 KB |
Output is correct |
2 |
Correct |
776 ms |
14080 KB |
Output is correct |
3 |
Correct |
206 ms |
12416 KB |
Output is correct |
4 |
Correct |
73 ms |
3364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
929 ms |
14412 KB |
Output is correct |
2 |
Correct |
864 ms |
15788 KB |
Output is correct |
3 |
Correct |
205 ms |
14036 KB |
Output is correct |
4 |
Correct |
72 ms |
3460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
801 ms |
15284 KB |
Output is correct |
2 |
Correct |
811 ms |
13656 KB |
Output is correct |
3 |
Correct |
155 ms |
17716 KB |
Output is correct |
4 |
Execution timed out |
1095 ms |
16124 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
798 ms |
15396 KB |
Output is correct |
2 |
Correct |
810 ms |
15564 KB |
Output is correct |
3 |
Execution timed out |
1084 ms |
17364 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
630 ms |
17708 KB |
Output is correct |