题意:有一个长板子,分成多段,有两种操作,第一种是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 线段树 区间更新+位运算的更多相关文章

  1. A Corrupt Mayor's Performance Art(线段树区间更新+位运算,颜色段种类)

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  2. poj 3468 线段树区间更新/查询

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  3. hdu 1698+poj 3468 (线段树 区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...

  4. POJ 3225 线段树区间更新(两种更新方式)

    http://blog.csdn.net/niuox/article/details/9664487 这道题明显是线段树,根据题意可以知道: (用0和1表示是否包含区间,-1表示该区间内既有包含又有不 ...

  5. POJ 3225 (线段树 区间更新) Help with Intervals

    这道题搞了好久,其实坑点挺多.. 网上找了许多题解,发现思路其实都差不多,所以就不在重复了. 推荐一篇比较好的题解,请戳这. 另外,如果因为可能要更新多次,但最终查询只需要一次,所以没有写pushup ...

  6. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  7. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  8. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  9. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

随机推荐

  1. 【算法学习】Fhq-Treap(无旋Treap)

    Treap——大名鼎鼎的随机二叉查找树,以优异的性能和简单的实现在OIer们中广泛流传. 这篇blog介绍一种不需要旋转操作来维护的Treap,即无旋Treap,也称Fhq-Treap. 它的巧妙之处 ...

  2. 深度解析eclipse控制台

    第一个按钮:scroll lock 控制台在打印sql语句的时候会一直滚动,用这个按钮可以固定住控制台不乱跑; 第二个按钮:show console when standard out changes ...

  3. redis tutorail

    命令 set     get    incr expire  秒  ttl    -1 不会过期 list  : lpush  rpush  lpop  rpop   lrange   llen se ...

  4. Git 撤销操作、删除文件和恢复文件

    大致介绍 经过前面的学习,已经建立了版本库,并上传了文件,这次来学习对这些文件进行基本的操作,即: ◆ 撤销操作 ◆ 删除文件 ◆ 恢复文件 我在此之前,已经将三个文件提交到了版本库 撤销操作 撤销操 ...

  5. Java输出文件到本地(输出流)

    package cn.buaa; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; im ...

  6. Linux下的IPC机制

    Linux下的IPC机制 IPC(Inter-Process Communication)是多个进程之间相互沟通的一种方法.在linux下有多种进程间通信的方法. 共享内存 Linux内存共享有多种, ...

  7. HBase混布MapReduce集群学习记录

    一.准备工作 1.1 部署环境 集群规模大概260多台,TSC10机型,机型参数如下: > 1个8核CPU(E5-2620v4) > 64G内存 > HBA,12*4T SATA,1 ...

  8. Struts 2 - Hello World Example

    As you learnt from the Struts 2 architecture, when you click on a hyperlink or submit an HTML form i ...

  9. C++拾遗——重新开始

    http://www.cnblogs.com/uniqueliu/category/307731.html

  10. MySQL慢查询优化

    MySQL数据库是常见的两个瓶颈是CPU和I/O的瓶颈,CPU在饱和的时候一般发生在大量数据进行比对或聚合时.磁盘I/O瓶颈发生在装入数据远大于内存容量的时候,如果应用分布在网络上,那么查询量相当大的 ...