zoukankan      html  css  js  c++  java
  • [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 edge{int 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 node{int 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
    ",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;
    }
    
  • 相关阅读:
    JSR 303
    Spring JSR-250注解
    java 内部类
    爬虫
    多线程异步编程示例和实践-Task
    多线程异步编程示例和实践-Thread和ThreadPool
    线程机制、CLR线程池以及应用程序域
    二维码的生成与识别
    Unicode 和 UTF-8 有何区别?
    json转换为自定义类型的集合
  • 原文地址:https://www.cnblogs.com/QAQAQ/p/10992287.html
Copyright © 2011-2022 走看看