线段树入门pku2777 涂颜色问题
来源:互联网 发布:淘宝销量数据分析 编辑:程序博客网 时间:2024/05/01 12:50
/*pku 2777http://poj.org/problem?id=2777一板子L长,有两个操作一个着色l到r为c这种颜色,一个是查询l到r有多少种颜色就是插入和查询操作Count ColorTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20838 Accepted: 6034DescriptionChosen 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.InputFirst 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.OutputOuput results of the output operation in order, each line contains a number.Sample Input2 2 4C 1 1 2P 1 2C 2 2 2P 1 2Sample Output21SourcePOJ Monthly--2006.03.26,dodo*/#include <iostream>using namespace std;const int N= 100010;const int C= 35;const int nocolor = -1;const int mucolor = -2;struct node{ int l,r,c;};node tree[N*4];int color[C];void create(int k,int l,int r){ tree[k].l = l; tree[k].r = r; tree[k].c = nocolor; if(l == r) return; int m = (l+r)>>1; create(k+k,l,m); create(k+k+1,m+1,r);}void insert(int k,int l,int r,int c){ if(tree[k].c==c) return; if(l==tree[k].l && r==tree[k].r) { tree[k].c = c; return; } // 要传递下去 if(mucolor != tree[k].c ) { tree[k+k].c = tree[k].c; tree[k+k+1].c = tree[k].c; tree[k].c = mucolor; } int m = (tree[k].l+tree[k].r)>>1; if(r<=m) { insert(k+k,l,r,c); return ;} if(l>m) { insert(k+k+1,l,r,c); return;} insert(k+k,l,m,c); insert(k+k+1,m+1,r,c);}void query(int k,int l,int r){ if(tree[k].c == nocolor) return; if(tree[k].c != mucolor ) { color[tree[k].c] = 1; return ; } int m = (tree[k].l + tree[k].r) >>1; if(r<=m) { query(k+k,l,r); return; } if(l>m) { query(k+k+1,l,r); return; } query(k+k,l,m); query(k+k+1,m+1,r);}int main(){ int L,T,O; int i,l,r,c,t,sum; char s[5]; scanf("%d%d%d",&L,&T,&O); create(1,1,L); tree[1].c = 1; while(O--) { scanf("%s",s); if(s[0]=='C') { scanf("%d%d%d",&l,&r,&c); if(l<=r) insert(1,l,r,c); else insert(1,r,l,c); }else { scanf("%d%d",&l,&r); for(i=1;i<=T;++i) color[i] = 0; sum = 0; if(l<=r) query(1,l,r); else query(1,r,l); for(i=1;i<=T;++i) { sum+=color[i]; } printf("%d\n",sum); } } return 0;}
- 线段树入门pku2777 涂颜色问题
- 【线段树】PKU2777 区间更新区间询问(位优化)
- 【线段树】线段树入门
- [线段树]线段树 入门
- [POJ2777] 统计颜色 - 线段树
- 【线段树】线段树入门之入门
- 【线段树】线段树入门之入门
- 【线段树】线段树入门之入门
- 【线段树】线段树入门之入门
- 线段树入门之涂色问题——ZOJ1610
- 线段树入门之入门
- 线段树学习入门
- 线段树 入门
- HDU1754 线段树入门
- HDU1556 线段树入门
- HDU1166 线段树入门
- 线段树入门【转】
- 线段树入门
- 异常处理最佳实践
- 【SSH2】延迟加载问题
- Linux软件包管理之RPM
- Linux软件包管理之YUM
- 解决"未能加载文件或程序集,或它的某一个依赖项,试图加载格式不正确的程序"问题一法
- 线段树入门pku2777 涂颜色问题
- 京沪高铁四天三次事故有力地驳斥了“中国高铁侵犯日本高铁专利”的不实之词。
- 不刷新页面的 可输可选下拉框
- struts1多模块开发
- Thinking in Java - Fourth Edition 章节练习个人解答——第3章
- Using the full potential of Jython to build compact and maintainable wsadmin scripts
- 详解Apache下.htaccess文件常用配置
- 线段树pku3264 [i,j]最大最小数差
- 全面整理的C++面试题