poj 3237 樹鏈剖分(區間更新,區間查詢)
http://poj.org/problem?id=3237
Description
You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbered 1 through N − 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions can be one of the following forms:
CHANGE i v |
Change the weight of the ith edge to v |
NEGATE a b |
Negate the weight of every edge on the path from a to b |
QUERY a b |
Find the maximum weight of edges on the path from a to b |
Input
The input contains multiple test cases. The first line of input contains an integer t (t ≤ 20), the number of test cases. Then follow the test cases.
Each test case is preceded by an empty line. The first nonempty line of its contains N (N ≤ 10,000). The next N − 1 lines each contains three integers a, b and c, describing an edge connecting nodes a and bwith
weight c. The edges are numbered in the order they appear in the input. Below them are the instructions, each sticking to the specification above. A lines with the word “DONE
” ends the test case.
Output
For each “QUERY
” instruction, output the result on a separate line.
Sample Input
1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE
Sample Output
1 3
/**
poj 3237 樹鏈剖分(區間更新,區間查詢)
題目大意:給定一棵樹,動態修改:1.對於給定的兩點之間的所有邊權取相反數,2對於給定邊修改值,動態查詢:指定兩點間邊權最大值
解題思路:樹鏈剖分。線上段樹區間維護的時候要維護一個最大值和一個最小值,因為一翻轉二者就會相互裝換
*/
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn=10005;
int fa[maxn],dep[maxn],son[maxn],d[maxn][3],num[maxn],top[maxn],siz[maxn];
int n,z,maxx[maxn*4],minn[maxn*4],col[maxn*4];
int head[maxn],ip;
void init()
{
memset(col,0,sizeof(col));
memset(head,-1,sizeof(head));
ip=0;
}
struct note
{
int v,w,next;
} edge[maxn*4];
void addedge(int u,int v,int w)
{
edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;
}
void dfs(int u,int pre)
{
son[u]=0,siz[u]=1,dep[u]=dep[pre]+1,fa[u]=pre;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(v==pre)continue;
dfs(v,u);
siz[u]+=siz[v];
if(siz[son[u]]<siz[v])
{
son[u]=v;
}
}
///printf("%d son fa dep %d %d %d\n",u,son[u],fa[u],dep[u]);
}
void init_que(int u,int tp)
{
num[u]=++z,top[u]=tp;
if(son[u])
{
init_que(son[u],tp);
}
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(v==fa[u]||v==son[u])continue;
init_que(v,v);
}
// printf("%d top num %d %d\n",u,top[u],num[u]);
}
void push_up(int root)
{
maxx[root]=max(maxx[root<<1],maxx[root<<1|1]);
minn[root]=min(minn[root<<1],minn[root<<1|1]);
}
void push_down(int root)
{
if(col[root])
{
maxx[root<<1|1]=-maxx[root<<1|1];
minn[root<<1|1]=-minn[root<<1|1];
swap(maxx[root<<1|1],minn[root<<1|1]);
maxx[root<<1]=-maxx[root<<1];
minn[root<<1]=-minn[root<<1];
swap(maxx[root<<1],minn[root<<1]);
col[root<<1]^=1;
col[root<<1|1]^=1;
col[root]=0;
}
}
void update(int root,int l,int r,int loc,int z)
{
if(l>loc||r<loc)return;
if(l==r)
{
col[root]=0;
maxx[root]=z;
minn[root]=z;
return;
}
push_down(root);
int mid=(l+r)>>1;
update(root<<1,l,mid,loc,z);
update(root<<1|1,mid+1,r,loc,z);
push_up(root);
}
void update1(int root, int l,int r,int x,int y)
{
if(l>y||r<x)return;
if(x<=l&&r<=y)
{
maxx[root]=-maxx[root];
minn[root]=-minn[root];
swap(maxx[root],minn[root]);
col[root]^=1;
return;
}
push_down(root);
int mid=(l+r)>>1;
update1(root<<1,l,mid,x,y);
update1(root<<1|1,mid+1,r,x,y);
push_up(root);
}
int query(int root ,int l,int r,int x,int y)
{
if(l>y||r<x)return -0x3f3f3f3f;
if(x<=l&&r<=y)
{
return maxx[root];
}
push_down(root);
int mid=(l+r)>>1;
return max(query(root<<1,l,mid,x,y),query(root<<1|1,mid+1,r,x,y));
}
int main()
{
///freopen("data.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
for(int i=1; i<n; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
d[i][0]=u,d[i][1]=v,d[i][2]=w;
addedge(u,v,w);
addedge(v,u,w);
}
int root=(n+1)>>1;
z=0,siz[0]=0,dep[0]=0;
dfs(root,0);
init_que(root,root);
for(int i=1; i<n; i++)
{
if(dep[d[i][0]]>dep[d[i][1]])
{
swap(d[i][0],d[i][1]);
}
update(1,1,z,num[d[i][1]],d[i][2]);
}
while(1)
{
char s[10];
scanf("%s",s);
if(s[0]=='D')break;
int x,y;
scanf("%d%d",&x,&y);
if(s[0]=='C')
{
update(1,1,z,num[d[x][1]],y);
}
else if(s[0]=='N')
{
int f1=top[x],f2=top[y];
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(f1,f2);
swap(x,y);
}
if(x!=y)
{
update1(1,1,z,num[f1],num[x]);
x=fa[f1],f1=top[x];
}
}
if(x!=y)
{
if(dep[x]>dep[y])
{
swap(x,y);
}
update1(1,1,z,num[son[x]],num[y]);
}
}
else
{
int f1=top[x],f2=top[y];
int sum=-0x3f3f3f3f;
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(f1,f2);
swap(x,y);
}
sum=max(sum,query(1,1,z,num[f1],num[x]));
x=fa[f1],f1=top[x];
}
if(x!=y)
{
if(dep[x]>dep[y])
{
swap(x,y);
}
sum=max(query(1,1,z,num[son[x]],num[y]),sum);
}
printf("%d\n",sum);
}
}
}
return 0;
}
相關文章
- HYSBZ 2243 樹鏈剖分(區間更新,區間查詢)較難
- spoj375 樹鏈剖分(單點更新,區間查詢)
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- TZOJ 8472 : Tree (重鏈剖分+線段樹) POJ 3237
- 【樹狀陣列 區間更新區間查詢】code陣列
- POJ 1195-Mobile phones(二維樹狀陣列-區間更新區間查詢)陣列
- 樹狀陣列單點更新和區間查詢陣列
- 樹狀陣列的區間查詢與區間修改陣列
- 芻議線段樹 2 (區間修改,區間查詢)
- POJ 3468 A Simple Problem with Integers (線段樹 區間更新)
- POJ 2777-Count Color(線段樹-區間染色查詢)
- POJ 3468-A Simple Problem with Integers(區間更新線段樹)
- poj 3468 區間更新 整個區間加一個數和區間求和操作
- HDU 1556【區間更新+單點查詢 樹狀陣列】陣列
- 區間更新+差分
- 日期區間查詢
- 樹鏈剖分
- 1082 線段樹練習 3 區間查詢與區間修改
- POJ 2528 Mayor's posters (線段樹 區間更新+離散化)
- POJ 2528 Mayor's posters (線段樹區間更新 + 離散化)
- (poj3468)A Simple Problem with Integers(區間更新)
- POJ 2155-Matrix(二維樹狀陣列-區間修改 單點查詢)陣列
- HDU1698 Just a Hook【線段樹基礎:區間修改+區間查詢】Hook
- [OI] 樹鏈剖分
- POJ 3468 A Simple Problem with Integers(線段樹區間操作)
- 樹鏈剖分總結
- 淺談樹鏈剖分
- hihocoder 1078 線段樹的區間修改 (線段樹 區間更新 模板)
- LintCode 二叉查詢樹中搜尋區間
- 變化的區間樹狀陣列,單點查詢陣列
- 線段樹(1)建樹、單點修改、單點查詢、區間查詢和例題
- POJ 3468 A Simple Problem with Integers (線段樹 區間共加)
- 關於區間操作查詢(字首和與差分)+樹狀陣列基礎陣列
- Laravel MongoDB 時間區間查詢的問題LaravelMongoDB
- 【筆記/模板】樹鏈剖分筆記
- #8. 「模板」樹鏈剖分
- POJ2955 Brackets (區間DP)Racket
- 樹鏈剖分學習筆記筆記