Advertisement
Guest User

Untitled

a guest
Jul 18th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <set>
  6. #include <cstring>
  7. #include <iomanip>
  8. #include <map>
  9. #include <algorithm>
  10. #include <stack>
  11. #include <queue>
  12. #include <list>
  13. #include <string>
  14. #include <vector>
  15. #include <new>
  16. #include <bitset>
  17. #include <ctime>
  18. #include <stdint.h>
  19. #include <unistd.h>
  20.  
  21.  using namespace std;
  22.  
  23. #define ll long long int
  24. #define INF 2147483647
  25. #define PI acos(-1.0)
  26. #define EPS 1e-9
  27.  
  28. template <typename X> X gcd(X a, X b){if(!b)return a; else return gcd(b, a%b);}
  29.  
  30. typedef vector<int> vi;
  31. typedef pair<int, int> ii;
  32.  
  33. const int N = 1<<17;
  34. int tt, n, m, p, q, i, d;
  35. ll t[5*N], c, lazy[5*N];
  36. void update(int, int, int, int, int, ll);
  37.  
  38. void update(int id, int l, int r, int a, int b, ll v){
  39.     if(l==a && r==b){
  40.         t[id]+=(ll)(r-l+1)*v;
  41.         lazy[id]+=v;
  42.         return;
  43.     }
  44.     int m=(l+r)>>1;
  45.     t[id*2]+=lazy[id]*(ll)(m-l+1);
  46.     t[id*2+1]+=lazy[id]*(ll)(r-m);
  47.     lazy[id*2]+=lazy[id];
  48.     lazy[id*2+1]+=lazy[id];
  49.     lazy[id]=0;
  50.     if(b<=m)
  51.         update(id*2, l, m, a, b, v);
  52.     else
  53.     if(a>m)
  54.         update(id*2+1, m+1, r, a, b, v);
  55.     else
  56.         update(id*2, l, m, a, m, v), update(id*2+1, m+1, r, m+1, b, v);
  57.     t[id]=t[id*2]+t[id*2+1];
  58. }
  59. ll query(int id, int l, int r, int a, int b){
  60.     if(l==a && r==b){
  61.         return t[id];
  62.     }
  63.     int m=(l+r)>>1;
  64.     t[id*2]+=lazy[id]*(ll)(m-l+1);
  65.     t[id*2+1]+=lazy[id]*(ll)(r-m);
  66.     lazy[id*2]+=lazy[id];
  67.     lazy[id*2+1]+=lazy[id];
  68.     lazy[id]=0;
  69.     if(b<=m)
  70.         return query(id*2, l, m, a, b);
  71.     else
  72.     if(a>m)
  73.         return query(id*2+1, m+1, r, a, b);
  74.     else
  75.         return query(id*2, l, m, a, m)+query(id*2+1, m+1, r, m+1, b);
  76. }
  77. int main(){
  78.     scanf("%d", &tt);
  79.     while(tt--){
  80.         scanf("%d %d", &n, &m);
  81.         for(i=1;i<5*N;++i)
  82.             t[i]=lazy[i]=0;
  83.         while(m--){
  84.             scanf("%d %d %d", &d, &p, &q);
  85.             if(!d){
  86.                 scanf("%lld", &c);
  87.                 update(1, 1, n, p, q, c);
  88.             }
  89.             else
  90.                 printf("%lld\n", query(1, 1, n, p, q));
  91.         }
  92.     }
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement