【CF915E】Physical Education Lessons(动态开点)

这是一道线段树动态开点裸题

空间一定能开多大开多大

#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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值