题意:有一个长板子,分成多段,有两种操作,第一种是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. Spring编程式和声明式事务实例讲解

    Java面试通关手册(Java学习指南):https://github.com/Snailclimb/Java_Guide 历史回顾: 可能是最漂亮的Spring事务管理详解 Spring事务管理 S ...

  2. 公司内网yum源

    新增yum源配置文件 vi /etc/yum.repos.d/szyum.repo 内容如下: #[redhat6.3] [base] name=redhat63 baseurl=http://10. ...

  3.  Meltdown论文翻译【转】

    转自:http://www.wowotech.net/basic_subject/meltdown.html#6596 摘要(Abstract) The security of computer sy ...

  4. 内核工具 – Sparse 简介【转】

    转自:http://www.cnblogs.com/wang_yb/p/3575039.html Sparse是内核代码静态分析工具, 能够帮助我们找出代码中的隐患. 主要内容: Sparse 介绍 ...

  5. STM8CubeMx来了

    几年前出来的STM32CubeMx是众多stm32开发者的福音,大大缩短了开发者的开发周期.就在前几天,st官网宣布针对stm8的图形配置工具stm8cube横空出世. 如果你还不知道STM32Cub ...

  6. opencv之dft及mat类型转换

    跑实验时用到dft这个函数,根据教程,需要先将其扩充到最优尺寸,但我用逆变换后发现得到的mat的维数竟然不一样.因此还是不要扩展尺寸了. 参考:http://www.xpc-yx.com/2014/1 ...

  7. P2448 无尽的生命

    Description 小 a有一个长度无限长的序列 p = (1, 2, 3, 4 --),初始时 pi = i 给出 m 个操作,每次交换两个位置的数 询问最后序列逆序对的个数 Solution ...

  8. Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not foun

    一段时间没亲自建新项目玩乐,今天建立了一Maven project的时候发现了以下异常,Description Resource Path Location Type The superclass & ...

  9. three.js是什么,能干嘛,和webgl什么关系

    如今浏览器的功能越来越强大,而且这些功能可能通过JavaScript直接调用.你可以用HTML5标签轻松地添加音频和视频,而且可以在HTML5画布上创建各种交互组件.现在这个功能集合里又有了一个新成员 ...

  10. 获取矩形局域的方法,Rect、Bounds、Point

    获取一个点和矩形区域的方法如下: var R: TRect; procedure TForm5.FormCreate(Sender: TObject); begin RadioGroup1.Items ...