POJ 2777 Count Color 线段树入门题
来源:互联网 发布:u盘恢复数据免费软件 编辑:程序博客网 时间:2024/05/18 01:11
今天开始看的线段树,感觉树存储的优势了啊,很强大、时间复杂度降低了很多啊。
在这里先说一下题目的大体意思是:给区间进行染色,后染的颜色会会覆盖掉之前染过的颜色,然后输入C是代表给区间(a,b)染色成c。P代表输出区间(a,b)一共有几种颜色。
典型的线段树的创建、修改、与查询问题啊。所以要用线段树来做啊、否则会超时的啊,我好像跑了969ms,有点龟速了啊,但是重点是学习算法,优化以后熟悉了再说吧。
PS:这是线段树,第一题。
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 32820 Accepted: 9889
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
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7#define M 10001000//#define LL __int64#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898const int maxn = 100100;using namespace std;struct node{ int a, b; int color; int ct;} f[4*maxn];int n, t, k;int cnum[maxn];int ans;void creat(int l, int r, int num){ f[num].a = l; f[num].b = r; f[num].color = (1<<1);//初始化颜色; f[num].ct = 0; int mid = (l+r)/2; if(r-l>=1) { creat(l, mid, 2*num);//递归创建线段树; creat(mid+1, r, 2*num+1); }}void Insert(int l, int r, int c, int num){ int a = f[num].a; int b = f[num].b; if(a == l && r == b)//递归的出口,就是找到了这个区间; { f[num].color = cnum[c]; f[num].ct = c; return; } int mid = (a+b)/2; if(f[num].ct != 0)//如果根节点被染过,就用它的子节点保存颜色,然后就把它自己的颜色去掉; { f[num*2].ct = f[num].ct; f[num*2].color = f[num].color; f[num*2+1].ct = f[num].ct; f[num*2+1].color = f[num].color; f[num].ct = 0; } if(mid >= r) { Insert(l, r, c, num*2);//偏左; } else if(mid < l) { Insert(l, r, c, num*2+1);//偏右; } else { Insert(l, mid, c, num*2);//中间分开的; Insert(mid+1, r, c, num*2+1); } f[num].color = f[num*2].color|f[num*2+1].color;}void Search(int l, int r, int num){ int a = f[num].a; int b = f[num].b; int mid = (a+b)/2; if(l <= f[num].a && f[num].b <= r) { ans = ans|f[num].color;//找到满足的区间然后标记颜色; return ; } else if((f[num].a <= l && r <= f[num].b) && f[num].ct != 0) { ans = ans|f[num].color; return ; } if(mid >= r) { Search(l, r, num*2); } else if(mid < l) { Search(l, r, num*2+1); } else { Search(l, mid, num*2); Search(mid+1, r, num*2+1); }}int dos(int x){ int sum = 0; for(int i = 1; i <= 30; i++) { if((x&(1 << i)) != 0) sum ++; } return sum;}int main(){ int i; for(i = 1; i <= 30; i++) cnum[i] = (1<<i); while(cin >>n>>t>>k) { creat(1, n, 1); int a, b, c; char str; while(k--) { cin >>str; if(str == 'C') { cin >>a>>b>>c; if(a > b) swap(a, b); Insert(a, b, c, 1); } else if(str == 'P') { cin >>a>>b; if(a > b) swap(a, b); ans = 0; Search(a, b, 1); cout<<dos(ans)<<endl; } } } return 0;}
0 0
- POJ 2777 Count Color 线段树入门题
- 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(线段树)
- cocos2d-x-2.2.2项目添加到vs
- 设计模式-创建者模式(Builder)
- poj 2501
- some useful blog
- 2013_ICCV_Efficient Image Dehazing with Boundary Constraint and Contextual Regularization
- POJ 2777 Count Color 线段树入门题
- 设计模式-代理模式(Proxy)
- java nio
- 设计模式
- 欢迎新同事!
- 设计模式9
- Matlab 距离变换
- 设计模式10
- 设计模式11