POJ Training Plan 2777_count Color (segment tree/segment update/interval staining)

Source: Internet
Author: User

Problem Solving Report

Test instructions

Dye the line segment. Ask about the number of color types in the segment range.

Ideas:

Originally dyed directly on the line segment tree, the LZ flag color. For each query, visit the segment tree to find out the number of colors. The result has timed out, and in the worst case, staining can dye the leaf nodes.

Change the number of colors in the interval so that each query does not have to find the leaf node. Use a bitwise OR to process the number of colors.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int sum[500000],lz[500000],ans;void push_down (int rt,int l,int R) {if (Lz[rt]) {lz[rt<<1]=lz[rt<        <1|1]=lz[rt];        sum[rt<<1]=lz[rt];        sum[rt<<1|1]=lz[rt];    lz[rt]=0; }}void push_up (int rt,int l,int r) {sum[rt]=sum[rt<<1]|sum[rt<<1|1];}        void Cbtree (int rt,int l,int R) {if (l==r) {sum[rt]=1;    return;    } int mid= (L+R) >>1;    Cbtree (Rt<<1,l,mid);    Cbtree (RT&LT;&LT;1|1,MID+1,R); PUSH_UP (rt,l,r);} void update (int rt,int l,int r,int ql,int Qr,int v) {if (ql>r| |    QR&LT;L) return;        if (QL&LT;=L&AMP;&AMP;R&LT;=QR) {lz[rt]=v;        Sum[rt]=v;    return;    } int mid= (L+R) >>1;    Push_down (RT,L,R);    Update (RT&LT;&LT;1,L,MID,QL,QR,V);    Update (RT&LT;&LT;1|1,MID+1,R,QL,QR,V); PUSH_UP (rt,l,r);} int _q (int rt,int l,int r,int ql,int qr) {if (ql>r| |    QR&LT;L) return 0;    if (QL&LT;=L&AMP;&AMP;R&LT;=QR) {return SUM[RT];    } push_down (Rt,l,r);    int mid= (L+R) >>1; Return _q (RT&LT;&LT;1,L,MID,QL,QR) | _q (RT&LT;&LT;1|1,MID+1,R,QL,QR);}    int main () {int n,t,q,ql,qr,i,j,a,k;    Char str[10];    scanf ("%d%d%d", &n,&t,&q);    Cbtree (1,1,n);        while (q--) {scanf ("%s", str);            if (str[0]== ' C ') {scanf ("%d%d%d", &ql,&qr,&a);            if (QL&GT;QR) swap (QL,QR);        Update (1,1,n,ql,qr,1<< (A-1));            } else {scanf ("%d%d", &AMP;QL,&AMP;QR);            if (QL&GT;QR) swap (QL,QR);            Ans=_q (1,1,N,QL,QR);            int cnt=0;                while (ans) {if (ans%2) cnt++;            ans/=2;        } printf ("%d\n", CNT); }} return 0;}


Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 35143 Accepted: 10591

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-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 had very few words to describe a color (red, green, blue, yellow ...), so if you could assume that the Tota L 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 <= 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 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 4C 1 1 2P 1 2C 2 2 2P 1 2

Sample Output

21st

POJ Training Plan 2777_count Color (segment tree/segment update/interval staining)

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.