Chef and Churu
- #include<stdio.h>
- #include<algorithm>
- #include<vector>
- using namespace std;
- typedef long long int lli;
- typedef long int li;
- li to=0;
- #define F first
- #define S second
- lli arr[100005];
- lli sum[100005],ran[100005];
- vector <pair<pair<int,int> ,int> > pa(100005),tre(300005);
- vector <pair<pair<int,int> ,int> > :: iterator it,it2;
- //build(1,t,0,t-1);
- int build(int index, int n, int s,int e )
- {
- if(s==e)
- {
- tre[index]=pa[s];
- return 0;
- }
- int m=(s+e)/2;
- build(index*2,n,s,m);
- build(index*2+1,n,m+1,e);
- int le=index*2,ri=index*2+1;
- if(tre[le].first.first<tre[ri].first.first)
- tre[index].F.F=tre[le].F.F;
- else tre[index].F.F=tre[ri].F.F;
- if(tre[le].F.S>tre[ri].F.S)
- tre[index].F.S=tre[le].F.S;
- else
- tre[index].F.S=tre[ri].F.S;
- }
- int query(int index,int l,int r,int p,int dif)
- {
- if(l==r && tre[index].F.F<=p && tre[index].F.S>=p)
- {
- // do the needful
- ran[tre[index].S]+=dif;
- return 0;
- }
- if(tre[index].F.F<=p && tre[index].F.S>=p)
- {
- query(index*2,l,(l+r)/2,p,dif);
- query(index*2+1,(l+r)/2+1,r,p,dif);
- }
- }
- int main()
- { // freopen("kr.in", "r", stdin);
- long long int t;
- lli a,b,c;
- for(long long int i=0;i<t;i++)
- {
- if(i==0)
- sum[i]=arr[i];
- else
- sum[i]=arr[i]+sum[i-1];
- }
- for(long long int i=0;i<t;i++)
- {
- a--,b--;
- if(a==0)
- ran[i]=sum[b];
- else
- ran[i]=sum[b]-sum[a-1];
- pa[i]=make_pair(make_pair(a,b),i);
- }
- long long int mm;
- sort(pa.begin(),pa.begin()+t);
- // build a new seg tree for
- build(1,t,0,t-1);
- /* for(int i=1;i<=t*2;i++)
- {
- printf("%d %d %d %d \n",i,tre[i].F.F+1,tre[i].F.S+1,tre[i].S);
- }*/
- for(long long int i=0;i<mm;i++)
- {
- if(a==1)//update
- {b--;
- to=0;
- lli dif=c-arr[b];
- arr[b]=c;
- //int query(int index,int l,int r,int p,int dif)
- query(1,0,t-1,b,dif);
- }
- else if(a==2)
- {
- b--;c--;
- lli tt=0;
- for(int i=b;i<=c;i++)
- {
- tt+=ran[i];
- }
- }
- }
- }
The important point is the sort the range segment tree so that we are able to minimize the range will of the parent and we are able to choose the path , so just a overhead of sorting the ranges using pair sort and the creating the range tree we have great optimization for the range segment tree..... Enjoy the new concept.
No comments:
Post a Comment