Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <fstream>
  9. #include <map>
  10. #include <set>
  11. #include <queue>
  12. using namespace std;
  13.  
  14. #pragma comment(linker,"/STACK:16777216")
  15. //Loops
  16. #define FOR(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
  17. #define ROF(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
  18. #define rep(i,n) for (int (i) = 0; (i) < (n); ++(i))
  19. #define fe(i,a) for (int (i) = 0; (i) < int((a).size()); ++(i))
  20. #define MEM(a,b) memset((a),(b),sizeof(a))
  21.  
  22. //Constants
  23. #define inf 1000000000
  24. #define pi 2*acos(0.0)
  25. #define N 100010
  26. #define eps 1e-9
  27.  
  28. //Functions
  29. #define pb push_back
  30. #define ppb pop_back
  31. #define all(c) (c).begin(), (c).end()
  32. #define sz(x) int((x).size())
  33. #define sqr(a) (a)*(a)
  34.  
  35. //Pairs
  36. #define mp(a,b) make_pair((a), (b))
  37. #define X first
  38. #define Y second
  39.  
  40. //Input-Output
  41. #define FREOPEN(a,b) freopen(a,"r",stdin); freopen(b,"w",stdout);
  42.  
  43. typedef vector<int> vint;
  44. typedef long long ll;
  45. typedef pair<int, int> pii;
  46.  
  47. struct node
  48. {
  49.    int add,t1,t2;
  50.  
  51. };
  52. node tree[4*N];
  53. int t0=0,ans=0;
  54. void update(int root,int tl,int tr,int l,int r)
  55. {
  56.    if(tl>tr || tr<l || tl>r || l>r)return;
  57.    if(tl>=l && tr<=r)
  58.    {
  59.        t0=(tr-tl+1)-tree[root].t1-tree[root].t2;
  60.        tree[root].add++;
  61.        tree[root].t2=tree[root].t1;
  62.        tree[root].t1=t0;
  63.    } else
  64.    {
  65.        int tm=(tl+tr)/2;
  66.        update(root*2+1,tl,tm,l,r);
  67.        update(root*2+2,tm+1,tr,l,r);
  68.        tree[root].t1=tree[root*2+1].t1+tree[root*2+2].t1;
  69.        tree[root].t2=tree[root*2+1].t2+tree[root*2+2].t2;
  70.        if(tree[root].add % 3 == 1)
  71.        {
  72.            t0=(tr-tl+1)-tree[root].t1-tree[root].t2;
  73.            tree[root].t2=tree[root].t1;
  74.            tree[root].t1=t0;
  75.        } else
  76.        if(tree[root].add % 3 == 2)
  77.        {
  78.            t0=(tr-tl+1)-tree[root].t1-tree[root].t2;
  79.            tree[root].t1=tree[root].t2;
  80.            tree[root].t2=t0;
  81.        }
  82.    }
  83. }
  84. void query(int root,int tl,int tr,int l,int r,int a)
  85. {
  86.    if(tl>tr || tl>r || tr<l || l>r)return;
  87.    if(tl>=l && tr<=r)
  88.    {
  89.        if(a % 3 == 1)ans+=tree[root].t2; else
  90.        if(a % 3 == 2)ans+=tree[root].t1; else
  91.                      ans+=(tr-tl+1)-tree[root].t1-tree[root].t2;
  92.    } else
  93.    {
  94.        int tm=(tl+tr)/2;
  95.        a+=tree[root].add;
  96.        query(root*2+1,tl,tm,l,r,a);
  97.        query(root*2+2,tm+1,tr,l,r,a);
  98.    }
  99. }
  100. int main()
  101. {
  102.  FREOPEN("input.txt","output.txt");
  103.  int n,q,type,l,r;
  104.  scanf("%d%d",&n,&q);
  105.  rep(i,q)
  106.  {
  107.      scanf("%d%d%d",&type,&l,&r);
  108.      if(!type)update(0,0,n-1,l,r); else
  109.      {
  110.          ans=0;
  111.          query(0,0,n-1,l,r,0);
  112.          printf("%d\n",ans);
  113.      }
  114.  }
  115.  return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement