poj 2777 线段树 区间更新+位运算
题意:有一个长板子,分成多段,有两种操作,第一种是C给从a到b那段染一种颜色c,另一种是P询问a到b有多少种不同的颜色。
Sample Input
2 2 4 板长 颜色数目 询问数目
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
sum用二进制记录区间内颜色状态,col记录染上的颜色(其他颜色会被染上的颜色完全覆盖)
2015-07-23:专题复习
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root 1,n,1
#define mid ((l+r)>>1)
const int MAXN=;
int n,m,t,Min;
int sum[MAXN<<],col[MAXN<<];
void pushup(int rt)
{
sum[rt]=sum[rt<<]|sum[rt<<|];
}
void pushdown(int rt)
{
if(col[rt]!=-)
{
sum[rt<<]=sum[rt<<|]=<<(col[rt]-);
col[rt<<]=col[rt<<|]=col[rt];
col[rt]=-;
}
}
void build()
{
sum[]=;
col[]=;
}
void update(int L,int R,int val,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
sum[rt]=(<<(val-));
col[rt]=val;
return;
}
pushdown(rt);
if(mid>=L) update(L,R,val,lson);
if(mid<R) update(L,R,val,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
pushdown(rt);
int ans=;
if(mid>=L) ans|=query(L,R,lson);
if(mid<R) ans|=query(L,R,rson);
return ans; }
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
int l,t,o,a,b,c;
while(scanf("%d%d%d",&l,&t,&o)!=EOF)
{
n=l;
build();
char s[];
while(o--)
{
scanf("%s",&s);
if(s[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b) swap(a,b);
update(a,b,c,root);
}
if(s[]=='P')
{
scanf("%d%d",&a,&b);
if(a>b) swap(a,b);
int tmp=query(a,b,root);
int ans=;
while(tmp)
{
if(tmp&)
ans++;
tmp>>=;
}
printf("%d\n",ans);
}
}
}
}
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root 1,n,1
#define mid ((l+r)>>1)
const int MAXN=;
int n,m,t,Min;
int sum[MAXN<<],col[MAXN<<];
void pushup(int rt){
sum[rt]=sum[rt<<]|sum[rt<<|];
}
void pushdown(int rt)
{
if(col[rt]!=-)
{
sum[rt<<]=sum[rt<<|]=<<(col[rt]-);
col[rt<<]=col[rt<<|]=col[rt];
col[rt]=-;
}
}
void build(){
sum[]=;
col[]=;
}
void update(int L,int R,int val,int l,int r,int rt)
{
if(l>=L&&r<=R)
{
col[rt]=val;
sum[rt]=(<<(val-));
return;
}
if(L>r||R<l)
return ;
pushdown(rt);
update(L,R,val,lson);
update(L,R,val,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt) {
if (l>=L&&r<=R){
return sum[rt];
}
if(L>r||R<l)
return ;
pushdown(rt);
return query(L,R,lson)|query(L,R,rson);
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
int l,t,o,a,b,c;
while(scanf("%d%d%d",&l,&t,&o)!=EOF)
{
build();
n=l;
char s[];
while(o--)
{
scanf("%s",&s);
if(s[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b) swap(a,b);
update(a,b,c,root);
}
if(s[]=='P')
{
scanf("%d%d",&a,&b);
if(a>b) swap(a,b);
int tmp=query(a,b,root);
int ans=;
while(tmp)
{
if(tmp&)
ans++;
tmp>>=;
}
printf("%d\n",ans);
}
}
}
}
poj 2777 线段树 区间更新+位运算的更多相关文章
- A Corrupt Mayor's Performance Art(线段树区间更新+位运算,颜色段种类)
A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100 ...
- poj 3468 线段树区间更新/查询
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- hdu 1698+poj 3468 (线段树 区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...
- POJ 3225 线段树区间更新(两种更新方式)
http://blog.csdn.net/niuox/article/details/9664487 这道题明显是线段树,根据题意可以知道: (用0和1表示是否包含区间,-1表示该区间内既有包含又有不 ...
- POJ 3225 (线段树 区间更新) Help with Intervals
这道题搞了好久,其实坑点挺多.. 网上找了许多题解,发现思路其实都差不多,所以就不在重复了. 推荐一篇比较好的题解,请戳这. 另外,如果因为可能要更新多次,但最终查询只需要一次,所以没有写pushup ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
随机推荐
- css单行文本和多行文本溢出实现省略号显示
1.单行文本溢出 文本内容 <div class="singleLine"> HelloWorldHelloWorldHelloWorldHelloWorldHello ...
- margin-bottom无效问题以及div里内容动态居中样式!
最近调前端样式时候,遇到一个需求,在中间文字不对等的情况下想让下面的操作文字距离底部对齐,如图: , 刚开始觉得使用margin-bottom就可以,后来发现只有margin-top是管用的,查了资料 ...
- 源码安装postgresql数据库
一般情况下,postgresql由非root用户启动. 1.创建postgres用户 groupadd postgres useradd -g postgres postgres 下面的操作都在pos ...
- aarch64_p1
PEGTL-devel-1.3.1-2.fc26.aarch64.rpm 2017-02-14 08:00 63K fedora Mirroring Project PackageKit-1.1.6- ...
- SQL 根据关联表更新主表中字段数据
今天遇到一个客户的数据更新问题,两个相关联的表,一个主表用于保存单据主要信息,一个副表用于保存单据的明细信息:现在要把主表的其中一个字段的数据更新到副表的一个字段中保存.精通的SQL语法的,当然是很简 ...
- Web测试技术要领
基于Web的系统测试与传统的软件测试既有相同之处,也有不同的地方,对软件测试提出了新的挑战.基于Web的系统测试不但需要检查和验证是否按照设计的要求运行,而且还要评价系统在不同用户的浏览器端的显示是否 ...
- 理解 Linux 的硬链接与软链接(待研究)
从 inode 了解 Linux 文件系统 硬链接与软链接是 Linux 文件系统中的一个重要概念,其涉及文件系统中的索引节点 (index node 又称 inode),而索引节点对象是 Linux ...
- 利用mysql的binlog恢复数据
MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...
- Shell脚本系列教程二: 开始Shell编程
Shell脚本系列教程二: 开始Shell编程 2.1 如何写shell script? (1) 最常用的是使用vi或者mcedit来编写shell脚本, 但是你也可以使用任何你喜欢的编辑器; (2) ...
- T-SQL创建前删除已存在存储过程
--判断是否存在addOneArticle这个存储过程 if Exists(select name from sysobjects where NAME = 'addOneArticle' and t ...