其他分享
首页 > 其他分享> > HDU-3966 Aragorn's Story

HDU-3966 Aragorn's Story

作者:互联网

Aragorn's Story

树链剖分模板

维护两个点之间路径的值,剖分之后用线段树 区间修改 单点查询

注意初始化(不会有人没初始化边,然后 MLE 了半个小时吧)

(为什么是 MLE 而不是 RE!!小编也不太清楚,小编也很想知道,[狗头])

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 5e4 + 10;
int tr[maxn << 2], num[maxn];
int dep[maxn], fa[maxn], hson[maxn], siz[maxn];
int top[maxn], dfn[maxn];
vector<int>gra[maxn];

void dfs1(int now, int cnt)
{
    dep[now] = cnt;
    siz[now] = 1;
    hson[now] = 0;
    for(int i=0; i<gra[now].size(); i++)
    {
        int nex = gra[now][i];
        if(nex == fa[now]) continue;
        fa[nex] = now;
        dfs1(nex, cnt + 1);
        siz[now] += siz[nex];
        if(siz[nex] > siz[hson[now]])
            hson[now] = nex;
    }
}

int tp = 0;
void dfs2(int now, int rt)
{
    top[now] = rt;
    tp++;
    dfn[now] = tp;
    if(hson[now])
    {
        dfs2(hson[now], rt);
        for(int i=0; i<gra[now].size(); i++)
        {
            int nex = gra[now][i];
            if(nex == hson[now] || nex == fa[now]) continue;
            dfs2(nex, nex);
        }
    }
}

void init(int n, int rt = 1)
{
    for(int i=0; i<=n; i++) top[i] = i;
    for(int i=n<<2; i>=0; i--) tr[i] = 0;
    tp = 0;
    fa[rt] = rt;
    dfs1(rt, 1);
    dfs2(rt, rt);
    for(int i=0; i<=n; i++) gra[i].clear();
}

void add(int now, int l, int r, int L, int R, int val)
{
    if(L <= l && r <= R)
    {
        tr[now] += val;
        return;
    }
    int mid = l + r >> 1;
    if(L <= mid)
        add(now << 1, l, mid, L, R, val);
    if(R > mid)
        add(now << 1 | 1, mid + 1, r, L, R, val);
}

int query(int now, int l, int r, int x)
{
    if(l == x && r == x)
        return tr[now];
    int mid = l + r >> 1;
    if(x <= mid)
        return query(now << 1, l, mid, x) + tr[now];
    else
        return query(now << 1 | 1, mid + 1, r, x) + tr[now];
}

void LCA(int n, int a, int b, int c)
{
    while(top[a] != top[b])
    {
        if(dep[top[a]] < dep[top[b]]) swap(a, b);
        add(1, 1, n, dfn[top[a]], dfn[a], c);
        a = fa[top[a]];
    }
    if(dep[a] > dep[b]) swap(a, b);
    add(1, 1, n, dfn[a], dfn[b], c);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m, q;
    while(cin >> n >> m >> q)
    {
        for(int i=1; i<=n; i++) cin >> num[i];
        while(m--)
        {
            int x, y;
            cin >> x >> y;
            gra[x].push_back(y);
            gra[y].push_back(x);
        }
        init(n);
        while(q--)
        {
            char t;
            cin >> t;
            if(t == 'Q')
            {
                int x;
                cin >> x;
                cout << query(1, 1, n, dfn[x]) + num[x] << '\n';
            }
            else
            {
                int a, b, c;
                cin >> a >> b >> c;
                if(t == 'D') c = -c;
                LCA(n, a, b, c);
            }
        }
    }
    return 0;
}

标签:rt,hson,HDU,now,int,cin,tp,3966,Aragorn
来源: https://www.cnblogs.com/dgsvygd/p/16440122.html