#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. BZOJ1305 [CQOI2009]dance跳舞 【网络流】

    1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 3714  Solved: 1572 [Submit][ ...

  2. 洛谷P1522 牛的旅行 Cow Tours

    ---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...

  3. 使用e.target.dataset的问题

    在微信开发中我们经常会用到标签中属性的属性值,有时候我们通过 data-* 和 e.target.dateset 来获取属性值会出现一点小bug,即是调用出来的数据是undefined. 1)方案1– ...

  4. 【数据结构】bzoj1455罗马游戏

    Description 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最近举行了一次平面几何测试,每个人都得到了一个分数. 皇帝很喜欢平面几何,他对那些得分很低的人嗤之以鼻 ...

  5. 【uva11855-求长度为1到n的相同子串出现的次数】sam

    题意:求长度为1到n的相同子串出现的次数,输到小于2为止. 题解: 用sam做. 建机,算right集合,然后用r[i]更新长度为step[i]的子串出现次数,然后ans[i]=maxx(ans[i] ...

  6. mysql七:数据备份、pymysql模块

    阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...

  7. 进一步认识golang中的并发

    如果你成天与编程为伍,那么并发这个名词对你而言一定特别耳熟.需要并发的场景太多了,例如一个聊天程序,如果你想让这个聊天程序能够同时接收信息和发送信息,就一定会用到并发,无论那是什么样的并发. 并发的意 ...

  8. Codeforces 219D Choosing Capital for Treeland 2次DP

    //选择一个根使得变换最少边的方向使得能够到达所有点#include <map> #include <set> #include <list> #include & ...

  9. PL/SQL 10 管理用户子程序

    --查看存储过程源代码IKKI@ test10g> select text from user_source where name='ADD_DEPT'; TEXT--------------- ...

  10. python模块之copy

    提供浅拷贝和深拷贝两种模式. =>copy(x):返回x的浅拷贝 =>deepcopy(x):返回x的深拷贝 浅拷贝和深拷贝: 浅拷贝复制不变对象,引用可变对象(如列表和字典): 深拷贝复 ...