hdu6326

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<bits/stdc++.h>
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<<j){
                if(ret.a[j]) {
                    b.a[i] ^= ret.a[j];
                }
                else {
                    ret.a[j] = b.a[i];break;
                }
            }
        }
    }
    return ret;
}

void build(int l,int r,int rt){
    if(l == r){
        memset(sum[rt].a,0,sizeof(sum[rt].a));
        for(int i = 30;i >= 0;i --){
            if(num[l]&(1<<i)) {sum[rt].a[i] = num[l];break;}
        }
        return ;
    }
    int mid = l+r>>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<<i))== 0){
                    ans ^= ret.a[i];
                }
            }
            printf("%d\n",ans);
        }


    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值