BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分裸题)

来源:互联网 发布:iphone好玩的软件 编辑:程序博客网 时间:2024/04/29 14:57

题意就是一棵树,每个点上有个权值,有3个操作,一个要求u到v路径上的最大权值,一个是求u到v路径上权值和,还有一个是修改u的权值为x。

树链剖分入门题。

树链剖分有个很重要的定理就是,保证从根节点到某个节点u,经过的轻边和重路径不会超过logn,我当时看书的时候把重路径看成重边,然后觉得如果真是这样那还用得到什么线段树。。。其实证明相当简单,关键点就在于,每当到走到轻边,树的个数至少减少一半(不然他就是重边了),然后轻边是把重路径分开的唯一条件,所以重路径也不会超过logn条。

这题是按点建的树。

简单写下算法流程吧。

dfs1:算出num(该节点为根的节点总数),depth(深度),f(父亲节点),son(重边的儿子)

dfs2:算出top(如果是轻边,top等于自己,反之,等于那条重路径的根节点),tree(节点在线段树的位置,树链剖分的目的就是把重边放在线段树连续的地方),pos(线段树的位置上是哪个节点,可以看出是前面的tree数组的逆)。

然后建线段树求区间最大和区间和。

求区间最大及区间和:刘汝佳的树上说的是到LCA(x,y),这样的确可以,但是我参考别人的代码,他们用的办法则比较简单粗暴,对比2个点的top值的深度,先把深的往上爬,直到最后top值相同了,这意味着要么在一个重路径上,要么在轻边对应的一个点上。最后再查一下就OK了。

修改的话直接在线段树里改就可以了。

AC代码:

//#pragma comment(linker, "/STACK:102400000,102400000")#include<cstdio>#include<ctype.h>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<cstdlib>#include<stack>#include<queue>#include<set>#include<map>#include<cmath>#include<ctime>#include<string.h>#include<string>#include<sstream>#include<bitset>using namespace std;#define ll __int64#define ull unsigned long long#define eps 1e-8#define NMAX 1000000000#define MOD 1000000#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define PI acos(-1)template<class T>inline void scan_d(T &ret){    char c;    int flag = 0;    ret=0;    while(((c=getchar())<'0'||c>'9')&&c!='-');    if(c == '-')    {        flag = 1;        c = getchar();    }    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();    if(flag) ret = -ret;}template<class T> inline T Max(T a, T b){ return a > b ? a : b; }template<class T> inline T Min(T a, T b){ return a < b ? a : b; }const int maxn = 30000+10;struct Edge{    int v,next;}e[maxn*2];int head[maxn],nct;int num[maxn],f[maxn],son[maxn],depth[maxn];int top[maxn],pos[maxn],tree[maxn],tot;int data[maxn];void add_edge(int u, int v){    e[nct].v = v; e[nct].next = head[u];    head[u] = nct++;    e[nct].v = u; e[nct].next = head[v];    head[v] = nct++;} void dfs1(int u, int fa, int dep){    num[u] = 1; depth[u] = dep; f[u] = fa;    for(int i = head[u]; i != -1; i = e[i].next)    {        int v = e[i].v;        if(v == fa) continue;        dfs1(v,u,dep+1);        num[u] += num[v];        if(!son[u] || num[v] > num[son[u]]) son[u] = v;    }} void dfs2(int u, int tp){    top[u] = tp; tree[u] = ++tot;    pos[tot] = u;    if(!son[u]) return;    dfs2(son[u],tp);    for(int i = head[u]; i != -1; i = e[i].next)    {        int v = e[i].v;        if(v == f[u] || v == son[u]) continue;        dfs2(v,v);    }} struct SegTree{    int mx,sum;};SegTree T[maxn<<2];void pushup(int rt){    T[rt].mx = Max(T[rt<<1].mx, T[rt<<1|1].mx);    T[rt].sum = T[rt<<1].sum+T[rt<<1|1].sum;} void build(int l, int r, int rt){    if(l == r)    {        T[rt].mx = T[rt].sum = data[pos[l]];        return;    }    int mid = (l+r)>>1;    build(lson);    build(rson);    pushup(rt);} void update(int L, int k, int l, int r, int rt){    if(l == r)    {        T[rt].mx = T[rt].sum = k;        return;    }    int mid = (l+r)>>1;    if(L <= mid) update(L, k, lson);    else update(L,k,rson);    pushup(rt);} int query1(int L, int R, int l, int r, int rt){    if(L <= l && R >= r)        return T[rt].mx;    int mid = (l+r)>>1,ret = -NMAX;    if(L <= mid) ret = Max(ret,query1(L,R,lson));    if(R > mid) ret = Max(ret,query1(L,R,rson));    return ret;} int query2(int L, int R, int l, int r, int rt){    if(L <= l && R >= r) return T[rt].sum;    int mid = (l+r)>>1,ret = 0;    if(L <= mid) ret += query2(L, R, lson);    if(R > mid) ret += query2(L, R, rson);    return ret;} int ask_max(int x, int y){    int t1 = top[x], t2 = top[y], tmp, ans = -NMAX;    while(t1 != t2)    {        if(depth[t1] < depth[t2])        {            tmp = t1; t1 = t2; t2 = tmp;            tmp = x; x = y; y = tmp;        }        ans = Max(ans, query1(tree[t1],tree[x],1,tot,1));        x = f[t1]; t1 = top[x];    }    ans = Max(ans,depth[x] > depth[y] ? query1(tree[y],tree[x],1,tot,1) : query1(tree[x],tree[y],1,tot,1));    return ans;} int ask_sum(int x, int y){    int t1 = top[x], t2 = top[y], tmp, ans = 0;    while(t1 != t2)    {        if(depth[t1] < depth[t2])        {            tmp = t1; t1 = t2; t2 = tmp;            tmp = x; x = y; y = tmp;        }        ans += query2(tree[t1],tree[x],1,tot,1);        x = f[t1]; t1 = top[x];    }    ans += depth[x] > depth[y] ? query2(tree[y],tree[x],1,tot,1) : query2(tree[x],tree[y],1,tot,1);    return ans;} int main(){#ifdef GLQ    freopen("input.txt","r",stdin);//    freopen("o.txt","w",stdout);#endif    int n,q;    char s[20];    while(~scanf("%d",&n))    {        memset(head,-1,sizeof(head));        memset(son,0,sizeof(son));        nct = 0;        tot = 0;        for(int i = 1; i < n; i++)        {            int u,v;            scanf("%d%d",&u,&v);            add_edge(u,v);        }        for(int i = 1; i <= n; i++)            scanf("%d",&data[i]);        dfs1(1,1,1);        dfs2(1,1);        build(1,tot,1);        scanf("%d",&q);        while(q--)        {            int a,b;            scanf("%s%d%d",s,&a,&b);            if(s[0] == 'C') update(tree[a],b,1,tot,1);            else if(s[1] == 'M') printf("%d\n",ask_max(a,b));            else printf("%d\n",ask_sum(a,b));        }    }    return 0;}


0 0
原创粉丝点击