POJ 2777 Count Color (线段树)
2013-04-19 10:57
519 查看
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 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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29789 | Accepted: 8895 |
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
做过poj2528和1436后,这题就水了。线段树成段更新,区间保存的是该区间的颜色,如果该区间的颜色为-1,表示颜色不止一种,往下继续找,统计颜色个数。否则若该颜色不为-1,则说明该区间是被一种颜色覆盖的,直接返回。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define SIZE 100005 #define ls l,mid,rt<<1 #define rs mid+1,r,rt<<1|1 using namespace std; int N,T,O,ans; int col[SIZE<<2]; bool vis[35]; void pushDown(int rt) { if(col[rt] != -1) { col[rt<<1] = col[rt<<1|1] = col[rt]; col[rt] = -1; } } void update(int l,int r,int rt,int L,int R,int w) { if(L <= l && r <= R) { col[rt] = w; return; } pushDown(rt); int mid = (l + r) >> 1; if(L <= mid)update(ls,L,R,w); if(R > mid)update(rs,L,R,w); } void query(int l,int r,int rt,int L,int R) { if(col[rt] != -1) { if(!vis[col[rt]]) { ans ++ ; vis[col[rt]] = true; } return; } if(l == r) return; int mid = (l + r) >> 1; if(L <= mid)query(ls,L,R); if(R > mid)query(rs,L,R); } int main() { scanf("%d%d%d",&N,&T,&O); for(int i=0; i<(SIZE<<2); i++)col[i] = 1; char c; int s,e,v; for(int i=1; i<=O; i++) { getchar(); scanf("%c%d%d",&c,&s,&e); if(c == 'C') { scanf("%d",&v); update(1,N,1,s,e,v); } else { memset(vis,0,sizeof(vis)); ans = 0; query(1,N,1,s,e); printf("%d\n",ans); } } return 0; }
相关文章推荐
- poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)
- POJ 2777--Count Color(线段树)
- POJ 2777 Count Color(线段树、lazy思想)
- poj_2777Count Color线段树_区间更新
- POJ 2777 Count Color(线段树)
- 【线段树(lazy)】poj 2777 Count Color
- POJ 2777 Count Color(线段树区间更新)
- [poj] 2777 - Count Color - 线段树 + 状态压缩思想(?)
- poj 2777 Count Color(线段树区间更新)
- poj 2777 染色线段树 count color
- POJ 2777 Count Color (线段树成段更新+二进制思维)
- POJ 2777——Count Color(线段树,区间染色+简单hash)
- poj2777--Count Color(线段树,二进制转化)
- poj 2777 Count Color(线段树区间更新+技巧)
- POJ-2777 Count Color(线段树,区间染色问题)
- POJ 2777 Count Color 暑假 —— 线段树1 ——H
- poj_2777 Count Color(线段树染色)
- POJ 2777Count Color (线段树区间更新 + 位运算压缩)
- POJ 2777 Count Color 线段树 区间更新
- [POJ 2777]Count Color[线段树区间更新查询]