HDU 4614(线段树)

成端更新+区间合并

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define lc l,m,index<<1
#define rc m+1,r,index<<1|1
#define inf 0x3f3f3f3f
#define ll long long
#define N 100005
struct node
{
	int temp,rmost,cnt,clean;
}seg[N<<2];
int n,m,k,a,b;
void cl(int l,int r,int index)
{
	seg[index].rmost=r;
	seg[index].cnt=r-l+1;
	seg[index].clean=1;
}
void ffull(int l,int r,int index)
{
	seg[index].clean=2;
	seg[index].cnt=0;
	seg[index].rmost=-1;
}
void pushup(int l,int r,int index)
{
	node& father=seg[index];
	node& lson=seg[index<<1];
	node& rson=seg[index<<1|1];
	father.rmost=max(lson.rmost,rson.rmost);
	father.cnt=lson.cnt+rson.cnt;
}
void pushdown(int l,int r,int index)
{
	int m=(l+r)>>1;
	node& father=seg[index];
	node& lson=seg[index<<1];
	node& rson=seg[index<<1|1];
	if(father.clean==1)
	{
		cl(lc);
		cl(rc);
		father.clean=0;
	}
	else if(father.clean==2)
	{
		ffull(lc);
		ffull(rc);
		father.clean=0;
	}
}
void build(int l,int r,int index)
{
	int m=(l+r)>>1;
	seg[index].clean=0;
	seg[index].cnt=r-l+1;
	seg[index].rmost=r;
	if(l==r)return;
	build(lc);
	build(rc);
}
int findl(int a,int l,int r,int index)
{
	int ret;
	int m=(l+r)>>1;
	if(l==r)
	{
		seg[index].temp=1;
		return l;
	}
	pushdown(l,r,index);
	if(seg[index<<1].rmost>=a)
	{
		ret=findl(a,lc);
		seg[index].temp=seg[index<<1].temp+seg[index<<1|1].cnt;
		return ret;
	}
	else 
	{
		ret=findl(a,rc);
		seg[index].temp=seg[index<<1|1].temp;
		return ret;
	}
}
int findr(int fl,int f,int l,int r,int index)
{
	int m=(l+r)>>1;
	if(l==r)return l;
	pushdown(l,r,index);
	if(fl>m)return findr(fl,f,rc);
	else if(fl<=m&&fl>=l)
	{
		if(f>seg[index<<1].temp&&seg[index<<1|1].cnt>0)return findr(fl,f-seg[index<<1].temp,rc);
		else return findr(fl,f,lc);
	}
	else
	{
		if(f>seg[index<<1].cnt&&seg[index<<1|1].cnt>0)return findr(fl,f-seg[index<<1].cnt,rc);
		else return findr(fl,f,lc);
	}
}
void insert(int L,int R,int l,int r,int index)
{
	int m=(l+r)>>1;
	if(L==l&&R==r)
	{
		ffull(l,r,index);
		return;
	}
	pushdown(l,r,index);
	if(R<=m)insert(L,R,lc);
	else if(L>m)insert(L,R,rc);
	else
	{
		insert(L,m,lc);
		insert(m+1,R,rc);
	}
	pushup(l,r,index);
}
int get(int L,int R,int l,int r,int index)
{
	int m=(l+r)>>1;
	if(L==l&&R==r)
	{
		return seg[index].cnt;;
	}
	pushdown(l,r,index);
	if(R<=m)return get(L,R,lc);
	else if(L>m)return get(L,R,rc);
	else
	{
		return get(L,m,lc)+get(m+1,R,rc);
	}
}
void del(int L,int R,int l,int r,int index)
{
	int m=(l+r)>>1;
	if(L==l&&R==r)
	{
		cl(l,r,index);
		return;
	}
	pushdown(l,r,index);
	if(R<=m)del(L,R,lc);
	else if(L>m)del(L,R,rc);
	else
	{
		del(L,m,lc);
		del(m+1,R,rc);
	}
	pushup(l,r,index);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		build(0,n-1,1);
		while(m--)
		{
			scanf("%d%d%d",&k,&a,&b);
			if(k==1)
			{
				int fl,fr;
				if(seg[1].rmost


你可能感兴趣的:(ACM算法,线段树)