#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的更多相关文章

  1. Codeforces #447 Div2 E

    #447 Div2 E 题意 给出一个由有向边构成的图,每条边上有蘑菇,假设有 \(n\) 个蘑菇,那么第一次走过这条边可以获得 \(n\) 个蘑菇,第二次 \(n-1\),第三次 \(n-1-2\) ...

  2. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  3. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  4. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  5. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  6. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  7. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  8. codeforces 447 A-E div2 补题

    A DZY Loves Hash 水题 #include<iostream> #include<cstdio> #include<cstdlib> #include ...

  9. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

随机推荐

  1. GDI绘图中的映射模式CDC::SetMapMode()

    原文链接:http://blog.csdn.net/charlessimonyi/article/details/8264572 在GDI绘图前,一般要设置映射模式.映射模式是什么呢?它是逻辑长度单位 ...

  2. AtCoder Grand Contest 028 B - Removing Blocks 解题报告

    B - Removing Blocks Time limit : 2sec / Memory limit : 1024MB Score : 600 points ## Problem Statemen ...

  3. THUSC2014酱油记

    Day0: 坐飞机到北京,然后报到...跟jason_yu分到一个房间,刚好可以蹭点RP.发现房间460RMB/晚,但再带一份早餐就500RMB,难道早餐是40RMB么...在一家川菜馆吃的午晚餐,感 ...

  4. Nginx的火速蔓延与其并发性处理优势

    Nginx是俄罗斯人编写的十分轻量级的HTTP服务器.Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.Ngi ...

  5. libusb 示例

    #include <usb.h> #include <stdio.h> #define VERSION "0.1.0" #define VENDOR_ID ...

  6. 浏览器 连不上网 (3):DNS 服务器问题

    解决:设置一下DNS服务器的地址 步骤: 打开网络和共享中心(网络和 Internet设置)-> 更改适配器 -> 双击我们连接的 无线网(WiFi式) 或 以太网(网线式): 从出现的窗 ...

  7. svn“Previous operation has not finished; run 'cleanup' if it was interrupted“ 或者不能cleanup,或者提示空目录 报错的解决方法

    参考了文档: http://blog.csdn.net/superch0054/article/details/38668017 今天碰到了个郁闷的问题,svn执行clean up命令时报错“Prev ...

  8. saltstack入门至放弃之salt安装部署

    学习了一段时间的saltstack,是时候记录下了.友提:学习环境是两台centos_7.2_x64机器 系统初始化: 两台机器执行以下脚本即可(友提:两台服务器的主机名配置在/etc/hosts中, ...

  9. kdtree学习记录

    [转载请注明来自 Galaxies的博客:http://cnblogs.com/galaxies] 这篇文章当做一个记录啦qwq 参考:<K-D Tree在信息学竞赛中的应用>(n+e, ...

  10. UVALIVE 3307 Adventurous Driving

    一开始重新建图搞的.然后参照了别人的博客.这个解法比较好 利用在SPFA维护入队次数.入队次数大于节点数目的位于负环. 那么负环中的点如何DFS到终点.(SPFA从起点开始如果能找到入队大于N那么一定 ...