poj 2777,hdu 5023 线段树区间染色+lazy+位运算

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

Sample Output

2
1


分析:

线段树用来维护区间颜色的种数。关键在于用按位或代替加法运算进行颜色种数统计。统计结果有多少个1,就说明区间有多少不同的颜色。

假设:
                    第一种颜色的二进制表示1
                    第二种颜色的二进制表示10
                    第三种颜色的二进制表示100
                    第四种颜色的二进制表示1000

若一个区间出现了第一种和第三种颜色,按位或运算之后得到 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|1

int conow[maxn<<2];//记录当前lazy的颜色 
int cosum[maxn<<2];//颜色种数 

void pushup(int rt){//当前结点信息更新到父结点
	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;//当前不需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);//更新(覆盖)之后总颜色数未必增加,需先记录,pushdown,再pushup后重新统计 
		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): 864


Problem Description
Corrupt 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?
 

Input
There 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 b
a 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.
 

Output
For 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 Input
  
  
5 10 P 1 2 3 P 2 3 4 Q 2 3 Q 1 3 P 3 5 4 P 1 2 7 Q 1 3 Q 3 4 P 5 5 8 Q 1 5 0 0
 

Sample Output
  
  
4 3 4 4 7 4 4 7 8


分析:

和上题的差别在于按序输出不同颜色,而非颜色种数。query()函数不需要改写,它的返回值包含各种颜色信息。


代码:

#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|1

int conow[maxn<<2];//记录当前lazy的颜色 
int cosum[maxn<<2];//颜色种数 

void pushup(int rt){//当前结点信息更新到父结点
	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;//当前不需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);//更新(覆盖)之后总颜色数未必增加,需先记录,pushdown,再pushup后重新统计 
		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;//别忘了加等号进行赋值 
                }
                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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值