E. Physical Education Lessons 动态开辟线段树区间更新
This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is very soon, but, unfortunately, Alex still hasn't attended a single lesson!
Since Alex doesn't want to get expelled, he wants to know the number of working days left until the end of the term, so he can attend physical education lessons during these days. But in BSU calculating the number of working days is a complicated matter:
There are n days left before the end of the term (numbered from 1 to n), and initially all of them are working days. Then the university staff sequentially publishes q orders, one after another. Each order is characterised by three numbers l, r and k:
- If k = 1, then all days from l to r (inclusive) become non-working days. If some of these days are made working days by some previous order, then these days still become non-working days;
- If k = 2, then all days from l to r (inclusive) become working days. If some of these days are made non-working days by some previous order, then these days still become working days.
Help Alex to determine the number of working days left after each order!
The first line contains one integer n, and the second line — one integer q (1 ≤ n ≤ 109, 1 ≤ q ≤ 3·105) — the number of days left before the end of the term, and the number of orders, respectively.
Then q lines follow, i-th line containing three integers li, ri and ki representing i-th order (1 ≤ li ≤ ri ≤ n, 1 ≤ ki ≤ 2).
Print q integers. i-th of them must be equal to the number of working days left until the end of the term after the first i orders are published.
4
6
1 2 1
3 4 1
2 3 2
1 3 2
2 4 1
1 4 2
2
0
2
3
1
4
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=15000000; int rt=1,LL[N],RR[N],V[N],tot=1,lazy[N]; void pdw(int x,int val){ if(lazy[x]!=-1) { if(!LL[x]) LL[x]=++tot;if(!RR[x]) RR[x]=++tot; if(lazy[x]) V[LL[x]]=(val+1)/2,V[RR[x]]=val/2; else V[LL[x]]=V[RR[x]]=0; lazy[LL[x]]=lazy[RR[x]]=lazy[x]; lazy[x]=-1; } } void update(int &x,int l,int r,int L,int R,bool f){ if(!x) x=++tot; if(L>=l&&R<=r) { if(f) V[x]=R-L+1;else V[x]=0; lazy[x]=f; return; } pdw(x,R-L+1); int mid=(L+R)>>1; if(l<=mid) update(LL[x],l,r,L,mid,f); if(r>mid) update(RR[x],l,r,mid+1,R,f); V[x]=V[LL[x]]+V[RR[x]]; } int main(){ int n,m,x,y,z; scanf("%d%d",&n,&m); memset(lazy,-1,sizeof(lazy)); for(int i=1;i<=m;++i) { scanf("%d%d%d",&x,&y,&z); update(rt,x,y,1,n,z==1); printf("%d\n",n-V[1]); } }