码迷,mamicode.com
首页 > 其他好文 > 详细

[WC2013]糖果公园

时间:2018-04-06 23:54:41      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:tmp   tchar   for   body   ref   span   char   lld   har   

题面

UOJ

Sol

树上带修改莫队

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;

template <class Int>
IL void Input(RG Int &x){
    RG int z = 1; RG char c = getchar(); x = 0;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= z;
}

const int maxn(1e5 + 5);

int n, m, q, first[maxn], cnt, w[maxn], f[maxn], c[maxn];
int dfn[maxn], st[20][maxn << 1], lg[maxn << 1], deep[maxn], idx, fa[maxn];
int sta[maxn], bl[maxn], blo, num, sum[maxn * 10], vis[maxn], tot1, tot2;
ll ans[maxn], tmpans;

struct Edge{
    int to, next;
} edge[maxn << 1];

struct Query{
    int l, r, id, t;

    IL int operator <(RG Query B) const{
        if(bl[l] != bl[B.l]) return bl[l] < bl[B.l];
        if(bl[r] != bl[B.r]) return bl[r] < bl[B.r];
        return t < B.t;
    }
} qry[maxn];

struct Change{
    int x, y;
} mdy[maxn];

IL void Add(RG int u, RG int v){
    edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}

IL void Dfs(RG int u){
    dfn[u] = ++idx, st[0][idx] = u;
    RG int l = sta[0];
    for(RG int e = first[u]; e != -1; e = edge[e].next){
        RG int v = edge[e].to;
        if(dfn[v]) continue;
        deep[v] = deep[u] + 1, fa[v] = u;
        Dfs(v);
        st[0][++idx] = u;
        if(sta[0] - l < blo) continue;
        for(++num; sta[0] != l; --sta[0]) bl[sta[sta[0]]] = num;
    }
    sta[++sta[0]] = u;
}

IL void Chk(RG int &x, RG int u, RG int v){
    x = deep[u] < deep[v] ? u : v;
}

IL int LCA(RG int u, RG int v){
    u = dfn[u], v = dfn[v];
    if(u > v) swap(u, v);
    RG int log2 = lg[v - u + 1], t;
    Chk(t, st[log2][u], st[log2][v - (1 << log2) + 1]);
    return t;
}

IL void Update(RG int x){
    if(vis[x]) tmpans -= 1LL * f[c[x]] * w[sum[c[x]]], --sum[c[x]];
    else ++sum[c[x]], tmpans += 1LL * f[c[x]] * w[sum[c[x]]];
    vis[x] ^= 1;
}

IL void Adjust(RG int x, RG int &y){
    if(vis[x]) Update(x), swap(c[x], y), Update(x);
    else swap(c[x], y);
}

IL void Modify(RG int u, RG int v){
    while(u != v){
        if(deep[u] > deep[v]) swap(u, v);
        Update(v), v = fa[v];
    }
}

IL void Init_Graph(){
    Input(n), Input(m), Input(q);
    blo = pow(n, 0.2 / 0.3);
    for(RG int i = 1; i <= m; ++i) Input(f[i]);
    for(RG int i = 1; i <= n; ++i) Input(w[i]), first[i] = -1;
    for(RG int i = 1, u, v; i < n; ++i) Input(u), Input(v), Add(u, v), Add(v, u);
    for(RG int i = 1; i <= n; ++i) Input(c[i]);
    Dfs(1);
    for(++num; sta[0]; --sta[0]) bl[sta[sta[0]]] = num;
    for(RG int i = 2; i <= idx; ++i) lg[i] = lg[i >> 1] + 1;
    for(RG int j = 1; j <= lg[idx]; ++j)
        for(RG int i = 1; i + (1 << j) - 1 <= idx; ++i)
            Chk(st[j][i], st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}

IL void Init_Task(){
    for(RG int i = 1, op, x, y; i <= q; ++i){
        Input(op), Input(x), Input(y);
        if(op){
            if(dfn[x] > dfn[y]) swap(x, y);
            qry[++tot1] = (Query){x, y, tot1, tot2};
        }
        else mdy[++tot2] = (Change){x, y};
    }
    sort(qry + 1, qry + tot1 + 1);
}

int main(RG int argc, RG char* argv[]){
    Init_Graph(), Init_Task();
    RG int j = 0, lca = LCA(qry[1].l, qry[1].r);
    while(j < qry[1].t) ++j, Adjust(mdy[j].x, mdy[j].y);
    Modify(qry[1].l, qry[1].r);
    Update(lca), ans[qry[1].id] = tmpans, Update(lca);
    for(RG int i = 2; i <= tot1; ++i){
        while(j < qry[i].t) ++j, Adjust(mdy[j].x, mdy[j].y);
        while(j > qry[i].t) Adjust(mdy[j].x, mdy[j].y), --j;
        lca = LCA(qry[i].l, qry[i].r);
        Modify(qry[i - 1].l, qry[i].l), Modify(qry[i - 1].r, qry[i].r);;
        Update(lca), ans[qry[i].id] = tmpans, Update(lca);
    }
    for(RG int i = 1; i <= tot1; ++i) printf("%lld\n", ans[i]);
    return 0;
}

[WC2013]糖果公园

标签:tmp   tchar   for   body   ref   span   char   lld   har   

原文地址:https://www.cnblogs.com/cjoieryl/p/8729174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!