題目:poj2777
Count Color| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 44465 | Accepted: 13479 |
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 2Sample Output
21分析:
線段樹用來維護(hù)區(qū)間顏色的種數(shù)。關(guān)鍵在于用按位或代替加法運(yùn)算進(jìn)行顏色種數(shù)統(tǒng)計(jì)。統(tǒng)計(jì)結(jié)果有多少個(gè)1,就說明區(qū)間有多少不同的顏色。
假設(shè): 第一種顏色的二進(jìn)制表示1 第二種顏色的二進(jìn)制表示10 第三種顏色的二進(jìn)制表示100 第四種顏色的二進(jìn)制表示1000
若一個(gè)區(qū)間出現(xiàn)了第一種和第三種顏色,按位或運(yùn)算之后得到 101
代碼:
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<ctype.h> //tower()#include<set> #include<map> #include<iomanip>// cout<<setprecision(1)<<fixed<<a;#include<vector> #include<assert.h> //assert#include<cmath> #include<algorithm>#include<bitset>#include<limits.h>#include<stack>#include<queue>using namespace std;const int maxn=100010;const int inf=0x7fffffff;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1int conow[maxn<<2];//記錄當(dāng)前l(fā)azy的顏色 int cosum[maxn<<2];//顏色種數(shù) void pushup(int rt){//當(dāng)前結(jié)點(diǎn)信息更新到父結(jié)點(diǎn) cosum[rt]=cosum[rt<<1]|cosum[rt<<1|1]; }void pushdown(int rt){ if(conow[rt]){ conow[rt<<1]=conow[rt<<1|1]=conow[rt]; cosum[rt<<1]=cosum[rt<<1|1]=conow[rt]; conow[rt]=0; }}void build(int l,int r,int rt){ conow[rt]=0;//當(dāng)前不需pushdown if(l==r){ cosum[rt]=1; return; } int mid=(l+r)>>1; build(lson); build(rson); pushup(rt);}void update(int a,int b,int c,int l,int r,int rt){ if(a<=l&&b>=r){ conow[rt]=1<<(c-1); cosum[rt]=1<<(c-1);//更新(覆蓋)之后總顏色數(shù)未必增加,需先記錄,pushdown,再pushup后重新統(tǒng)計(jì) return; } pushdown(rt); int mid=(l+r)>>1; if(a<=mid) update(a,b,c,lson); if(b>mid) update(a,b,c,rson); pushup(rt);}int query(int a,int b,int l,int r,int rt){ if(a<=l&&b>=r) return cosum[rt]; pushdown(rt); int mid=(l+r)>>1; int ans=0; if(a<=mid) ans|=query(a,b,lson); if(b>mid) ans|=query(a,b,rson); return ans;}int main(){//2744K 438MS int n,num,m,a,b,c,t,ans; char s[2]; while(scanf("%d%d%d",&n,&num,&m)==3){ build(1,n,1); while(m--){ scanf("%s",s); if(s[0]=='P'){//查詢 scanf("%d%d",&a,&b); if(a>b) swap(a,b); t=query(a,b,1,n,1); ans=0; while(t){ if(t&1) ans++; t>>=1; } printf("%d/n",ans); } else{//染色 scanf("%d%d%d",&a,&b,&c); if(a>b) swap(a,b); update(a,b,c,1,n,1); } } } return 0;}hdu 5023
題目:
A Corrupt Mayor's Performance Art
Time Limit: 2000/1000 MS (java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 2425 Accepted Submission(s): 864Problem DescriptionCorrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money.Because a lot of people praised mayor X's painting(of course, X was a mayor), mayor X believed more and more that he was a very talented painter. Soon mayor X was not satisfied with only making money. He wanted to be a famous painter. So he joined the local painting associates. Other painters had to elect him as the chairman of the associates. Then his painting sold at better price.The local middle school from which mayor X graduated, wanted to beat mayor X's horse fart(In Chinese English, beating one's horse fart means flattering one hard). They built a wall, and invited mayor X to paint on it. Mayor X was very happy. But he really had no idea about what to paint because he could only paint very abstract paintings which nobody really understand. Mayor X's secretary suggested that he could make this thing not only a painting, but also a performance art work.This was the secretary's idea:The wall was divided into N segments and the width of each segment was one cun(cun is a Chinese length unit). All segments were numbered from 1 to N, from left to right. There were 30 kinds of colors mayor X could use to paint the wall. They named those colors as color 1, color 2 .... color 30. The wall's original color was color 2. Every time mayor X would paint some consecutive segments with a certain kind of color, and he did this for many times. Trying to make his performance art fancy, mayor X declared that at any moment, if someone asked how many kind of colors were there on any consecutive segments, he could give the number immediately without counting.But mayor X didn't know how to give the right answer. Your friend, Mr. W was an secret officer of anti-corruption bureau, he helped mayor X on this problem and gained his trust. Do you know how Mr. Q did this? InputThere are several test cases.For each test case:The first line contains two integers, N and M ,meaning that the wall is divided into N segments and there are M operations(0 < N <= 1,000,000; 0<M<=100,000) Then M lines follow, each representing an operation. There are two kinds of operations, as described below: 1) P a b c a, b and c are integers. This operation means that mayor X painted all segments from segment a to segment b with color c ( 0 < a<=b <= N, 0 < c <= 30).2) Q a ba and b are integers. This is a query operation. It means that someone asked that how many kinds of colors were there from segment a to segment b ( 0 < a<=b <= N).Please note that the operations are given in time sequence.The input ends with M = 0 and N = 0. OutputFor each query operation, print all kinds of color on the queried segments. For color 1, print 1, for color 2, print 2 ... etc. And this color sequence must be in ascending order. Sample Input5 10P 1 2 3P 2 3 4Q 2 3Q 1 3P 3 5 4P 1 2 7Q 1 3Q 3 4P 5 5 8Q 1 50 0 Sample Output43 44 744 7 8分析:
和上題的差別在于按序輸出不同顏色,而非顏色種數(shù)。query()函數(shù)不需要改寫,它的返回值包含各種顏色信息。
代碼:
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<ctype.h> //tower()#include<set> #include<map> #include<iomanip>// cout<<setprecision(1)<<fixed<<a;#include<vector> #include<assert.h> //assert#include<cmath> #include<algorithm>#include<bitset>#include<limits.h>#include<stack>#include<queue>using namespace std;const int maxn=1000010;const int inf=0x7fffffff;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1int conow[maxn<<2];//記錄當(dāng)前l(fā)azy的顏色 int cosum[maxn<<2];//顏色種數(shù) void pushup(int rt){//當(dāng)前結(jié)點(diǎn)信息更新到父結(jié)點(diǎn) cosum[rt]=cosum[rt<<1]|cosum[rt<<1|1]; }void pushdown(int rt){ if(conow[rt]){ conow[rt<<1]=conow[rt<<1|1]=conow[rt]; cosum[rt<<1]=cosum[rt<<1|1]=conow[rt]; conow[rt]=0; }}void build(int l,int r,int rt){ conow[rt]=0;//當(dāng)前不需pushdown if(l==r){ cosum[rt]=2; return; } int mid=(l+r)>>1; build(lson); build(rson); pushup(rt);}void update(int a,int b,int c,int l,int r,int rt){ if(a<=l&&b>=r){ conow[rt]=1<<(c-1); cosum[rt]=1<<(c-1);//更新(覆蓋)之后總顏色數(shù)未必增加,需先記錄,pushdown,再pushup后重新統(tǒng)計(jì) return; } pushdown(rt); int mid=(l+r)>>1; if(a<=mid) update(a,b,c,lson); if(b>mid) update(a,b,c,rson); pushup(rt);}int query(int a,int b,int l,int r,int rt){ if(a<=l&&b>=r) return cosum[rt]; pushdown(rt); int mid=(l+r)>>1; int s=0; if(a<=mid) s|=query(a,b,lson); if(b>mid) s|=query(a,b,rson); return s;}int main(){//312MS 18000K int n,m,a,b,c,t,ans,bt[31]; char s[2]; while(scanf("%d%d",&n,&m)==2){ if(!n&&!m) break; build(1,n,1); while(m--){ scanf("%s",s); if(s[0]=='Q'){//查詢 scanf("%d%d",&a,&b); ans=query(a,b,1,n,1); // int flag=0; //另一種處理方法 280MS 18020K // for(int i=1;i<=30;++i){// if(t>>(i-1)&1 && (!flag)){// printf("%d",i);// flag = 1;// }// else if(t>>(i-1)&1)// printf(" %d",i);// }// puts(""); int i=0; memset(bt,0,sizeof(bt)); while(ans){ bt[i++]=ans&1; ans>>=1;//別忘了加等號(hào)進(jìn)行賦值 } int flag=1; for(int i=0;i<=30;++i){ if(bt[i]&&flag) printf("%d",i+1),flag=0; else if(bt[i]) printf(" %d",i+1); } puts(""); } else{//染色 scanf("%d%d%d",&a,&b,&c); update(a,b,c,1,n,1); } } } return 0;}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注