[SPOJ375]QTREE

Posted qaqaq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[SPOJ375]QTREE相关的知识,希望对你有一定的参考价值。

这题可能是QZC论文中最板子的题了。。

我们只需要把边权记在比边深的点上,然后便就是树链剖分的板子了。。

由于题目卡语言。。以下代码需要转C才能过。。

#include <bits/stdc++.h>
using namespace std;

const int N = 10005;

int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N], num;
vector<int> v[N];

struct edgeint x,y,val;;

edge e[N];

inline void dfs1(int u, int f, int d) 
    dep[u] = d,siz[u] = 1,son[u] = 0,fa[u] = f;
    for (int i = 0; i < v[u].size(); i++) 
        int ff = v[u][i];
        if (ff == f) continue;
        dfs1(ff, u, d + 1),
        siz[u] += siz[ff];
        if (siz[son[u]] < siz[ff]) son[u] = ff;
    


inline void dfs2(int u, int tp) 
    top[u] = tp,id[u] = ++num;
    if (son[u]) dfs2(son[u], tp);
    for (int i = 0; i < v[u].size(); i++) 
        int ff = v[u][i];
        if (ff == fa[u] || ff == son[u]) continue;
        dfs2(ff, ff);
    


#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)

struct nodeint l,r,val;;

node tree[4*N];

inline void pushup(int x) tree[x].val = max(tree[lson(x)].val, tree[rson(x)].val);

inline void build(int l,int r,int v)
    tree[v].l=l,tree[v].r=r;
    if(l==r)tree[v].val = val[l];return ;
    int mid=(l+r)>>1;
    build(l,mid,v*2),build(mid+1,r,v*2+1),pushup(v);


inline void update(int o,int v,int val)
    if(tree[o].l==tree[o].r)tree[o].val = val;return ;
    int mid = (tree[o].l+tree[o].r)/2;
    if(v<=mid) update(o*2,v,val);
    else update(o*2+1,v,val);
    pushup(o);


inline int query(int x,int l, int r)
    if (tree[x].l >= l && tree[x].r <= r) return tree[x].val;
    int mid = (tree[x].l + tree[x].r) / 2, ans = 0;
    if (l <= mid) ans = max(ans, query(lson(x),l,r));
    if (r > mid) ans = max(ans, query(rson(x),l,r));
    return ans;


inline int maxw(int u, int v) 
    int tp1 = top[u], tp2 = top[v], ans = 0;
    for (;tp1 != tp2;) 
        if (dep[tp1] < dep[tp2]) swap(tp1, tp2),swap(u, v);
        ans = max(query(1,id[tp1], id[u]), ans),
        u = fa[tp1],tp1 = top[u];
    
    if (u == v) return ans;
    if (dep[u] > dep[v]) swap(u, v);
    ans = max(query(1,id[son[u]], id[v]), ans);
    return ans;


int main()
    int T;
    scanf("%d",&T);
    for(;T;--T)
        int n;
        scanf("%d",&n);
        for(int i=1;i<n;i++)
            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].val),
            v[e[i].x].push_back(e[i].y),v[e[i].y].push_back(e[i].x);
        num = 0,
        dfs1(1,0,1),dfs2(1,1);
        for (int i = 1; i < n; i++) 
            if (dep[e[i].x] < dep[e[i].y]) swap(e[i].x, e[i].y);
            val[id[e[i].x]] = e[i].val;
        
        build(1,num,1);
        char s[200];
        for(;~scanf("%s",&s) && s[0]!='D';)
            int x,y;
            scanf("%d%d",&x,&y);
            if(s[0]=='Q') printf("%d\n",maxw(x,y));
            if (s[0] == 'C') update(1,id[e[x].x],y);
        
        for(int i=1;i<=n;i++) v[i].clear();
    
    return 0;

以上是关于[SPOJ375]QTREE的主要内容,如果未能解决你的问题,请参考以下文章

Spoj375 Qtree--树链剖分

SPOJ 375 QTREE

spoj375 QTREE - Query on a tree

[spoj 375]QTREE - Query on a tree[树链剖分]

SPOJ QTREE 树链剖分

SPOJ QTREE 系列解题报告