线段树--Color the ball(多次染色问题)
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Description
Input
当N = 0,输入结束。
Output
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
中文题你懂的;
一看到这道题我就想做线段树,好快写出来了,可是尼玛各种bug啊卧槽,后来搞了好久才解决 待会说下我的问题;唉还是太弱了;
struct Tree{
int type;
int po;
}tree[maxn<<2];
po用来记录该节点是否被走过,type用来记录当前节点存储了多少颜色这里采用懒惰标记,当然 如果要往子节点继续找的话,懒惰标记下移,po=1表示被走过;
具体看代码;
在正常的update之后;还要把节点信息继续下移;下移的条件是,该节点被走过,也就是说子节点或更下面的节点有值,要更新到没被走过的那个节点;
具体看 newupdate函数;
void newupdate(int L,int R,int l,int r,int rt){
if(tree[rt].po==false)return;
if(tree[rt].type>0){
PushDown(rt);
}
int m = (l + r) >> 1; // 而如果你是要更新[1,1](必定访问到标记2) 那么先进行标记2 让两儿子的value更新
if (L <= m) newupdate(L , R , lson); //记得这时候儿子type被赋值,自己type=0;
if (R > m) newupdate(L , R , rson);
}
最后用一个query函数来输出,输出的条件是当前节点的type>0 因为我们之前已经往下更新了;
最后记住输出的问题,我这里定义了一个全局变量first;初始化0只要准备输出之前让它++;只有first==1的时候输出type,不然前面要多输出一个空格
具体看代码
不过我这个程序居然跑了1400ms 感觉不够快,希望大牛们能够给予指点
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL long long
const int maxn = 111111;
int first;
struct Tree{ int type;
int po;
}tree[maxn<<2]; void PushDown(int rt) {
if (tree[rt].type) {
tree[rt<<1].type += tree[rt].type;
tree[rt<<1|1].type += tree[rt].type ;
tree[rt].type = 0;
}
tree[rt].po=1;
}
void build(int l,int r,int rt) {
tree[rt].po=0;
tree[rt].type=0;
if (l == r){
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
}
void update(int L,int R,int c,int l,int r,int rt) {//(L,R)是我们要找的范围
if (L <= l && r <= R) {//标记1 //更新到这就行,不一定更新到最底端
tree[rt].type += c;
// tree[rt].value += c;
return ;
}
PushDown(rt );//延迟标记,这时候后推, (标记2)
int m = (l + r) >> 1;
if (L <= m) update(L , R , c , lson);
if (R > m) update(L , R , c , rson);
}
void newupdate(int L,int R,int l,int r,int rt){ if(tree[rt].po==false)return;
if(tree[rt].type>0){
PushDown(rt);
}
int m = (l + r) >> 1; // 而如果你是要更新[1,1](必定访问到标记2) 那么先进行标记2 让两儿子的value更新
if (L <= m) newupdate(L , R , lson); //记得这时候儿子type被赋值,自己type=0;
if (R > m) newupdate(L , R , rson); }
void query(int L,int R,int l,int r,int rt) {
if(tree[rt].type>0){
for(int i=l;i<=r;i++){
first++;
if(first==1)cout<<tree[rt].type; //输出的处理
else
cout<<" "<<tree[rt].type; //输出的处理
}
}
else if(l==r&&tree[rt].type==0){
first++; //输出的处理
if(l==1)cout<<"0"; //输出的处理
else //输出的处理
cout<<" 0";} //输出的处理
else{
int m = (l + r) >> 1;
if (L <= m) query(L , R , lson);
if (R > m) query(L , R , rson);
}
}
int main() {
int n;int a,b;
while(cin>>n){
if(n==0)break;
first=0;
build( 1, n, 1);
for(int i=1;i<=n;i++){
cin>>a>>b;
update(a,b,1,1,n,1); }
newupdate(1,n,1,n,1); query(1,n,1,n,1);
cout<<endl; }
return 0;
}
/*
*
6
1 2
3 4
1 4
2 4
3 5
2 2
2 4 4 4 1 0
*/
版权声明:本文为博主原创文章,未经博主允许不得转载。
线段树--Color the ball(多次染色问题)的更多相关文章
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
- hdoj 1556 Color the ball【线段树区间更新】
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1199 Color the Ball(离散化线段树)
Color the Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- POJ训练计划2777_Count Color(线段树/成段更新/区间染色)
解题报告 题意: 对线段染色.询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话訪问线段树,求出颜色种数.结果超时了,最坏的情况下,染色能够染到叶子节点. 换成存下区 ...
- Color the ball(树状数组+线段树+二分)
Color the ball Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- Color the ball(hdu1556)(hash)或(线段树,区间更新)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ-2777 Count Color(线段树,区间染色问题)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40510 Accepted: 12215 Descrip ...
随机推荐
- 将数组之中的省份市区地区ID改成对用中文字符
数据表数据源的省市区联动: 原始数据: //获取所有学校信息 $school=D('school'); $info=$school->getList(); 数据如下: 1 => array ...
- Python pass 语句使用示例
Python pass 语句的使用方法示例.Python pass是空语句,pass语句什么也不做,一般作为占位符或者创建占位程序,是为了保持程序结构的完整性,pass语句不会执行任何操作,比如: P ...
- php中intval()函数
格式:int intval(mixed $var [, int $base]); 1.intval()的返回值是整型,1或者0.可作用于数组或者对象(对象报错信息:Notice: Object of ...
- Internet Explorer 无法启用 JavaScript 怎么办?
在 Internet Expllorer 8/9 中,有些同学在浏览网页时,收到提示:“需要启用 JavaScript …”,并且会发现网页上某些功能不能用了,比如点击网页里的按钮没反应等等. 怎么启 ...
- android使用library工程问题
在windows系统下,library project必须和project处于相同的盘符中,因为如果在不同盘符,project.properties中的android.library.referenc ...
- DevExpress 重编译 替换强命名 修改源码
本文以DevExpress 11.1.8举例 必须满足几个条件 1. 必须有DXperience相应版本的全部源代码SourceCode.把全部源代码复制到X:\Program Files\DevEx ...
- jquery的prop()和attr()
jQuery1.6以后prop()和attr()的应用场景如下: 第一原则:只添加属性名称该属性就会立即生效应该使用prop(); 第二原则:只存在true/false的属性应该使用prop(); 设 ...
- C# 自定义集合
自定义类型 public class Product { public int Id { get; set; } // 自增ID public string Name { get; set; } // ...
- oracle 表空间和表 read only迁移后不再read only
DB : 11.2.0.3.0 1.将tablespace read only , 不允许再对表进行update.insert操作,测试dmp到另一个用户.表空间后是否可以update.insert ...
- WCF 已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性
我出现这个问题主要是服务器返回数据量过大引起了,需要客户端服务端都要进行配置:我会说其实有神器的么....(工具=>wcf服务配置编辑器),用工具编辑下,就会完全搞定这个问题,再也不用纠结了 服 ...