POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 53312 | Accepted: 16050 |
Description
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
Output
Sample Input
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
题意就是区间染色,然后查询区间颜色数量,不能直接线段树上更新值,会被覆盖掉,所以要用二进制状态压缩一下,学到了小技巧,用二进制进行状态压缩。
有关位运算的老是记不住,笨死了。。。
贴一下大佬的有关位运算的:
位运算总结(按位与,或,异或)
还有一篇有关二进制状态压缩的:
状态压缩 位运算
本篇博客完全就是为了备忘才水的。。。
我写的时候,初始化写挫了,查询的时候,ret应该初始化为0,我写的1。。。
运算规则:0|0=0; 0|1=1; 1|0=1; 1|1=1;
即 :参加运算的两个对象只要有一个为1,其值为1。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int tree[maxn<<],lazy[maxn<<]; void pushup(int rt)
{
tree[rt]=tree[rt<<]|tree[rt<<|];//颜色汇总
} void pushdown(int rt)
{
if(lazy[rt]){
lazy[rt<<]=lazy[rt<<|]=lazy[rt];
tree[rt<<]=tree[rt<<|]=lazy[rt];
lazy[rt]=;
}
} void build(int l,int r,int rt)
{
lazy[rt]=;
tree[rt]=;
if(l==r){
return ;
} int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(r<L||l>R) return ;
if(L<=l&&r<=R){
lazy[rt]=<<(c-);//二进制状态压缩保存颜色
tree[rt]=<<(c-);
return ;
} pushdown(rt);
int m=(l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt)
{
if(r<L||l>R) return ;
if(L<=l&&r<=R){
return tree[rt];
} pushdown(rt);
int m=(l+r)>>;
int ret=;
if(L<=m) ret|=query(L,R,lson);
if(R> m) ret|=query(L,R,rson);
return ret;
} int main()
{
ios;
int n,m,q;
cin>>n>>m>>q;
build(,n,);
while(q--){
char op[];
int l,r,color;
cin>>op;
if(op[]=='C'){
cin>>l>>r>>color;
if(l>r) swap(l,r);
update(l,r,color,,n,);
}
else{
cin>>l>>r;
if(l>r) swap(l,r);
int cnt=query(l,r,,n,);
int ans=;
while(cnt){
if(cnt&) ans++;
cnt>>=;
}
cout<<ans<<endl;
}
}
return ;
}
。。。
POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。的更多相关文章
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- POJ P2777 Count Color——线段树状态压缩
Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...
- POJ 2777 Count Color(段树)
职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...
- POJ2777 Count Color 线段树区间更新
题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...
随机推荐
- Spring注解@Resource和@Autowired的区别
@Resource和@Autowired都是用来做bean的依赖注入的,两者都可以写在字段和setter方法上. java为我们提供了 javax.annotation.Resource这个注解. s ...
- 51Nod 1087 1 10 100 1000 | 数学
Input示例 3 1 2 3 Output示例 1 1 0 #include "bits/stdc++.h" using namespace std; #define LL lo ...
- 搜索:BFS
如果题目真的要考察宽度优先搜索,那么这类题目往往具有比较大的编码难度,换个说法,就是细枝末节特别多,状态特别复杂.. 剥茧抽丝,这里以一个比较“裸”的BFS作为例子,了解一下实现BFS的一些规范. 直 ...
- 2018 Multi-University Training Contest 1-1002 -Balanced Sequence(括号匹配+贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6299 题目: 题意:t组数据,每组数据给你一个n表示给你n个括号串,这n个括号串之间进行组合,求能够匹 ...
- Javascript prototype 及 继承机制的设计思想
我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类"的概念,也没有"类"(class)和"实例" ...
- eCharts_数据过多底部滚动条实现数据展示
效果图: 实现原理: 1.添加dataZoom属性 效果实现代码: <!DOCTYPE html> <html> <head> <meta charset=& ...
- 安装Vue.js devtools
1.下载安装 https://github.com/vuejs/vue-devtools#vue-devtools 通过以上地址下载安装包,解压以后进入文件,按住shift,点击鼠标右键打开命令窗口 ...
- Part2-HttpClient官方教程-Chapter6-HTTP缓存(HTTP Caching)
原文链接 6.1. 一般概念 HttpClient Cache提供了一个与HTTP / 1.1兼容的缓存层与HttpClient(浏览器缓存的Java等价物.)一起使用.该实现遵循责任链设计模式,其中 ...
- perl6中函数参数(1)
sub F($number is copy){ $number++; say $number; } F(); #下面是错误的 sub F($number){ $number++; say $numbe ...
- js中的return
retrun true: 返回正确的处理结果. return false:分会错误的处理结果,终止处理. return:把控制权返回给页面(如果条件满足,后面的逻辑就不执行了). if(this.in ...