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 ...
随机推荐
- Webservice与CXF框架快速入门
1. Webservice Webservice是一套远程调用技术规范 远程调用RPC, 实现了系统与系统进程间的远程通信.java领域有很多可实现远程通讯的技术,如:RMI(Socket + 序列化 ...
- mybatis 热部署xml文件(spring boot和springmvc两种方式)
参考:http://thinkgem.iteye.com/blog/2304557 步骤:1.创建两个java类 (1)MapperRefresh.java :用于刷新mapper (2)SqlS ...
- Linux修改用户密码
1. root修改自己 # passwd 2. root修改别人 # passwd oracle //修改oracle的密码
- flask 自定义url转换器
from werkzeug.routing import BaseConverter app = Flask(__name__) class TeleConveter(BaseConverter): ...
- JNLP Slave connection error解决办法
Replace in jnlp-file <argument>-workDir</argument> <argument /> with <argume ...
- [cerc2012][Gym100624B]20181013
- 【Atcoder】ARC084 Small Multiple
[题意]求一个k的倍数使其数位和最小,输出数位和,k<=10^5. [算法]最短路 [题解]考虑极端情况数字是可能爆long long的(例如k*num=100...000),所以确定基本方向是 ...
- bzoj 1058 bst
因为是数列的维护,所以我们可以考虑用splay来维护,每次在x插入的时候就在x+1前面插入就行了,然后用bst来维护两问的答案,但是应该会tle.我们来考虑这个问题的性质,首先因为这个数列没有删除操作 ...
- ie8下input文字偏上select文字偏下
1.ie8下input文字偏上 正常情况下input的显示情况如下 当设置input的高度时,就会出现文字不垂直居中偏上的情况,如图 解决方案 强input的行高line-height与其高度设置一致 ...
- Linux线程同步
1. 线程同步: 当多个控制线程共享相同的内存时,需要确保每个线程看到一致的数据视图.当某个线程可以修改变量,而其他线程也可以读取或者修改这个变量的时候,就需要对这些线程进行同步,以确保他们在访问变量 ...