HDU5023A Corrupt Mayor's Performance Art (2014 ACM/ICPC Asia Regional Guangzhou Online)

bigbigship發表於2014-09-23

分析:

線段樹模板題(區間更新 ,區間求和)

初始的顏色為2,一開始設成1了掛了好幾發

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=1000001;
struct node
{
	int left,right,cover;
};
node tree[MAX*4];
bool used[31];
void built(int L,int R,int id)//建樹
{
	tree[id].cover=2;//初始化顏色
	tree[id].left=L;
	tree[id].right=R;
	if(L<R)
	{
		int mid=(L+R)/2;
		built(L,mid,2*id); //左子樹
		built(mid+1,R,2*id+1); //右子樹
	}
}
void insert(int id,int L,int R,int col)
{
	if(tree[id].left>=L&&tree[id].right<=R)
	{
		tree[id].cover=col;
		return;
	}
	if(tree[id].left<tree[id].right)
	{
		int mid=(tree[id].left+tree[id].right)>>1;
		if(tree[id].cover>0)
		{
			tree[id*2].cover=tree[id].cover;
			tree[id*2+1].cover=tree[id].cover;
		}
		tree[id].cover=-1;
		if(R<=mid)
			insert(id*2,L,R,col);
		else if(L>mid)
			insert(id*2+1,L,R,col);
		else
		{
			insert(id*2,L,mid,col);
			insert(id*2+1,mid+1,R,col);
		}
	}
}
void cnt(int L,int R,int id)
{
	if(tree[id].cover>0)
	{
		used[tree[id].cover]=true;
		return;
	}
	if(tree[id].left<tree[id].right)
	{
		int mid=(tree[id].left+tree[id].right)>>1;
		if(R<=mid)
			cnt(L,R,id*2);
		else if(L>mid)
			cnt(L,R,id*2+1);
		else
		{
			cnt(L,mid,id*2);
			cnt(mid+1,R,id*2+1);
		}
	}
}
int main()
{
    int n,Q;
    while(~scanf("%d%d",&n,&Q)){
        if(n==0&&Q==0)
            break;
        built(1,n,1);
        int a,b,c;
        char ord[2];
        while(Q--){
            scanf("%s",ord);
            if(ord[0]=='P'){
                scanf("%d%d%d",&a,&b,&c);
                if(a<b) insert(1,a,b,c);
                else insert(1,b,a,c);
            }
            else{
                scanf("%d%d",&a,&b);
                memset(used,0,sizeof(used));
                if(a<b) cnt(a,b,1);
                else cnt(b,a,1);
                bool tt=1;
                for(int i=1;i<=30;i++){
                   if(used[i]){
                    if(tt) tt=0;
                    else printf(" ");
                    printf("%d",i);
                   }
                }
                puts("");
            }
        }
    }
	return 0;
}


相關文章