POJ 2777 Count Color

Source: Internet
Author: User

C-Count ColorTime limit:MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64u SubmitStatusPractice POJ 2777Appoint Description:System Crawler (2015-07-22)

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

There are two methods: one is to count the color of the corresponding interval delay tag on each query, which can be implemented with a set or a simple hash.

One is to use a binary representation of the corresponding interval coated with the first several colors, so that each interval in addition to the delay tag, you can open an array of statistics currently painted which color. This is the same as the normal line tree.

Finally, let's count the number of 1.

#include <iostream> #include <cstdio> #include <cstring>using namespace std; #define MAXN 100005# Define INF 0x3f3f3f3ftypedef long long Ll;int sum[maxn<<2],col[maxn<<2],ll[maxn<<2],rr[maxn< <2];inline void pushup (int i) {sum[i]=sum[i<<1]|sum[i<<1|1];}        inline void pushdown (int i) {if (Col[i]) {col[i<<1]=col[i<<1|1]=col[i];        sum[i<<1]=col[i];        sum[i<<1|1]=col[i];    col[i]=0;    }}void Build (int l,int r,int i) {ll[i]=l;    Rr[i]=r;    Col[i]=1;    if (l==r) return;    int m= (ll[i]+rr[i]) >>1,ls=i<<1,rs=ls|1;    Build (L,m,ls);    Build (M+1,R,RS); Pushup (i);}        void update (int l,int r,int V,int i) {if (l<=ll[i]&&rr[i]<=r) {col[i]=1<< (V-1);        Sum[i]=col[i];    return;    } pushdown (i);    int m= (ll[i]+rr[i]) >>1,ls=i<<1,rs=ls|1;    if (l<=m) update (L,R,V,LS);    if (m<r) update (L,R,V,RS); Pushup (i);} int query (int l,int r,inti) {if (l<=ll[i]&&rr[i]<=r) {return sum[i];    } pushdown (i);    int m= (ll[i]+rr[i]) >>1,ls=i<<1,rs=ls|1;    int ans=0;    if (l<=m) ans=ans|query (L,R,LS);    if (m<r) ans=ans|query (L,R,RS); return ans;}    int main () {int l,t,o,a,b,c;    Char q[2];    Freopen ("In.txt", "R", stdin);        while (~SCANF ("%d%d%d", &l,&t,&o)) {build (1,l,1);            for (int i=0;i<o;i++) {scanf ("%s%d%d", q,&a,&b);            if (a>b) swap (A, b);                if (q[0]== ' C ') {scanf ("%d", &c);            Update (a,b,c,1);                } else {int res=query (a,b,1);                int ans=0;                    while (res) {if (res&1) ans++;                res=res>>1;            } printf ("%d\n", ans); }        }    }}

#include <iostream> #include <cstdio> #include <cstring>using namespace std; #define MAXN 100005# Define INF 0x3f3f3f3ftypedef long long ll;int ll[maxn<<2],rr[maxn<<2],col[maxn<<2],vis[32];int ans;        inline void pushdown (int i,int m) {if (Col[i]) {col[i<<1]=col[i<<1|1]=col[i];    col[i]=0;   }}void Build (int l,int r,int i) {ll[i]=l;   Rr[i]=r;   Col[i]=1;   if (l==r) {return;   } int m= (Ll[i]+rr[i]) >>1,ls=i<<1,rs=ls|1;   Build (L,m,ls); Build (M+1,r,rs);}    void update (int l,int r,int V,int i) {if (l<=ll[i]&&rr[i]<=r) {col[i]=v;   return;   } pushdown (i,rr[i]-ll[i]+1);   int m= (ll[i]+rr[i]) >>1,ls=i<<1,rs=ls|1;   if (l<=m) update (L,R,V,LS); if (m<r) update (L,R,V,RS);}        void query (int l,int r,int i) {if (Col[i]) {if (!vis[col[i]]) {ans++;      Vis[col[i]]=1;   } return;   } pushdown (i,rr[i]-ll[i]+1);   int m= (ll[i]+rr[i]) >>1,ls=i<<1,rs=ls|1; Ifl<=m) query (L,R,LS); if (m<r) query (L,R,RS);}    int main () {int l,t,o,a,b,c;    Char q[2];    Freopen ("In.txt", "R", stdin);        while (~SCANF ("%d%d%d", &l,&t,&o)) {build (1,l,1);            for (int i=0;i<o;i++) {scanf ("%s%d%d", q,&a,&b);            if (a>b) swap (A, b);                if (q[0]== ' C ') {scanf ("%d", &c);            Update (a,b,c,1);                } else {ans=0;                memset (vis,0,sizeof Vis);                Query (a,b,1);            printf ("%d\n", ans); }        }    }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2777 Count Color

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.