Tree
Problem Description
You are given a tree with N N N nodes which are numbered by integers 1.. N . 1..N. 1..N. Each node is associated with an integer as the weight.
Your task is to deal with M M M operations of 4 4 4 types:
-
Delete an edge ( x , y ) (x, y) (x,y) from the tree, and then add a new edge ( a , b ) . (a, b). (a,b). We ensure that it still constitutes a tree after adding the new edge.
-
Given two nodes a a a and b b b in the tree, change the weights of all the nodes on the path connecting node a a a and b b b (including node a a a and b b b) to a particular value x x x.
-
Given two nodes a a a and b b b in the tree, increase the weights of all the nodes on the path connecting node a a a and b b b (including node a a a and b b b) by a particular value d . d. d.
-
Given two nodes a a a and b b b in the tree, compute the second largest weight on the path connecting node a a a and b b b (including node a a a and b b b), and the number of times this weight occurs on the path. Note that here we need the strict second largest weight. For instance, the strict second largest weight of { 3 , 5 , 2 , 5 , 3 } \{3, 5, 2, 5, 3\} {3,5,2,5,3} is 3. 3. 3.
Input
The first line contains an integer T ( T ≤ 3 ) , T\ (T\leq 3), T (T≤3), which means there are T T T test cases in the input.
For each test case, the first line contains two integers N N N and M ( N , M ≤ 1 0 5 ) . M (N, M\leq10^5). M(N,M≤105). The second line contains N N N integers, and the i − t h i-th i−th integer is the weight of the i − t h i-th i−th node in the tree (their absolute values are not larger than 1 0 4 10^4 104).
In next N − 1 N-1 N−1 lines, there are two integers a a a and b ( 1 ≤ a , b ≤ N ) , b (1\leq a, b\leq N), b(1≤a,b≤N), which means there exists an edge connecting node a a a and b b b.
The next M M M lines describe the operations you have to deal with. In each line the first integer is c ( 1 ≤ c ≤ 4 ) , c (1\leq c\leq 4), c(1≤c≤4), which indicates the type of operation.
If
c
=
1
,
c = 1,
c=1, there are four integers
x
,
y
,
a
,
b
(
1
≤
x
,
y
,
a
,
b
≤
N
)
x, y, a, b (1\leq x, y, a, b \leq N)
x,y,a,b(1≤x,y,a,b≤N) after
c
.
c.
c.
If
c
=
2
,
c = 2,
c=2, there are three integers
a
,
b
,
x
(
1
≤
a
,
b
≤
N
,
∣
x
∣
≤
1
0
4
)
a, b, x (1\leq a, b\leq N, |x|\leq 10^4)
a,b,x(1≤a,b≤N,∣x∣≤104) after
c
.
c.
c.
If
c
=
3
,
c = 3,
c=3, there are three integers
a
,
b
,
d
(
1
≤
a
,
b
≤
N
,
∣
d
∣
≤
1
0
4
)
a, b, d (1\leq a, b\leq N, |d|\leq 10^4)
a,b,d(1≤a,b≤N,∣d∣≤104) after
c
.
c.
c.
If
c
=
4
c = 4
c=4 (it is a query operation), there are two integers
a
,
b
(
1
≤
a
,
b
≤
N
)
a, b (1\leq a, b\leq N)
a,b(1≤a,b≤N) after
c
.
c.
c.
All these parameters have the same meaning as described in problem description.
Output
For each test case, first output “Case #x:”" ( x x x means case I D ID ID) in a separate line.
For each query operation, output two values: the second largest weight and the number of times it occurs. If the weights of nodes on that path are all the same, just output “ALL SAME” (without quotes).
Sample Input
2
3 2
1 1 2
1 2
1 3
4 1 2
4 2 3
7 7
5 3 2 1 7 3 6
1 2
1 3
3 4
3 5
4 6
4 7
4 2 6
3 4 5 -1
4 5 7
1 3 4 2 4
4 3 6
2 3 6 5
4 3 6
Sample Output
Case #1:
ALL SAME
1 2
Case #2:
3 2
1 1
3 2
ALL SAME
Source
2014 ACM/ICPC Asia Regional Anshan Online
题意
- 就是给你一棵带有点权的树,然后实现以下操作
- 删除边 ( x , y ) (x,y) (x,y),连接 ( a , b ) (a,b) (a,b),抱枕任何时候都是一颗树
- 使 a → b a\rightarrow b a→b的路径上所有节点的点权变为 x x x
- 使 a → b a\rightarrow b a→b的路径上所有节点的点权加上 x x x
- 查询 a → b a\rightarrow b a→b的路径上的严格第二大点权的值和出现的数量
题解
- 有加边和删边操作,显然是 L C T LCT LCT题
- 这里有一个问题就是出现了两种 l a z y m a r k lazy\ mark lazy mark(除去 L C T LCT LCT自身的翻转 m a r k mark mark),那么如何定义 p u s h _ d o w n push\_down push_down时候的标记下放顺序?显然优先区间改值而不是区间加值,因为区间改值前面一定没有了区间加值,因为放区间改值的 m a r k mark mark的时候给区间加值的 l a z y m a r k lazy\ mark lazy mark清空了!!!
- 然后需要查询次大值的话显然 s p l a y splay splay维护子树的最大次大值以及数量即可
- 注意区间改值的 l a z y m a r k lazy\ mark lazy mark数组要初始化为 [ − 1 0 4 , 1 0 4 ] [-10^4,10^4] [−104,104]以外的数
- 另外区间加值的时候给次大值需要判断一下是否存在再加上这个值
- 发现这个维护最大次大值的函数是真好用啊
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
#define inf 0x3f3f3f3f
namespace LCT{
int ch[maxn][2],fa[maxn],mark[maxn];
int val[maxn],f[maxn],s[maxn],cntf[maxn],cnts[maxn],lazy[maxn][2],siz[maxn]; //lazy第一维:update 第二维:add
inline void init(int k) {
for(int i=1;i<=k;i++) {
ch[i][0]=ch[i][1]=fa[i]=mark[i]=val[i]=cntf[i]=cnts[i]=siz[i]=lazy[i][1]=0;
f[i]=s[i]=-inf;
lazy[i][0]=inf;
}
}
inline bool not_root(int x) {return ch[fa[x]][0]==x||ch[fa[x]][1]==x;}
inline int dir(int x) {return ch[fa[x]][1]==x;}
inline void mark_rev(int x) {swap(ch[x][0],ch[x][1]);mark[x]^=1;} //mark_表示给节点x添加标记
inline void mark_upd(int x,int v) {
val[x]=v;
lazy[x][0]=v,lazy[x][1]=0;
f[x]=v,cntf[x]=siz[x];
s[x]=-inf,cnts[x]=0;
}
inline void mark_add(int x,int v) {
val[x]+=v;
lazy[x][1]+=v;
f[x]+=v;
if(s[x]!=-inf) s[x]+=v;
}
inline void push_down(int x) {
if(mark[x]) {
if(ch[x][0]) mark_rev(ch[x][0]);
if(ch[x][1]) mark_rev(ch[x][1]);
mark[x]=0;
}
if(lazy[x][0]!=inf) {
if(ch[x][0]) mark_upd(ch[x][0],lazy[x][0]);
if(ch[x][1]) mark_upd(ch[x][1],lazy[x][0]);
lazy[x][0]=inf;
}
if(lazy[x][1]) {
if(ch[x][0]) mark_add(ch[x][0],lazy[x][1]);
if(ch[x][1]) mark_add(ch[x][1],lazy[x][1]);
lazy[x][1]=0;
}
}
inline void maintain(int &fir,int &sec,int val) {//插入val维护最大次大值
if(val>fir) {sec=fir;fir=val;}
else if(val>sec&&val<fir) {sec=val;}
}
inline void push_up(int x) {
siz[x]=1;
int fir=val[x],sec=-inf;
if(ch[x][0]) {
siz[x]+=siz[ch[x][0]];
maintain(fir,sec,f[ch[x][0]]);
maintain(fir,sec,s[ch[x][0]]);
}
if(ch[x][1]) {
siz[x]+=siz[ch[x][1]];
maintain(fir,sec,f[ch[x][1]]);
maintain(fir,sec,s[ch[x][1]]);
}
f[x]=fir,s[x]=sec,cntf[x]=cnts[x]=0;
if(val[x]==fir) cntf[x]++;
if(val[x]==sec) cnts[x]++;
if(ch[x][0]) {
if(f[ch[x][0]]==fir) cntf[x]+=cntf[ch[x][0]];
if(f[ch[x][0]]==sec&&sec!=-inf) cnts[x]+=cntf[ch[x][0]];
if(s[ch[x][0]]==sec&&sec!=-inf) cnts[x]+=cnts[ch[x][0]];
}
if(ch[x][1]) {
if(f[ch[x][1]]==fir) cntf[x]+=cntf[ch[x][1]];
if(f[ch[x][1]]==sec&&sec!=-inf) cnts[x]+=cntf[ch[x][1]];
if(s[ch[x][1]]==sec&&sec!=-inf) cnts[x]+=cnts[ch[x][1]];
}
}
inline void pushall(int x) {
if(not_root(x)) pushall(fa[x]);
push_down(x);
}
inline void rotate(int x){
int y=fa[x],z=fa[y],k=dir(x);
if(ch[x][k^1]) fa[ch[x][k^1]]=y;ch[y][k]=ch[x][k^1];
if(not_root(y)) ch[z][dir(y)]=x;fa[x]=z;
ch[x][k^1]=y;fa[y]=x;
push_up(y);
}
inline void splay(int x,int goal=0) {
pushall(x);
while(not_root(x)) {
int y=fa[x],z=fa[y];
if(not_root(y)) {
if(dir(x)==dir(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
push_up(x);
}
inline void access(int x) { //从原树的根向x拉一条实链
for(int y=0;x;y=x,x=fa[x]) {
splay(x);ch[x][1]=y;push_up(x);
}
}
inline void make_root(int x) { //使x成为splay的根
access(x);splay(x);mark_rev(x);
}
inline int find_root(int x) { //找到x在原树中的根
access(x);splay(x);
while(ch[x][0]) push_down(x),x=ch[x][0];
splay(x);
return x;
}
inline void split(int x,int y) { //拉出一条x->y的实链,y为splay根
make_root(x);access(y);splay(y);
}
inline bool link(int x,int y) { //连接x与y,若已经在同一颗原树中,返回0
make_root(x);
if(find_root(y)==x) return 0;
fa[x]=y;return 1;
}
inline bool cut(int x,int y) {
make_root(x);
if(find_root(y)!=x||fa[y]!=x||ch[y][0]) return 0;
fa[y]=ch[x][1]=0;
push_up(x);
return 1;
}
inline void update(int u,int v,int w) { //区间改值
split(u,v);mark_upd(v,w);
}
inline void add(int u,int v,int w) { //区间加值
split(u,v);mark_add(v,w);
}
inline void solve(int u,int v) {
split(u,v);
if(s[v]==-inf) {printf("ALL SAME\n");return;}
printf("%d %d\n",s[v],cnts[v]);
}
};
using namespace LCT;
int n,m,a,b,x,y,opt;
int main()
{
int t;scanf("%d",&t);
for(int cas=1;cas<=t;cas++) {
scanf("%d %d",&n,&m);init(n);
for(int i=1;i<=n;i++) scanf("%d",&val[i]);
for(int i=1,u,v;i<n;i++) {
scanf("%d %d",&u,&v);
link(u,v);
}
printf("Case #%d:\n",cas);
for(int i=1;i<=m;i++) {
scanf("%d",&opt);
if(opt==1) {
scanf("%d %d %d %d",&x,&y,&a,&b);
cut(x,y);link(a,b);
}else if(opt==2) {
scanf("%d %d %d",&a,&b,&x);
update(a,b,x);
}else if(opt==3) {
scanf("%d %d %d",&a,&b,&x);
add(a,b,x);
}else {
scanf("%d %d",&a,&b);
solve(a,b);
}
}
}
}