Given an array a[] has n non-negative elements. We have 2 types of queries:
A x y v: Find the first index i (x<=i<=y) that a[i]>v
B u v: Update a[u]=v;
I use segment tree but it's TLE in some test.
This is my code.
it max(it A,it B)
{
it C;
C.mx=max(A.mx,B.mx);
return C;
}
This is the function that build the first tree.
void build(int id,int l,int r)
{
if(l==r)
{
st[id].mx=a[l];
st[id].pos=l;
return;
}
int m=l+r>>1;
build(id<<1,l,m);
build(id<<1|1,m+1,r);
st[id]=max(st[id<<1],st[id<<1|1]);
}
The update query:
void update(int id,int l,int r,int u,int v)
{
if(u<l||r<u) return;
if(l==r)
{
st[id].mx=v;
return;
}
int m=l+r>>1;
update(id<<1,l,m,u,v);
update(id<<1|1,m+1,r,u,v);
st[id]=max(st[id<<1],st[id<<1|1]);
}
The "find" query:
int getmin(int id,int l,int r,int u,int v,int value)
{
if(v<l||r<u) return 1e9;
if(l==r)
{
return st[id].pos;
}
int m=l+r>>1;
//cout<<"->"<<id<<" "<<l<<" "<<r<<endl;
if(st[id<<1].mx<=value)
{
if(st[id<<1|1].mx<=value)
return 1e9;
else return getmin(id<<1|1,m+1,r,u,v,value);
}
else
{
if(st[id<<1|1].mx<=value)
return getmin(id<<1,l,m,u,v,value);
else return min(getmin(id<<1,l,m,u,v,value),getmin(id<<1|1,m+1,r,u,v,value));
}
}