codeforces 915E 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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 using namespace std; 8 queue<int>Q; 9 int pos,ch[15000001][2],c[15000001],n,q,lazy[15000001],root; 10 void pushdown(int rt,int l,int mid,int r) 11 { 12 if (lazy[rt]) 13 { 14 if (!ch[rt][0]&&lazy[rt]==2) 15 ch[rt][0]=++pos; 16 if (!ch[rt][1]&&lazy[rt]==2) 17 ch[rt][1]=++pos; 18 lazy[ch[rt][0]]=lazy[rt]; 19 lazy[ch[rt][1]]=lazy[rt]; 20 c[ch[rt][0]]=(lazy[rt]-1)*(mid-l+1); 21 c[ch[rt][1]]=(lazy[rt]-1)*(r-mid); 22 lazy[rt]=0; 23 } 24 } 25 void update(int &rt,int l,int r,int L,int R,int d) 26 { 27 if (!rt) 28 { 29 if (Q.empty()==0) rt=Q.front(); 30 else rt=++pos; 31 } 32 if (l>=L&&r<=R) 33 { 34 c[rt]=d*(r-l+1); 35 lazy[rt]=d+1; 36 return; 37 } 38 int mid=(l+r)/2; 39 pushdown(rt,l,mid,r); 40 if (L<=mid) update(ch[rt][0],l,mid,L,R,d); 41 if (R>mid) update(ch[rt][1],mid+1,r,L,R,d); 42 c[rt]=c[ch[rt][0]]+c[ch[rt][1]]; 43 } 44 int main() 45 {int l,r,k; 46 cin>>n>>q; 47 while (q--) 48 { 49 scanf("%d%d%d",&l,&r,&k); 50 if (k==1) 51 { 52 update(root,1,n,l,r,1); 53 } 54 else 55 { 56 update(root,1,n,l,r,0); 57 } 58 printf("%d\n",n-c[root]); 59 } 60 }