这是一道线段树动态开点裸题
空间一定能开多大开多大
#include<bits/stdc++.h>
#define N 15000005
using namespace std;
template<class T>
inline void read(T &x)
{
x=0;
static char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
}
int n,Q,tot,root,lson[N],rson[N],sum[N],lazy[N];
inline void pushdown(int now,int l,int r)
{
if(lazy[now]==-1) return;
int k=lazy[now],m=(l+r)>>1;
if(!lson[now]) lson[now]=++tot;
sum[lson[now]]=k*(m-l+1);
lazy[lson[now]]=lazy[now];
if(!rson[now]) rson[now]=++tot;
sum[rson[now]]=k*(r-(m+1)+1);
lazy[rson[now]]=lazy[now];
lazy[now]=-1;
}
void modify(int &now,int l,int r,int ql,int qr,int k)
{
if(!now) now=++tot;
if(ql<=l&&r<=qr)
{
sum[now]=(r-l+1)*k;
lazy[now]=k;
return;
}
pushdown(now,l,r);
int m=(l+r)>>1;
if(ql<=m) modify(lson[now],l,m,ql,qr,k);
if(qr>m) modify(rson[now],m+1,r,ql,qr,k);
sum[now]=sum[lson[now]]+sum[rson[now]];
}
int main()
{
cin>>n>>Q;
root=tot=1;
memset(lazy,-1,sizeof(lazy));
while(Q--)
{
int l,r,k;
read(l); read(r); read(k);
if(k==1) modify(root,1,n,l,r,1);
else modify(root,1,n,l,r,0);
printf("%d\n", n-sum[1]);
}
return 0;
}