POJ2777 Count Color 线段树区间更新
题目描写叙述:
长度为L个单位的画板,有T种不同的颜料。现要求按序做O个操作,操作分两种:
1.“C A B C”,即将A到B之间的区域涂上颜色C
2.“P A B”。查询[A,B]区域内出现的颜色种类
出现操作2时。请输出答案
PS:初始状态下画板颜色为1
一開始没有想那么好,用int整型位移来取代颜色。还是使用了最传统的bool color[来记录。但是不知道错在了哪里。
#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#include<cctype> #define ll long long #define LL __int64 #define eps 1e-8 #define inf 0xfffffff //const LL INF = 1LL<<61; using namespace std; //vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p; const int N = 100000 + 5; typedef struct Node {
int color;
int l,r;
}; Node tree[N * 4];
bool vis[50]; void init() {
memset(vis,false,sizeof(vis));
} void build(int l,int r,int id) {
tree[id].color = 1;
tree[id].l = l;
tree[id].r = r;
if(tree[id].l == tree[id].r)return;
int mid = (l + r)/2;
build(l,mid,id<<1);
build(mid + 1,r,id<<1|1);
} void update(int l,int r,int id,int color) {
if(tree[id].l >= l && tree[id].r <= r) {
tree[id].color = color;return;
}
int mid = (tree[id].l + tree[id].r)/2;
tree[id].color = -1;
if(r <= mid) update(l,r,id<<1,color);
else {
if(l > mid)update(l,r,id<<1|1,color);
else {
update(l,mid,id<<1,color);
update(mid+1,r,id<<1|1,color);
}
}
} void update2(int l,int r,int id) {
if(tree[id].color > 0) {
vis[tree[id].color] = true;return;
}
if(tree[id].l == tree[id].r)return;
int mid = (tree[id].l + tree[id].r)/2;
if(r <= mid) update2(l,r,id<<1);
else {
if(l > mid)update2(l,r,id<<1|1);
else {
update2(l,mid,id<<1);
update2(mid+1,r,id<<1|1);
}
}
} int find(int x) {
int ans = 0;
for(int i=1;i<=x;i++)
if(vis[i])
ans++;
return ans;
} int main() {
int n,m,q;
while(scanf("%d %d %d",&n,&m,&q) == 3 ){
memset(tree,0,sizeof(tree));
init();
build(1,n,1);
while(q--) {
char s[2];
scanf("%s",s);
if(s[0] == 'C') {
int x,y,c;
scanf("%d %d %d",&x,&y,&c);
if(x > y)swap(x,y);
update(x,y,1,c);
}
else {
int x,y;
scanf("%d %d",&x,&y);
init();
if(x > y)swap(x,y);
update2(x,y,1);
printf("%d\n",find(m));
} }
}
return 0;
}
后来看了别人的一下,看到方法不一样,跑了案例也没发现自己的错误,继续检查还是不行。难道真的是方法不行?换了个方法过了。可是上面的代码错误原因还是没有查出来,WA哭
#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#include<cctype> #define ll long long #define LL __int64 #define eps 1e-8 #define inf 0xfffffff //const LL INF = 1LL<<61; using namespace std; //vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p; const int N = 100000 + 5; typedef struct Node {
int l,r;
int color;
int flag;
}; Node tree[N * 4]; void init() {
memset(tree,0,sizeof(tree));
} void cal(int id) {
tree[id].color = tree[id<<1].color | tree[id<<1|1].color;
} void build(int l,int r,int id) {
tree[id].l = l;
tree[id].r = r;
tree[id].color = 1;
tree[id].flag = 1;
if(tree[id].l == tree[id].r) return;
int mid = (l + r)/2;
build(l,mid,id<<1);
build(mid+1,r,id<<1|1);
} void cover(int id) {
tree[id<<1].color = tree[id].color;
tree[id<<1].flag = 1;
tree[id<<1|1].color = tree[id].color;
tree[id<<1|1].flag = 1;
tree[id].flag = 0;
} void updata(int l,int r,int id,int col) {
if(l <= tree[id].l && r >= tree[id].r) {
tree[id].color = col;
tree[id].flag = 1;
return;
}
if(tree[id].color == col)return;
if(tree[id].flag)cover(id);
int mid = (tree[id].l + tree[id].r)/2;
if(r <= mid) updata(l,r,id<<1,col);
else if(l > mid)updata(l,r,id<<1|1,col);
else {
updata(l,mid,id<<1,col);
updata(mid+1,r,id<<1|1,col);
}
cal(id);
} int ans; void query(int l,int r,int id) {
if(l <= tree[id].l && r >= tree[id].r) {
ans |= tree[id].color;return;
}
if(tree[id].flag) {
ans |= tree[id].color;return;
}
int mid = (tree[id].l + tree[id].r)/2;
if(r <= mid) query(l,r,id<<1);
else if(l > mid) query(l,r,id<<1|1);
else {
query(l,mid,id<<1);
query(mid+1,r,id<<1|1);
}
} int main() {
int n,m,q;
while(scanf("%d %d %d",&n,&m,&q) == 3) {
init();
build(1,n,1);
char s[2];
while(q--) {
scanf("%s",s);
if(s[0] == 'C') {
int x,y,c;
scanf("%d %d %d",&x,&y,&c);
if(x > y)swap(x,y);
updata(x,y,1,1<<(c-1));//把颜色改成用正向委员算表示
}
else {
int x,y;
scanf("%d %d",&x,&y);
if(x > y)swap(x,y);
ans = 0;
query(x,y,1);
int cnt = 0;
while(ans) {
if(ans%2)cnt++;
ans /= 2;
}
printf("%d\n",cnt);
}
}
}
return 0;
}
POJ2777 Count Color 线段树区间更新的更多相关文章
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- [poj2777] Count Color (线段树 + 位运算) (水题)
发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- hdu1556Color the ball线段树区间更新
题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...
随机推荐
- 《Typecript 入门教程》 3、接口
转载:<TypeScript 中文入门教程> 3.接口 介绍 TypeScript的核心原则之一是对值所具有的shape进行类型检查. 它有时被称做“鸭式辨型法”或“结构性子类型化”. 在 ...
- 胖ap和瘦ap的区别
一,什么是AP,胖瘦AP如何区分? 先说说AP的概念.AP是Access Point的简称,即无线接入点,其作用是把局域网里通过双绞线传输的有线信号(即电信号)经过编译,转换成无线电信号传 ...
- MySQL的or/in/union与索引优化 | 架构师之路
假设订单业务表结构为: order(oid, date, uid, status, money, time, …) 其中: oid,订单ID,主键 date,下单日期,有普通索引,管理后台经常按照da ...
- HashMap的尾部遍历问题--Tail Traversing
在看网上HashMap的resize()设计时,提到尾部遍历. JDK1.7的HashMap在实现resize()时,新table[]的列表采用LIFO方式,即队头插入.这样做的目的是:避免尾部遍 ...
- [转帖]c++ 面试整理
1. 继承方式 public 父类的访问级别不变 protected 父类的public成员在派生类编程protected,其余的不变 private 父类的所有成员变成pr ...
- Hive扩展功能(三)--使用UDF函数将Hive中的数据插入MySQL中
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
- 在VirtualBox上安装Solaris 10全教程(包括下载)
您可以在博文的最下方留下评价, 也可以点击左边的 关注 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐 来支持一下喔 如果您对博文有任何疑问, 可以通过评论或发邮件的 ...
- jQuery——类的添加与删除
添加类:addClass 删除类:removeClass 判断类是否存在:hasClass <!DOCTYPE html> <html lang="en"> ...
- VC++ 遍历文件夹
}; strcpy_s(szFind, MAX_PATH, m_szDir); strcat_s(szFind, "\\*.*"); WIN32_FIND_DATA wfd; HA ...
- Java编译器、JVM、解释器
Java虚拟机(JVM)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该系统上运行.本文首先简要介绍从Java文件的编译 ...