http://poj.org/problem?id=3237
树链剖分区间更新模板
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 0x3f3f3f3f
struct node1
{
int u;
int v;
int w;
};
struct node2
{
int v;
int next;
};
struct node3
{
int l;
int r;
int laz;
int maxx;
int minn;
};
node1 pre[10010];
node2 edge[20010];
node3 tree[40010];
int val[10010],first[10010],fa[10010],deep[10010],sum[10010],son[10010],top[10010],mp1[10010],mp2[10010];
int n,num;
void addedge(int u,int v)
{
edge[num].v=v;
edge[num].next=first[u];
first[u]=num++;
}
void dfsI(int cur)
{
int i,v;
sum[cur]=1,son[cur]=-1;
for(i=first[cur];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa[cur])
{
fa[v]=cur,deep[v]=deep[cur]+1;
dfsI(v);
sum[cur]+=sum[v];
if(son[cur]==-1||sum[son[cur]]<sum[v])
{
son[cur]=v;
}
}
}
}
void dfsII(int cur,int tp)
{
int i,v;
num++;
top[cur]=tp,mp1[cur]=num,mp2[num]=cur;
if(son[cur]==-1) return;
dfsII(son[cur],tp);
for(i=first[cur];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa[cur]&&v!=son[cur])
{
dfsII(v,v);
}
}
}
void pushup(int cur)
{
tree[cur].maxx=max(tree[2*cur].maxx,tree[2*cur+1].maxx);
tree[cur].minn=min(tree[2*cur].minn,tree[2*cur+1].minn);
}
void pushdown(int cur)
{
if(tree[cur].laz==-1)
{
tree[2*cur].maxx*=-1,tree[2*cur].minn*=-1;
swap(tree[2*cur].maxx,tree[2*cur].minn);
tree[2*cur].laz*=-1;
tree[2*cur+1].maxx*=-1,tree[2*cur+1].minn*=-1;
swap(tree[2*cur+1].maxx,tree[2*cur+1].minn);
tree[2*cur+1].laz*=-1;
tree[cur].laz=1;
}
}
void build(int l,int r,int cur)
{
int m;
tree[cur].l=l;
tree[cur].r=r;
tree[cur].laz=1;
tree[cur].maxx=-N;
tree[cur].minn=N;
if(l==r)
{
tree[cur].maxx=val[mp2[l]];
tree[cur].minn=val[mp2[l]];
return;
}
m=(l+r)/2;
build(l,m,2*cur);
build(m+1,r,2*cur+1);
pushup(cur);
}
void updateI(int tar,int val,int cur)
{
if(tree[cur].l==tree[cur].r)
{
tree[cur].maxx=val;
tree[cur].minn=val;
return;
}
pushdown(cur);
if(tar<=tree[2*cur].r) updateI(tar,val,2*cur);
else updateI(tar,val,2*cur+1);
pushup(cur);
}
void updateII(int pl,int pr,int cur)
{
if(pl<=tree[cur].l&&tree[cur].r<=pr)
{
tree[cur].maxx*=-1,tree[cur].minn*=-1;
swap(tree[cur].maxx,tree[cur].minn);
tree[cur].laz*=-1;
return;
}
pushdown(cur);
if(pl<=tree[2*cur].r) updateII(pl,pr,2*cur);
if(pr>=tree[2*cur+1].l) updateII(pl,pr,2*cur+1);
pushup(cur);
}
void solveI(int u,int v)
{
while(top[u]!=top[v])
{
if(deep[top[u]]<deep[top[v]]) swap(u,v);
updateII(mp1[top[u]],mp1[u],1);
u=fa[top[u]];
}
if(u==v) return;
if(deep[u]<deep[v]) swap(u,v);
updateII(mp1[son[v]],mp1[u],1);
}
int query(int pl,int pr,int cur)
{
int res;
if(pl<=tree[cur].l&&tree[cur].r<=pr)
{
return tree[cur].maxx;
}
pushdown(cur);
res=-N;
if(pl<=tree[2*cur].r) res=max(res,query(pl,pr,2*cur));
if(pr>=tree[2*cur+1].l) res=max(res,query(pl,pr,2*cur+1));
return res;
}
int solveII(int u,int v)
{
int res;
res=-N;
while(top[u]!=top[v])
{
if(deep[top[u]]<deep[top[v]]) swap(u,v);
res=max(res,query(mp1[top[u]],mp1[u],1));
u=fa[top[u]];
}
if(u==v) return res;
if(deep[u]<deep[v]) swap(u,v);
res=max(res,query(mp1[son[v]],mp1[u],1));
return res;
}
int main()
{
int t,i,u,v,w;
char op[10];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(first,-1,sizeof(first));
num=0;
for(i=1;i<=n-1;i++)
{
scanf("%d%d%d",&pre[i].u,&pre[i].v,&pre[i].w);
addedge(pre[i].u,pre[i].v);
addedge(pre[i].v,pre[i].u);
}
fa[1]=-1,deep[1]=1;
dfsI(1);
num=0;
dfsII(1,1);
val[1]=-N;
for(i=1;i<=n-1;i++)
{
if(deep[pre[i].u]<deep[pre[i].v]) swap(pre[i].u,pre[i].v);
val[pre[i].u]=pre[i].w;
}
build(1,n,1);
while(1)
{
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d%d",&i,&w);
updateI(mp1[pre[i].u],w,1);
}
else if(op[0]=='N')
{
scanf("%d%d",&u,&v);
solveI(u,v);
}
else if(op[0]=='Q')
{
scanf("%d%d",&u,&v);
printf("%d\n",solveII(u,v));
}
else break;
}
}
return 0;
}