Count Color (线段树,区间更新)
来源:互联网 发布:阿里云 mongodb 连接 编辑:程序博客网 时间:2024/06/02 00:48
Problem 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
题目大概:
在一个单行表格上,对他们进行区间涂色,最后查询区间的颜色数。
思路:
线段树,区间查询,根据别人ac代码写的。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct poin{ int l,r,id,v;}a[101000<<2];void build(int L,int R,int rt){ a[rt].l=L; a[rt].r=R; if(L==R)return; int m=(L+R)>>1; build(L,m,rt<<1); build(m+1,R,rt<<1|1);}void update(int L,int R,int v,int rt){ if(L==a[rt].l&&R==a[rt].r) { a[rt].id=1; a[rt].v=1<<(v-1); return; } if(a[rt].id) { a[rt].id=0; a[rt<<1].id=a[rt<<1|1].id=1; a[rt<<1].v=a[rt<<1|1].v=a[rt].v; } int m=(a[rt].l+a[rt].r)>>1; if(R<=m) { update(L,R,v,rt<<1); } else if(L>m) { update(L,R,v,rt<<1|1); } else { update(L,m,v,rt<<1); update(m+1,R,v,rt<<1|1); } a[rt].v=(a[rt<<1].v|a[rt<<1|1].v); if(a[rt<<1].id&&a[rt<<1|1].id&&a[rt<<1].v==a[rt<<1|1].v) a[rt].id=1;}int quert(int L,int R,int rt){ if(a[rt].l==L&&a[rt].r==R) { return a[rt].v; } if(a[rt].id) { return a[rt].v; } int m=(a[rt].l+a[rt].r)>>1; if(R<=m) { return quert(L,R,rt<<1); } else if(L>m) { return quert(L,R,rt<<1|1); } else { return quert(L,m,rt<<1)|quert(m,R,rt<<1|1); }}int main(){ int l,k,n; while(~scanf("%d%d%d\n",&l,&k,&n)) { build(1,l,1); a[1].v=1; a[1].id=1; while(n--) { char q[2]; scanf("%s",q); if(q[0]=='C') { int w,e,r; scanf("%d%d%d",&w,&e,&r); update(w,e,r,1); } else { int w,e; scanf("%d%d",&w,&e); int sum=quert(w,e,1); int su=0; for(int j=0;j<k;j++) { if(sum&(1<<j))su++; } printf("%d\n",su); } } } return 0;}
阅读全文
0 0
- Count Color (线段树,区间更新)
- POJ2777 Count Color 线段树区间更新
- POJ2777 Count Color 线段树区间更新
- 【POJ2777】Count Color-线段树区间更新
- 线段树区间更新 Count Color
- POJ 2777 Count Color (线段树区间更新)
- PKU 2777 Count Color (线段树区间更新)
- poj 2777 Count Color (线段树区间更新)
- POJ2777 - 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[线段树区间更新查询]
- poj2777 Count Color 线段树区间更新+位操作
- poj 2777 Count Color (线段树 区间更新 染色)
- LinuxStudyNote(34)-Linux系统运行级别、/etc/inittab、runlevel查看系统运行级别
- 《Modern Python Cookbook》(Python编程范例)笔记1.4真除法和向下截断除法(floor除法)
- [Git]Window下创建.gitignore以及.gitignore的语法
- JAVA自学笔记(1)
- Java后台框架篇--Struts2.0标签库
- Count Color (线段树,区间更新)
- 杭电1006 Tick and Tick
- bzoj 1055: [HAOI2008]玩具取名(区间DP)
- 杭电1007 Quoit Design
- 杭电1008 Elevator
- oracle 使用sql操作arcgis st_geometry类型
- 坚持#第196天~云计算网络方面了解到了
- mount vbox addition失败
- HDU 5952 Counting Cliques 爆搜