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 ...
随机推荐
- 关于我之前写的修改Windows系统Dos下显示的用户名之再修改测试
最近看到蛮多网友反映,自己修改Dos下用户名后出现了很多的问题--今天抽了时间,再次修改测试... ================= 提前说明:我自己修改了很多次没发现任何问题,<为避免修改可 ...
- Centos7 安装rabbitmq(转载)
原文地址:http://blog.csdn.net/wenyu826/article/details/71108279 安装Erlang 从链接https://packages.erlang-solu ...
- shell编程:条件测试与比较(六)
条件测试方法综述 test条件测试的简单语法及测试 范例6-1 测试文件(在test命令中使用-f选项:文件存在且为不同文件则表达式成立) [root@adminset ~]# test -f fil ...
- 游戏编程入门之Bomb Catcher游戏
首先是代码: MyDirectX.h: #pragma once //header file #define WIN32_EXTRA_LEAN #define DIRECTINPUT_VERSION ...
- (转)如何在windows 2008 安装IIS
首先声明本文转自http://www.pc6.com/infoview/Article_54712.html ,作者为清晨 转载的原因有两个,一是怕原文挂了,而是打算写一下在阿里云部署django的文 ...
- HDU 2553 N皇后问题 (深搜)
题目链接 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对 ...
- vue数组操作不触发前端重新渲染
暂时使用给数组先赋值 [ ] ,然后重新赋值的方式解决. 此外,能够监听的数组变异方法 https://cn.vuejs.org/v2/guide/list.html#%E5%8F%98%E5%BC% ...
- div+css实现表头固定内容滚动表格
<div class="m-demo"> <table> <thead> <tr><th>定宽a</th>& ...
- poj 1837 Balance(背包)
题目链接:http://poj.org/problem?id=1837 Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissi ...
- Python模块学习 - openpyxl
openpyxl模块介绍 openpyxl模块是一个读写Excel 2010文档的Python库,如果要处理更早格式的Excel文档,需要用到额外的库,openpyxl是一个比较综合的工具,能够同时读 ...