Count Color
来源:互联网 发布:oracle建表sql语句 编辑:程序博客网 时间:2024/04/28 14:30
http://acm.hust.edu.cn:8080/judge/contest/viewProblem.action?pid=45730
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.
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
#include<stdio.h>#include<algorithm>#include<string.h>#include<iostream>using namespace std;#define MAXN 100010bool flag[MAXN];struct node {int l,r,color;}t[MAXN*4];void make(int st,int ed ,int num){t[num].l=st;t[num].r=ed;t[num].color=1;if(st==ed)return ;int mid=(st+ed)/2;make(st,mid,num*2);make(mid+1,ed,num*2+1);}void updata(int st,int ed,int num,int color){if(t[num].l==st&&t[num].r==ed){t[num].color=color;return ;}if(t[num].color>0){t[num*2].color=t[num].color;t[num*2+1].color=t[num].color;t[num].color=0;}int mid=(t[num].l+t[num].r)/2;if(st>mid)updata(st,ed,num*2+1,color);else if(ed<=mid)updata(st,ed,num*2,color);else {updata(st,mid,num*2,color);updata(mid+1,ed,num*2+1,color);}}void query(int st,int ed,int num){if(t[num].color>0){flag[t[num].color]=1;return ;}int mid=(t[num].l+t[num].r)/2;if(st>mid)query(st,ed,num*2+1);else if(ed<=mid)query(st,ed,num*2);else {query(st,mid,num*2);query(mid+1,ed,num*2+1);}}int main(){int T,L,O;char str[3];int ans;scanf("%d%d%d",&L,&T,&O);make(1,L,1);while(O--){scanf("%s",str);if(str[0]=='C'){int x,y,clo;scanf("%d%d%d",&x,&y,&clo);updata(x,y,1,clo);}if(str[0]=='P'){int x;int y;memset(flag,0,sizeof(flag));scanf("%d%d",&x,&y);ans=0;query(x,y,1);for(int i=1;i<=30;i++){if(flag[i]==1)ans++;}printf("%d\n",ans);}}return 0;}
第二份代码:
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;struct {int l,r,color;}g[100005*4];int a[35];void creat(int l,int r,int k){g[k].l=l;g[k].r=r;g[k].color=1;int mid;if(l<r){mid=(l+r)/2;creat(l,mid,k*2);creat(mid+1,r,k*2+1);}}void update(int l,int r,int add,int k){if(g[k].l>=l&&g[k].r<=r){g[k].color=add;return ;}int mid;if(g[k].l<g[k].r){if(g[k].color>0){g[k*2].color=g[k*2+1].color=g[k].color;}g[k].color=-1;mid=(g[k].l+g[k].r)/2;if(r<=mid)update(l,r,add,k*2);else if(l>mid)update(l,r,add,k*2+1);else{update(l,mid,add,k*2);update(mid+1,r,add,k*2+1);}}}void quire(int l,int r,int k){if(g[k].color>0)//不用将整个叶子节点全部求出;{a[g[k].color]=1;return;}int mid;if(g[k].l<g[k].r){ mid=(g[k].l+g[k].r)/2; if(r<=mid) quire(l,r,k*2); else if(l>mid) quire(l,r,k*2+1); else{ quire(l,mid,k*2); quire(mid+1,r,k*2+1);} }}int main(){int m,n,i,j,k,l,r,add;char c;while(scanf("%d%d%d",&m,&n,&k)!=EOF){creat(1,m,1);while(k--){scanf(" %c",&c);if(c=='C'){ scanf("%d%d%d",&l,&r,&add); if(l>r){l^=r^=l^=r;}update(l,r,add,1);}else{scanf("%d%d",&l,&r);memset(a,0,sizeof(a));if(l>r){l^=r^=l^=r;}quire(l,r,1);int d=0;for(i=1;i<=n;i++){if(a[i]>0)d++;}printf("%d\n",d);}}}return 0;}//延时标记
第三份代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 100005struct node{int l,r,color;bool flag;}tree[3*maxn];int n,m,tt; void BuildTree(int p,int l,int r){tree[p].l=l; tree[p].r=r;tree[p].color=1; tree[p].flag=false;if(l==r) return;if(l<r){int mid=(l+r)/2;BuildTree(2*p,l,mid);BuildTree(2*p+1,mid+1,r);}//tree[p].color=tree[2*p].color|tree[2*p+1].color;} void change(int p,int l,int r,int x){if(tree[p].l>=l&&tree[p].r<=r){tree[p].color=x;tree[p].flag=true;return ;}if(tree[p].l<tree[p].r){if(tree[p].flag){tree[2*p].color=tree[p].color;tree[2*p].flag=true;tree[2*p+1].color=tree[p].color;tree[2*p+1].flag=true;tree[p].flag=false;}int mid=(tree[p].l+tree[p].r)/2;if(r<=mid)change(2*p,l,r,x);else if(l>mid)change(2*p+1,l,r,x);else{change(2*p,l,mid,x);change(2*p+1,mid+1,r,x);}tree[p].color=tree[2*p].color|tree[2*p+1].color;}}int que(int p,int l,int r){if(tree[p].flag)returntree[p].color;if(tree[p].l==l&&tree[p].r==r)return tree[p].color;if(tree[p].l<tree[p].r){if(tree[p].flag){tree[2*p].flag=true;tree[2*p].color=tree[p].color;tree[2*p+1].flag=true;tree[2*p+1].color=tree[p].color;tree[p].flag=false;}int mid=(tree[p].l+tree[p].r)/2;if(r<=mid)return que(2*p,l,r);else if(l>mid)return que(2*p+1,l,r);elsereturn que(2*p,l,mid)|que(2*p+1,mid+1,r);}} int make_ans(int x){int ans=0;while(x){if(x%2)ans++;x/=2;}return ans;} int main(){int i,j,x,y,d;char c;scanf("%d%d%d",&n,&m,&tt);BuildTree(1,1,n);getchar();while(tt--){scanf("%c",&c);if(c=='C'){scanf("%d%d%d",&x,&y,&d);if(x>y)swap(x,y);change(1,x,y,1<<(d-1));}else if(c=='P'){scanf("%d%d",&x,&y);if(x>y)swap(x,y);int ans=que(1,x,y);printf("%d\n",make_ans(ans));}getchar();}return 0;}
- Count Color
- Count Color
- Count Color
- count color
- Count Color
- Count Color
- pku2777 Count Color
- POJ 2777 Count Color
- PKU 2777 Count Color
- poj 2777 Count Color
- Count Color[poj2777]
- PKU 2777 Count Color
- C - Count Color
- poj 2777 count color
- poj 2777 Count color
- poj 2777 Count Color
- 【線段樹】Count Color
- Poj 2777 Count Color
- 乔帮主逝去了……
- FPDF 1.52 參考手冊
- DacImportExportCli : Import / Export Data from SQL Azure to SQL Server (vice versa)
- 组合数奇偶性判断 应用位运算方法
- accept非阻塞
- Count Color
- AT91Bootstrap1.16第一阶段汇编程序详解
- 百度三维地图体验(坑爹啊有图有真相)
- 一波三折——危险的“未分配”引脚
- 10个管理工作时间的小技巧
- C语言:运行中获取宏名字的技巧
- 炒菜减少油量的方法
- 杀死团队的武器与修复团队的方法
- C++中extern “C”含义深层探索