POJ 2777 Count Color ( 线段树&&位运算
来源:互联网 发布:章泽天 见过本人 知乎 编辑:程序博客网 时间:2024/06/08 10:14
Count Color
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:
- “C A B C” Color the board from segment A to segment B with color C.
- “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
题意
有两个操作
1 把一个区间变成一个颜色
2 查询区间中的颜色总量
AC代码
#include <cstdio>#include <algorithm>using namespace std;#define LL long longconst int MAXN = 1e5+10;struct node{ int l, r; int lazy; int color;}tree[MAXN*4];void build(int l,int r,int st){ tree[st].l = l; tree[st].r = r; tree[st].lazy = 0; tree[st].color = 0; if(l == r) return ; int mid = (l+r) >> 1; build(l,mid,st<<1); build(mid+1,r,st<<1|1);}void update(int l, int r,int st,int val){ if(tree[st].l==l && tree[st].r==r) { tree[st].lazy = 1; tree[st].color = 1<<(val-1); //我们知道颜色的种类数一共不超过30,所以TAT return; } if(tree[st].lazy) { tree[st].lazy = 0; tree[st<<1].lazy = tree[st<<1|1].lazy = 1; tree[st<<1].color = tree[st<<1|1].color = tree[st].color; } int mid = (tree[st].l+tree[st].r) >> 1; if(r <= mid) update(l,r,st<<1,val); else if(l > mid) update(l,r,st<<1|1,val); else { update(l,mid,st<<1,val); update(mid+1,r,st<<1|1,val); } tree[st].color = tree[st<<1].color | tree[st<<1|1].color; if(tree[st<<1].lazy && tree[st<<1|1].lazy && tree[st<<1].color==tree[st<<1|1].color) tree[st].lazy = 1;}int query(int l,int r,int st){ if(l==tree[st].l && r==tree[st].r) return tree[st].color; if(tree[st].lazy) return tree[st].color; //剪下枝 int mid = (tree[st].l+tree[st].r) >> 1; if(r <= mid) return query(l,r,st<<1); else if(l > mid) return query(l,r,st<<1|1); else { return query(l,mid,st<<1)|query(mid+1,r,st<<1|1); }}int main(){ int n, t, q; while(~scanf("%d%d%d",&n,&t,&q)) { build(1,n,1); tree[1].lazy = 1; tree[1].color = 1; while(q--) { char ch[10]; scanf("%s",ch); int x, y, z; if(ch[0] == 'C') { scanf("%d%d%d",&x,&y,&z); if(x > y) swap(x,y); update(x,y,1,z); } else { scanf("%d%d",&x,&y); if(x > y) swap(x,y); LL k = query(x,y,1); int num = 0; for(int i = 0; i < t; i++) { if(k&(1<<i)) num++; } printf("%d\n",num); } } }return 0;}
- 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 ( 线段树&&位运算
- hdu5023A Corrupt Mayor's Performance Art(线段树+位运算) poj 2777Count Color(线段树+位运算)
- Count Color(线段树+位运算 POJ2777)
- Count Color(线段树+位运算)
- POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
- POJ 2777 Count Color 【线段树 区间更新 按位或运算】
- POJ 2777 Count Color(线段树区间修改+位运算)
- poj 2777 Count Color(线段树+lazy优化+位运算)
- POJ 2777Count Color (线段树区间更新 + 位运算压缩)
- 如何在word2016中使用自带的公式编辑器
- unity拍照保存到ios手机
- 项目维护总结——为什么这么累
- 软件工程师中的软件维护概述
- Flink读写数据(hdfs)(DataSet)
- POJ 2777 Count Color ( 线段树&&位运算
- 如何复制或导出托管磁盘
- IOS发送短信附件
- CSS使用详解
- Adobe Photoshop CC 2017破解方法
- 快速开发平台
- 有哪些好的软件外包网站
- ionic/angularjs 加载百度地图
- httping测对端应用情况