fork download
  1. // segfill - https://o...content-available-to-author-only...i.info/problem/tinhyeumangtheo_segfill
  2. #include<iostream>
  3. using namespace std;
  4. #define ll long long
  5. const ll maxn=100000;
  6. ll max(const ll& a,const ll& b){return(a>b)?a:b;}
  7. ll min(const ll& a,const ll& b){return(a<b)?a:b;}
  8. struct node{ll val=0,laz=-1;}st[4*maxn+5];
  9. ll n,q,t,l,r;
  10. void dd(ll id,ll l,ll r){ // hàm down
  11. if(st[id].laz==-1){return;}
  12. ll t=st[id].laz,m=l+r>>1;
  13. st[2*id].val=t*(m-l+1);
  14. st[2*id].laz=t;
  15. st[2*id+1].val=t*(r-m);
  16. st[2*id+1].laz=t;
  17. st[id].laz=-1;
  18. }
  19. void upd(ll id,ll l,ll r,ll u,ll v,ll val){ // hàm update
  20. if(u>r||v<l){return;}
  21. if(u<=l&&v>=r){
  22. st[id].val=val*(r-l+1);st[id].laz=val;
  23. return;
  24. }
  25. dd(id,l,r);
  26. ll m=l+r>>1;
  27. upd(2*id,l,m,u,v,val);
  28. upd(2*id+1,m+1,r,u,v,val);
  29. st[id].val=st[2*id].val+st[2*id+1].val;
  30. }
  31. ll quer(ll id,ll l,ll r,ll u,ll v){ // hàm query
  32. if(u>r||v<l){return 0;}
  33. if(u<=l&&v>=r){return st[id].val;}
  34. dd(id,l,r);
  35. ll m=l+r>>1;
  36. return quer(2*id,l,m,u,v)+quer(2*id+1,m+1,r,u,v);
  37. }
  38. int main(){
  39. cin.tie(0)->sync_with_stdio(0);
  40. cin>>n>>q;
  41. while(q--){
  42. cin>>t>>l>>r;
  43. if(t==1){
  44. upd(1,1,n,l,r,1);
  45. }else if(t==0){
  46. upd(1,1,n,l,r,0);
  47. }else{
  48. cout<<quer(1,1,n,l,r)<<"\n";
  49. }
  50. }
  51. }
  52.  
Success #stdin #stdout 0.01s 5308KB
stdin
7 7
1 1 5
0 2 3
2 1 4
1 3 7
1 3 5
0 4 7
2 1 7
stdout
2
2