【HDU5770】【线段树】【扫描线】Treasure 题解

题目描述了一张由N个节点和N-1条双向道路组成的宝物地图,玩家从节点A出发到节点B,途中收集钥匙并打开对应宝箱获取最大价值。输入包含多个测试用例,每个用例包含节点数、宝物数、道路和宝箱信息。玩家需规划最佳路径,求解所能获得的最大宝物总价值。样例给出了两组输入和输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Treasure

?? has got a treasure map,the treasure map consists of N nodes,there are N-1 bidirectional roads, Each of them connects a pair of nodes,the N nodes are all connected by the N-1 bidirectional roads.?? can travel through the roads.
There are M treasures in some node. Each treasure has a value, treasures are put into boxs,each treasure corresponds to a box and each box corresponds to a key which is also in some node. Now ?? wants to plan a simple path, the path’s start point is node A,and the path’s destination is node B,?? will go from node A to node B. When ?? goes through a node(including node A and node B), he must first collects all keys, and then must open the boxes he can open in this node. The keys he got never disappear during the travel. Now ?? wants to know the maximum value he could get if he plan a best path.Can you help him?

Input
The first line of the input gives the number of case T,T test cases follow.
Each case first line contains two integer n,m(n,m<=100000)indicating the number of nodes and the number of treasures.
Next n-1 lines each line contains two numbers ui and vi(1<=ui,vi<=n) indicates that there’s one road connecting node ui node vi.
Next m lines each line contains three number ui,vi,wi(1<=ui,vi<=n,-1000<=wi<=1000) indicates the position of ith-box’s key and the position of ith-box, and the value of treasure in the ith-box.

T<=30
The sum of n in all the test cases is no more than 600000
The sum of m in all the test cases is no more than 600000

Output
For each that case output”Case #x: y”,where x is the test case number(starting from 1),and y is the answer you get for that case.
Sample Input
2
4 2
1 2
2 3
3 4
1 1 100
2 4 -5
4 3
1 2
1 3
1 4
2 1 1
1 3 2
1 3 5
Sample Output
Case #1: 100
Case #2: 8

附LMY大大的AC代码,蒟蒻不会

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define rez(i,x,y) for(int i=x;i>=y;i--)
#define res(i,x,y) for(int i=x;i<=y;i++)
#define INF 0x3f3f3f3f
#define maxn 111111
#define ll long long
#define ls (t<<1)
#define rs (t<<1|1)
#define clr(x)  memset(x,0,sizeof(x))
using namespace std;
template<class T>inline void readin(T &res){
    static char ch;
    while((ch=getchar())>'9'||ch<'0');
    res=ch-48;
    while((ch=getchar())<='9'&&ch>='0')
    res=ch-48+res*10;
}
struct node{
    int y,l,r,v;
    node(){};
    node(int _y,int _l,int _r,int _v){
        y=_y,l=_l,r=_r,v=_v;
    } 
    bool operator <(const node &b)const{
        return y<b.y;
    }
}a[maxn*10];
vector<int>g[maxn];
int p[maxn][20],deep[maxn],vis[maxn];
int index,l[maxn],r[maxn];
int T,n,m,res,Case=1;
void dfs(int u){
    l[u]=++index;
    vis[u]=1;
    for(register int i=0;i<g[u].size();i++){
        int v=g[u][i];
        if(vis[v])continue;
        deep[v]=deep[u]+1;
        p[v][0]=u;
        dfs(v);
    }
    r[u]=index;
}
int lca(int a,int b){
    if(deep[a]<deep[b])swap(a,b);
    register int i,j;
    for(i=0;(1<<i)<=deep[a];i++);
    i--;
    for(j=i;j>=0;j--)
        if(deep[a]-(1<<j)>=deep[b])
            a=p[a][j];
    if(a==b) return a;
    for(j=i;j>=0;j--)
        if(p[a][j]!=-1&&p[a][j]!=p[b][j])
            a=p[a][j],b=p[b][j];
    return p[a][0];
}
int find(int x,int step){
    for(register int i=0;i<20;i++)
        if(step&(1<<i))x=p[x][i];
    return x;
}
void init(int n){
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i<=n;i++)
            if(~p[i][j-1])p[i][j]=p[p[i][j-1]][j-1];
}
void deal(int y1,int y2,int x1,int x2,int v){
    a[res++]=node(y1,x1,x2,v);
    if(y2<n)a[res++]=node(y2+1,x1,x2,-v);
}
struct Tree{
    int Max,lazy;
}tree[maxn<<2];
void push_up(int t){
    tree[t].Max=max(tree[ls].Max,tree[rs].Max);
}
void push_down(int t){
    int lazy=tree[t].lazy;
    if(!lazy)return ;
    tree[ls].Max+=lazy,tree[ls].lazy+=lazy;
    tree[rs].Max+=lazy,tree[rs].lazy+=lazy;
    tree[t].lazy=0;
}
void build(int l,int r,int t){
    tree[t].Max=tree[t].lazy=0;
    if(l==r)return ;
    int mid=(l+r)>>1;
    build(l,mid,ls),build(mid+1,r,rs);
}
void update(int l,int r,int L,int R,int t,int v){
    if(l==L&&r==R){
        tree[t].Max+=v,tree[t].lazy+=v;
        return ;
    }
    push_down(t);
    int mid=(l+r)>>1;
    if(R<=mid)update(l,mid,L,R,ls,v);
    else if(L>mid)update(mid+1,r,L,R,rs,v);
    else {
        update(l,mid,L,mid,ls,v);
        update(mid+1,r,mid+1,R,rs,v);
    }
    push_up(t);
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)g[i].clear();
        for(int i=1;i<n;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v),g[v].push_back(u);
        }
        memset(p,-1,sizeof(p));
        memset(vis,0,sizeof(vis));
        deep[1]=1;index=0;
        dfs(1);
        init(n);
        res=0;
        while(m--){
            int a,b,v;
            scanf("%d%d%d",&a,&b,&v);
            int c=lca(a,b);
            if(a==b){
                deal(1,n,1,n,v);
                for(int i=0;i<g[a].size();i++){
                    int d=g[a][i];
                    if(deep[d]<deep[a])continue;
                    deal(l[d],r[d],l[d],r[d],-v);
                }
                if(l[a]>1)deal(1,l[a]-1,1,l[a]-1,-v);
                if(r[a]<n)deal(r[a]+1,n,r[a]+1,n,-v);
                if(l[a]>1&&r[a]<n){
                    deal(1,l[a]-1,r[a]+1,n,-v);
                    deal(r[a]+1,n,1,l[a]-1,-v);
                }
            }
            else if(a!=c&&b!=c){
                deal(l[a],r[a],l[b],r[b],v);
            }
            else if(a==c){
                int d=find(b,deep[b]-deep[a]-1);
                if(l[d]>1)deal(1,l[d]-1,l[b],r[b],v);
                if(r[d]<n)deal(r[d]+1,n,l[b],r[b],v);
            }
            else if(b==c){
                int d=find(a,deep[a]-deep[b]-1);
                if(l[d]>1)deal(l[a],r[a],1,l[d]-1,v);
                if(r[d]<n)deal(l[a],r[a],r[d]+1,n,v);
            }
        }
        sort(a,a+res);
        int ans=-INF;
        build(1,n,1);
        for(int i=1,j=0;i<=n;i++){
            for(;j<res&&a[j].y==i;j++)
                update(1,n,a[j].l,a[j].r,1,a[j].v);
            ans=max(ans,tree[1].Max);
        }
        printf("Case #%d: %d\n",Case++,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值