树链剖分系列

树链剖分 代码集

QTREE

# include <iostream>
# include <cstdio>
# include <cstring>
# include <vector>
using namespace std;
# define MAXN 10020

struct node{
    int m, L, R;
    node *l, *r;
    node(int a,int b):
        L(a),R(b),l(NULL),r(NULL){}
};

struct List{
    List *next;
    int x, w;
    List(int c,int d,List *p):
        x(c),w(d),next(p){}
} *G[MAXN];

#define tr(p,c)\
    for(List *p=c;p;p=p->next)

typedef int Array[MAXN];

class SegTree{
    public:
        int father;
        void init(int n,int *_c)
        {
            c = _c; 
            root=build(0,n);
        }
        
        int ask(int l,int r)
        {
            L = l; R = r; 
            return query(root);
        }
        
        void change(int pos,int k)
        {
            L = pos; R = k;
            _change(root);
        }
    private:
        node *root;  int *c;
        
        node *build(int L,int R)
        {
            node *x = new node(L,R-1);
            if(L+1==R){
                x -> m = c[L];
                return x;
            }
            x->l = build(L,(L+R)>>1);
            x->r = build((L+R)>>1,R);
            x->m = max(x->l->m, x->r->m);
            return x;
        }
        
        int L, R; 
        int query(node *x)
        {
            if (R<x->L || x->R<L) return 1<<31;
            if (L<=x->L&&x->R<=R) return x->m;
            return max(query(x->l),query(x->r));
        }
        
        void _change(node *cur)
        {
            if(cur->L==cur->R) {
                cur->m = R;
                return;
            }
            
            (cur->l->R>=L)?
                _change(cur->l):
                _change(cur->r);
            cur->m = max(cur->l->m,cur->r->m);
        }
} T[MAXN];

Array blg,dep,fa,son,cur,size,tag,W;
pair<int,int> edge[MAXN];
int css;
#define FILL(c) memset(c,0,sizeof(c))
void INIT_ALL()
{
    FILL(dep); FILL(fa); FILL(son); 
    FILL(edge); FILL(G); css = 0;
}

void build(int x)
{
    int n = 0;
    for(int y=x; y; y=son[y])
        cur[n] = W[y], tag[y] = n++, blg[y]=css;
    T[css].father = fa[x];
    T[css++].init(n,cur);
}

void DFS(int x,int F)
{
    size[x]=1; dep[x]=dep[F]+1;
    tr(i,G[x])if(i->x!=F)
    {
        DFS(i->x,x); size[x]+=size[i->x];
        if(size[son[x]]<size[i->x])
            son[x]=i->x;
        fa[i->x]=x; W[i->x]=i->w;
    }
}
void Query(int x,int y)
{
    int res = 1<<31;
    while (blg[x]!=blg[y])
    {
        if(dep[T[blg[x]].father]<dep[T[blg[y]].father]) swap(x,y);
        res = max(res, T[blg[x]].ask(0,tag[x]));
        x = T[blg[x]].father;
    }
    if (tag[x]>tag[y]) swap(x,y);
    if(x!=y)res=max(res, T[blg[x]].ask(tag[x]+1, tag[y]));
    printf("%d\n",res); 
}

void Change(int index,int V)
{
    int x = edge[index].first;
    int y = edge[index].second;
    if (dep[x]<dep[y]) swap(x,y);
    T[blg[x]].change(tag[x],V);
}

void solve()
{
    int n,x,y,z;
    scanf("%d",&n);
    INIT_ALL();
    for(int i=1; i<n; i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        G[x]=new List(y,z,G[x]);
        G[y]=new List(x,z,G[y]);
        edge[i] = make_pair(x,y);
    }
    
    DFS(1,0); W[1]=1<<31;
    for(int i=1; i<=n; i++)
        if(son[fa[i]]!=i)build(i);

    char o[30];
    while(true)
    {
        scanf("%s",o);
        if(o[0]=='D')break;
        scanf("%d%d",&x,&y);
        o[0]=='Q'?
            Query(x,y):
            Change(x,y);
    }
}

int main()
{
    int Case; scanf("%d",&Case);
    while(Case--)solve();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值