【LOJ】#2182. 「SDOI2015」寻宝游戏

题解

终于了解怎么动态维护虚树了

就是把点按照dfs序排个序啊

这道题显然是求虚树上所有边长的两倍

我们把dfs序排完序,相邻两个点加上路径长(包括首尾),删除的时候删一个点减去它到两边再加上新近相邻的两个点即可
增加同理

代码

#include 
#define fi first
#define se second
#define pii pair
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
template
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
    }
    res *= f;
}
template
void out(T x) {
    if(x < 0) {putchar('-');x = -x;}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}

int N,M;
struct node {
    int to,next;int64 val;
}E[MAXN * 2];
int head[MAXN],sumE,st[MAXN * 2][20],len[MAXN * 2],pos[MAXN],dep[MAXN],dfn[MAXN],idx,tot;
int pre[MAXN],suf[MAXN];
int64 dis[MAXN];
bool vis[MAXN];
struct cmp {
    bool operator () (const int &a,const int &b) const {
    return dfn[a] < dfn[b];
    }
};
set S;
void add(int u,int v,int64 c) {
    E[++sumE].to = v;
    E[sumE].next = head[u];
    E[sumE].val = c;
    head[u] = sumE;
}
int min_dep(int a,int b) {
    return dep[a] < dep[b] ? a : b;
}
int lca(int a,int b) {
    a = pos[a];b = pos[b];
    if(a > b) swap(a,b);
    int l = len[b - a + 1];
    return min_dep(st[a][l],st[b - (1 << l) + 1][l]);
}
int64 dist(int a,int b) {
    return dis[a] + dis[b] - 2 * dis[lca(a,b)];
}
void dfs(int u,int fa) {
    dfn[u] = ++idx;
    st[++tot][0] = u;
    dep[u] = dep[fa] + 1;
    pos[u] = tot;
    for(int i = head[u] ; i ; i = E[i].next) {
    int v = E[i].to;
    if(v != fa) {
        dis[v] = dis[u] + E[i].val;
        dfs(v,u);
        st[++tot][0] = u;
    }
    }
}
void Init() {
    read(N);read(M);
    int u,v;int64 c;
    for(int i = 1 ; i < N ; ++i) {
    read(u);read(v);read(c);
    add(u,v,c);add(v,u,c);
    }
    dfs(1,0);
    for(int j = 1 ; j <= 19 ; ++j) {
    for(int i = 1 ; i <= tot ; ++i) {
        if(i + (1 << j) - 1 > tot) break;
        st[i][j] = min_dep(st[i][j - 1],st[i + (1 << j - 1)][j - 1]);
    }
    }
    for(int i = 2 ; i <= tot ; ++i) len[i] = len[i / 2] + 1;
}
void Solve() {
    int t;
    int64 ans = 0;
    while(M--) {
    read(t);
    if(vis[t]) {
        vis[t] = 0;
        S.erase(t);
        ans -= dist(t,pre[t]) + dist(t,suf[t]);
        ans += dist(pre[t],suf[t]);
        suf[pre[t]] = suf[t];
        pre[suf[t]] = pre[t];
    }
    else {
        vis[t] = 1;
        S.insert(t);
        auto k = S.find(t);
        if(k == S.begin()) {
        pre[t] = *(--S.end());
        }
        else {--k;pre[t] = *k;}
        k = S.find(t);++k;
        if(k == S.end()) {
        suf[t] = *(S.begin());
        }
        else {suf[t] = *k;}
        ans -= dist(pre[t],suf[t]);
        ans += dist(t,pre[t]) + dist(t,suf[t]);
        suf[pre[t]] = t;
        pre[suf[t]] = t;
    }
    out(ans);enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Init();
    Solve();
}

转载于:https://www.cnblogs.com/ivorysi/p/9888185.html

你可能感兴趣的:(【LOJ】#2182. 「SDOI2015」寻宝游戏)