ZJOI2008树的统计bzoj1036

太弱了太弱了这种入门题居然因为少打了等于号用别人的程序来替换调试,太弱了太弱了

/**************************************************************
    Problem: 1036
    User: BPM136
    Language: C++
    Result: Accepted
    Time:2488 ms
    Memory:6476 kb
****************************************************************/
 
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#define LL long long
#define inf 100000007
#define N 30005
using namespace std;
struct Edge
{
        int y,next;
}edge[N*2];
struct Node
{
        int l,r,sum,ma;
}stree[N*4];
int son[N],fa[N],top[N],dep[N],num[N],fp[N],p[N],s[N],ls[N],tot,pos;
 
void add(int u,int v)
{
        edge[tot].y=v;edge[tot].next=ls[u];ls[u]=tot++;
}
 
void dfs(int u,int pre,int d)
{
        dep[u]=d;
        fa[u]=pre;
        num[u]=1;
        for (int i=ls[u];i!=-1;i=edge[i].next)
        {
                int v=edge[i].y;
                if (v!=pre)
                {
                        dfs(v,u,d+1);
                        num[u]+=num[v];
                        if (son[u]==-1||num[v]>num[son[u]])
                                son[u]=v;
                }
        }
}
 
void getpos(int u,int ad)
{
        top[u]=ad;
        p[u]=pos++;
        fp[p[u]]=u;
        if (son[u]==-1) return;
        getpos(son[u],ad);
        for (int i=ls[u];i!=-1;i=edge[i].next)
        {
                int v=edge[i].y;
                if (v!=son[u]&&v!=fa[u])
                        getpos(v,v);
        }
}
 
void pushup(int i)
{
        stree[i].sum=stree[i<<1].sum+stree[(i<<1)|1].sum;
        stree[i].ma=max(stree[i<<1].ma,stree[(i<<1)|1].ma);
}
 
void build(int x,int l,int r)
{
        stree[x].l=l;stree[x].r=r;
        if (l==r)
        {
                stree[x].ma=stree[x].sum=s[fp[l]];
                return;
        }
        int mid=(l+r)/2;
        build(x<<1,l,mid);build((x<<1)|1,mid+1,r);
        pushup(x);
}
 
void up(int i,int k,int val)
{
        if(stree[i].l==k&&stree[i].r==k)
        {
                stree[i].sum=stree[i].ma=val;
                return;
        }
        int mid=(stree[i].l+stree[i].r)/2;
        if (k<=mid)up(i<<1,k,val);else up((i<<1)|1,k,val);
        pushup(i);
}
 
int querymax(int x,int l,int r)
{
        if (stree[x].l==l&&stree[x].r==r)
                return stree[x].ma;
        int mid=(stree[x].l+stree[x].r)/2;
        if (r<=mid) return querymax(x<<1,l,r);
        else if (l>mid) return querymax((x<<1)|1,l,r);
        else return max(querymax(x<<1,l,mid),querymax((x<<1)|1,mid+1,r));
}
 
int querysum(int x,int l,int r)
{
        if (stree[x].l==l&&stree[x].r==r)
                return stree[x].sum;
        int mid=(stree[x].l+stree[x].r)/2;
        if (r<=mid) return querysum(x<<1,l,r);
        else if (l>mid) return querysum((x<<1)|1,l,r);
        else return (querysum(x<<1,l,mid)+querysum((x<<1)|1,mid+1,r));
}
 
int findmax(int u,int v)
{
        int f1=top[u],f2=top[v],tmp=-1000000000;
        while(f1!=f2)
        {
                if(dep[f1]<dep[f2]){swap(f1,f2);swap(u,v);}
                tmp=max(tmp,querymax(1,p[f1],p[u]));
                u=fa[f1];f1=top[u];
        }
        if(dep[u]>dep[v]) swap(u,v);
        return max(tmp,querymax(1,p[u],p[v]));
}
 
int findsum(int u,int v)
{
        int f1=top[u],f2=top[v];
        int tmp=0;
        while(f1!=f2)
        {
                if(dep[f1]<dep[f2]) {swap(f1,f2);swap(u,v);}
                tmp+=querysum(1,p[f1],p[u]);
                u=fa[f1];f1=top[u];
        }
        if(dep[u]>dep[v]) swap(u,v);
        return tmp+querysum(1,p[u],p[v]);
}
 
int main()
{
        //freopen("data.in","r",stdin);freopen("out.txt","w",stdout);
        int q,u,v,n;
        char op[20];
        while(scanf("%d",&n)==1)
        {
                tot=0;
                memset(ls,-1,sizeof(ls));
                pos=0;
                memset(son,-1,sizeof(son));
                for(int i=1;i<n;i++)
                {
                        scanf("%d%d",&u,&v);
                        add(u,v);add(v,u);
                }
                for(int i=1;i<=n;i++) scanf("%d",&s[i]);
                dfs(1,0,0);
                getpos(1,1);
                build(1,0,pos-1);
                scanf("%d",&q);
                while(q--)
                {
                        scanf("%s%d%d",op,&u,&v);
                        if(op[0]=='C') up(1,p[u],v);
                        else if(strcmp(op,"QMAX")==0) printf("%d\n",findmax(u,v));
                        else printf("%d\n",findsum(u,v));
                }
        }
        return 0;
}


你可能感兴趣的:(树链剖分)