Tarango

SPOJ GSS2 Updated AC

Oct 20th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.84 KB | None | 0 0
  1. ///==================================================================//
  2. /// Problem     : Contest Code                                       //
  3. /// Author      : NSU Aristocrats                                    //
  4. /// Represents  : North South University                             //
  5. /// Email       : [email protected]                          //
  6. /// Codeforces  : http://codeforces.com/team/21235                   //
  7. /// Verdict     : In Queue                                           //
  8. /// Date        : 02/10/2016                                         //
  9. ///==================================================================//
  10.  
  11. ///====================== TEMPLATE STARTS HERE =====================///
  12. //#include <bits/stdc++.h>
  13. #include<cstdio>
  14. #include<cstring>
  15. #include<cmath>
  16. #include<algorithm>
  17. #include<map>
  18. #include<queue>
  19. #include<stack>
  20. #include<vector>
  21. #include<deque>
  22. #include<functional>
  23. #include<string>
  24. #include<iostream>
  25. #include<cctype>
  26. #include<set>
  27. #include<climits>
  28. #include<iomanip>
  29. #include<cassert>
  30. #include<sstream>
  31.  
  32. #define pb push_back
  33. #define nl puts ("")
  34. #define sp printf ( " " )
  35. #define phl printf ( "hello\n" )
  36. #define ff first
  37. #define ss second
  38. #define POPCOUNT __builtin_popcountll
  39. #define RIGHTMOST __builtin_ctzll
  40. #define LEFTMOST(x) (63-__builtin_clzll((x)))
  41. #define MP make_pair
  42. #define FOR(i,x,y) for(vlong i = (x) ; i <= (y) ; ++i)
  43. #define ROF(i,x,y) for(vlong i = (y) ; i >= (x) ; --i)
  44. #define CLR(x,y) memset(x,y,sizeof(x))
  45. #define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())
  46. #define MIN(a,b) ((a)<(b)?(a):(b))
  47. #define MAX(a,b) ((a)>(b)?(a):(b))
  48. #define NUMDIGIT(x,y) (((vlong)(log10((x))/log10((y))))+1)
  49. #define SQ(x) ((x)*(x))
  50. #define ABS(x) ((x)<0?-(x):(x))
  51. #define FABS(x) ((x)+eps<0?-(x):(x))
  52. #define ALL(x) (x).begin(),(x).end()
  53. #define LCM(x,y) (((x)/gcd((x),(y)))*(y))
  54. #define SZ(x) ((vlong)(x).size())
  55. #define NORM(x) if(x>=mod)x-=mod;
  56. #define MOD(x,y) (((x)*(y))%mod)
  57. #define ODD(x) (((x)&1)==0?(0):(1))
  58. #define dbgA(A,i) debug("[",i,"] = ",A[i])
  59. #define pf printf
  60. #define sf scanf
  61. #define LL long long
  62. #define LLU long long unsigned int
  63. #define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)
  64. #define pline pf("\n\n/// ================================ ///\n\n")
  65.  
  66. using namespace std;
  67.  
  68. typedef long long vlong;
  69. typedef unsigned long long uvlong;
  70. typedef pair < vlong, vlong > pll;
  71. typedef pair < int, int > pii;
  72. typedef vector<pll> vll;
  73. typedef vector<vlong> vl;
  74.  
  75. #ifdef forthright48
  76.      #include <ctime>
  77.      clock_t tStart = clock();
  78.      #define debug(args...) {dbg,args; cerr<<endl;}
  79.      #define timeStamp debug ("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)
  80.      #define bug printf("%d\n",__LINE__);
  81.  
  82. #else
  83.     #define debug(args...)  // Just strip off all debug tokens
  84.     #define timeStamp
  85. #endif
  86.  
  87. struct debugger{
  88.     template<typename T> debugger& operator , (const T& v){
  89.         cerr<<v<<" ";
  90.         return *this;
  91.     }
  92. }dbg;
  93.  
  94. //int knightDir[8][2] = { {-2,1},{-1,2},{1,2},{2,1},{2,-1},{-1,-2},{1,-2},{-2,-1} };
  95. //int dir4[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
  96. //int dir8[8][2] = {{-1,0},{0,1},{1,0},{0,-1},{-1,-1},{1,1},{1,-1},{-1,1}};
  97.  
  98. inline vlong gcd ( vlong a, vlong b ) {
  99.     a = ABS ( a ); b = ABS ( b );
  100.     while ( b ) { a = a % b; swap ( a, b ); } return a;
  101. }
  102.  
  103. vlong ext_gcd ( vlong A, vlong B, vlong *X, vlong *Y ){
  104.     vlong x2, y2, x1, y1, x, y, r2, r1, q, r;
  105.     x2 = 1; y2 = 0;
  106.     x1 = 0; y1 = 1;
  107.     for (r2 = A, r1 = B; r1 != 0; r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y ) {
  108.         q = r2 / r1;
  109.         r = r2 % r1;
  110.         x = x2 - (q * x1);
  111.         y = y2 - (q * y1);
  112.     }
  113.     *X = x2; *Y = y2;
  114.     return r2;
  115. }
  116.  
  117. inline vlong modInv ( vlong a, vlong m ) {
  118.     vlong x, y;
  119.     ext_gcd( a, m, &x, &y );
  120.     x %= m;
  121.     if ( x < 0 ) x += m;
  122.     return x;
  123. }
  124.  
  125. inline vlong power ( vlong a, vlong p ) {
  126.     vlong res = 1, x = a;
  127.     while ( p ) {
  128.         if ( p & 1 ) res = ( res * x );
  129.         x = ( x * x ); p >>= 1;
  130.     }
  131.     return res;
  132. }
  133.  
  134. inline vlong bigmod ( vlong a, vlong p, vlong m ) {
  135.     vlong res = 1 % m, x = a % m;
  136.     while ( p ) {
  137.         if ( p & 1 ) res = ( res * x ) % m;
  138.         x = ( x * x ) % m; p >>= 1;
  139.     }
  140.     return res;
  141. }
  142.  
  143. int  Set(int N,int cur){ return N = N | (1<<cur); }
  144. int  Reset(int N,int cur){ return N = N & ~(1<<cur); }
  145. bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }
  146.  
  147. const vlong inf = 2147383647;
  148. const double pi = 2 * acos ( 0.0 );
  149. const double eps = 1e-9;
  150.  
  151. ///======================  TEMPLATE ENDS HERE  =====================///
  152.  
  153. const int MAX = 100001;
  154. const int INF = 0x7f7f7f7f;
  155.  
  156. struct vert{
  157.     LL best,slice,lazy,bstLazy;
  158. };
  159.  
  160. int N,M;
  161. int A[MAX];
  162. vert T[1<<18];
  163.  
  164. void updateChilds(vert &cur,vert &L,vert &R){
  165.     L.bstLazy = MAX(L.bstLazy, L.lazy + cur.bstLazy);
  166.     L.lazy += cur.lazy;
  167.  
  168.     R.bstLazy = MAX(R.bstLazy, R.lazy + cur.bstLazy);
  169.     R.lazy += cur.lazy;
  170.  
  171.     cur.lazy = 0;
  172.     cur.bstLazy = 0;
  173. }
  174.  
  175. void mergeChild(vert &cur,vert &L,vert &R){
  176.     cur.best = MAX(MAX(L.best,L.slice + L.bstLazy), MAX(R.best,R.slice + R.bstLazy));
  177.     cur.slice = MAX(L.slice + L.lazy, R.slice + R.lazy);
  178. }
  179.  
  180. void uT(int cur,int s,int e,int u,int v,int val){
  181.     if(e < u || s > v) return;
  182.     if(s >= u && e <= v){
  183.         T[cur].lazy += val;
  184.         T[cur].bstLazy = MAX(T[cur].lazy, T[cur].bstLazy);
  185.         return;
  186.     }
  187.     int L = cur*2,R = cur*2+1,M = (s+e)/2;
  188.  
  189.     updateChilds(T[cur],T[L],T[R]);
  190.  
  191.     uT(L,s,M,u,v,val);
  192.     uT(R,M+1,e,u,v,val);
  193.     mergeChild(T[cur],T[L],T[R]);
  194. }
  195.  
  196. LL qT(int cur,int s,int e,int u,int v){
  197.     if(e < u || s > v) return 0;
  198.     if(s >= u && e <= v){
  199.         return MAX(T[cur].best, T[cur].slice + T[cur].bstLazy);
  200.     }
  201.     int L = cur*2,R = cur*2+1,M = (s+e)/2;
  202.  
  203.     updateChilds(T[cur],T[L],T[R]);
  204.     mergeChild(T[cur],T[L],T[R]);
  205.  
  206.     LL v1 = qT(L,s,M,u,v);
  207.     LL v2 = qT(R,M+1,e,u,v);
  208.     return MAX(v1,v2);
  209. }
  210.  
  211.  
  212. struct qry{
  213.     int u,v,idx;
  214. };
  215.  
  216. bool cmp(qry a,qry b){
  217.     if(a.v == b.v) return a.u<b.u;
  218.     return a.v < b.v;
  219. }
  220.  
  221. qry Q[MAX];
  222. LL res[MAX];
  223. int lstOcr[MAX*2];
  224.  
  225. int main(){
  226.     #ifdef forthright48
  227.     freopen ( "Ainput.txt", "r", stdin );
  228.     //freopen ( "output.txt", "w", stdout );
  229.     #endif // forthright48
  230.  
  231.     int N,M;
  232.     sf("%d",&N);
  233.     FOR(i,1,N){
  234.         sf("%d", &A[i]);
  235.     }
  236.     sf("%d",&M);
  237.     FOR(i,0,M-1){
  238.         sf("%d %d",&Q[i].u,&Q[i].v);
  239.         Q[i].idx = i;
  240.     }
  241.  
  242.     sort(Q,Q+M,cmp);
  243.     int cq = 0;
  244.     for(int ed = 1;ed<=N;ed++){
  245.         int val = A[ed]+100000;
  246.         uT(1, 1, N, lstOcr[val] + 1, ed, A[ed]);
  247.         lstOcr[val] = ed;
  248.         while(Q[cq].v == ed){
  249.             if(cq>=M) break;
  250.             res[Q[cq].idx] = qT(1, 1, N, Q[cq].u, Q[cq].v);
  251.             cq++;
  252.         }
  253.     }
  254.  
  255.     FOR(i,0,M-1){
  256.         pf("%lld\n", res[i]);
  257.     }
  258.     return 0;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment