POJ Count Color 2777(线段树)
来源:互联网 发布:算法复杂性 Johnson 编辑:程序博客网 时间:2024/06/10 05:04
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 43739 Accepted: 13237
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.
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 4C 1 1 2P 1 2C 2 2 2P 1 2
Sample Output
21
Source
POJ Monthly--2006.03.26,dodo
这道题不是多难,是我太菜,先上一张图,错的都快哭了
题意:统计区间有多少不同的颜色,颜色最多有30种,一开始颜色都是1,这就是线段树的区间操作
分析:网上别人用二进制来统计颜色看不懂,问了一下别人,暴力统计颜色,因为一共才30种,
sum[ ] 表示这个区间的颜色,如果这个区间有两种以上的颜色,sum[rt]=0,
lazy[ ] 懒惰标记
找的时候用数组标记一下颜色,找到的这个区间如果sum[ ]=0,就继续向下找,如果不为0,就说明这个区间就这一种颜色,不用继续向下找了,说的不清,看代码
#include <stdio.h>#include <string.h>#include <algorithm>#include <stdlib.h>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define maxx 100100int sum[maxx<<2]; //当前区间的颜色,如果为0,则代表当前区间有不同的颜色int lazy[maxx<<2];int v[40];//标记颜色int flag;void gengxin(int rt){ if(sum[rt<<1]!=sum[rt<<1|1]) { sum[rt]=0; } else if(sum[rt<<1]==sum[rt<<1|1]) { sum[rt]=sum[rt<<1]; }}void genfxinlazy(int rt){ if(lazy[rt]) { sum[rt<<1]=sum[rt<<1|1]=lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt]; lazy[rt]=0; }}void build(int l,int r,int rt){ memset(sum,0,sizeof(sum)); memset(lazy,0,sizeof(lazy));}void gengxinqujian(int x,int y,int k,int l,int r,int rt){ if(x<=l && r<=y) { sum[rt]=k; lazy[rt]=k; // printf("%d %d %d\n",l,r,rt); return ; } genfxinlazy(rt); int m=(l+r)>>1; if(x<=m) gengxinqujian(x,y,k,lson); if(m<y) gengxinqujian(x,y,k,rson); gengxin(rt);}void zhao(int rt){ if(sum[rt]!=0) { v[sum[rt]]++; return ; } else { zhao(rt<<1); zhao(rt<<1|1); }}void query(int x,int y,int l,int r,int rt){ if(x>r || y<l) { flag=1; return ; } if(x<=l && r<=y) { zhao(rt); return ; } genfxinlazy(rt); int m=(l+r)>>1; if(x<=m) query(x,y,lson); if(m<y) query(x,y,rson); //gengxin(rt);}int main(){ int n,m,i,j,t; int x,y,k; char s[5]; while(~scanf("%d%d%d",&n,&t,&m)) { build(1,n,1); gengxinqujian(1,n,1,1,n,1); while(m--) { scanf("%s",s); if(s[0]=='C') { scanf("%d%d%d",&x,&y,&k); if(x>y)swap(x,y); if(k<=t && k>=1) gengxinqujian(x,y,k,1,n,1); } if(s[0]=='P') { int ans=0; flag=0; scanf("%d%d",&x,&y); if(x>y)swap(x,y); memset(v,0,sizeof(v)); query(x,y,1,n,1); for(i=1;i<=t;i++) { if(v[i]) { //printf("%d ",i); ans++; } } if(flag) ans=0; printf("%d\n",ans); } } } return 0;}
1 0
- POJ 2777 Count Color 线段树
- POJ 2777 Count Color 线段树
- POJ 2777 Count Color(线段树#1)
- 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(线段树)
- POJ 2777 Count Color(线段树 )
- yii2.0 使用coreseek
- 调试语句
- 第一天 菜鸟前进之道 activity
- GloVe学习之Python中简单的词向量SVD分解
- WCF绑定的选择
- POJ Count Color 2777(线段树)
- CheckBox全选与反转
- Struts中ActionContext和ServletActionContext的比较
- Java多态性理解,好处及精典实例
- AngularJS系列之JavaScript函数和对象
- MySQL的TPS和QPS 计算方式
- ReactiveCocoa之RACMulticastConnection使用(六)
- 使用LiveCD制作Linux Ubantu系统img镜像文件
- 面向对象的5个基本设计原则