线段树区间更新——POJ 2777

对应POJ题目:点击打开链接

Count Color

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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

Source

题意:1~N开始被刷上颜色1,有O个操作,C  a,b,c 表示把a~b区间刷成颜色c;   P a,b 表示求a~b区间共有多少种颜色。

思路:线段树;color[rt] 表示结点rt有哪几种颜色,用二进制表示;查询时不断并上符合要求的区间的值就可以了。这道题学习了求一个整数有多少个1的几种方法,还是有点收获的。。。

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define ms(x,y) memset(x,y,sizeof(x))
#define N 100000+10
const int MAXN=1000+10;
const int INF=1<<30;
using namespace std;
int color[N<<2];
int ans;

#if 0

int cal(int x)//求一个整数中1的个数普通方法,速度一般
{
	int cnt=0;
	while(x)
	{
		if(x&1) cnt++;
		x>>=1;
	}
	return cnt;
}

/*
一个整数不为0,那么这个整数至少有一位是1。如果我们把这个整数减去1,那么原来处在整数最右边的1就会变成0,原来在1后面的所有 的0都会变成1。其余的所有位将不受到影响。举个例子:一个二进制数1100,从右边数起的第三位是处于最右边的一个1。减去1后,第三位变成0,它后面 的两位0变成1,而前面的1保持不变,因此得到结果是1011。
我们发现减1的结果是把从最右边一个1开始的所有位都取反了。这个时候如果我们再把原来的整数和减去1之后的结果做与运算,从原来整数最右边一个1那一位 开始所有位都会变成0。如1100&1011=1000。也就是说,把一个整数减去1,再和原整数做与运算,会把该整数最右边一个1变成0。那么 一个整数的二进制有多少个1,就可以进行多少次这样的操作。
 */
int cal(int x)//速度较快
{
	int cnt=0;
	while(x)
	{
		++cnt;
		x=(x-1)&x;
	}
	return cnt;
}

#endif

//HAKMEM算法:
int cal(unsigned x)//速度更快
{
    unsigned n;
    n = (x >> 1) & 033333333333;
    x = x - n;
    n = (n >> 1) & 033333333333;
    x = x - n;
    x = (x + (x >> 3)) & 030707070707;
    x = x%63;
    return x;
} 

void updata(int rt, int left, int right, int l, int r, int c)
{
	if(left==l && right==r){
		color[rt]=1<<(c-1);//这里开始是直接=c,好久才发现问题。。。
		return;
	}
	int cnt=cal(color[rt]);//如果该结点只有一种颜色,则向下更新
	if(cnt==1){
		color[rt<<1]=color[rt];
		color[rt<<1|1]=color[rt];
	}
	int mid=(left+right)>>1;
	if(mid>=r) updata(rt<<1, left, mid, l, r, c);
	else if(mid<l) updata(rt<<1|1, mid+1, right, l, r, c);
	else{
		updata(rt<<1, left, mid, l, mid, c);
		updata(rt<<1|1, mid+1, right, mid+1, r, c);
	}
	color[rt]=color[rt<<1]|color[rt<<1|1];//合并
}

void query(int rt, int left, int right, int l, int r)
{
	if(l==left && right==r){
		ans|=color[rt];
		return;
	}
	if(left==right) return;
	int cnt=cal(color[rt]);
	if(cnt==1){
		color[rt<<1]=color[rt];
		color[rt<<1|1]=color[rt];
	}
	int mid=(left+right)>>1;
	if(mid>=r) query(rt<<1, left, mid, l, r);
	else if(mid<l) query(rt<<1|1, mid+1, right, l, r);
	else{
		query(rt<<1, left, mid, l, mid);
		query(rt<<1|1, mid+1, right, mid+1, r);
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	int L,T,O;
	int a,b,c;
	char op;
	scanf("%d%d%d", &L,&T,&O);
	for(int i=1; i<(N<<2); i++) color[i]=1;
	while(O--)
	{
		scanf("%s", &op);
		if(op=='C'){
			scanf("%d%d%d", &a,&b,&c);
			if(b<a){//不断WA,坑!网上看人的才知道a可能大于b。。。
				int tmp=a; a=b; tmp=a;
			}
			updata(1,1,N,a,b,c);
		}
		else{
			scanf("%d%d", &a,&b);
			if(b<a){
				int tmp=a; a=b; tmp=a;
			}
			ans=0;
			query(1,1,N,a,b);
			printf("%d\n", cal(ans));
		}
	}
	return 0;
}
时间: 2024-10-15 00:28:36

线段树区间更新——POJ 2777的相关文章

线段树区间更新——POJ 1436

对应POJ题目:点击打开链接 Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description There is a number of disjoint vertical line segments in the plane. We say that two segments are horizon

POJ 2777 Count Color (线段树区间更新加查询)

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 d

POJ 2777 &amp;&amp; ZOJ 1610 &amp;&amp;HDU 1698 --线段树--区间更新

直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 -- 区间更新的话 分为 增减 或者 修改 主要就是个 laze 标记 就是延迟更新 对于区间更新的写法 一般是有2种 其一 仔细划分到每个细小的区间    另一 粗略划分 反正 ==我的代码里会给出2种写法 看自己喜好 hdu 1 //线段树 成段更新 ---> 替换 根结点的查询 2 3 #i

poj 2777 Count Color (线段树区间更新)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37647   Accepted: 11315 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.

POJ 2528 Mayor&#39;s posters (线段树区间更新+离散化)

题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值.由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案. 写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目. 1 #include <iostream> 2 #include <

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

线段树 + 区间更新 + 模板 ---- poj 3468

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59798   Accepted: 18237 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

PKU 2777 Count Color (线段树区间更新)

题意: 给你三个数:L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000),表示有一长度为L的板(1~L), 有T种颜色(1~T),然后有O个操作,初始板1~L的颜色为1,"C A B C"表示在区间A,B图上C颜色, "P A B" 表示询问 A,B区间有几种不同的颜色. #include <stdio.h> #include <iostr

线段树区间更新+向量知识——POJ 2991

对应POJ题目:点击打开链接 Crane Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description ACM has bought a new crane (crane -- je?áb) . The crane consists of n segments of various lengths, connected by flexible joints