求树上点集(带修改)的最小覆盖线段集合的值

结论,点集按dfs序从小到大首尾相连,则答案为距离和的二分之一

#define LOCAL
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define p_queue priority_queue

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}
int n, m;
//前向星
struct e{
    int t, w ,next;
}edge[200005];
int head[100005], cnt;
void add(int f, int t, int w)
{
    edge[cnt].t = t;
    edge[cnt].w = w;
    edge[cnt].next = head[f];
    head[f] = cnt ++;
}
//维护
struct node{
    int v, dfn;
    bool operator < (const node& no) const{
        return dfn > no.dfn;
    }
    node(int a, int b):v(a),dfn(b){}
};
set <node> s;
int times;
//LCA
int bz[100005][30], dep[100005], dfn[100005];
ll dp[100005];
void dfs(int u , int deep)
{
    dfn[u] = ++ times;
    dep[u] = deep;
    for(int i = head[u] ; i != - 1 ; i = edge[i].next)
    {
        int v = edge[i].t , w = edge[i].w;
        if(!dep[v])
        {
            bz[v][0] = u;
            dp[v] = dp[u] + w;
            dfs(v,deep+1);
        }
    }
}
void deal()
{
    for(int i = 1 ; i <= 25 ; i ++)
        for(int j = 1 ; j <= n ; j ++)
            bz[j][i] = bz[bz[j][i-1]][i-1];
}
int LCA(int x, int y)
{
    if(dep[x] < dep[y])
        swap(x,y);
    for(int i = 25 ; i >= 0 ; i --)
        if(dep[bz[x][i]] >= dep[y])
            x = bz[x][i];
    if(x == y) return x;
    for(int i = 25 ; i >= 0 ; i --)
        if(bz[x][i] ^ bz[y][i])
            x = bz[x][i] , y = bz[y][i];
    return bz[x][0];
}
ll dis(int u , int v)
{
    return (ll)dp[u] + dp[v] - 2*dp[LCA(u,v)];
}
int main(void)
{
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("odata.out", "w", stdout);
#endif
    mem(head,-1);
    char ch;
    int u ,v, w;
    cin>>n;
    for(int i = 1 ; i < n ; i ++) {
        cin >> u >> v >> w;
        add(u,v,w);
        add(v,u,w);
    }
    dfs(1,1);
    deal();
    cin>>m;
    ll ans = 0;
    for(int i = 1 ; i <= m ; i ++)
    {
        cin>>ch;
        if(ch == '+')
        {
            cin>>v;
            if(s.empty()) {
                s.insert(node(v, dfn[v]));
                continue;
            }
            auto l = -- s.lower_bound(node(v,dfn[v]));
            auto r = s.lower_bound(node(v,dfn[v]));
            if(r == s.begin()) l = --s.end();
            if(r == s.end()) r = s.begin();
            ans -= dis(l->v,r->v);
            ans += dis(l->v,v) + dis(v,r->v);
            s.insert(node(v,dfn[v]));

        }
        else if(ch == '-')
        {
            cin>>v;
            s.erase(node(v,dfn[v]));
            if(s.empty()) continue;
            auto l = -- s.lower_bound(node(v,dfn[v]));
            auto r = s.lower_bound(node(v,dfn[v]));
            if(r == s.begin()) l = --s.end();
            if(r == s.end()) r = s.begin();
            ans -= dis(l->v,v) + dis(v,r->v);
            ans += dis(l->v,r->v);
        }
        else if(ch == '?')
            cout << ans / 2 << endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值