線段樹之poj 2777

Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30727   Accepted: 9174

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

題意

       兩種操作,一是將A到B區間內的顏色全部染成C顏色,另外就是查詢A至B區間內有多少種顏色。A B大小關係不確定。(這道題當時用線段樹提交上去後居然超時了,瞬間傻眼,後來才發現是h數組開大了,所有顏色種類不超過30,而我開個100000,杯具啊。。。)

代碼:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

struct node{
    int left,right,mid,color;
}tree[400010];
int  h[31];

void build(int le,int ri,int num)
{
    int ls,rs;
    ls=num<<1;rs=num<<1|1;
    tree[num].left=le;
    tree[num].right=ri;
    tree[num].mid=(le+ri)>>1;
    tree[num].color=1; //  開始時都爲塗有顏色1
    if(le==ri)
        return ;
    build(le,tree[num].mid,ls);
    build(tree[num].mid+1,ri,rs);
}

void change(int le,int ri,int num,int col)
{
    int ls=num<<1,rs=num<<1|1;
    if(tree[num].left==le&&tree[num].right==ri)
    {
        tree[num].color=col;
        return ;
    }
    if(tree[num].color==col)   // 這個剪枝還優40多MS,效果還蠻不錯的。
        return;
//    printf("cha%d %d\n",num,tree[num].color);
    if(tree[num].color)
    {
        tree[ls].color = tree[num].color;
        tree[rs].color = tree[num].color;
        tree[num].color = 0;
    }
    if(ri<=tree[num].mid)
        change(le,ri,ls,col);
    else if(le>tree[num].mid)
        change(le,ri,rs,col);
    else
    {
        change(le,tree[num].mid,ls,col);
        change(tree[num].mid+1,ri,rs,col);
    }
}

void sove(int le,int ri,int num)
{
    int ls=num<<1,rs=num<<1|1;
    if(tree[num].color)
    {
        h[tree[num].color]=1;
        return;
    }
 //   printf("sov%d %d\n",num,h[tree[num].color]);
    if(ri<=tree[num].mid)
        sove(le,ri,ls);
    else if(le>tree[num].mid)
        sove(le,ri,rs);
    else
    {
        sove(le,tree[num].mid,ls);
        sove(tree[num].mid+1,ri,rs);
    }
}

int main()
{
    int L,T,O,i,j,le,ri,col,sum;
    char z;
    scanf("%d %d %d",&L,&T,&O);
        getchar();
        build(1,L,1);
        memset(h,0,sizeof(h));
        for(i=0;i<O;i++)
        {
            scanf("%c %d %d",&z,&le,&ri);
            getchar();//防止將換行符存入z中
            if(z=='C')
            {
                scanf("%d",&col);
                getchar();//同上
                if(le<=ri)  
                   change(le,ri,1,col);
                else
                    change(ri,le,1,col);

             }
             else
             {
                if(le<=ri)
                    sove(le,ri,1);
                else
                    sove(ri,le,1);
                for(j=1,sum=0;j<=T;j++)
                    sum += h[j];
                 printf("%d\n",sum);
                 memset(h,0,sizeof(h));//此處一定要注意清零
             }
        }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章