Guest
Public paste!

GSS1 BUG IS ALIVE :P

By: a guest | Sep 5th, 2010 | Syntax: None | Size: 2.86 KB | Hits: 21 | Expires: Never
Copy text to clipboard
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. typedef int LL;
  5. int B[1000000];//Debugging stuff
  6. int E[1000000];//Debugging stuff
  7. struct range
  8. {
  9.     LL sum,sumhi,sumlo,limhi,limlo,tot;
  10.     bool imp;
  11.     range(LL _sum=0,LL _sumhi=0,LL _sumlo=0,LL _tot=0,bool _imp = false)
  12.     {
  13.         tot=_tot;imp=_imp;
  14.         sumhi=_sumhi;sumlo=_sumlo;
  15.         sum=_sum;
  16.     }
  17. };
  18. LL arr[50000];
  19. int n;
  20. range tree[1000000];
  21. void fill(int node = 1, int b = 0, int e = n-1)
  22. {
  23.     if(b==e)
  24.     {
  25.         tree[node].sumhi=arr[b];
  26.         tree[node].sumlo=arr[b];
  27.         tree[node].sum=arr[b];   //this 4 are for assigning a leaf to SEGMENT TREE
  28.         tree[node].tot=arr[b];
  29.         B[node]=E[node]=b; //Debugging stuff
  30.     }
  31.     else
  32.     {
  33.         fill(2*node,b,(b+e)/2);// fill childs first
  34.         fill(2*node+1,(b+e)/2+1,e); // fill childs first
  35.        
  36.         range p1=tree[2*node],p2=tree[2*node+1];  //simplify use of child
  37.         range joint;
  38.         B[node]=b;
  39.         E[node]=e;
  40.  
  41.         //---Here i join segment 2*node && 2*node+1 ----
  42.         joint.tot=p1.tot+p2.tot;
  43.         joint.sum=max(p1.sum,p2.sum);
  44.         joint.sum=max(joint.sum,p1.sumhi+p2.sumlo);
  45.         joint.sumhi=max(p2.sumhi,p1.sumhi+p2.tot);
  46.         joint.sumlo=max(p1.sumlo,p2.sumlo+p1.tot);
  47.         joint.sum=max(joint.sum,joint.sumlo);
  48.         joint.sum=max(joint.sum,joint.sumhi);
  49.         tree[node]=joint;
  50.     }
  51. }
  52.  
  53. range query(int i,int j,int node = 1, int b = 0, int e = n-1)
  54. {
  55.     if(j<b || e<i)
  56.         return (-1,-1,-1,-1,true); // return imposible this child is not in query range
  57.     if(b >=i && e<=j)
  58.         return tree[node]; //its completely contained no need to call childs so return [node]
  59.  
  60.     range p1 = query(i,j,2*node,b,(b+e)/2); // call childs
  61.     range p2 = query(i,j,2*node+1,(b+e)/2+1,e);
  62.  
  63.     if(p1.imp)return p2; //if p1 is not in range return p2
  64.     if(p2.imp)return p1; //if p2 is not in the range return p1
  65.    
  66.     //same join as up .... its always the same as the initiallize in SEGMENT TREE
  67.     range joint;
  68.     joint.tot=p1.tot+p2.tot;
  69.     joint.sum=max(p1.sum,p2.sum);
  70.     joint.sum=max(joint.sum,p1.sumhi+p2.sumlo);
  71.     joint.sumhi=max(p2.sumhi,p1.sumhi+p2.tot);
  72.     joint.sumlo=max(p1.sumlo,p2.sumlo+p1.tot);
  73.     joint.sum=max(joint.sum,joint.sumlo);
  74.     joint.sum=max(joint.sum,joint.sumhi);
  75.     return joint;
  76. }
  77.  
  78. main()
  79. {
  80.     scanf("%d",&n);
  81.     for(int i=0;i<n;++i)
  82.         scanf("%d",&arr[i]);
  83.     fill(); //fill the tree NlgN suzed tree
  84.    
  85.     //Debugging stuff----------
  86.     for(int i=0;i<2*n;++i)
  87.         printf("%d-%d ---> %d , ---> %d\n",B[i],E[i],tree[i].tot,tree[i].sumhi);
  88.     //End of debugging stuf ---------
  89.    
  90.     //This part is the real part of the program
  91.     int T;scanf("%d",&T);
  92.     while(T--)
  93.     {
  94.         int st,en;scanf("%d %d",&st,&en);
  95.         printf("%d\n",query(st-1,en-1).sumhi);
  96.     }
  97. }