SuitNdtie

The trail segment tree with qs

May 27th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include<stdio.h>
  2. int const N2 = 500010;
  3. typedef long long int ll;
  4.  
  5. int n;
  6.  
  7. ll maxtree[N2];
  8. ll sumtree[N2];
  9.  
  10. ll max(ll a,ll b){
  11.     return (a > b ? a : b);
  12. }
  13.  
  14. ll buildmax(int I){
  15.     if(I >= n*2)return -1e18;
  16.     if(I >= n)return maxtree[I];
  17.     return maxtree[I] = max(buildmax(I*2),buildmax(I*2 + 1));
  18. }
  19.  
  20. ll buildsum(int I){
  21.     if(I >= n*2)return 0;
  22.     if(I >= n)return sumtree[I];
  23.     return sumtree[I] = buildsum(I*2) + buildsum(I*2 + 1);
  24. }
  25.  
  26. ll qmax(int L,int R){
  27.     L += n - 1;
  28.     R += n - 1;
  29.     ll maxa = max(maxtree[L],maxtree[R]);
  30.     while(L <= R){
  31.         if(L % 2 == 1){
  32.             maxa = max(maxa , maxtree[L++]);
  33.         }
  34.         if(R % 2 == 0){
  35.             maxa = max(maxa , maxtree[R--]);
  36.         }
  37.         L/=2;
  38.         R/=2;
  39.     }
  40.     return maxa;
  41. }
  42. /*
  43. ll qsum(int L,int R){
  44.     L += n - 1;
  45.     R += n - 1;
  46.     ll suma = 0;
  47.     while(L <= R){
  48.         if(L % 2 == 1){
  49.             suma += sumtree[L++];
  50.         }
  51.         if(R % 2 == 0){
  52.             suma += sumtree[R--];
  53.         }
  54.         L/=2;
  55.         R/=2;
  56.     }
  57.     return suma;
  58. }
  59. */
  60. ll qs[100010];
  61. int main()
  62. {
  63.     int m;
  64.     scanf("%d %d",&n,&m);
  65.     for(int i = 0 ; i < n ; i ++){
  66.         scanf("%lld",&maxtree[i+n]);
  67.     }
  68.     for(int i = 1 ; i <= n ; i ++){
  69.         scanf("%lld",&qs[i]);
  70.         qs[i] = qs[i-1] + qs[i];
  71.     }
  72.     buildmax(1);
  73.  
  74.     for(int i = 0 ; i < m ; i ++){
  75.         int L,R;
  76.         scanf("%d %d",&L,&R);
  77.         L++;
  78.         R++;
  79.     //  printf("%lld %lld\n",qmax(L,R),qsum(L,R));
  80.         printf("%lld %lld\n",qmax(L,R),qs[R] - qs[L-1]);
  81.     }
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment