Description

N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
 

Input

每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。  当N = 0,输入结束。
 

Output

每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
 

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
 
 #include<cmath>
#include<cstdio>
#define maxn 300000
class Node{
public:
int l,r;
__int64 add;//附加值
__int64 sum;
}node[maxn];
int getRight(int n){//获得满足2^x>=n的最小x[从0层开始,给编号获得层数]
return ceil(log10(n*1.0)/log10(2.0));
}
void build(int l,int r,int num){//输入区间[1,2^getRight(n)],num=1建树
if(l==r){
node[num].l=node[num].r=l;node[num].add=;node[num].sum=;
return;
}
node[num].l=l;node[num].r=r;node[num].add=;node[num].sum=;
build(l,(l+r)/,num*);
build((l+r)/+,r,num*+);
}
void add(int o,int l,int r,__int64 v){//从o节点开始递归[只要调用时o=1即可]在区间[l,r]全部加v
if(l<=node[o].l && r>=node[o].r){//全覆盖[递归边界]
node[o].add+=v;
}else{
int M=node[o].l+(node[o].r-node[o].l)/;
if(r<=M)add(o*,l,r,v);
else if(l>M)add(o*+,l,r,v);
else{
add(o*,l,M,v);
add(o*+,M+,r,v);
}
}
//维护节点o
if(node[o].l!=node[o].r){//如果区间只是一个元素就不算
node[o].sum=node[*o].sum+node[*o+].sum;
}else node[o].sum=;
node[o].sum+=node[o].add*(node[o].r-node[o].l+);
} //这里addadd是从上往下这条路的累计addadd值[一同回溯记录这条路节点所有add之和,减少了一次回溯累加add值]
//初始时直接令其为0
__int64 sum=;
void ask(int o,int l,int r,__int64 addadd){//从o节点开始递归[只要调用时o=1即可]在区间[l,r]的和
if(l<=node[o].l && r>=node[o].r){//全覆盖[递归边界]
sum+=(node[o].sum+addadd*(node[o].r-node[o].l+));
}else{
int M=node[o].l+(node[o].r-node[o].l)/;
if(r<=M)ask(o*,l,r,node[o].add+addadd);
else if(l>M)ask(o*+,l,r,node[o].add+addadd);
else{
ask(o*,l,M,node[o].add+addadd);
ask(o*+,M+,r,node[o].add+addadd);
}
}
}
int main(){
int N;
int i,j;
while(scanf("%d",&N) && N){
build(,<<getRight(N),);
for(int k=;k<N;k++){
scanf("%d%d",&i,&j);
add(,i,j,);
}
sum=;
ask(,,,);
printf("%lld",sum);
for(i=;i<=N;i++){
sum=;
ask(,i,i,);
printf(" %lld",sum);
}
printf("\n");
}return ;
}

[ACM_数据结构] Color the ball [线段树水题][数组开大]的更多相关文章

  1. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  2. POJ 3468 A Simple Problem with Integers(线段树水题)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 135904 ...

  3. hdu 1754 I Hate It(线段树水题)

    >>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...

  4. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  5. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  6. hdu 1556 Color the ball (线段树+代码详解)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. ZOJ 2301 Color the Ball 线段树(区间更新+离散化)

    Color the Ball Time Limit: 2 Seconds      Memory Limit: 65536 KB There are infinite balls in a line ...

  8. hdu 1556 Color the ball 线段树

    题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...

  9. HDU 1556 Color the Ball 线段树 题解

    本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...

随机推荐

  1. 让IE浏览器支持CSS3表现

    http://www.zhangxinxu.com/wordpress/2010/04/%e8%ae%a9ie6ie7ie8%e6%b5%8f%e8%a7%88%e5%99%a8%e6%94%af%e ...

  2. Win32 Debug & Release

    今天帮汤老师调试程序,他生成的程序不能运行,怀疑子程序之间编译顺序的问题:我试了之后,也出现同样的问题,但是把Win32 Debug 换成Win32 Release却可以运行了. 网上搜索了下,在CV ...

  3. pre换行段落间距

    <!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"& ...

  4. vprintf 和 vsnpintf 的用法

    函数定义: int vprintf ( const char * format, va_list arg ); printf() and friends are for normal use. vpr ...

  5. [leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  6. Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例

    1.重写equals方法实例   部分代码参考http://blog.csdn.net/wangloveall/article/details/7899948 重写equals方法的目的是判断两个对象 ...

  7. Spring框架的Bean管理的配置文件方式

    1. id属性和name属性的区别 * id -- Bean起个名字,在约束中采用ID的约束,唯一 * 取值要求:必须以字母开始,可以使用字母.数字.连字符.下划线.句话.冒号 id:不能出现特殊字符 ...

  8. Js下载文件到本地(兼容多浏览器)

    在客户端通过js下载文件,试过几种下载方式,iframe方式仅限于IE浏览器,window.open(url),location.href=url 这两种方式在chrome浏览器还会是直接打开文件而不 ...

  9. Laravel5.5 使用队列 Queue

    使用队列# 上一章节中我们开发了自动生成 Slug 功能,但是因为我们的需要实时请求百度翻译接口,这将会是一个系统性能隐患. 一般情况下,网络请求会存在各种不确定性,如果请求 API 出现超时情况,或 ...

  10. db2学习笔记

    a.服务端安装 v11.1_win64_expc.zip 官网下载 b.客户端安装 Toad for DB2 Freeware 6.1 百度找找 .建数据库 create database HRA_G ...