Advertisement
raman92

Untitled

Mar 22nd, 2013
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <fstream>
  5. #include <map>
  6. #include <algorithm>
  7. #include <bitset>
  8. #include <set>
  9. #include <vector>
  10. #include <utility>
  11. #include <cstring>
  12. #include <string>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <climits>
  16. using namespace std;
  17. typedef vector<int> vi;
  18. typedef vector< vi > vvi;
  19. typedef pair<int,int> pi;
  20. typedef vector< pi > vpi;
  21. typedef vector< vpi > vvpi;
  22. #define rep(i,a,b) for(i = a;i<=b;i++)
  23. #define all(c) (c).begin(),(c).end()
  24. #define present(c,x) (c).find(x)!=(c).end()
  25. #define gpresent(c,x) find(all(c),x)
  26. #define tr(c,it) for( typeof( (c).begin()) it = (c).begin();it!=(c).end();it++ )
  27. #define accu(c) accumulate(all(c),0)
  28. #define scalar(c1,c2) inner_product(all(c1),(c2).begin(),0)
  29. #define maxel(c) max_element(all(c))
  30. #define minel(c) min_element(all(c))
  31. #define fx first
  32. #define sx second
  33. #define pb(a) push_back(a)
  34. #define mp(a,b) make_pair(a,b)
  35. #define inf -300010
  36. ifstream in("input.in",ios::in);
  37. ofstream out("output.out",ios::out);
  38. /////////////////////////////////////////////////////////////////////////////////////////////////////
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////
  40. ////////////////////////////////////////////////////////////////////////////////////////////////////
  41. //////////////////////////////////////////START/////////////////////////////////////////////////////
  42. int flag[10000000];
  43. int memo[10000000];
  44.  
  45. int update(int nd,int b,int e,int i,int j){
  46. if(e<i||b>j||e<b)
  47. return 0;
  48.  
  49. if(b>=i&&e<=j){
  50. if(flag[nd]==1){
  51. flag[nd] = 0;
  52. flag[2*nd+1] = (flag[2*nd+1]+1)%2;
  53. flag[2*nd+2] = (flag[2*nd+2]+1)%2;
  54. return 2*memo[nd] - (e-b+1);
  55. }
  56. memo[nd] = (e-b+1)-memo[nd];
  57. flag[2*nd+1] = (flag[2*nd+1]+1)%2;
  58. flag[2*nd+2] = (flag[2*nd+2]+1)%2;
  59. return 2*memo[nd] - (e-b+1);
  60. }
  61.  
  62. if(flag[nd]==1){
  63. flag[nd] = 0;
  64. flag[2*nd+1] = (flag[2*nd+1]+1)%2;
  65. flag[2*nd+2] = (flag[2*nd+2]+1)%2;
  66. memo[nd] = (e-b+1)-memo[nd];
  67. }
  68.  
  69. int p1 = update(2*nd+1,b,(b+e)/2,i,j);
  70. int p2 = update(2*nd+2,(b+e)/2+1,e,i,j);
  71.  
  72. memo[nd]+=p1+p2;
  73. return p1+p2;
  74.  
  75. }
  76.  
  77. int query(int nd,int b,int e,int i,int j){
  78. if(e<i||b>j||e<b)
  79. return -1;
  80.  
  81. if(b>=i&&e<=j){
  82. if(flag[nd]==1){
  83. return (e-b+1)-memo[nd];
  84. }
  85. return memo[nd];
  86. }
  87.  
  88. if(flag[nd]==1){
  89. flag[nd] = 0;
  90. flag[2*nd+1] = (flag[2*nd+1]+1)%2;
  91. flag[2*nd+2] = (flag[2*nd+2]+1)%2;;
  92. memo[nd] = (e-b+1) - memo[nd];
  93. }
  94.  
  95. int p1 = query(2*nd+1,b,(b+e)/2,i,j);
  96. int p2 = query(2*nd+2,(b+e)/2+1,e,i,j);
  97. int sum = 0;
  98.  
  99. if(p1!=-1)
  100. sum+=p1;
  101. if(p2!=-1)
  102. sum+=p2;
  103.  
  104. return sum;
  105. }
  106.  
  107. int main(){
  108. int n,q,i,a,b,t;
  109. cin>>n>>q;
  110.  
  111. rep(i,1,q){
  112. cin>>t>>a>>b;
  113. if(t==0){
  114. update(0,0,n-1,a,b);
  115. }
  116. else
  117. cout<<query(0,0,n-1,a,b)<<"\n";
  118. }
  119. return 0;
  120. }
  121.  
  122. /*TEST ON WHICH IT IS GIVING WRONG:-
  123. 6 11 ans test
  124. 1 0 5 0 correct
  125. 0 0 3
  126. 1 0 3 4 correct
  127. 1 0 1 2 correct
  128. 0 1 2
  129. 1 0 3 2 correct
  130. 0 0 5
  131. 1 0 5 4 correct
  132. 0 2 5
  133. 1 1 1 1 correct
  134. 1 3 3 0 wrong(correct answer is 1)
  135. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement