Segment tree, interval update, bitwise operation: Poj2777count Color

Source: Internet
Author: User
Tags bitwise

Description
Chosen Problem solving and program design as a optional course, you is required to solve all kinds of problems. Here, we get a new problem.

There is a very long board with length L centimeter, L was a positive integer, so we can evenly divide the board into L seg ments, and they is labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we has to color the Board-one segment with only one color. We can do following and operations on the board: "C a B C" Color the board from segment A to segment B with Color C. "P A B "Output the number of different colors painted between segment A and segment B (including).

In our daily life, we had very few words to describe a color (red, green, blue, yellow ...), so if you could assume that T Otal 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 is painted in color 1. Now the rest of problem are left to your.
Input
First line of input contains L (1 <= L <= 100000), T (1 <= t <=) and O (1 <= O <= 100 ). Here O denotes the number of operations. Following O lines, each contains "c a b C" or "P a B" (here A, B, C is integers, and A may is larger than B) as an Operat Ion 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

1, at the beginning of the time did not think to use in place operation, the result is silly smacking to open an array storage state, the results in various changes in the state of time, very happy time-out;
2,cover array is very important, fully embodies the advantages of line tree, if the color of the interval, There is no need to divide the interval;
3, using BITS to save the state of the color is very good (see the color is not more than 30 when you should think of this, the problem is less, not enough sensitive), saving storage space, and convenient back to the State transformation ~

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define Lson l,m,
Rt<<1 #define Rson m+1,r,rt<<1|1 using namespace std;
const int maxn=100009; int N,cover[maxn<<2],color[maxn<<2];//cover Mark whether this interval is the same color, color indicates the colour state of this interval,//Because the color does not exceed 30 kinds,
So you can use each bit of int to represent the color, compared to open an array to be more convenient int temp;

int l,t,o;
void pushup (int rt) {color[rt]=color[rt<<1]|color[rt<<1|1];//-bit operations are efficient.
        } void Pushdown (int rt) {if (Cover[rt]) {cover[rt<<1]=cover[rt<<1|1]=cover[rt];
        color[rt<<1]=color[rt<<1|1]=color[rt];
    cover[rt]=0;
    }} void Build (int l,int R,int RT) {color[rt]=1;
    Cover[rt]=1;
    if (l==r) return;
    int m=l+r>>1;
    Build (Lson);
Build (Rson);
        } void Update (int l,int r,int c,int l,int r,int RT) {if (l<=l&&r<=r) {cover[rt]=1;
        Color[rt]=c;
    Return } if (Color[rt]==c)//If the state at this time is the same as the state to be changed, there is no need toto return;
    int m=l+r>>1;
    Pushdown (RT);
    if (l<=m) update (L,r,c,lson);
    if (r>m) update (L,r,c,rson);
Pushup (RT);
        } void query (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) {TEMP|=COLOR[RT];
    Return
    } if (Cover[rt]) {temp|=color[rt];//If the color is the same, there is no need to continue dividing the interval, very important oh ~ return;
    } int m=l+r>>1;
    Pushdown (RT);
    if (m>=l) query (L,r,lson);
    if (m<r) query (L,r,rson);
Pushup (RT);
    } int main () {scanf ("%d%d%d", &l,&t,&o);
    Build (1,l,1);
        while (o--) {char ch[10];
        int a,c,b;
        scanf ("%s", ch);
            if (ch[0]== ' C ') {scanf ("%d%d%d", &a,&b,&c);
            if (a>b) swap (A, b);
        Update (a,b,1<< (c-1), 1,l,1);
            } else {scanf ("%d%d", &a,&b);
            if (a>b) swap (A, b);
            Temp=0;
            Query (a,b,1,l,1); int sum=0;
                 while (temp) {if (temp&1) sum++;
            temp>>=1;
        } printf ("%d\n", sum);
}} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.