#include<bits/stdc++.h>
using namespace std;
const int lim=2097190;
int t[lim][4];
int n,m,base=1;
int l,r,k;
void makebase(){
while(base<n) base<<=1;
}
void pull(int x, int num){
int x2=x<<1; //printf("x %d\n",x);
t[x][num]=t[x2][num]+t[x2+1][num];
}
void push(int x, int num, int siz){
if(num==0 || num==1){
int siz2=siz-t[x][2];
if(t[x][num]==siz2){
int x2=x<<1;
siz>>=1;
t[x2][num]=siz-t[x2][2]; t[x2+1][num]=siz-t[x2+1][2];
}
}
else{
if(t[x][num]==siz){
int x2=x<<1;
siz>>=1;
t[x2][num]=siz; t[x2+1][num]=siz;
}
}
}
void wyp(){
int pocz=1;
while(pocz<=16){
for(int i=pocz;i<pocz*2;i++) printf("%d ",t[i][1]); printf("\n");
pocz<<=1;
}
}
void update(int a, int b, int typ, int x=1, int low=0, int high=base-1){
//printf("kfdklsdaj");
if(a>b) return;
if(a==low && b==high){
t[x][3]=b+1-a;
t[x][typ]=b+1-a-t[x][2]; //printf("debu: a %d b %d typ %d val: %d\n",a,b,typ,t[x][typ]);
return;
}
int siz=high+1-low;
if(t[x][2]==siz) return;
if(t[x][typ]==(siz-t[x][2])) return;
push(x,3,siz);
int mid=(low+high)>>1;
int x2=x<<1;
update(a,min(b,mid),typ,x2,low,mid);
update(max(mid+1,a),b,typ,x2+1,mid+1,high);
pull(x,typ); pull(x,3);
}
void remove(int a, int b, int x=1, int low=0, int high=base-1){
if(a>b) return;
if(a==low && b==high){
t[x][3]=b+1-a;
//printf("debr: %d %d\n",a,b);
t[x][0]=0; t[x][1]=0; t[x][2]=b+1-a; return;
}
int siz=high+1-low;
if(t[x][2]==siz) return;
push(x,3,siz); push(x,1,siz); push(x,0,siz);
int mid=(low+high)>>1;
int x2=x<<1;
remove(a,min(b,mid),x2,low,mid);
remove(max(mid+1,a),b,x2+1,mid+1,high);
pull(x,1); pull(x,0); pull(x,3); pull(x,2);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
makebase();
while(m--){
cin>>l>>r>>k;
if(k==3) remove(l-1,r-1); else update(l-1,r-1,k-1);
// wyp();
// printf("val %d\n",t[1][1]);
}
// printf("check: %d %d %d %d\n",t[1][0],t[1][1],t[1][2],t[1][3]);
cout<<t[1][0]+t[1][1]-(t[1][3]-t[1][2]);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include<bits/stdc++.h> using namespace std; const int lim=2097190; int t[lim][4]; int n,m,base=1; int l,r,k; void makebase(){ while(base<n) base<<=1; } void pull(int x, int num){ int x2=x<<1; //printf("x %d\n",x); t[x][num]=t[x2][num]+t[x2+1][num]; } void push(int x, int num, int siz){ if(num==0 || num==1){ int siz2=siz-t[x][2]; if(t[x][num]==siz2){ int x2=x<<1; siz>>=1; t[x2][num]=siz-t[x2][2]; t[x2+1][num]=siz-t[x2+1][2]; } } else{ if(t[x][num]==siz){ int x2=x<<1; siz>>=1; t[x2][num]=siz; t[x2+1][num]=siz; } } } void wyp(){ int pocz=1; while(pocz<=16){ for(int i=pocz;i<pocz*2;i++) printf("%d ",t[i][1]); printf("\n"); pocz<<=1; } } void update(int a, int b, int typ, int x=1, int low=0, int high=base-1){ //printf("kfdklsdaj"); if(a>b) return; if(a==low && b==high){ t[x][3]=b+1-a; t[x][typ]=b+1-a-t[x][2]; //printf("debu: a %d b %d typ %d val: %d\n",a,b,typ,t[x][typ]); return; } int siz=high+1-low; if(t[x][2]==siz) return; if(t[x][typ]==(siz-t[x][2])) return; push(x,3,siz); int mid=(low+high)>>1; int x2=x<<1; update(a,min(b,mid),typ,x2,low,mid); update(max(mid+1,a),b,typ,x2+1,mid+1,high); pull(x,typ); pull(x,3); } void remove(int a, int b, int x=1, int low=0, int high=base-1){ if(a>b) return; if(a==low && b==high){ t[x][3]=b+1-a; //printf("debr: %d %d\n",a,b); t[x][0]=0; t[x][1]=0; t[x][2]=b+1-a; return; } int siz=high+1-low; if(t[x][2]==siz) return; push(x,3,siz); push(x,1,siz); push(x,0,siz); int mid=(low+high)>>1; int x2=x<<1; remove(a,min(b,mid),x2,low,mid); remove(max(mid+1,a),b,x2+1,mid+1,high); pull(x,1); pull(x,0); pull(x,3); pull(x,2); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; makebase(); while(m--){ cin>>l>>r>>k; if(k==3) remove(l-1,r-1); else update(l-1,r-1,k-1); // wyp(); // printf("val %d\n",t[1][1]); } // printf("check: %d %d %d %d\n",t[1][0],t[1][1],t[1][2],t[1][3]); cout<<t[1][0]+t[1][1]-(t[1][3]-t[1][2]); } |
English