POJ 2777 Count Color
2015-08-23 00:29
253 查看
题目链接:http://poj.org/problem?id=2777
题意:给出一面长度为len的墙,一开始的颜色是1,进行2种操作,一是将[l,r]涂成颜色p,二是询问[l,r]区间一共有多少种颜色
思路:对区间进行修改和询问,很容易想到用线段树,但是如何保存有多少种颜色呢,因为颜色少于30种(我又看了题解),所以可以用位运算进行状态压缩,剩下的就是线段树普通的区间修改和更新了(写题的时候花式出错,晚上太亢奋了吧……)
题意:给出一面长度为len的墙,一开始的颜色是1,进行2种操作,一是将[l,r]涂成颜色p,二是询问[l,r]区间一共有多少种颜色
思路:对区间进行修改和询问,很容易想到用线段树,但是如何保存有多少种颜色呢,因为颜色少于30种(我又看了题解),所以可以用位运算进行状态压缩,剩下的就是线段树普通的区间修改和更新了(写题的时候花式出错,晚上太亢奋了吧……)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define maxn 100030 using namespace std; struct Tree { int l,r,date; }tree[maxn*3]; int lazy[maxn*3]; void build(int root,int l,int r) { tree[root].l=l; tree[root].r=r; if (l==r) { tree[root].date=(1<<1); return; } int mid=(l+r)>>1; build (root<<1,l,mid); build (root<<1|1,mid+1,r); tree[root].date=tree[root<<1].date|tree[root<<1|1].date; } void update(int root,int l,int r,int val) { if (tree[root].l>=l && tree[root].r<=r) { tree[root].date=(1<<val); lazy[root]=(1<<val); return; } if (lazy[root]!=0) { tree[root<<1].date=tree[root].date; tree[root<<1|1].date=tree[root].date; lazy[root<<1]=lazy[root]; lazy[root<<1|1]=lazy[root]; lazy[root]=0; } int mid=(tree[root].l+tree[root].r)>>1; if (l<=mid) update(root<<1,l,r,val); if (r>mid) update(root<<1|1,l,r,val); tree[root].date=tree[root<<1].date|tree[root<<1|1].date; } int que(int root,int l,int r) { if (lazy[root]!=0) return lazy[root]; if (tree[root].l==l && tree[root].r==r) return tree[root].date; int mid=(tree[root].l+tree[root].r)>>1; if (r<=mid) return que(root<<1,l,r); else if (l>mid) return que(root<<1|1,l,r); else return que(root<<1,l,mid)|que(root<<1|1,mid+1,r); } int main() { int len,n,m; while (scanf("%d%d%d",&len,&n,&m)!=EOF) { memset(lazy,0,sizeof(lazy)); build (1,1,len); for (int i=0;i<m;i++) { char tem; cin>>tem; if (tem=='C') { int a,b,p; scanf("%d%d%d",&a,&b,&p); if (a>b) swap(a,b); update(1,a,b,p); } else { int a,b,res=0,cl; scanf("%d%d",&a,&b); if (a>b) swap(a,b); cl=que(1,a,b); // cout<<":"<<cl<<endl; for (int i=1;i<=n;i++) { if ((cl&(1<<i))!=0) res++; } printf("%d\n",res); } } } }
相关文章推荐
- 基于TCP的STM32 IAP bootloader初步设计
- 第四章 栈与队列
- note.js之 Nodejs+Express4在windows下的配置
- 一个月备考,一次通过信息系统项目管理师(高项)
- Linux常用基础命令 date cal ls cd pwd tty which shutdown who
- 第三章 线性表
- Linux命令练习
- Building a Space Station POJ 2031 【最小生成树 prim】
- openssl自签名证书生成与单双向验证
- 第二章 算法
- jquery easyui dialog 在iframe 标记的使用。可在dialog 里打开一个页
- 20150820-Y1506401-19+benz2015+常用命令
- 《Docker源码分析》作者:孙宏亮
- MySQL 的数据去哪了
- 毕业了
- 精妙的单片机非阻塞延时程序设计
- 第一章 数据结构绪论
- myFFDrop - 带有自动响度校正的小型视频批量转码工具
- TSP问题
- 母函数法解决整数划分