fork(1) download
  1. // just segt.. i want this one to TLE
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. struct node{
  6. int count;
  7. bool coin;
  8. };
  9. node st[100005];
  10. void init(int lo,int hi,int idx){
  11. for(int i=0;i<100005;i++){
  12. st[i].coin=false;
  13. st[i].count=0;
  14. }
  15. return;
  16. if(lo==hi){
  17. st[idx].coin=false;//means its tails...
  18. st[idx].count=0;
  19. return ;
  20. }
  21. init(lo,(lo+hi)/2,2*idx);
  22. init(((lo+hi)/2) +1,hi,2*idx+1);
  23. st[idx].count=(st[2*idx].count + st[2*idx+1].count);
  24. }
  25.  
  26. void flip(int lo,int hi,int s,int e,int idx){
  27. if(s>hi || e<lo)return ;
  28. if(lo==hi){
  29. st[lo].coin=1-st[lo].coin;
  30. if(st[lo].coin)st[lo].count=1;
  31. else st[lo].count=0;
  32. return ;
  33. }
  34. flip(lo,(lo+hi)/2, s,e,2*idx);
  35. flip(((lo+hi)/2) +1,hi, s,e,2*idx+1);
  36. st[idx].count=st[2*idx].count + st[2*idx+1].count;
  37. }
  38. node combine(node l,node r){
  39. node pp;
  40. pp.count=l.count+r.count;
  41. return pp;
  42. }
  43. node query(int lo,int hi,int s,int e,int idx){
  44. //if(s>hi || e<lo)return 0;//nasty..
  45. if(lo>=s && hi<=e)return st[idx];
  46. int mid=((lo+hi)/2);
  47. if(mid<hi)
  48. query(lo,(lo+hi)/2,s,e,2*idx);
  49. else
  50. query((lo+hi)/2,hi,s,e,2*idx+1);
  51. return combine(query(lo,mid,s,mid,2*idx),query(mid+1,hi,mid+1,e,2*idx+1));
  52. }
  53. int main(){
  54. int n,q,i;
  55. init(1,n,1);
  56. scanf("%d%d", &n,&q);
  57. for(i=0;i<n;i++){
  58. int tp,a,b;
  59. scanf("%d%d%d",&tp,&a,&b);
  60. a+=1;b+=1;
  61. if(tp==0)flip(1,n,a,b,1);
  62. else printf("%d\n",query(1,n,a,b,1).count);
  63. }
  64. return 0;
  65. }
  66.  
Runtime error #stdin #stdout 0.01s 12296KB
stdin
Standard input is empty
stdout
Standard output is empty