Advertisement
Guest User

Untitled

a guest
Jul 26th, 2014
1,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <queue>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <map>
  6. #include <algorithm>
  7. #include <bitset>
  8. #include <cstdlib>
  9. #include <list>
  10. #include <stack>
  11. #include <deque>
  12. #include <cmath>
  13. #include <string>
  14. #include <string.h>
  15. #include <iomanip>
  16. using namespace std;
  17. #define rep(i,n) for(int i = 0;  i < n ; ++i)
  18. #define REP(i,a,b) for(int i = a ; i <= b ; ++i)
  19. #define s(n) scanf("%d",&n)
  20. #define rev(i,n) for(int i = n ; i >= 0 ; --i)
  21. #define REV(i,a,b) for(int i = a ; i >= b ; --i)
  22. #define miN(a,b) (((a)<(b))?(a):(b))
  23. #define sc(n) scanf("%c",&n)
  24. #define tr(c,i) for(typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
  25. #define INF 1000000000
  26. #define pp pair<int,int>
  27. #define pb(a) push_back(a)
  28. struct SegTree{
  29.     long long sum ;
  30.     long long update ;  
  31. };
  32.  
  33.  
  34. SegTree tree[4*100000] = {0} ;
  35.  
  36.  
  37. void build_tree(int node , int a , int b )
  38. {
  39.     tree[node].update = 0 ;
  40.     tree[node].sum = 0 ;
  41.     if(a == b)
  42.     {
  43.         tree[node].sum = 0 ;
  44.         return ;  
  45.     }
  46.     build_tree(2*node , a , (a+b)/2) ;
  47.     build_tree(2*node + 1 , (a+b)/2 + 1 , b) ;
  48.  
  49. }
  50.  
  51. void update(int node , int a , int b , int i , int j , long long v)
  52. {
  53.     if(tree[node].update)
  54.     {
  55.         tree[node].sum += tree[node].update*(b - a + 1) ;
  56.         if(a != b)
  57.         {
  58.             tree[2*node].update += tree[node].update ;
  59.             tree[2*node + 1].update += tree[node].update ;
  60.         }
  61.         tree[node].update = 0 ;
  62.     }
  63.     if(i > b || j < a)
  64.     {
  65.         return ;
  66.     }
  67.  
  68.     if(a >= i && b <= j)
  69.     {
  70.         tree[node].sum += v*(b - a + 1) ;
  71.         if(a != b)
  72.         {
  73.             tree[2*node].update += v ;
  74.             tree[2*node + 1].update += v ;
  75.         }
  76.         return ;
  77.     }
  78.     update(2*node , a , (a+b)/2 , i , j , v) ;
  79.     update(2*node + 1 , (a+b)/2 + 1 , b , i , j , v) ;
  80.     tree[node].sum = tree[2*node].sum + tree[2*node + 1].sum ;
  81.  
  82. }
  83.  
  84. long long query(int node ,int a , int b , int i , int j)
  85. {
  86.     if(tree[node].update)
  87.     {
  88.         tree[node].sum += tree[node].update*(b - a + 1) ;
  89.         if(a != b)
  90.         {
  91.             tree[2*node].update += tree[node].update ;
  92.             tree[2*node + 1].update += tree[node].update ;
  93.         }
  94.         tree[node].update = 0 ;
  95.     }
  96.     if(i > b || j < a)
  97.     {
  98.         return 0;
  99.     }
  100.     if(a >= i && b <= j)
  101.     {
  102.         return tree[node].sum ;
  103.     }
  104.  
  105.     return (query(2*node , a , (a+b)/2 , i , j) + query(2*node + 1 , (a+b)/2 + 1 , b , i , j)) ;
  106. }
  107.  
  108.  
  109. int main()
  110. {
  111.     //freopen("input.txt","r",stdin) ;
  112.     int N , type , x , y , Q;
  113.     long long v , ans;
  114.     int tc ;
  115.     s(tc);
  116.     while(tc--)
  117.     {
  118.         s(N) ;
  119.         build_tree(1,1,N) ;
  120.         s(Q) ;
  121.         while(Q--)
  122.         {
  123.             s(type) ;
  124.             s(x) ;
  125.             s(y) ;
  126.             if(type == 1)
  127.             {
  128.                 ans = query(1,1,N,x,y) ;
  129.                 printf("%lld\n",ans) ;
  130.             }
  131.             else
  132.             {
  133.                 scanf("%lld",&v) ;
  134.                 update(1,1,N,x,y,v) ;
  135.             }
  136.         }
  137.     }
  138.     return 0 ;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement