Little Q is fighting against scary monsters in the game ``Monster Hunter’’. The battlefield consists of n intersections, labeled by 1,2,…,n, connected by n−1 bidirectional roads. Little Q is now at the 1-th intersection, with X units of health point(HP).
There is a monster at each intersection except 1. When Little Q moves to the k-th intersection, he must battle with the monster at the k-th intersection. During the battle, he will lose ai units of HP. And when he finally beats the monster, he will be awarded bi units of HP. Note that when HP becomes negative(<0), the game will over, so never let this happen. There is no need to have a battle at the same intersection twice because monsters do not have extra life.
When all monsters are cleared, Little Q will win the game. Please write a program to compute the minimum initial HP that can lead to victory.
Input
The first line of the input contains an integer T(1≤T≤2000), denoting the number of test cases.
In each test case, there is one integer n(2≤n≤100000) in the first line, denoting the number of intersections.
For the next n−1 lines, each line contains two integers ai,bi(0≤ai,bi≤109), describing monsters at the 2,3,…,n-th intersection.
For the next n−1 lines, each line contains two integers u and v, denoting a bidirectional road between the u-th intersection and the v-th intersection.
It is guaranteed that ∑n≤106.
Output
For each test case, print a single line containing an integer, denoting the minimum initial HP.
Sample Input
1
4
2 6
5 4
6 2
1 2
2 3
3 4
Sample Output
3
题意:有一个树,除了1节点每个节点有一个怪物,打到第i个怪物需要扣掉ai的血量,然后得到bi的血量,问从1
出发,用最优的路线(每个点可以走多次,只有第一次经过需要打怪物),问最少需要多少初始血量,保证全程血量不为负。
这里有个一个比较好的解释:
http://www.cnblogs.com/ZERO-/p/9462635.html
#include
using namespace std;
const int N = 1e5+100;
struct node{
int a[32];
node(){
memset(a,0,sizeof(a));
}
};
node sum[N<<2];
int num[N];
node get(node a,node b){
node ret = a;
for(int i = 0;i <= 30;i ++){
for(int j = 30;j >= 0;j --){
if(b.a[i]&1<= 0;i --){
if(num[l]&(1<>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
sum[rt] = get(sum[rt<<1],sum[rt<<1|1]);
}
node query(int L,int R,int l,int r,int rt){
if(L <= l && R >= r) return sum[rt];
int mid = l+r>>1;
if(mid >= L && mid < R) return get(query(L,R,l,mid,rt<<1),query(L,R,mid+1,r,rt<<1|1));
if(mid >= L) return query(L,R,l,mid,rt<<1);
return query(L,R,mid+1,r,rt<<1|1);
}
int main(){
int T;
cin >> T;
while(T--){
int n,q,k;
scanf("%d %d %d",&n,&q,&k);
for(int i = 1;i <= n;i ++){
scanf("%d",&num[i]);
}
build(1,n,1);
for(int i = 1;i <= q;i ++){
int ql,qr;
scanf("%d %d",&ql,&qr);
node ret = query(ql,qr,1,n,1);
int ans = k;
for(int i = 30;i >= 0;i --){
if(ret.a[i] && (ans&(1<