Link: Click to open the link
Alex graduated from high school. He is now a freshman. Although he studied programming, he still had to take physical education, which was a complete surprise to him. It's almost the end of the term, but unfortunately Alex's PE credits are still zero!
Alex doesn't want to be fired. He wants to know how many working days are left until the end of the term so that he can take sports credits on those days. But calculating workdays here is not easy:
There are still n days (from 1 to n numbers) between now and the end of the semester. They are working days at the beginning. Next, the school staff will issue q instructions in turn. Each instruction can be described by three parameters l,r,k:
If k=1, all days from l to r (including endpoints) become non working days.
If k=2, all days from l to r (including endpoints) become working days.
Help Alex count the remaining working days after each instruction is issued
(1≤n≤10^9,1≤q≤3*10^5)
code:
#include <map> #include <stack> #include <queue> #include <stack> #include <string> #include <vector> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; const int siz=15000005; int n,m,id; int ll[siz],rr[siz],sum[siz],lazy[siz]; void pushdown(int l,int r,int rt){ if(lazy[rt]!=-1){ int m=(l+r)>>1; if(l!=r){ //If it is not a leaf node, it needs to open a left node or a right node if(ll[rt]==0) ll[rt]=++id; //So as to pass on the mark if(rr[rt]==0) rr[rt]=++id; lazy[ll[rt]]=lazy[rt]; lazy[rr[rt]]=lazy[rt]; sum[ll[rt]]=(m-l+1)*lazy[rt]; sum[rr[rt]]=(r-m)*lazy[rt]; } lazy[rt]=-1; } } void change(int L,int R,int p,int l,int r,int &rt){ if(!rt) rt=++id; //Dynamic open point is when to use and when to open point if(L<=l&&r<=R){ lazy[rt]=p; sum[rt]=p*(r-l+1); return; } pushdown(l,r,rt); int m=(l+r)>>1; if(L<=m) change(L,R,p,l,m,ll[rt]); if(R>m) change(L,R,p,m+1,r,rr[rt]); sum[rt]=sum[ll[rt]]+sum[rr[rt]]; } int main(){ //Because n is very large, it can be directly opened dynamically int i,j,x,y,z,rt; //Complexity O(qlogn), complexity may not be direct while(scanf("%d%d",&n,&m)!=EOF){ //Discrete O(qlogq) is excellent, but it is relatively simple to implement id=rt=0; memset(ll,0,sizeof(ll)); memset(rr,0,sizeof(rr)); memset(sum,0,sizeof(sum)); memset(lazy,-1,sizeof(lazy)); for(i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&z); if(z==1) change(x,y,1,1,n,rt); else change(x,y,0,1,n,rt); printf("%d\n",n-sum[1]); } } return 0; } /* 10 3 2 3 1 1 5 1 4 6 1 */