HELLO every one ================== I was recently solving this problem using sparse segment trees and I got WA in TC3, so can you help me to know where is the issue in my code . Thanks in advance . this is my code :
/** * * * * * * * * * * * * * **\
* *
* Author: Haidara Nassour *
* Coded in: YYYY\M\D HH:MM:SS *
* Lang: C++ *
* *
\** * * * * * * * * * * * * * **/
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define itn int
#define rep(i,x,n) for(int i=(x);i<(n);i++)
#define FOR(i,n) rep(i,0,n)
#define per(i,x,n) for(int i=(x);i>(n);i--)
#define ROF(i,x) for(int i=x;i>=0;i--)
#define v(i) vector< i >
#define p(i,j) pair< i , j >
#define pii pair<int,int>
#define m(i,j) map< i , j >
#define um(i,j) unordered_map< i , j >
#define max_heap(i) priority_queue< i >
#define min_heap(i) priority_queue< i , vector< i > ,greater< i > >
#define ff first
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define ss second
#define pp push_back
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
#define debug(x) cout<<#x<<" ";
using namespace std;
using namespace __gnu_pbds;
void SIO(string name="")
{
if(name!="")
{
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
}
template <class T> using o_set = tree<T, null_type, less<T>,rb_tree_tag, tree_order_statistics_node_update>;
///order_of_key = find index of element x ( returned val is integer )
///find_by_order = find value at index x ( returned val is pointer )
const double pi=2.0*acos(0.0);
const int inf=1LL<<60LL;
const int mod=1e9+7;
const int maxn=200200;
int n;
struct node
{
int mx,lazy,a,x,lc,rc;
bool go;
node():lazy(0),go(0),a(0),lc(-1),rc(-1),x(0),mx(0){}
}st[3000000];
int start,value;
void push(int inx)
{
st[inx].mx=max(st[st[inx].lc].mx,st[st[inx].rc].mx);
}
int cur=2;
void create(int inx,int no)
{
if(no&1)
{
if(st[inx].lc==-1)
st[inx].lc=cur++;
}
else
{
if(st[inx].rc==-1)
st[inx].rc=cur++;
}
}
void pull(int inx,int l,int r)
{
if(st[inx].go)
st[inx].mx=max(st[inx].a,st[inx].a+st[inx].x*(r-l));
st[inx].mx+=st[inx].lazy;
if(st[inx].mx<0)
st[inx].mx=0;
if(l!=r)
{
create(inx,1);
create(inx,2);
st[st[inx].lc].lazy+=st[inx].lazy;
st[st[inx].rc].lazy+=st[inx].lazy;
if(st[inx].go)
{
int mid=l+(r-l)/2;
st[st[inx].lc].lazy=st[inx].lazy;
st[st[inx].rc].lazy=st[inx].lazy;
st[st[inx].lc].a=st[inx].a;
st[st[inx].lc].x=st[inx].x;
st[st[inx].rc].x=st[inx].x;
st[st[inx].rc].a=st[inx].a+st[inx].x*(mid-l+1);
st[st[inx].lc].go=1;
st[st[inx].rc].go=1;
}
}
st[inx].lazy=0;
st[inx].go=0;
st[inx].a=0;
st[inx].x=0;
}
void update(int ql,int qr,int val,int l=1,int r=n,int inx=1)
{
pull(inx,l,r);
if(ql==l&&r==qr)
{
st[inx].lazy=0;
st[inx].go=1;
st[inx].a=value+val*(l-start+1);
st[inx].x=val;
pull(inx,l,r);
return ;
}
int mid=l+(r-l)/2;
if(qr<=mid)
update(ql,qr,val,l,mid,st[inx].lc);
else if(ql>=mid+1)
update(ql,qr,val,mid+1,r,st[inx].rc);
else
update(ql,mid,val,l,mid,st[inx].lc),update(mid+1,qr,val,mid+1,r,st[inx].rc);
pull(st[inx].lc,l,mid);
pull(st[inx].rc,mid+1,r);
push(inx);
}
int query(int pos,int l=1,int r=n,int inx=1)
{
pull(inx,l,r);
if(!pos)
return 0;
if(l==r)
return st[inx].mx;
int mid=l+(r-l)/2;
int res;
if(pos<=mid)
res=query(pos,l,mid,st[inx].lc);
else
res=query(pos,mid+1,r,st[inx].rc);
push(inx);
return res;
}
void add(int val,int ql,int qr=n,int l=1,int r=n,int inx=1)
{
pull(inx,l,r);
if(ql==l&&r==qr)
{
st[inx].lazy+=val;
pull(inx,l,r);
return ;
}
int mid=l+(r-l)/2;
if(qr<=mid)
add(val,ql,qr,l,mid,st[inx].lc);
else if(ql>=mid+1)
add(val,ql,qr,mid+1,r,st[inx].rc);
else
add(val,ql,mid,l,mid,st[inx].lc),add(val,mid+1,qr,mid+1,r,st[inx].rc);
pull(st[inx].lc,l,mid);
pull(st[inx].rc,mid+1,r);
push(inx);
}
int run(int val,int l=1,int r=n,int inx=1)
{
pull(inx,l,r);
if(l==r)
return l-(st[inx].mx>val);
int mid=l+(r-l)/2;
pull(st[inx].lc,l,mid);
pull(st[inx].rc,mid+1,r);
int res;
if(st[st[inx].lc].mx<=val)
res=run(val,mid+1,r,st[inx].rc);
else
res=run(val,l,mid,st[inx].lc);
push(inx);
return res;
}
signed main()
{
cin>>n;
char t='X';
while(t!='E')
{
cin>>t;
if(t=='I')
{
int l,r,h;
cin>>l>>r>>h;
start=l,value=query(l-1);
int last=query(r);
update(l,r,h);
if(r<n)
add(query(r)-last,r+1);
}
else if(t=='Q')
{
int h;
cin>>h;
cout<<run(h)<<'\n';
}
}
}








