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 ,别的没什么大 ...
随机推荐
- Strusts2笔记9--防止表单重复提交和注解开发
防止表单重复提交: 用户可能由于各种原因,对表单进行重复提交.Struts2中使用令牌机制防止表单自动提交.以下引用自北京动力节点:
- 推荐一本springBoot学习书籍---深入浅出springBoot2.x
花了几周时间读完了这本书,确实是一本特别详细全面的书,而且不单单只是springBoot, 书中还介绍了许多工作中常用的技术与springBoot的整合使用,当然,也有一些小bug, 因为在代码实践过 ...
- 【Python项目】爬取新浪微博个人用户信息页
微博用户信息爬虫 项目链接:https://github.com/RealIvyWong/WeiboCrawler/tree/master/WeiboUserInfoCrawler 1 实现功能 这个 ...
- mysql远程连接数据库
配置mysql允许远程连接的方法. (1)查看3306端口状态 netstat -an | grep 3306 (2)修改mysql配置文件 ubuntu系统:vim /etc/mysql/mysql ...
- 【前端vue开发】vue单页应用添加百度统计
前言 申请百度统计后,会得到一段JS代码,需要插入到每个网页中去,在Vue.js项目首先想到的可能就是,把统计代码插入到index.html入口文件中,这样就全局插入,每个页面就都有了;这样做就涉及到 ...
- 消息 8101,级别 16,状态 1,第 1 行仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'CUSTOMER_TBL'中的标识列指定显式值。
像这样的问题怎么解决呢? 问题分析: 意思是你的主键是自动编号类型的,所以不能向该列插入数据. 解决办法: 执行 语句 :SET IDENTITY_INSERT CUSTOMER_TBL ON 然后在 ...
- ETL工具kettle基本使用
1.下载kettle:https://sourceforge.net/projects/pentaho/files/Data%20Integration/7.0/pdi-ce-7.0.0.0-25.z ...
- 移动端布局 - REM方式
默认以宽度为640px的设计稿为基准页面,然后通过JS获取当前显示设备的尺寸,对应的调整 html 标签的font-size大小,从而实现通过以rem为单位的移动端布局适配. 具体代码 (functi ...
- Registry私有仓库搭建及认证
本节内容: Registry相关概念 Registry V1和V2 安装Docker 搭建本地registry v2 搭建外部可访问的Registry 添加认证 更高级的认证 registry web ...
- JavaScript 三个常用对话框
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...