首页 > 代码库 > POJ 2777 Count Color(线段树 )
POJ 2777 Count Color(线段树 )
Language: 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 2 2 4 C 1 1 2 P 1 2 C 2 2 2 P 1 2 Sample Output 2 1 Source POJ Monthly--2006.03.26,dodo |
题意:给你n个板子,num个颜色,m个操作,c代表把 le 到 ri ,涂成va颜色,p代表询问le到ri 有几种颜色
线段树+lazy思想,颜色用二进制保存, 注意 | 运算的应用
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) using namespace std; #define N 100005 int n,m; int sum; struct stud{ int le,ri; int color; int cover; }f[N*4]; void pushdown(int pos) { if(!f[pos].cover) return ; f[L(pos)].color=f[R(pos)].color=f[pos].color; f[L(pos)].cover=f[R(pos)].cover=1; f[pos].cover=0; } void pushup(int pos) { f[pos].color=f[L(pos)].color|f[R(pos)].color; } void build(int pos,int le,int ri) { f[pos].le=le; f[pos].ri=ri; f[pos].color=1<<1; f[pos].cover=1; if(le==ri) return ; int mid=MID(le,ri); build(L(pos),le,mid); build(R(pos),mid+1,ri); } void update(int pos,int le,int ri,int va) { if(f[pos].le==le&&f[pos].ri==ri) { f[pos].color=va; f[pos].cover=1; return ; } pushdown(pos); int mid=MID(f[pos].le,f[pos].ri); if(mid>=ri) update(L(pos),le,ri,va); else if(mid<le) update(R(pos),le,ri,va); else { update(L(pos),le,mid,va); update(R(pos),mid+1,ri,va); } pushup(pos); } void query(int pos,int le,int ri) { if(f[pos].le==le&&f[pos].ri==ri) { sum|=f[pos].color; return ; } pushdown(pos); int mid=MID(f[pos].le,f[pos].ri); if(mid>=ri) query(L(pos),le,ri); else if(mid<le) query(R(pos),le,ri); else { query(L(pos),le,mid); query(R(pos),mid+1,ri); } pushup(pos); //这个题收获之处 不pushup也对 } void print() { int ans=0; for(int i=1;i<31;i++) if(sum&(1<<i)) ans++; printf("%d\n",ans); } int main() { int i,j,num; while(~scanf("%d%d%d",&n,&num,&m)) { build(1,1,n); char c[10]; int le,ri,va; while(m--) { scanf("%s",c); if(c[0]=='C') { scanf("%d%d%d",&le,&ri,&va); update(1,le,ri,1<<va); } else { sum=0; scanf("%d%d",&le,&ri); if(le>ri) {i=le;le=ri;ri=i;} query(1,le,ri); print(); } } } return 0; }
POJ 2777 Count Color(线段树 )
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。