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

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 from 1 to (n - 1), the i-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 city Ai and an integer Hi. He wants to make some tours starting from this city. He can choose any city in the Binary Country (including Ai) as the terminal city for a tour. He gains happiness (Hi - L) during a tour, where L 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 and m (1 ≤ n ≤ 106, 1 ≤ m ≤ 105).

(n - 1) lines follow, each line contains one integer Li (1 ≤ Li ≤ 105), which denotes the length of the i-th road.

m lines follow, each line contains two integers Ai and Hi (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 2
5
1 8
2 4
Output
11
4
Input
6 4
2
1
1
3
2
2 4
1 3
3 2
1 7
Output
11
6
3

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 long
vector<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值