codeforces 894d Ralph And His Tour in Binary Country

D. Ralph And His Tour in Binary Country

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

Note

Here is the explanation for the second sample.

Ralph's first query is to start tours from city 2 and Hi equals to 4. Here are the options:

He can choose city 5 as his terminal city. Since the distance between city 5 and city 2 is 3, he can gain happiness 4 - 3 = 1.
He can choose city 4 as his terminal city and gain happiness 3.
He can choose city 1 as his terminal city and gain happiness 2.
He can choose city 3 as his terminal city and gain happiness 1.
Note that Ralph can choose city 2 as his terminal city and gain happiness 4.
Ralph won't choose city 6 as his terminal city because the distance between city 6 and city 2 is 5, which leads to negative happiness for Ralph.
So the answer for the first query is 1 + 3 + 2 + 1 + 4 = 11.

题意

有n个城市,i和2i存在一条边,i和2i+1也连一条边。
给你每条边的长度。
现在给你m个询问,每个询问给你x和h,表示起点为x,然后让你求sigma(max(h-dist[i],0))

题解

每个点都是往后连的,我们定义函数query(x,y)表示起点为x,与他相连且只考虑比他大的点,能获得的快乐一共是多少。

然后我们再不停的/2去算这个log就好了。

。。。看不懂题解没关系,读读代码就好了,代码很容易理解的。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;

int n,m;
vector<long long> l[maxn],s[maxn];
int t[maxn];
long long query(int x,long long h){
    if(h<=0)return 0;
    int p = upper_bound(l[x].begin(),l[x].end(),h)-l[x].begin()-1;
    return (p+1)*h-s[x][p];
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=2;i<=n;i++){
        scanf("%d",&t[i]);
    }
    for(int i=n;i>=1;i--){
        l[i].push_back(0);
        vector<int>b={i*2,i*2+1};
        for(int x:b){
            if(x>n)continue;
            for(int y:l[x]){
                l[i].push_back(y+t[x]);
            }
        }
        sort(l[i].begin(),l[i].end());
        s[i].resize(l[i].size());
        for(int j=1;j<l[i].size();j++){
            s[i][j]=s[i][j-1]+l[i][j];
        }
    }
    for(int i=1;i<=m;i++){
        int a,h;
        scanf("%d%d",&a,&h);
        long long ans = query(a,h);
        while(a>1){
            int b = a;
            h-=t[a];
            a>>=1;
            if(h<=0)break;
            ans+=h;
            int x=a*2==b?a*2+1:a*2;
            if(x<=n){
                ans+=query(x,h-t[x]);
            }
        }
        cout<<ans<<endl;
    }
}
 
 
具体查找的时候就是往比当前节点编号大的节点走收获query个 然后由编号比当前节点小的节点往当前节点走收获h[i]个 然后由当前节点往兄弟节点与编号比兄弟节点大的且与兄弟节点相连的节点走收获query个
还有 s就是前缀和 排序是为了便于筛选出比h小的路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值