CH # 56C vision Stone

description

Adera is a Microsoft application store in a puzzle game.
Stone is a different time and space into the vision of Adera guide was there a map of Adera in different time and space. There are N points on the map, there are N-1 bidirectional communication edges them together. At first no vision stone on the map, the next M in time, each time one of three types of events:
the emergence of a vision stone (not already appear on the map, some point reappears);
; 2. visions stone on the map some point be destroyed (no points will not destroy vision stone)
overall length 3. Ask the players to the point where all the visions stone edge set of communicating The minimum is.
As a player you please answer these questions.

Input Format

The first line has an integer N, the number of points.
Next each row N-1 rows of three integers x, y, z, represents between points x and y have a length of z bidirectional edge.
N + 1 th line have a positive integer M.
The next M lines each is an event, the event is one of three formats:
+ x represents the emergence of a vision stone on the point x
- x represents the vision of the stone was destroyed on the spot x
represents ask that all current vision? set point communication side where the stone required minimum total length is.

Output Format

For each? Event, the output of a integer answer.

Sample input

6
1 2 1
1 3 5
4 1 7
4 5 3
6 4 2
10
+ 3
+ 1
?
+ 6
?
+ 5
?
- 6
- 3
?

Sample Output

5
14
17
10

Data range and Conventions

  • Data for 30%, 1 ≤ n, m ≤ 1000.
  • For the other 20% of the data, the map is a chain, or a chrysanthemum.
  • To 100% of the data, 1 ≤ n, m ≤ 10 ^ 5, 1 ≤ x, y ≤ n, x ≠ y, 1 ≤ z ≤ 10 ^ 9.

answer

The stone twice a vision point sequence according to their size DFS writing an annular sequence, from the sequence of numbers and is adjacent to answer.

Maintenance set with this sequence, record the answer with a variable, insert or delete a number in the sequence is inserted or destroyed, and change the answer to. To find the distance may be multiplied lca.

Time complexity \ (O ((n + m ) \ log n) \)

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
    for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
typedef pair<int,int> pii;

co int N=1e5+1;
int n,m,t,f[N][18],dep[N],dfn[N],tot;
vector<pii> e[N];
ll d[N],ans;
set<pii> st;
typedef set<pii>::iterator it;
void dfs(int x){
    dfn[x]=++tot;
    for(int i=0,y;i<e[x].size();++i){
        if(dep[y=e[x][i].first]) continue;
        dep[y]=dep[x]+1;
        d[y]=d[x]+e[x][i].second;
        f[y][0]=x;
        for(int j=1;j<=t;++j) f[y][j]=f[f[y][j-1]][j-1];
        dfs(y);
    }
}
int lca(int x,int y){
    if(dep[x]>dep[y]) swap(x,y);
    for(int i=t;i>=0;--i)
        if(dep[f[y][i]]>=dep[x]) y=f[y][i];
    if(x==y) return x;
    for(int i=t;i>=0;--i)
        if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
    return f[x][0];
}
il ll path(int x,int y){
    return d[x]+d[y]-2*d[lca(x,y)];
}
il void insert(int x){
    st.insert(pii(dfn[x],x));
    it l=st.find(pii(dfn[x],x));
    l=l==st.begin()?--st.end():--l;
    it r=st.find(pii(dfn[x],x));
    r=r==--st.end()?st.begin():++r;
    ans-=path(l->second,r->second);
    ans+=path(l->second,x)+path(x,r->second);
}
il void remove(int x){
    it l=st.find(pii(dfn[x],x));
    l=l==st.begin()?--st.end():--l;
    it r=st.find(pii(dfn[x],x));
    r=r==--st.end()?st.begin():++r;
    ans-=path(l->second,x)+path(x,r->second);
    ans+=path(l->second,r->second);
    st.erase(pii(dfn[x],x));
}
int main(){
    read(n);
    for(int i=1,x,y,z;i<n;++i){
        read(x),read(y),read(z);
        e[x].push_back(pii(y,z)),e[y].push_back(pii(x,z));
    }
    t=log(n)/log(2)+1;
    dep[1]=1,dfs(1);
    for(read(m);m--;){
        char s[2];scanf("%s",s);
        if(s[0]=='?') printf("%lld\n",ans/2);
        else{
            int x=read<int>();
            s[0]=='+'?insert(x):remove(x);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/10935047.html