[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 截取区间不能有半点差错.否则答案错误. 这两点 ...
随机推荐
- PIE结对编程
学习进度条 点滴成就 学习时间 新编写代码行数 博客量 学到知识点 第一周 8 0 0 了解软件工程 第二周 7 0 1 了解软件工程 第三周 11 0 1 用例图 第四周 6 25 0 结对编程 第 ...
- ALTER数据库
alter table dbo.Sheet1$ alter column UserId int null
- OpenCV(图像处理)—访问像素的三种方法
方法一:用指针访问像素 #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include < ...
- [leetcode]174. Dungeon Game地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- ROS学习笔记二(创建ROS软件包)
catkin软件包的组成 一个软件包必须满足如下条件才能被称之为catkin软件包: 必须包含一个catkin编译文件package.xml(manifests文件),此文件包含了描述该软件包的重要信 ...
- OrgChart 组织架构与PHP结合使用
一.OrgChart下载地址: https://github.com/dabeng/OrgChart 二.组织架构的表设计 CREATE TABLE `org_info` ( `id` int(11) ...
- 搭建事务管理转账案例的环境(强调:简化开发,以后DAO可以继承JdbcDaoSupport类)
1. 步骤一:创建WEB工程,引入需要的jar包 * IOC的6个包 * AOP的4个包 * C3P0的1个包 * MySQL的驱动包 * JDBC目标2个包 * 整合JUnit测试包2.步骤二:创建 ...
- cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'.
笔者最近学习一些spring mvc,在复制别人代码的时候报这个错.报错来源web.xml,原因是不符合xsd对xml的约束 源文件 <?xml version="1.0" ...
- PAT 1073 多选题常见计分法(20)(代码+思路)
1073 多选题常见计分法(20 分) 批改多选题是比较麻烦的事情,有很多不同的计分方法.有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到 50% 分数:如果考生 ...
- Zookeeper 系列(四)ZKClient API
Zookeeper 系列(四)ZKClient API 环境准备: <dependency> <groupId>com.101tec</groupId> <a ...