POJ 2777 Count Color(位运算+线段树+lazy+区间更新)

来源:互联网 发布:网络电视华人台 编辑:程序博客网 时间:2024/06/06 18:03
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 39905 Accepted: 12034

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 4C 1 1 2P 1 2C 2 2 2P 1 2

Sample Output

21写完之后就是wrong,看别人说要用位运算,第一次用位运算ac代码:
#include<stdio.h>struct s{int color;int left;int right;int lazy;}tree[4000000+1000];void pushdown(int i){if(tree[i].lazy){tree[i*2].lazy=tree[i*2+1].lazy=1;tree[i*2].color=tree[i].color;tree[i*2+1].color=tree[i].color;tree[i].lazy=0;}}void build(int l,int r,int i){int mid;tree[i].left=l;tree[i].right=r;tree[i].lazy=1;tree[i].color=1;if(l==r)return;mid=(l+r)/2;build(l,mid,i*2);build(mid+1,r,i*2+1);}void update(int l,int r,int c,int i){int mid;if(tree[i].left>=l&&tree[i].right<=r){tree[i].lazy=1;tree[i].color=(1<<(c-1));return;}pushdown(i);mid=(tree[i].left+tree[i].right)/2;if(r<=mid)update(l,r,c,i*2);else if(l>mid)update(l,r,c,i*2+1);else{update(l,mid,c,i*2);update(mid+1,r,c,i*2+1);}tree[i].color=tree[i*2].color|tree[i*2+1].color;}int query(int l,int r,int i){int mid;if(l<=tree[i].left&&r>=tree[i].right)return tree[i].color;pushdown(i);mid=(tree[i].left+tree[i].right)/2;if(r<=mid)return query(l,r,i*2);else if(l>mid)return query(l,r,i*2+1);elsereturn query(l,mid,i*2)|query(mid+1,r,i*2+1);}int fun(int x){int num=0;while(x){if(x%2)num++;x/=2;}return num;}int main(){int i,n,l,t;int a,b,c,ans;char ch[3];while(scanf("%d%d%d",&l,&t,&n)!=EOF){build(1,l,1);for(i=0;i<n;i++){scanf("%s",ch);if(ch[0]=='C'){scanf("%d%d%d",&a,&b,&c);if(b<a)update(b,a,c,1);elseupdate(a,b,c,1);}else if(ch[0]=='P'){scanf("%d%d",&a,&b);if(b<a)ans=query(b,a,1);elseans=query(a,b,1);printf("%d\n",fun(ans));}}    }return 0;}


 
0 0
原创粉丝点击