【线段树】 HDU 5025 A Corrupt Mayor's Performance Art

更新区间内颜色

输出区间内的颜色总数

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 5040;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 0x3f3f3f3f;
const int mod = 10000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid (l+r)>>1
#define maxn 1100000
int sum[maxn<<2],col[maxn<<2];
int num[34];
void pushup(int rt) //向上更新
{
    sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void pushdown(int rt)
{
    if(col[rt]) //向下更新
    {
        col[rt<<1]=col[rt<<1|1]=col[rt];
        sum[rt<<1]=sum[rt<<1|1]=sum[rt];
        col[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    sum[rt]=num[2];//初始为颜色1
    if(l==r)
    {
        return;
    }
    int m=mid;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        sum[rt]=num[c];
        col[rt]=num[c];
        return;
    }
    pushdown(rt);
    int m=mid;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return sum[rt];
    }
    pushdown(rt);
    int m=mid;
    int res=0;
    if(L<=m) res=res|query(L,R,lson);
    if(R>m) res=res|query(L,R,rson);
    return res;
}
int main()
{
    int n,q;
    memset(num,0,sizeof(num));
    num[1]=1;
    for(int i=2; i<=30; i++) //先储存每种颜色。。。
        num[i]=num[i-1]<<1;
    //IN;
    while(scanf("%d%d ",&n,&q),n+q)
    {
        build(1,n,1);
        while(q--)
        {
            char s[3];
            scanf("%s",s);
            int a,b,c;
            if(s[0]=='Q')
            {
                int ans[33],wei=1,point=0;
                scanf("%d%d",&a,&b);
                int x=query(a,b,1,n,1);
                while(x)//统计颜色个数
                {
                    if(x&1==1)
                        ans[point++]=wei;
                    x=x>>1;
                    wei++;
                }
                for(int i=0;i<point;i++)
                    printf("%d%c",ans[i],i==point-1?'\n':' ');
            }
            else
            {
                scanf("%d%d%d",&a,&b,&c);
                update(a,b,c,1,n,1);
            }
        }
    }
    return 0;
}


posted @ 2014-09-20 18:59  kewowlo  阅读(119)  评论(0编辑  收藏  举报