【BZOJ3052】【WC2013】糖果公园(树上带修莫队)

4 篇文章 0 订阅
2 篇文章 0 订阅

Description

良心洛谷比BZOJ不知道高到哪里去了,我在洛谷上谈笑风生


Solution

树上莫队的方法戳这里:COT2

这个题其实也差不多啦,只是兹磁待修就行了。
emm,记得块的大小是 n23 n 2 3 ,时间复杂度 O(n53) O ( n 5 3 )

看不懂YMY的树分块诶,果然还是太菜了233


Code

/**************************************
 * Au: Hany01
 * Prob: [BZOJ3052][WC2013] 糖果公园
 * Date: May 15th, 2018
 * Email: hany01@foxmail.com
**************************************/

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia 
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    register char c_; register int _, __;
    for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-')  __ = -1;
    for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 100005;

int n, m, cnt[maxn], cntt, lst[maxn << 1], v[maxn << 1], nex[maxn << 1], beg[maxn], e, fa[maxn][18], dep[maxn], bkt[maxn], in[maxn], out[maxn], tot, bel[maxn << 1], block, num, qsum, usum, co[maxn], q;
LL val[maxn], w[maxn], wsum[maxn], ans[maxn], sum;

struct Question
{
    int l, r, u, v, lca, ty, id, tm;

    bool operator < (const Question& A) const {
        return bel[l] == bel[A.l] ? (bel[r] == bel[A.r] ? tm < A.tm : bel[r] < bel[A.r]) : bel[l] < bel[A.l];
    }
}Q[maxn];

struct Update
{
    int pos, co;
}U[maxn];

inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }

void getdfn(int u, int pa)
{
    in[u] = ++ tot, lst[tot] = u, fa[u][0] = pa, dep[u] = dep[pa] + 1;
    for (register int i = beg[u]; i; i = nex[i])
        if (v[i] != pa) getdfn(v[i], u);
    out[u] = ++ tot, lst[tot] = u;
}

inline int LCA(int u, int v)
{
    if (dep[u] < dep[v]) swap(u, v);
    register int dt = dep[u] - dep[v];
    Fordown(i, 17, 0) if (dt >= (1 << i))
        u = fa[u][i], dt -= (1 << i);
    if (u == v) return u;
    Fordown(i, 17, 0) if (fa[u][i] != fa[v][i])
        u = fa[u][i], v = fa[v][i];
    return fa[u][0];
}

inline void del(int v) { sum -= w[bkt[v] --] * val[v]; }
inline void ins(int v) { sum += w[++ bkt[v]] * val[v]; }

inline void changecolor(int& nowco, int& nexco, int ty) {
    if (ty) del(nowco), ins(nexco);
    swap(nowco, nexco);
}

int main()
{
#ifdef hany01
    File("bzoj3052");
#endif

    //Statement
    static int uu, vv, ty;

    //Input
    n = read(), m = read(), q = read();
    For(i, 1, m) val[i] = read();
    For(i, 1, n) wsum[i] = wsum[i - 1] + (w[i] = read());
    For(i, 2, n) uu = read(), vv = read(), add(uu, vv), add(vv, uu);
    For(i, 1, n) co[i] = read();

    //Divide into blocks and initialize
    getdfn(1, 0);
    block = pow(tot, .66666), num = (n - 1) / block + 1;
    For(i, 1, tot) bel[i] = (i - 1) / block + 1;
    For(j, 1, 17) For(i, 1, n) fa[i][j] = fa[fa[i][j - 1]][j - 1];

    //Sort questions
    For(i, 1, q) {
        ty = read();
        if (ty) {
            ++ qsum, Q[qsum].id = qsum, Q[qsum].u = read(), Q[qsum].v = read(), Q[qsum].tm = usum;
            if (in[Q[qsum].u] > in[Q[qsum].v]) swap(Q[qsum].u, Q[qsum].v);
            Q[qsum].lca = LCA(Q[qsum].u, Q[qsum].v);
            if (Q[qsum].lca != Q[qsum].u) Q[qsum].ty = 1, Q[qsum].l = out[Q[qsum].u], Q[qsum].r = in[Q[qsum].v];
            else Q[qsum].l = in[Q[qsum].u], Q[qsum].r = in[Q[qsum].v];
        } else U[++ usum].pos = read(), U[usum].co = read();
    }
    sort(Q + 1, Q + 1 + qsum);

    //Deal with questions by Mo's algorithm
    static int L = 1, R = 0, cur = 0;
    For(i, 1, qsum)
    {
        while (cur < Q[i].tm) {
            ++ cur;
            changecolor(co[U[cur].pos], U[cur].co, cnt[U[cur].pos] & 1);
        }
        while (cur > Q[i].tm) {
            changecolor(co[U[cur].pos], U[cur].co, cnt[U[cur].pos] & 1);
            -- cur;
        }
        while (L > Q[i].l) {
            -- L;
            if (!cnt[lst[L]]) ins(co[lst[L]]);
            else del(co[lst[L]]);
            ++ cnt[lst[L]];
        }
        while (R < Q[i].r) {
            ++ R;
            if (!cnt[lst[R]]) ins(co[lst[R]]);
            else del(co[lst[R]]);
            ++ cnt[lst[R]];
        }
        while (L < Q[i].l) {
            -- cnt[lst[L]];
            if (!cnt[lst[L]]) del(co[lst[L]]);
            else ins(co[lst[L]]);
            ++ L;
        }
        while (R > Q[i].r) {
            -- cnt[lst[R]];
            if (!cnt[lst[R]]) del(co[lst[R]]);
            else ins(co[lst[R]]);
            -- R;
        }
        ans[Q[i].id] = sum;
        if (Q[i].ty) ans[Q[i].id] += val[co[Q[i].lca]] * w[bkt[co[Q[i].lca]] + 1];
    }
    For(i, 1, qsum) printf("%lld\n", ans[i]);

    return 0;
}
//绿酒初尝人易醉。一枕小窗浓睡。
//    -- 晏殊《清平乐·金风细细》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值