QTREE - Query on a tree
no tags
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3…N-1.
We will ask you to perfrom some instructions of the following form:
CHANGE i ti : change the cost of the i-th edge to ti
or
QUERY a b : ask for the maximum edge cost on the path from node a to node b
Input
The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.
For each test case:
In the first line there is an integer N (N <= 10000),
In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 1000000),
The next lines contain instructions “CHANGE i ti” or “QUERY a b”,
The end of each test case is signified by the string “DONE”.
There is one blank line between successive tests.
Output
For each “QUERY” operation, write one integer representing its result.
Example
Input:
1
3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE
Output:
1
3
Submit solution!
这道题只是把点权改成了边权,在做的时候只是维护一下这个点的上一条边和重链顶端的下一条边就好了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=10100;
int n,point[N],next[N*10],tot=1,siz[N]={0},fa[N][20]={0},son[N]={0};
int deep[N]={0},num=0,pos[N]={0},belong[N]={0},r[N]={0},map[N]={0},tr[N*4];
struct S{
int st,en,va;
}aa[N*10];
bool use[N];
inline void add(int x,int y,int z)
{
tot+=1;next[tot]=point[x];point[x]=tot;
aa[tot].st=x;aa[tot].en=y;aa[tot].va=z;
tot+=1;next[tot]=point[y];point[y]=tot;
aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;
}
inline void dfs_1(int x)
{
int i;
siz[x]=1;
use[x]=false;
for(i=1;i<=14;++i){
if(deep[x]<(1<<i)) break;
fa[x][i]=fa[fa[x][i-1]][i-1];
}
for(i=point[x];i;i=next[i])
if(use[aa[i].en]){
fa[aa[i].en][0]=x;
deep[aa[i].en]=deep[x]+1;
dfs_1(aa[i].en);
siz[x]+=siz[aa[i].en];
}
}
inline void dfs_2(int x,int y)
{
int i,k=0;
num+=1;
belong[x]=y;
for(i=point[x];i;i=next[i]){
if(deep[aa[i].st]>deep[aa[i].en]) map[aa[i].st]=i>>1;
else map[aa[i].en]=i>>1;
}
for(i=point[x];i;i=next[i])
if(deep[x]<deep[aa[i].en]&&siz[k]<siz[aa[i].en])
k=aa[i].en,son[x]=i>>1,pos[i>>1]=num;
if(k==0) {num-=1; return ;}
dfs_2(k,y);
for(i=point[x];i;i=next[i])
if(deep[x]<deep[aa[i].en]&&k!=aa[i].en)
num+=1,pos[i>>1]=num,dfs_2(aa[i].en,aa[i].en);
}
inline int LCA(int x,int y)
{
int i;
if(deep[x]<deep[y])swap(x,y);
int t=deep[x]-deep[y];
for(i=0;i<=14;++i)
if(t&(1<<i))x=fa[x][i];
for(i=14;i>=0;--i)
if(fa[x][i]!=fa[y][i])
{x=fa[x][i];y=fa[y][i];}
if(x==y)return x;
else return fa[x][0];
}
#define mid (l+r)/2
#define L k<<1,l,mid
#define R k<<1|1,mid+1,r
inline void insert(int k,int l,int r,int x,int y)
{
if(l==r&&l==x){
tr[k]=y;
return ;
}
if(x<=mid) insert(L,x,y);
else insert(R,x,y);
tr[k]=max(tr[k<<1],tr[k<<1|1]);
}
inline int qurry(int k,int l,int r,int x,int y)
{
int maxn=-210000000;
if(x<=l&&y>=r) return tr[k];
if(x<=mid) maxn=max(maxn,qurry(L,x,y));
if(y>mid) maxn=max(maxn,qurry(R,x,y));
return maxn;
}
inline int ask(int x,int y)
{
int maxn=-210000000,z;
if(map[x]==0) return maxn;
if(belong[x]==x&&x!=y){
maxn=max(maxn,r[map[x]]);
x=fa[x][0];
}
if(map[x]==0) return maxn;
while(belong[x]!=belong[y]){
maxn=max(maxn,qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]]));
z=fa[belong[x]][0];
maxn=max(maxn,qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]));
x=z;
}
if(map[x]==0) return maxn;
maxn=max(maxn,qurry(1,1,n-1,pos[son[y]],pos[map[x]]));
return maxn;
}
int main()
{
int i,j,x,y,z;
char ch[10];
memset(use,1,sizeof(use));
scanf("%d",&n);
for(i=1;i<n;++i){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);r[i]=z;
}
dfs_1(1);
dfs_2(1,1);
for(i=1;i<n;++i) insert(1,1,n-1,pos[i],r[i]);
while(scanf("%*c%s",&ch)){
if(ch[0]=='D') break;
scanf("%d%d",&x,&y);
if(ch[0]=='C') insert(1,1,n-1,pos[x],y),r[x]=y;
else{
int lca=LCA(x,y);
printf("%d\n",max(ask(x,lca),ask(y,lca)));
}
}
}
Tree
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 5672 Accepted: 1560
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 b with 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
思路:这个题只是加个标记就好了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10100;
int n,point[N],next[N*10],tot=1,siz[N]={0},fa[N][20]={0},son[N]={0};
int deep[N]={0},num=0,pos[N]={0},belong[N]={0},r[N]={0},map[N]={0},de[N*4]={0};
struct S{
int st,en,va;
}aa[N*10];
struct C{
int maxn,minn,lazy;
}tr[N*4];
bool use[N];
inline void add(int x,int y,int z)
{
tot+=1;next[tot]=point[x];point[x]=tot;
aa[tot].st=x;aa[tot].en=y;aa[tot].va=z;
tot+=1;next[tot]=point[y];point[y]=tot;
aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;
}
inline void dfs_1(int x)
{
int i;
siz[x]=1;
use[x]=false;
for(i=1;i<=14;++i){
if(deep[x]<(1<<i)) break;
fa[x][i]=fa[fa[x][i-1]][i-1];
}
for(i=point[x];i;i=next[i])
if(use[aa[i].en]){
fa[aa[i].en][0]=x;
deep[aa[i].en]=deep[x]+1;
dfs_1(aa[i].en);
siz[x]+=siz[aa[i].en];
}
}
inline void dfs_2(int x,int y)
{
int i,k=0;
num+=1;
belong[x]=y;
for(i=point[x];i;i=next[i]){
if(deep[aa[i].st]>deep[aa[i].en]) map[aa[i].st]=i>>1;
else map[aa[i].en]=i>>1;
}
for(i=point[x];i;i=next[i])
if(deep[x]<deep[aa[i].en]&&siz[k]<siz[aa[i].en])
k=aa[i].en,son[x]=i>>1,pos[i>>1]=num;
if(k==0) {num-=1; return ;}
dfs_2(k,y);
for(i=point[x];i;i=next[i])
if(deep[x]<deep[aa[i].en]&&k!=aa[i].en)
num+=1,pos[i>>1]=num,dfs_2(aa[i].en,aa[i].en);
}
inline int LCA(int x,int y)
{
int i;
if(deep[x]<deep[y])swap(x,y);
int t=deep[x]-deep[y];
for(i=0;i<=14;++i)
if(t&(1<<i))x=fa[x][i];
for(i=14;i>=0;--i)
if(fa[x][i]!=fa[y][i])
{x=fa[x][i];y=fa[y][i];}
if(x==y)return x;
else return fa[x][0];
}
#define mid (l+r)/2
#define L k<<1,l,mid
#define R k<<1|1,mid+1,r
inline void solve(int k)
{
swap(tr[k].maxn,tr[k].minn);
tr[k].maxn=-tr[k].maxn;
tr[k].minn=-tr[k].minn;
}
inline void pushdown(int k)
{
if(de[k]!=0){
if((k<<1)<N*4){
solve(k<<1); de[k<<1]=!de[k<<1];
solve(k<<1|1); de[k<<1|1]=!de[k<<1|1];
}
de[k]=0;
}
}
inline void insert(int k,int l,int r,int x,int y)
{
pushdown(k);
if(l==r&&l==x){
tr[k].maxn=tr[k].minn=y;
return ;
}
if(x<=mid) insert(L,x,y);
else insert(R,x,y);
tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
}
inline int qurry(int k,int l,int r,int x,int y)
{
int maxn=-210000000;
pushdown(k);
if(x<=l&&y>=r) return tr[k].maxn;
if(x<=mid) maxn=max(maxn,qurry(L,x,y));
if(y>mid) maxn=max(maxn,qurry(R,x,y));
return maxn;
}
inline int ask(int x,int y)
{
int maxn=-210000000,z;
if(map[x]==0) return maxn;
if(belong[x]==x&&x!=y){
maxn=max(maxn,qurry(1,1,n-1,pos[map[x]],pos[map[x]]));
x=fa[x][0];
}
if(map[x]==0) return maxn;
while(belong[x]!=belong[y]){
maxn=max(maxn,qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]]));
z=fa[belong[x]][0];
maxn=max(maxn,qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]));
x=z;
}
if(map[x]==0) return maxn;
maxn=max(maxn,qurry(1,1,n-1,pos[son[y]],pos[map[x]]));
return maxn;
}
inline void change(int k,int l,int r,int x,int y)
{
pushdown(k);
if(x<=l&&y>=r){
solve(k); de[k]=1;
return ;
}
if(x<=mid) change(L,x,y);
if(y>mid) change(R,x,y);
tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
}
inline void negate_(int x,int y)
{
int z;
if(map[x]==0) return ;
if(belong[x]==x&&x!=y){
change(1,1,n-1,pos[map[x]],pos[map[x]]);
x=fa[x][0];
}
if(map[x]==0) return ;
while(belong[x]!=belong[y]){
change(1,1,n-1,pos[son[belong[x]]],pos[map[x]]);
z=fa[belong[x]][0];
change(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]);
x=z;
}
if(map[x]==0) return ;
change(1,1,n-1,pos[son[y]],pos[map[x]]);
return ;
}
int main()
{
int i,j,x,y,z;
char ch[10];
memset(use,1,sizeof(use));
scanf("%d",&n);
for(i=1;i<n;++i){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);r[i]=z;
}
dfs_1(1);
dfs_2(1,1);
for(i=1;i<n;++i) insert(1,1,n-1,pos[i],r[i]);
while(scanf("%*c%s",&ch)){
if(ch[0]=='D') break;
scanf("%d%d",&x,&y);
if(ch[0]=='C') insert(1,1,n-1,pos[x],y);
if(ch[0]=='Q'){
int lca=LCA(x,y);
printf("%d\n",max(ask(x,lca),ask(y,lca)));
}
if(ch[0]=='N'){
int lca=LCA(x,y);
negate_(x,lca);negate_(y,lca);
}
}
}
[bzoj]2157: 旅游
Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 420 Solved: 248
[Submit][Status][Discuss]
Description
Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。
Input
输入的第一行包含一个整数N,表示T 城中的景点个数。景点编号为 0…N − 1。接下来N − 1 行,每行三个整数u、v 和w,表示有一条u 到v,使 Ray 愉悦度增加w 的桥。桥的编号为1…N − 1。|w| <= 1000。输入的第N + 1 行包含一个整数M,表示Ray 的操作数目。接下来有M 行,每行描述了一个操作,操作有如下五种形式: C i w,表示Ray 对于经过第i 座桥的愉悦度变成了w。 N u v,表示Ray 对于经过景点u 到v 的路径上的每一座桥的愉悦度都变成原来的相反数。 SUM u v,表示询问从景点u 到v 所获得的总愉悦度。 MAX u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最大愉悦度。 MIN u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最小愉悦度。测试数据保证,任意时刻,Ray 对于经过每一座桥的愉悦度的绝对值小于等于1000。
Output
对于每一个询问(操作S、MAX 和MIN),输出答案。
思路:跟上面的题一样。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100100;
int n,m,point[N],next[N*10],tot=1,siz[N]={0},fa[N][20]={0},son[N]={0};
int deep[N]={0},num=0,pos[N]={0},belong[N]={0},r[N]={0},map[N]={0},de[N*4]={0};
struct S{
int st,en,va;
}aa[N*10];
struct C{
int maxn,minn,sum;
}tr[N*4];
bool use[N];
inline void add(int x,int y,int z)
{
tot+=1;next[tot]=point[x];point[x]=tot;
aa[tot].st=x;aa[tot].en=y;aa[tot].va=z;
tot+=1;next[tot]=point[y];point[y]=tot;
aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;
}
inline void dfs_1(int x)
{
int i;
siz[x]=1;
use[x]=false;
for(i=1;i<=14;++i){
if(deep[x]<(1<<i)) break;
fa[x][i]=fa[fa[x][i-1]][i-1];
}
for(i=point[x];i;i=next[i])
if(use[aa[i].en]){
fa[aa[i].en][0]=x;
deep[aa[i].en]=deep[x]+1;
dfs_1(aa[i].en);
siz[x]+=siz[aa[i].en];
}
}
inline void dfs_2(int x,int y)
{
int i,k=0;
num+=1;
belong[x]=y;
for(i=point[x];i;i=next[i]){
if(deep[aa[i].st]>deep[aa[i].en]) map[aa[i].st]=i>>1;
else map[aa[i].en]=i>>1;
}
for(i=point[x];i;i=next[i])
if(deep[x]<deep[aa[i].en]&&siz[k]<siz[aa[i].en])
k=aa[i].en,son[x]=i>>1,pos[i>>1]=num;
if(k==0) {num-=1; return ;}
dfs_2(k,y);
for(i=point[x];i;i=next[i])
if(deep[x]<deep[aa[i].en]&&k!=aa[i].en)
num+=1,pos[i>>1]=num,dfs_2(aa[i].en,aa[i].en);
}
inline int LCA(int x,int y)
{
int i;
if(deep[x]<deep[y])swap(x,y);
int t=deep[x]-deep[y];
for(i=0;i<=14;++i)
if(t&(1<<i))x=fa[x][i];
for(i=14;i>=0;--i)
if(fa[x][i]!=fa[y][i])
{x=fa[x][i];y=fa[y][i];}
if(x==y)return x;
else return fa[x][0];
}
#define mid (l+r)/2
#define L k<<1,l,mid
#define R k<<1|1,mid+1,r
inline void solve(int k)
{
swap(tr[k].maxn,tr[k].minn);
tr[k].maxn=-tr[k].maxn;
tr[k].minn=-tr[k].minn;
tr[k].sum=-tr[k].sum;
}
inline void pushdown(int k)
{
if(de[k]!=0){
if((k<<1)<N*4){
solve(k<<1); de[k<<1]=!de[k<<1];
solve(k<<1|1); de[k<<1|1]=!de[k<<1|1];
}
de[k]=0;
}
}
inline void insert(int k,int l,int r,int x,int y)
{
pushdown(k);
if(l==r&&l==x){
tr[k].maxn=tr[k].minn=tr[k].sum=y;
return ;
}
if(x<=mid) insert(L,x,y);
else insert(R,x,y);
tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
}
inline int qurry(int k,int l,int r,int x,int y,int kind)
{
int maxn=-210000000,minn=210000000,sum=0;
pushdown(k);
if(x<=l&&y>=r){
if(kind==0) return tr[k].maxn;
if(kind==1) return tr[k].minn;
if(kind==2) return tr[k].sum;
}
if(x<=mid){
if(kind==0) maxn=max(maxn,qurry(L,x,y,kind));
if(kind==1) minn=min(minn,qurry(L,x,y,kind));
if(kind==2) sum+=qurry(L,x,y,kind);
}
if(y>mid){
if(kind==0) maxn=max(maxn,qurry(R,x,y,kind));
if(kind==1) minn=min(minn,qurry(R,x,y,kind));
if(kind==2) sum+=qurry(R,x,y,kind);
}
if(kind==0) return maxn;
if(kind==1) return minn;
if(kind==2) return sum;
}
inline int work(int x,int y,int kind)
{
int maxn=-210000000,z,minn=210000000,sum=0;
if(map[x]==0){if(kind==0) return maxn;if(kind==1) return minn;if(kind==2) return sum;}
if(belong[x]==x&&x!=y){
if(kind==0) maxn=max(maxn,qurry(1,1,n-1,pos[map[x]],pos[map[x]],kind));
if(kind==1) minn=min(minn,qurry(1,1,n-1,pos[map[x]],pos[map[x]],kind));
if(kind==2) sum+=qurry(1,1,n-1,pos[map[x]],pos[map[x]],kind);
x=fa[x][0];
}
if(map[x]==0){if(kind==0) return maxn;if(kind==1) return minn;if(kind==2) return sum;}
while(belong[x]!=belong[y]){
if(kind==0) maxn=max(maxn,qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]],kind));
if(kind==1) minn=min(minn,qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]],kind));
if(kind==2) sum+=qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]],kind);
z=fa[belong[x]][0];
if(kind==0) maxn=max(maxn,qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]],kind));
if(kind==1) minn=min(minn,qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]],kind));
if(kind==2) sum+=qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]],kind);
x=z;
}
if(map[x]==0){if(kind==0) return maxn;if(kind==1) return minn;if(kind==2) return sum;}
if(kind==0) maxn=max(maxn,qurry(1,1,n-1,pos[son[y]],pos[map[x]],kind));
if(kind==1) minn=min(minn,qurry(1,1,n-1,pos[son[y]],pos[map[x]],kind));
if(kind==2) sum+=qurry(1,1,n-1,pos[son[y]],pos[map[x]],kind);
if(kind==0) return maxn;if(kind==1) return minn;if(kind==2) return sum;
}
inline void change(int k,int l,int r,int x,int y)
{
pushdown(k);
if(x<=l&&y>=r){
solve(k); de[k]=1;
return ;
}
if(x<=mid) change(L,x,y);
if(y>mid) change(R,x,y);
tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
}
inline void negate_(int x,int y)
{
int z;
if(map[x]==0) return ;
if(belong[x]==x&&x!=y){
change(1,1,n-1,pos[map[x]],pos[map[x]]);
x=fa[x][0];
}
if(map[x]==0) return ;
while(belong[x]!=belong[y]){
change(1,1,n-1,pos[son[belong[x]]],pos[map[x]]);
z=fa[belong[x]][0];
change(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]);
x=z;
}
if(map[x]==0) return ;
change(1,1,n-1,pos[son[y]],pos[map[x]]);
return ;
}
int main()
{
int i,j,x,y,z;
char ch[10];
memset(use,1,sizeof(use));
scanf("%d",&n);
for(i=1;i<n;++i){
scanf("%d%d%d",&x,&y,&z);
x+=1;y+=1;
add(x,y,z);r[i]=z;
}
dfs_1(1);
dfs_2(1,1);
for(i=1;i<n;++i) insert(1,1,n-1,pos[i],r[i]);
scanf("%d",&m);
while(m--){
scanf("%*c%s",&ch);
scanf("%d%d",&x,&y);
if(ch[0]=='C') insert(1,1,n-1,pos[x],y);
else{
x+=1;y+=1;
int lca=LCA(x,y);
if(ch[0]=='N') negate_(x,lca),negate_(y,lca);
if(ch[0]=='S') printf("%d\n",work(x,lca,2)+work(y,lca,2));
if(ch[0]=='M'){
if(ch[1]=='A') printf("%d\n",max(work(x,lca,0),work(y,lca,0)));
if(ch[1]=='I') printf("%d\n",min(work(x,lca,1),work(y,lca,1)));
}
}
}
}