POJ 2777(线段树)

                                                                                  Count Color

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 42507   Accepted: 12856

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.

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

题目大意:

有L个画板,30种颜色,o个操作:P a b :询问a-b 种有多少种颜色不同的,C  a b c:把a-b全部涂成c的颜色(覆盖掉)

解题思路:

 线段树+二进制判重

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int n,m,q;
char c;
int x,y,z;
int sum;
struct Node
{
    int l,r,col;//col用位运算,颜色30种, 
    int cover;//延时更新,是否涂了颜色
}no[4*N];
void pushup(int u)
{
    no[u].col=no[u*2].col|no[u*2+1].col;
    return;
}
void pushdown(int u)
{
    no[u].cover=0;
    no[u*2].cover=1;
    no[u*2].col=no[u].col;
    no[u*2+1].cover=1;
    no[u*2+1].col=no[u].col;
    return;
}
void build(int u,int left,int right)
{
    no[u].l=left;
    no[u].r=right;
    no[u].col=1;
    no[u].cover=0;//初始状态全为1 
    if(left==right)
    return;//没有赋值 
    int mid=(no[u].l+no[u].r)>>1;
    build(u*2,left,mid);
    build(u*2+1,mid+1,right);
    pushup(u);
}
void updata(int u,int left,int right,int val)
{
    if(no[u].l==left&&no[u].r==right)
    {
        no[u].col=1<<(val-1);//直接等于,覆盖原有颜色 
        no[u].cover=1;
        return;
    }
    if(no[u].col==1<<(val-1))return;//剪枝:如果颜色一样,不用更新 
    if(no[u].cover)pushdown(u);//延时更新
    int mid=(no[u].l+no[u].r)>>1;
    if(right<=mid)updata(u*2,left,right,val);
    else if(left>mid)updata(u*2+1,left,right,val);
    else 
    {
        updata(u*2,left,mid,val);
        updata(u*2+1,mid+1,right,val);
    } 
    pushup(u);
}
void query(int u,int left,int right)
{
    if(no[u].l==left&&no[u].r==right)
    {
        sum|=no[u].col;
        return ;
    }
    if(no[u].cover)pushdown(u);
    int mid=(no[u].l+no[u].r)>>1;
    if(right<=mid)query(u*2,left,right);
    else if(left>mid)query(u*2+1,left,right);
    else 
    {
        query(u*2,left,mid);
        query(u*2+1,mid+1,right);
    }
}
int Ans(int sum)
{
    int ans=0;
    while(sum)
    {
        if(sum&1)
        ans++;
        sum=(sum>>1);
    }
    return ans;
}
int main()
{
    freopen("poj2777.in","r",stdin);
    freopen("poj2777.out","w",stdout);
    scanf("%d%d%d",&n,&m,&q);
    build(1,1,n);
    getchar();
    for(int i=1;i<=q;i++)
    {
        scanf("%s",&c);
        if(c=='C')
        {
            scanf("%d%d%d",&x,&y,&z);
            if(x>y)swap(x,y);
            getchar();
            updata(1,x,y,z);
        }
        else
        {
            scanf("%d%d",&x,&y);
            getchar();
            sum=0;
            if(x>y)swap(x,y);//x可能>y 
            query(1,x,y);
            cout<<Ans(sum)<<endl;;
        }
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/abcfrey/p/5668991.html