线段树区间更新 Count Color
来源:互联网 发布:歌曲mv制作软件 编辑:程序博客网 时间:2024/06/07 00:06
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.
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.
Ouput results of the output operation in order, each line contains a number.
2 2 4C 1 1 2P 1 2C 2 2 2P 1 2
21
这个的关键是用二进制存 每位的0,1表示这位有没有被涂过色
每次输入的时候 就是1<<(add-1)
所以更新到父节点的话 就要用 |
最后统计颜色 用与运算 都是1才为1
#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn=100005;int sum[maxn*4];struct node{ int l,r,val,mark;}tree[maxn*4];void build(int i,int l,int r){ tree[i].mark=0; tree[i].val=1; tree[i].l=l,tree[i].r=r; //cout<<l<<" "<<r<<endl; if(l==r)return; int m=(l+r)/2; build(i*2,l,m); build(i*2+1,m+1,r);}void pushdown(int i){ if(tree[i].mark!=0) { tree[i*2].mark=tree[i].mark; tree[i*2+1].mark=tree[i].mark; tree[i*2+1].val=tree[i].val; tree[i*2].val=tree[i].val; tree[i].mark=0; }}void update(int i,int l,int r,int add){ //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl; if(tree[i].l==l&&r==tree[i].r) //要更新的区间比i所含的区间大 全部包含 { tree[i].mark=1; tree[i].val=1<<(add-1); //用不同位上的1表示 //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl; return; } if(tree[i].mark!=0)pushdown(i); int m=(tree[i].l+tree[i].r)/2; if(r<=m)update(i*2,l,r,add); else if(l>m)update(i*2+1,l,r,add); else { update(i*2,l,m,add); update(i*2+1,m+1,r,add); } tree[i].val=(tree[i*2].val)|(tree[i*2+1].val); //cout<<"CC"<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl; if(tree[i*2].mark&&tree[i*2+1].mark&&tree[i*2].val==tree[i*2+1].val) tree[i].mark=1; //如果左右子树都被标记且颜色相同 就把父节点标记 这样查询的时候可以省时}int query(int i,int l,int r){ //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl; if(tree[i].l>=l&&tree[i].r<=r) return tree[i].val; pushdown(i); int ans=0; int m=(tree[i].l+tree[i].r)/2; if(l<=m)ans|=query(i*2,l,r); if(r>m)ans|=query(i*2+1,l,r); return ans;}int main(){ int l,t,o; while(scanf("%d%d%d",&l,&t,&o)!=-1) { char op; int a,b,c; memset(tree,0,sizeof(tree)); build(1,1,l); while(o--) { scanf(" %c",&op); //cout<<op<<endl; if(op=='C') { scanf("%d%d%d",&a,&b,&c); if(a>b)swap(a,b); update(1,a,b,c); } else { scanf("%d%d",&a,&b); if(a>b)swap(a,b); int tmp=query(1,a,b); int count=0; for(int i=0;i<t;i++) { if(tmp&(1<<i))count++; } cout<<count<<endl; } } } return 0;}
阅读全文
0 0
- POJ2777 Count Color 线段树区间更新
- POJ2777 Count Color 线段树区间更新
- 【POJ2777】Count Color-线段树区间更新
- 线段树区间更新 Count Color
- Count Color (线段树,区间更新)
- 线段树区间更新,区间统计 poj 2777 Count Color
- POJ 2777 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(线段树 区间更新)
- poj2777 Count Color 线段树区间更新+位操作
- poj 2777 Count Color (线段树 区间更新 染色)
- POJ 2777 Count Color 线段树 区间更新
- poj 2777 Count Color 线段树区间更新
- POJ 2777 Count Color (线段树区间更新)
- Q107:Mac系统下GDB对PBRT-V3进行debug
- 浅析前端MVC、MVP、MVVM模式
- 第七篇 Linux的文件权限与目录配置(下)
- python scipt学习笔记(详细注释加图表解释)
- 增强学习 | AlphaGo背后的秘密
- 线段树区间更新 Count Color
- Return of the Nim 山东省赛
- Android开发之高德地图实现定位
- Reporting Service WebService [Method].SetPolicy
- 北京市地面数字电视节目表
- HDU5716(shiftand算法)
- UDP 通信
- SpringBoot 创建启动运行类
- 算法设计与应用基础系列4