您的位置:首页 > 其它

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

2016-10-06 22:22 597 查看
Count Color

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 43629 Accepted: 13205
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


题意就是有L个排成一排的气球和T种颜色,对于气球有2种操作,分别是把从A到B的气球都染成颜色C,第二种是询问从A到B的气球有多少种不同的颜色,一共有O次操作。说实话刚刚看题觉得题挺简单的,就是一个裸的线段树区间延迟查询,但是但是坑点真的多!
除了要注意A和B的大小不确定外,就是数据的贼多,试了几发都是TLE,网上一查原来是要用二进制来记录颜色,不过实在不会,所以就找了了一种方法来减少递归的次数:就是你要查询 3 ~ 4时,而 如果1~8 都是同一种颜色, 3~4是 1~8的子区间,所以你只要在1~8区间的时候判断一下颜色是否出现过就行,并不用递归到 3~4区间,这样就减少了递归的次数!不懂延迟查询的自己学,这里就不再多说。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int big = 1e5 + 7;
struct node
{
int l, r, data, mid;
} a[big<<2];
bool vis[55];
int hehe;
void Buildtree(int key, int l, int r)
{
a[key].l = l;
a[key].r = r;
a[key].data = 1;
a[key].mid = (l + r)>>1;
if(l == r) return;
Buildtree( key<<1, l, a[key].mid);
Buildtree( key<<1|1, a[key].mid + 1, r);
}
void delay(int key)
{
a[key<<1].data = a[key<<1|1].data = a[key].data;
a[key].data = 0;
}
void update(int key, int l, int r, int w)
{
if(a[key].l == l && a[key].r == r)
{
a[key].data = w;
return;
}
if(a[key].data == w) return;
if(a[key].data) delay(key);
if(r <= a[key].mid) update( key<<1, l, r, w);
else if( l > a[key].mid) update( key<<1|1, l, r, w);
else
{
update( key<<1, l, a[key].mid, w);
update( key<<1|1, a[key].mid + 1, r, w);
}
}
void Show(int key, int l, int r)
{
if(a[key].data != 0)
{
vis[a[key].data] = true;
return;
}
if(r <= a[key].mid) Show( key<<1, l, r);
else if(l > a[key].mid) Show( key<<1|1, l, r);
else
{
Show( key<<1, l, a[key].mid);
Show( key<<1|1, a[key].mid + 1, r);
}
}
int main()
{
int n, m, t, x, y, z;
char k[20];
scanf("%d %d %d", &n, &t, &m);
Buildtree( 1, 1, n);
for(int i = 1; i <= m; i ++)
{
scanf("%s", k);
if(k[0] == 'C')
{
scanf("%d %d %d", &x, &y, &z);
if(x > y) swap( x, y);
update( 1, x, y, z);
}
else
{
memset( vis, false, sizeof( vis));
scanf("%d %d", &x, &y);
if(x > y) swap( x, y);
Show( 1, x, y);
hehe = 0;
for(int i = 1; i <= t; i ++) if(vis[i]) hehe ++;
printf("%d\n", hehe);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: