您的位置:首页 > 其它

poj_2777Count Color线段树_区间更新

2014-10-11 11:43 411 查看
Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36037 Accepted: 10882
DescriptionChosen 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 segmentwith 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 thebeginning, the board was painted in color 1. Now the rest of problem is left to your. InputFirst 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 operationdefined previously.OutputOuput 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 Output21做完本题推荐做POJ2528题,是这题的升级版
#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define mem0(a) memset(a,0,sizeof(a))#define maxn  100000+10int vis[35];//记录颜色struct node{int l,r,cor;//保存线段树每个结点代表区间的l,r值}a[maxn<<2]; //cur作为根结点时起标记作用,作为子叶结点时表示颜色void build(int l,int r,int cur){a[cur].l = l;a[cur].r = r;a[cur].cor = 1;//初始化颜色都为1if( l < r ){int mid = ( l + r )>>1;build(l,mid,cur<<1);build(mid+1,r,cur<<1|1);}}void update(int l,int r,int v,int cur){//插入函数,着色if( l  <= a[cur].l&&  r>=a[cur].r){//这里的l,r使要查询的范围a[cur].cor = v;//a[cur].l和a[cur].r是线段树区间范围return ;}                               //自上向下传递颜色if(a[cur]. l < a[cur].r ){int mid =(a[cur].l + a[cur].r )>>1;if(a[cur].cor > 0 ){//向左右子树覆着色a[cur<<1|1].cor=a[cur<<1].cor =a[cur].cor;}a[cur].cor = -1;//清除本结点标记if(r <= mid )//插入左子树update(l,r,v,cur<<1);else if(l > mid )update(l,r,v,cur<<1|1);else {update(l,mid,v,cur<<1);update(mid+1,r,v,cur<<1|1);}}}void query(int l,int r,int cur){//统计多少种颜色if(a[cur].cor > 0 ){//从根节点算起,vis[a[cur].cor]=1;return ;}if(a[cur].l < a[cur].r){int mid = (a[cur].l + a[cur].r )>>1;if(r <= mid)query(l,r,cur<<1);else if(l > mid )query(l,r,cur<<1|1);else {query(l,mid,cur<<1);query(mid+1,r,cur<<1|1);}}}int main(){int n,t,o;while(scanf("%d%d%d",&n,&t,&o)!=EOF){build(1,n,1);int x,y,c;char s[10];while(o--){scanf("%s",s);if(s[0]=='C'){scanf("%d%d%d",&x,&y,&c);if(x > y ){swap(x,y);}update(x,y,c,1);}else if(s[0]=='P'){scanf("%d%d",&x,&y);mem0(vis);if(x>y) swap(x,y);query(x,y,1);int ans = 0 ;for(int i = 1 ; i<= 30 ; i++){if(vis[i])ans++;}printf("%d\n",ans);}}}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线段树 区间修改