codeforces Round 447 div2 D Ralph And His Tour in Binary Country
来源:互联网 发布:java破解验证码 编辑:程序博客网 时间:2024/06/07 02:45
Ralph is in the Binary Country. The Binary Country consists of n cities and (n - 1) bidirectional roads connecting the cities. The roads are numbered from1 to (n - 1), thei-th road connects the city labeled (here⌊ x⌋ denotes the x rounded down to the nearest integer) and the city labeled(i + 1), and the length of the i-th road is Li.
Now Ralph gives you m queries. In each query he tells you some cityAi and an integerHi. He wants to make some tours starting from this city. He can choose any city in the Binary Country (includingAi) as the terminal city for a tour. He gains happiness(Hi - L) during a tour, whereL is the distance between the city Ai and the terminal city.
Ralph is interested in tours from Ai in which he can gain positive happiness. For each query, compute the sum of happiness gains for all such tours.
Ralph will never take the same tour twice or more (in one query), he will never pass the same city twice or more in one tour.
The first line contains two integers n andm (1 ≤ n ≤ 106,1 ≤ m ≤ 105).
(n - 1) lines follow, each line contains one integerLi (1 ≤ Li ≤ 105), which denotes the length of thei-th road.
m lines follow, each line contains two integersAi andHi (1 ≤ Ai ≤ n,0 ≤ Hi ≤ 107).
Print m lines, on the i-th line print one integer — the answer for the i-th query.
2 251 82 4
114
6 4211322 41 33 21 7
116328
题意:给你一棵树 每个节点i和节点i/2相连,接下来有m次询问,每次给出一个根节点x和H,求一x为起点,任意与x相距d<=H的节点为终点,求H-d之和。
对于每个节点,预处理出它子节点到他的距离并排序,假设以x为根,那么只要二分查找距离<=k的节点,再用前缀和求出贡献。不断的以x的父亲为根遍历所有情况,就可以得出答案了。
#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>using namespace std;const int maxm = 1000005;#define ll long longvector<int>v[maxm];vector<ll>s[maxm];int a[maxm], dis[maxm];void merge_sort(int x, int y){int rev = a[y], cnt = 0;int lx = v[x].size(), ly = v[y].size(), l = 0, r = 0;while (l < lx&&r < ly){if (v[x][l] < v[y][r] + rev)dis[cnt++] = v[x][l++];else dis[cnt++] = v[y][r++] + rev;}while (l < lx) dis[cnt++] = v[x][l++];while (r < ly) dis[cnt++] = v[y][r++] + rev;for (int i = 0;i < v[x].size();i++) v[x][i] = dis[i];for (int i = v[x].size();i < cnt;i++) v[x].push_back(dis[i]);}ll query(int x, int k){int l = 0, r = v[x].size() - 1;while (l <= r){int mid = (l + r) / 2;if (v[x][mid] <= k) l = mid + 1;else r = mid - 1;}if (r < 0) return 0;return (ll)k*(r + 1) - s[x][r];}int main(){int n, i, j, k, sum, m, x;scanf("%d%d", &n, &m);for (i = 2;i <= n;i++) scanf("%d", &a[i]);for (i = 1;i <= n;i++) v[i].push_back(0);for (i = n;i >= 2;i--) merge_sort(i / 2, i);for (i = 1;i <= n;i++){for (j = 0;j < v[i].size();j++){s[i].push_back(v[i][j]);if (j) s[i][j] += s[i][j - 1];}}ll ans;int pre;for (i = 1;i <= m;i++){scanf("%d%d", &x, &k);ans = 0;for (j = x, pre = 0;j > 0;k -= a[j], pre = j, j /= 2){if (k < 0) break;ans += k;int ls = j * 2, rs = j * 2 + 1;if (ls <= n&&ls != pre) ans += query(ls, k - a[ls]);if (rs <= n&&rs != pre) ans += query(rs, k - a[rs]);}printf("%lld\n", ans);}return 0;}
- codeforces Round 447 div2 D Ralph And His Tour in Binary Country
- Codeforces Round #447 (Div. 2) D. Ralph And His Tour in Binary Country
- Codeforces Round #447 (Div. 2) D. Ralph And His Tour in Binary Country(dfs)
- Codeforces Round #447(Div.2)Problem D Ralph And His Tour in Binary Country(归并排序+二分)
- Codeforces Round #447 (Div. 2) D. Ralph And His Tour in Binary Country 二叉树、预处理、二分、数据结构
- D. Ralph And His Tour in Binary Country
- Ralph And His Tour in Binary Country
- [Codeforces 894D. Ralph And His Tour in Binary Country]预处理+二分
- Codeforces894D Ralph And His Tour in Binary Country
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field(数论,组合数学)
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field(思维)
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field(数学???)
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 推规律+证明
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field
- Codeforces Round#447 E Ralph and Mushrooms
- Codeforces #Round 447Div2 D
- PyCharm 自动添加文件头注释
- 673. Number of Longest Increasing Subsequence【Medium】 一维动归
- 数据库mariadb
- caffe层解析之softmaxwithloss层
- Raptor中统计不同的字符
- codeforces Round 447 div2 D Ralph And His Tour in Binary Country
- 倒立摆系统分析及控制
- 【PAT Practise】1038. Recover the Smallest Number (30)
- niagara架构入门级理解
- C语言基础二(函数)
- 搭建lua开发环境(mac&&linux)
- HTML标签<br><br/>的区别在哪里?
- Java小白之路
- Linux内核学习经验总结