poj 2777 Count Color(线段树)
2013-08-04 16:49
441 查看
题目地址:http://poj.org/problem?id=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 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
【题解】:典型线段树区间更新
【code】:
Count Color
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 30995 | Accepted: 9285 |
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
【题解】:典型线段树区间更新
【code】:
/** result:Accepted memory:4264K time:375MS language:C++ code lenght: 2147B Acmer:cj */ #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #define N 100010 #define lson p<<1 #define rson p<<1|1 using namespace std; struct Nod { int l,r; int color; //颜色 int flag; //[l,r]区间是否同色 }node[N<<2]; int mark[50]; //颜色统计标记 void building(int l,int r,int p) //建树 { node[p].l = l; node[p].r = r; node[p].color = 1; node[p].flag = 1; if(l==r) return; int mid = (l+r)>>1; building(l,mid,lson); building(mid+1,r,rson); } void update(int l,int r,int p,int c) { if(node[p].l==l&&node[p].r==r) { node[p].flag = 1; node[p].color = c; return; } if(node[p].color!=c&&node[p].flag) //向下更新 { node[p].flag = 0; node[lson].flag=node[rson].flag = 1; node[lson].color=node[rson].color = node[p].color; } int mid = (node[p].l+node[p].r)>>1; if(r<=mid) update(l,r,lson,c); else if(l>mid) update(l,r,rson,c); else { update(l,mid,lson,c); update(mid+1,r,rson,c); } if(node[lson].flag&&node[rson].flag&&node[lson].color==node[rson].color) //向上更新 { node[p].flag = 1; node[p].color = node[lson].color; } } void query(int l,int r,int p) { if(node[p].flag) //区间同色 { mark[node[p].color]=1; //标记颜色出现过 return; } int mid = (node[p].l+node[p].r)>>1; if(r<=mid) query(l,r,lson); else if(l>mid) query(l,r,rson); else { query(l,mid,lson); query(mid+1,r,rson); } } int main() { int L,T,O; scanf("%d%d%d",&L,&T,&O); building(1,L,1); while(O--) { char op[5]; int a,b,c; scanf("%s",op); if(op[0]=='C') { scanf("%d%d%d",&a,&b,&c); update(a,b,1,c); } else if(op[0]=='P') { int i; scanf("%d%d",&a,&b); memset(mark,0,sizeof(mark)); //初始化颜色标记 query(a,b,1); int ans = 0; for(i=1;i<=T;i++) if(mark[i]) ans++; printf("%d\n",ans); } } return 0; }
相关文章推荐
- 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 (线段树)
- 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(线段树,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(线段树 Lazy-Tag思想 成段更新+区间统计)