poj 2777 Count Color (成段更新+区间求和)
来源:互联网 发布:逗斗车 知乎 编辑:程序博客网 时间:2024/06/06 18:54
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 36646 Accepted: 11053
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
题意就是求一段墙壁颜色种类,因为颜色种类最多30,可以用二进制表示颜色的总数。
#include<cstdio>#include<cmath>#include<queue>#include<iostream>#include<algorithm>using namespace std;#define ll __int64#define N 100005struct node{ int l,r; int s,v,f; //颜色种类二级制表示,区间颜色、是否需要向下更新}f[N*3];void creat(int t,int l,int r){ f[t].l=l; f[t].r=r; f[t].v=1; f[t].f=0; f[t].s=1; //起始颜色为1,可以用二进制表示 if(l==r) { return ; } int tmp=t<<1,mid=(l+r)>>1; creat(tmp,l,mid); creat(tmp|1,mid+1,r);}void update(int t,int l,int r,int v){ int tmp=t<<1,mid=(f[t].l+f[t].r)>>1; if(f[t].l==l&&f[t].r==r) { f[t].v=v; f[t].s=(1<<v); f[t].f=1; return ; } if(f[t].f) //向下更新 { f[tmp].f=f[tmp|1].f=1; f[tmp].v=f[tmp|1].v=f[t].v; f[tmp].s=f[tmp|1].s=(1<<f[t].v); f[t].f=0; } if(r<=mid) update(tmp,l,r,v); else if(l>mid) update(tmp|1,l,r,v); else { update(tmp,l,mid,v); update(tmp|1,mid+1,r,v); } f[t].f=0; //向上求和 f[t].s=f[tmp].s|f[tmp|1].s; }int query(int t,int l,int r){ if(f[t].l==l&&f[t].r==r) { return f[t].s; } int tmp=t<<1,mid=(f[t].l+f[t].r)>>1; if(f[t].f) { f[tmp].f=f[tmp|1].f=1; f[tmp].v=f[tmp|1].v=f[t].v; f[tmp].s=f[tmp|1].s=(1<<f[t].v); f[t].f=0; } if(r<=mid) return query(tmp,l,r); else if(l>mid) return query(tmp|1,l,r); else { return query(tmp,l,mid)|query(tmp|1,mid+1,r); }}int main(){ int n,t,q,l,r,c; char ch; while(~scanf("%d%d%d",&n,&t,&q)) { creat(1,1,n); while(q--) { getchar(); scanf("%c ",&ch); if(ch=='C') { scanf("%d%d%d",&l,&r,&c); if(l>r) swap(l,r); update(1,l,r,c-1); } else { scanf("%d%d",&l,&r); if(l>r) swap(l,r); int tmp=query(1,l,r),ans=0; while(tmp) { if(tmp&1) ans++; tmp>>=1; } printf("%d\n",ans); } } } return 0;}
0 0
- poj 2777 Count Color (成段更新+区间求和)
- poj 2777 Count Color(线段树 Lazy-Tag思想 成段更新+区间统计)
- poj(2777)——Count Color(lazy思想,成段更新,区间统计)
- POJ 2777 Count Color 线段树 成段更新
- 线段树区间更新,区间统计 poj 2777 Count Color
- Poj 3667 Hotel 成段更新,区间合并,区间求和
- poj 2777 Count Color【线段树段更新】
- C 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 线段树 区间更新
- IOS-常见控件用法例子(一)
- Mac Word的显示问题
- HTTP协议头部与Keep-Alive模式详解
- android组件搭配
- 循环-07. 爬动的蠕虫(15)
- poj 2777 Count Color (成段更新+区间求和)
- 国外intel总结的比较全的Android 3D游戏引擎介绍
- 杭电OJ(HDOJ)1032题:The 3n + 1 problem(穷举,水题)
- windb symbols
- SS5 windows移植
- hql查询语句的拼接要注意
- 多线程
- Servlet与CGI的比较
- 为什么析构函数通常很虚?