[ACM_数据结构] Color the ball [线段树水题][数组开大]
Description
Input
Output
Sample Input
Sample Output
#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 [线段树水题][数组开大]的更多相关文章
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- POJ 3468 A Simple Problem with Integers(线段树水题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 135904 ...
- hdu 1754 I Hate It(线段树水题)
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- 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) T ...
- ZOJ 2301 Color the Ball 线段树(区间更新+离散化)
Color the Ball Time Limit: 2 Seconds Memory Limit: 65536 KB There are infinite balls in a line ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
- HDU 1556 Color the Ball 线段树 题解
本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...
随机推荐
- MySql初步II
[MySql初步II] 1.Order By 你可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列. 默认情况下,它是按升排列. 实例: 2.Join语法 Join不是一个关键字 ...
- np.eye()
今天在完成深度学习的相关编程作业的时候,发现代码中出现了一个关于np.eye()的函数,这个函数的用法非常的简单,但是在预制的代码中,这个函数的用法并非单单制造一个对角矩阵,而是通过其来将一个labe ...
- Find Peak Element(ARRAY - Devide-and-Conquer)
QUESTION A peak element is an element that is greater than its neighbors. Given an input array where ...
- 两台Linux之间传文件
安装sudo apt-get install openssh-client openssh-server 使用scp命令: scp john@~/hallo.h /usr/include 将左边移动到 ...
- 条款2:尽量以const, enum, inline替换#define
原因: 1. 追踪困难,由于在编译期已经替换,在记号表中没有. 2. 由于编译期多处替换,可能导致目标代码体积稍大. 3. define没有作用域,如在类中定义一个常量不行. 做法: 可以用const ...
- NPOI创建doc
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- SqlServer中批量update
现在我有两张表分别是S_PERSON,S_USER S_PERSON S_USER 我现在想把S_USER表中的ACCOUNT批量修改成S_PERSON的ACCOUNT 我们可以发现S_USER表中有 ...
- BZOJ 1211[HNOI2004]树的计数 - prufer数列
描述 一个有n个结点的树,设它的结点分别为v1, v2, …, vn,已知第i个结点vi的度数为di,问满足这样的条件的不同的树有多少棵.给定n,d1, d2, …, dn,编程需要输出满足d(vi) ...
- Mina 系列(二)之基础
Mina 系列(二)之基础 Mina 使用起来多么简洁方便呀,就是不具备 Java NIO 的基础,只要了解 Mina 常用的 API,就可以灵活使用并完成应用开发. 1. Mina 概述 首先,看 ...
- msys2 git status显示中文文件名问题
git config [--global] core.quotepath off https://stackoverflow.com/questions/5854967/git-msysgit-acc ...