线段树模板求调
查看原帖
线段树模板求调
333800
qip101楼主2022/10/11 23:04
#include <bits/stdc++.h>
#define MAXN 500010 
using namespace std;
int w[MAXN],lzy[MAXN],length,m;
void pushup(const int u)
{
	w[u]=w[u*2]+w[u*2+1];
}
void build(const int u,int l,int r)
{
	if(l==r)
		w[u]=0;
	int M=(l+r)/2;
	build(u*2,l,M);
	build(u*2+1,M+1,r);
	pushup(u);
}
bool InRange(int l,int r,int L,int R)
{
	return (r<=R) && (l>=L);
}
bool OutofRange(int l,int r,int L,int R)
{
	return (l>r) || (r<L);
}
void maketag(int u,int len,int p)
{
	lzy[u]+=p;
	w[u]+=len*p;
}
void pushdown(const int u,int L,int R)
{
	int M=(L+R)/2;
	maketag(u*2,M-L+1,lzy[u]);
	maketag(u*2+1,R-M,lzy[u]);
	lzy[u]=0;
}
void update(const int u,int L,int R,int l,int r,int p)
{
	if(InRange(l,r,L,R))
		maketag(u,R-L+1,p);
	else if(!OutofRange(l,r,L,R)) 
	{
		int M=(l+r)/2;
		pushdown(u,l,r);
		update(u*2,L,R,l,M,p);
		update(u*2+1,L,R,M+1,R,p);
		pushup(u);
	}
}
int query(const int u,int L,int R,int l,int r)
{
	if(InRange(l,r,L,R))
		return w[u];
	else if(!OutofRange(l,r,L,R))
	{
		int M=(l+r)/2;
		pushdown(u,l,r);
		return query(u*2,L,R,l,M)+query(u*2+1,L,R,M+1,r);
	}
	else
		return 0;
}
int main()
{
	cin >> length >> m;
	build(1,1,m);
	while(m--)
	{
		int u,v;
		cin >> u >> v;
		update(1,1,m,u,v,1);
	}
	cout << length-query(1,1,m,1,m) << endl;
	return 0;
}

2022/10/11 23:04
加载中...