Codeforces #447 Div2 D
#447 Div2 D
题意
给一棵完全二叉树,每条边有权值为两点间的距离,每次询问 \(x, h\) ,从结点 \(x\) 出发到某一结点的最短路的距离 \(d\) 如果小于 \(h\) ,则答案加上 \(h - d\) ,考虑所有结点并输出答案。
分析
通过建树过程可以发现这是一棵完全二叉树,也就是说树很矮。
可以预处理这棵树,对于每一个结点,我们可以计算出以这个结点为根结点的子树中的所有结点到当前子树的根结点的距离,从根结点向下 DFS 即可,然后自下而上合并,类似归并排序合并的过程。再预处理下前缀和,这样就很容易求得子树中有多少结点到根结点的距离小于 \(h\) ,向上走 \(log(n)\) 次一定可以到根结点。
完全二叉树 很矮!祖先结点很少!很多情况都可以遍历(暴力)祖先结点!
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int f, s, mx;
int n, m, a[N], l[N], r[N];
vector<int> G[N];
vector<ll> S[N];
void mergeUp(int rt, int cl, int cr) {
int i = 0, j = 0;
while(i < cl || j < cr) {
if(i == cl) G[rt].push_back(r[j++]);
else if(j == cr) G[rt].push_back(l[i++]);
else {
if(l[i] < r[j]) G[rt].push_back(l[i++]);
else G[rt].push_back(r[j++]);
}
}
ll sum = 0;
for(int i = 0; i < G[rt].size(); i++) {
sum += G[rt][i];
S[rt].push_back(sum);
}
}
void build(int rt) {
if(rt >= s - mx + 1) return;
build(rt * 2);
build(rt * 2 + 1);
int cl = 0, cr = 0;
if(rt * 2 <= n) for(int i = 0; i < G[rt * 2].size(); i++) {
l[cl++] = G[rt * 2][i] + a[rt * 2];
}
if(rt * 2 + 1 <= n) for(int i = 0; i < G[rt * 2 + 1].size(); i++) {
r[cr++] = G[rt * 2 + 1][i] + a[rt * 2 + 1];
}
if(cl + cr > 0) mergeUp(rt, cl, cr);
}
int main() {
scanf("%d%d", &n, &m);
s = 1, mx = 1, f = 0;
while(s < n) {
mx *= 2;
s += mx;
f++;
}
for(int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
a[i + 1] = x;
G[i].push_back(0);
}
G[n].push_back(0);
build(1);
while(m--) {
int now, h, pre = -1;
scanf("%d%d", &now, &h);
ll ans = h;
while(now) {
if(now != 1 && a[now] < h) ans += h - a[now];
if(pre == -1) {
int pos = lower_bound(G[now].begin(), G[now].end(), h) - G[now].begin() - 1;
if(pos > 0) ans += 1LL * pos * h - S[now][pos];
} else {
if(now * 2 == pre && now * 2 + 1 <= n) {
int nxt = now * 2 + 1;
int pos = lower_bound(G[nxt].begin(), G[nxt].end(), h - a[nxt]) - G[nxt].begin() - 1;
if(pos > 0) ans += 1LL * pos * (h - a[nxt]) - S[nxt][pos];
if(a[nxt] < h) ans += h - a[nxt];
} else if(now * 2 + 1 == pre && now * 2 <= n) {
int nxt = now * 2;
int pos = lower_bound(G[nxt].begin(), G[nxt].end(), h - a[nxt]) - G[nxt].begin() - 1;
if(pos > 0) ans += 1LL * pos * (h - a[nxt]) - S[nxt][pos];
if(a[nxt] < h) ans += h - a[nxt];
}
}
h -= a[now];
pre = now;
now /= 2;
}
cout << ans << endl;
}
return 0;
}
Codeforces #447 Div2 D的更多相关文章
- Codeforces #447 Div2 E
#447 Div2 E 题意 给出一个由有向边构成的图,每条边上有蘑菇,假设有 \(n\) 个蘑菇,那么第一次走过这条边可以获得 \(n\) 个蘑菇,第二次 \(n-1\),第三次 \(n-1-2\) ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- codeforces 447 A-E div2 补题
A DZY Loves Hash 水题 #include<iostream> #include<cstdio> #include<cstdlib> #include ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
随机推荐
- POJ2155 Matrix 【二维线段树】
题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...
- bzoj进度条
好久没发进度了 这个月没有上个月那么猛,肯能使因为这个月不想水题吧 No. 510 Solved Problems List Solved 368 10001001100210071008101210 ...
- 树剖模板by fcdalao
#include<bits/stdc++.h> using namespace std; ; *MX]; *MX]; int n,Index,fir[MX],fa[MX],dfn[MX], ...
- JS Cookie相关操作
function setCookie(cookieName, cookieValue, expires) { // 设置Cookie function getCookieName(cookieName ...
- bzoj 5094 [Lydsy1711月赛]硬盘检测 概率dp
[Lydsy1711月赛]硬盘检测 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 273 Solved: 75[Submit][Status][Dis ...
- ES6学习笔记(五)—— 编程风格
1. 块级作用域 let 取代 var —— let 只在声明的代码块内有效,而且不存在变量提升的效用 const 取代 let —— const 比较符合函数式编程的思想,运算不改变值,只是新建值: ...
- 3中转换JSON数据的方式
一:前言 来公司一个星期,把最近做的东西梳理下,并把觉得有必要的知识点记载下,现在传数据很多都是用JSON来传数据,所以我就找了集中传json的方式,其实是有五种的,但是有一个我没有用过,太陌生了,上 ...
- TCP(二)
TCP半连接和全连接问题 TCP握手过程详解 如上图所示,关键部分:syns queue(半连接队列)和accept queue(全连接队列) 正常情况下的处理过程如下: 1)当server端收到cl ...
- bzoj 1045糖果传递 数学贪心
首先我们假设平均数为ave 那么对于第1个人,我们假设他给第N个人K个糖果,第2个人给1,第3个人给2,第n个人给第n-1个人 那么对于第1个人给完n,第2个人给完1,第一个人不会再改变糖果数了,所以 ...
- python3 正则表达式re模块
正则表达式的功能:字符串的模糊匹配查询import re元字符 . ---->匹配除换行符意外的任意字符 ^ ---->匹配行首位置 $ ---->匹配行尾位置 关于重复的元字符 * ...