[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 截取区间不能有半点差错.否则答案错误. 这两点 ...
随机推荐
- Java GC的原理
Java GC(garbage collec,垃圾收集,回收) GC是对JVM中的内存进行标记和回收,Sun公司的JDK用的虚拟机都是HotSpot 对象化的实例是放在heap堆内存中的,这里讲的分代 ...
- hdoj3038(带权并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:对于给定的a1..an,通过询问下标x..y,给出a[x]+...+a[y],但给出的值可 ...
- discrete
discrete - 必应词典 美[dɪ'skrit]英[dɪ'skriːt] adj.离散的:分离的:各别的 网络不连续的:分立的:离散型
- sqlserver2008debug存储过程
新建一个查询窗口,exec proc_name params…… 然后启动调试 右侧可以选择“逐过程”“逐语句”“跳出”,下方展示变量的值 参考文章:https://www.cnblogs.com/s ...
- 25-删除m位数是剩下的最大
/* 寻找最大数 题目内容: 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大,比如当n=92081346 ...
- cookie的基本用法
1.创建cookie对象 Cookie newCookie=new Cookie(String ky,String value); 2.写入cookie response.addCookie(newC ...
- Halcon的C#二次开发及经验分享
本文涉及面较广,因此很难在所有方面都讲解得很详细,故适合具有一定Halcon开发经验的人阅读. 1.Halcon二次开发的两种方式 ① 使用C#的语法方式逐句改写Halcon代码 优点:各种变量的类型 ...
- oracle导入导出dmp 解决exp-00011
解决办法,批量修改Segment.1先查找所有数据表为空的表 select table_name from user_tables where NUM_ROWS=0; 2若查不出数据,是因为 NUM_ ...
- AJAX 请求中多出了一次 OPTIONS 请求 导致 Laravel 中间件无法对 Header 传入的 Token 无法获取
背景知识: 我们会发现,在很多post,put,delete等请求之前,会有一次options请求.本文主要是来讨论一下这是什么原因引起的. 根本原因就是,W3C规范这样要求了!在跨域请求中,分为简单 ...
- 2、C++
2.2定义变量 2.2.1命名规则 赋予变量的名称叫做标识符,或者更方便地称之为变量名.变量名可用字母(包括大小写),数字,以及下划线,其他字符不允许.以下划线或者字母开头.在Visual C++20 ...