sdoi 2014 旅行 bzoj 3531

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3531
思路:疯狂RE,,,就是偷了个小懒lca写错了,,,结果后来WA那就是因为一个小细节,,,好久没写连剖了,,,第二道连剖?
代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#define N 100000
using namespace std;
typedef long long LL;
struct edge{ int nxt,point,v;};
struct node { int maxi,sum;int son[2];};
edge e[(N<<4)+5];
node A[10000005],ans;
int n,q,w[N+5],c[N+5],sz[N+5],deep[N+5],belong[N+5],fa[N+5][30],cnt,top,dfn[N+5],
l[N+5],r[N+5],root[N+5];
inline void addedge(int u1,int v1){
    e[++cnt].nxt = e[u1].point; e[u1].point = cnt; e[cnt].v = v1;}
inline void insert(int u1,int v1){ addedge(u1,v1); addedge(v1,u1);}
inline int getnum(){
    char c; int num;
    while (!isdigit(c = getchar()));
    num = c - '0';
    while (isdigit(c = getchar())) num = 10 * num + c - '0';
    return num;
}

void init(){
    int u1,v1;
    n = getnum(); q = getnum(); cnt = 0; top = 0;
    for (int i = 1;i <= n; ++i) w[i] = getnum(),c[i] = getnum();
    for (int i = 1;i < n; ++i){
        u1 = getnum(); v1 = getnum();
        insert(u1,v1);
    }
    cnt = 0;
}

inline void dfs1(int x){
     sz[x] = 1;
     for (int i = 1;i <= 20; ++i) fa[x][i] = fa[fa[x][i-1]][i-1];
     for (int p = e[x].point; p; p = e[p].nxt)
       if (e[p].v != fa[x][0]){
            deep[e[p].v] = deep[x] + 1; fa[e[p].v][0] = x;
            dfs1(e[p].v);
            sz[x] += sz[e[p].v]; 
       }
}

inline void dfs2(int x,int chain){
    int k = 0; 
    dfn[++top] = x; l[x] = top; belong[x] = chain;
    for (int p = e[x].point;p; p = e[p].nxt)
      if (e[p].v != fa[x][0]&&sz[e[p].v] > sz[k]) k = e[p].v;
    if (!k) return; 
    dfs2(k,chain);
    for (int p =  e[x].point; p; p = e[p].nxt)
      if (e[p].v != fa[x][0] && e[p].v != k) dfs2(e[p].v,e[p].v);
    r[x] = top;
}

inline int LCA(int u,int v){
    if (deep[u] < deep[v]) swap(u,v);
    int ll = deep[u] - deep[v];
    for (int i = 0;i <= 20; ++i)
      if (ll&(1<<i)) u = fa[u][i];
    for (int i = 20;i >= 0; --i)
      if (fa[u][i] != fa[v][i]) u = fa[u][i],v = fa[v][i];
    return (u == v) ? u : fa[u][0];
}

inline void update(int rt){
    A[rt].sum = A[A[rt].son[0]].sum + A[A[rt].son[1]].sum;
    A[rt].maxi = max(A[A[rt].son[0]].maxi,A[A[rt].son[1]].maxi);
}

inline void change(int l,int r,int &rt,int x,int ww){
    int mid = (l + r)>>1;
    if (!rt) rt = ++cnt;
    if (l == r) { A[rt].sum = A[rt].maxi = ww; return;}
    if (x <= mid) change(l,mid,A[rt].son[0],x,ww);
    else change(mid + 1,r,A[rt].son[1],x,ww);
    update(rt); 
}

inline node query(int l,int r,int rt,int ll,int rr){
    node a = (node){0,0,0,0},b = (node){0,0,0,0};
    int mid = (l + r)>>1;
    if (ll <= l&& rr >= r) return A[rt];
    if (ll <= mid ) a = query(l,mid,A[rt].son[0],ll,rr);
    if (rr > mid) b = query(mid + 1,r,A[rt].son[1],ll,rr);
    return (node){max(a.maxi,b.maxi),a.sum + b.sum,0,0};
}

inline node find(int u,int v,int cc){
    node a,b;
    a.sum = a.maxi = 0;
    while (belong[u] != belong[v]){
        b = query(1,n,root[cc],l[belong[u]],l[u]);
        a.sum += b.sum; a.maxi = max(a.maxi,b.maxi);
        u = fa[belong[u]][0];
    }
    b = query(1,n,root[cc],l[v],l[u]);
    a.sum += b.sum; 
    a.maxi = max(a.maxi,b.maxi);
    return a;
}

inline void make_it(int x,int y){
    int lca = LCA(x,y);
    node ans1 = find(x,lca,c[x]),ans2 = find(y,lca,c[x]);
    if (c[x] == c[lca]) ans1.sum -= w[lca];
    ans.maxi = max(ans1.maxi,ans2.maxi);
    ans.sum = ans1.sum + ans2.sum;
}

void DO_IT(){
    char ch[50]; int x,y;
    dfs1(1); dfs2(1,1);
    for (int i = 1;i <= n; ++i) change(1,n,root[c[i]],l[i],w[i]);
    for (int i = 1;i <= q; ++i){
        scanf("%s",ch); x = getnum(); y = getnum();
        if (ch[0] == 'C') 
          if (ch[1] == 'C') change(1,n,root[c[x]],l[x],0),change(1,n,root[y],l[x],w[x]),c[x] = y;
          else if (ch[1] == 'W') change(1,n,root[c[x]],l[x],y),w[x] = y;
        if (ch[0] == 'Q'){
            make_it(x,y);
            if (ch[1] == 'S') printf("%d\n",ans.sum);
            else printf("%d\n",ans.maxi);
        }
    }
}

int main(){
    init();
    DO_IT();
    return 0;
}

你可能感兴趣的:(sdoi 2014 旅行 bzoj 3531)