codeforces Round 447 div2 D Ralph And His Tour in Binary Country

来源:互联网 发布:java破解验证码 编辑:程序博客网 时间:2024/06/07 02:45
D. Ralph And His Tour in Binary Country
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 (herex 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.

Input

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).

Output

Print m lines, on the i-th line print one integer — the answer for the i-th query.

Examples
Input
2 251 82 4
Output
114
Input
6 4211322 41 33 21 7
Output
1163

28

题意:给你一棵树 每个节点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;}


阅读全文
0 0
原创粉丝点击