您的位置:首页 > 其它

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

2016-07-24 19:32 399 查看
Count Color

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 42721 Accepted: 12942
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<=color<=t && 1<=t<=30)

操作分为 C 和 P

C 是区间段染色

P 是查询某区间的颜色数 并且输出结果

因为颜色数不超过30  所以颜色可以用二进制位保存

这题就是区间更新

按区间更新的方法找到区间范围内的节点   对这个节点的tag标记为新的颜色    这个节点的颜色种类标记变为这个新的颜色代表的二进制数

返回的时候注意更新节点的颜色种类标记  (这个节点的颜色种类就是子节点的所有颜色种类 这里可以进行 | 运算)

区间查询的时候  对查询的结果  统计其中二进制位为1的个数  这个个数就是这个区间的颜色种类数

要注意pushdown

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define pi acos(-1.0)
#define inf 1<<29
#define INF 0x3f3f3f3f
#define zero 1e-8

const int li[] = { -1, 0, 1, 0};
const int lj[] = {0, -1, 0, 1};

const int N = 1e5 + 10;

using namespace std;

struct node {

int data;
int tag;

} tree[N * 4];

int fin;

void build(int node, int Begin, int End)
{
tree[node].tag = 0;
if (Begin == End) {
tree[node].data = 1;
return;
}
build(node << 1, Begin, (Begin + End) >> 1);
build(node << 1 | 1, ((Begin + End) >> 1) + 1, End);
tree[node].data = 1;
}
void pushdown(int node, int l, int r)
{

if (!tree[node].tag) return;
tree[node << 1].tag = tree[node].tag;
tree[node << 1].data = 1 << (tree[node].tag - 1);
tree[node << 1 | 1].tag = tree[node].tag;
tree[node << 1 | 1].data = 1 << (tree[node].tag - 1);

tree[node].tag = 0;
}

void update(int node, int b, int e, int l, int r, int col)
{

if (l > e || r < b) return;

if (l >= b && r <= e) {
tree[node].tag = col ;
tree[node].data = 1 << (col - 1);
return;
}

pushdown(node, l, r);
update(node << 1 , b, e, l, (l + r) >> 1, col);
update(node << 1 | 1, b, e, ((l + r) >> 1) + 1, r, col);
tree[node].data = tree[node << 1].data | tree[node << 1 | 1].data;
}

void query(int node, int b, int e, int l, int r)
{

if (l > e || r < b) return;

if (l >= b && r <= e) {
fin |= tree[node].data;
return;
}
pushdown(node, l, r);
query(node << 1 , b, e, l, (l + r) >> 1);
query(node << 1 | 1, b, e, ((l + r) >> 1) + 1, r);
}
int main()
{

int n = 100000, q, t;
while(~scanf("%d %d %d", &n, &t, &q)) {

int kind = 2;

build(1, 1, n);

for (int i = 0; i < q; ++i) {

char ch[10];
scanf("%s", ch);
if (ch[0] == 'C') {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
int minn = a > b ? b : a;
int maxx = b > a ? b : a;
update(1, minn, maxx, 1, n, c);

} else if (ch[0] == 'P') {
int a, b, ans = 0;
scanf("%d%d", &a, &b);
int minn = a > b ? b : a;
int maxx = b > a ? b : a;
fin = 0;
query(1, minn, maxx, 1, n);

for (int k = 0; k < t; ++k)
ans += (fin >> k) & 1;
printf("%d\n", ans);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: