Advertisement
Guest User

Halt The War

a guest
Jun 11th, 2016
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. typedef long long ll;
  6. using namespace std;
  7. using namespace __gnu_pbds;
  8.  
  9. template <typename T>
  10. using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
  11.  
  12. #define all(x) x.begin(), x.end()
  13. #define f(i,a,b) for(int i = (a); i <= (b); i++)
  14. #define fd(i,a,b) for(int i = (a); i >= (b); i--)
  15. #define mp make_pair
  16. #define faster_io() ios_base::sync_with_stdio(false)
  17. #define pb push_back
  18. #define pii pair<int,int>
  19. #define SZ(x) ((int)x.size())
  20. #define vii vector<pair<int,int>>
  21.  
  22. const int INF = 1000000005;
  23. const ll INFLL = 1000000000000000002ll;
  24. const ll MOD = 1000000007;
  25.  
  26. inline ll min(ll a, ll b, ll c){return min(min(a,b),c);}
  27. inline ll min(ll a, ll b, ll c, ll d){return min(min(min(a,b),c),d);}
  28. inline ll max(ll a, ll b, ll c){return max(max(a,b),c);}
  29. inline ll max(ll a, ll b, ll c, ll d){return max(max(max(a,b),c),d);}
  30.  
  31. // ----------------------------------------------------------------------------------------------------------
  32.  
  33. const int RIGHT = 131072;
  34. const int SIZE = 265000;
  35. int L[SIZE], N, Q, Tests;
  36. ll T[SIZE];
  37.  
  38. ll query(int l, int r, int n, int a, int b)
  39. {
  40.     if(a > r || b < l) return 0;
  41.     if(a >= l && b <= r) return T[n];
  42.     if(L[n])
  43.     {
  44.         T[2*n] += (ll) L[n] * ((b-a+1)/2);
  45.         T[2*n+1] += (ll) L[n] * ((b-a+1)/2);
  46.         L[2*n] += L[n];
  47.         L[2*n+1] += L[n];
  48.         L[n] = 0;
  49.     }
  50.     int mid = (a+b) / 2;
  51.     return query(l,r,2*n,a,mid) + query(l,r,2*n+1,mid+1,b);
  52. }
  53.  
  54. void update(int l, int r, int n, int a, int b)
  55. {
  56.     if(a > r || b < l) return;
  57.     if(a >= l && b <= r)
  58.     {
  59.         T[n] += b-a+1;
  60.         L[n]++;
  61.         return;
  62.     }
  63.     if(L[n])
  64.     {
  65.         T[2*n] += (ll) L[n] * ((b-a+1)/2);
  66.         T[2*n+1] += (ll) L[n] * ((b-a+1)/2);
  67.         L[2*n] += L[n];
  68.         L[2*n+1] += L[n];
  69.         L[n] = 0;
  70.     }
  71.     int mid = (a+b) / 2;
  72.     update(l,r,2*n,a,mid), update(l,r,2*n+1,mid+1,b);
  73.     T[n] = T[2*n] + T[2*n+1];
  74. }
  75.  
  76. int main()
  77. {
  78.     cin >> Tests;
  79.     f(tt,1,Tests)
  80.     {
  81.         f(i,0,SIZE-1) T[i] = L[i] = 0;
  82.         printf("Scenario #%d:\n", tt);
  83.         scanf("%d %d", &N, &Q);
  84.         while(Q--)
  85.         {
  86.             char s[15];
  87.             int l,r;
  88.             scanf("%s %d %d", s, &l, &r);
  89.             if(s[0] == 'a') printf("%lld\n", query(l,r,1,1,RIGHT));
  90.             else update(l,r,1,1,RIGHT), printf("OK\n");
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement