POJ 2777 Count Color 线段树涂色问题

Count Color

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 50943   Accepted: 15366

Description

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. 

There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board: 

1. "C A B C" Color the board from segment A to segment B with color C. 
2. "P A B" Output the number of different colors painted between segment A and segment B (including). 

In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your. 

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output

2
1

Source

POJ Monthly--2006.03.26,dodo

一开始纠结如何记录每个节点的覆盖情况

后来发现只有30个颜色完全可以用二进制来表示

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1e5+10;
struct node
{
    int l,r;
    int tag,num;
}tree[maxn<<2];
void pushup(int rt)
{
    tree[rt].num=tree[rt<<1].num|tree[rt<<1|1].num;
}
void pushdown(int rt)
{
    if(tree[rt].tag)
    {
        tree[rt<<1].tag=tree[rt<<1|1].tag=tree[rt].tag;
        tree[rt<<1].num=tree[rt<<1|1].num=tree[rt].tag;
        tree[rt].tag=0;
    }
}
void build(int l,int r,int rt)
{
    tree[rt].l=l;tree[rt].r=r;
    tree[rt].tag=0;tree[rt].num=1;
    if(l==r)return;
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
}
void update(int x,int y,int rt,int v)
{
    if(x<=tree[rt].l&&tree[rt].r<=y)
    {
        tree[rt].tag=v;
        tree[rt].num=v;
        return ;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(x<=mid) update(x,y,rt<<1,v);
    if(mid<y) update(x,y,rt<<1|1,v);
    pushup(rt);
}
int query(int x,int y,int rt)
{
    if(x<=tree[rt].l&&tree[rt].r<=y)
    {
        return tree[rt].num;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1,ans=0;
    if(x<=mid) ans|=query(x,y,rt<<1);
    if(y>mid) ans|=query(x,y,rt<<1|1);
    pushup(rt);
    return ans;
}
int main()
{
    int n,m,t;
    while(scanf("%d%d%d",&n,&t,&m)!=EOF)
    {
        build(1,n,1);
        while(m--)
        {
            char op[2];
            int t1,t2;
            scanf("%s%d%d",op,&t1,&t2);
            if(t1>t2) swap(t1,t2);
            if(op[0]=='C')
            {
                int t3;
                scanf("%d",&t3);
                update(t1,t2,1,1<<(t3-1));
            }
            else
            {
                int t3=query(t1,t2,1),ans=0;
                while(t3)
                {
                    if(t3&1)
                    ans++;
                    t3=t3>>1;
                }
                printf("%d\n",ans);
            }

        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ljq199926/article/details/81318512