Posts

Showing posts with the label RMQ

maximum contiguous sum in the range [l,r] .

                                                 http://www.spoj.com/problems/GSS1/ GSS1 - Can you answer these queries I In this question we have to find the maximum contiguous sum in the range[l,r].  To find this  1) total sum is required  2) suffix max is required  3) prefix max is required = max(left child prefix max ,  left child sum + ri8 child prefix max ). 4) maximum contiguous = max ( ri8 child maximum contiguous , left child maximum contiguous,                                                           left child suffix max + ri8 child prefix max )                              ...

Multiples of 3

                                                  http://www.spoj.com/problems/MULTQ3/                                           MULTQ3 - Multiples of 3 #include<bits/stdc++.h> using namespace std; struct node {     int zer;     int one;     int two; }; typedef struct node grp; /*  int read_int()  { char r; bool start=false,neg=false;  int ret=0; while(true){ r=getchar(); if((r-'0'<0 || r-'0'>9) && r!='-' && !start){ continue; } if((r-'0'<0 || r-'0'>9) && r!='-' && start){ break; } if(start)ret*=10; start=true; if(r=='-')neg=true; else ret+=r-'0'; } if(!neg) return ret; else return -ret;...

Horrible queries (With Lazy Propagation)

                                     http://www.spoj.com/problems/HORRIBLE/                       HORRIBLE - Horrible Queries If the node is lazy. firstly ,it is needed to update that node. and after that making their childs as lazy (if they have childs en!=st). #include<bits/stdc++.h> using namespace std; long long int tre[1000000],a[1000000],lazy[1000000]; long long int query(long long int node,long long int st,long long int en,long long int fs,long long int fe)   {   //     cout<<st<<" "<<en<<endl;   if(lazy[node]!=0)   {       tre[node]+=(en-st+1)*lazy[node];       if(st!=en)       {           lazy[2*node]+=lazy[node];           lazy[2*node+1]+=lazy[nod...

RMQ with update without Lazy

          RMQSQ - Range Minimum Query with update #include<bits/stdc++.h> using namespace std; int tre[1000000],a[1000000]; void build_tree(int in,int st,int en) {  if(st>en)         return ;  if(st==en)  {      tre[in]=a[st];      return ;  }  else  {      build_tree(2*in,st,(st+en)/2);      build_tree(2*in+1,(st+en)/2+1,en);      tre[in]=tre[2*in]+tre[2*in+1];  } } int query(int node,int st,int en,int fs,int fe) {  if(fe<st || fs>en || fs>fe)     return 0;  if(fs<=st && fe>=en)  {      return tre[node];  }  else  {      return query(2*node,st,(st+en)/2,fs,fe)+query(1+2*node,1+(st+en)/2,en,fs,fe) ;  } } void update(int node,int st,int en,int fs,int fe,int sum) {   if(f...

Range Minimum query using segment tree

                                        http://www.spoj.com/problems/RMQSQ/                                   RMQSQ - Range Minimum Query fistly tree is built , 1st index will keep the minimum of the range (0,n-1). In Built tree   IF  st==en  i.e singleton  node is there  , then tre[node] will have same value as a[st]; ELSE  node will call its childs 2*node and 2*node+1  with there ranges.   tre[in]  will keep the minimum of its two childs. #include<bits/stdc++.h> using namespace std; int tre[1000000],a[1000000]; void build_tree(int in,int st,int en) {  if(st>en)         return ;  if(st==en)  {      tre[in]=a[st];      return ;  }  else ...

new idea for add in the range [a,b] ( no segment tree )

                             A. Greg and Array http://codeforces.com/problemset/problem/295/A If it is told to add in the range [a,b] with c . we just add array[a]+=c  and array[b+1]-=c; After that we just need to do cumulative sum. It  will give the final array :) #include<bits/stdc++.h> using namespace std; long long int a[500005],b[500005]; long long int op[500005],k[100005][3]; int main() { long long int n,i,j,l,ut,ui,p,q,ad; cin>>n>>ut>>ui; for(i=1;i<=n;i++) cin>>a[i]; for(i=1;i<=ut;i++) {  cin>>k[i][0]>>k[i][1]>>k[i][2]; } for(i=1;i<=n+1;i++)     op[i]=0; for(i=1;i<=ui;i++) {  cin>>p>>q;  op[p]++;  op[q+1]--; } for(i=2;i<=ut;i++) op[i]+=op[i-1]; /*for(i=1;i<=n;i++)     cout<<op[i]<<" ";      cout<...