Your task is to deal with M operations of 4 types:
1.Delete an edge (x, y) from the tree, and then add a new edge (a, b). We ensure that it still constitutes a tree after adding the new edge.
2.Given two nodes a and b in the tree, change the weights of all the nodes on the path connecting node a and b (including node a and b) to a particular value x.
3.Given two nodes a and b in the tree, increase the weights of all the nodes on the path connecting node a and b (including node a and b) by a particular value d.
4.Given two nodes a and b in the tree, compute the second largest weight on the path connecting node a and b (including node a and 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} is 3.
For each test case, the first line contains two integers N and M (N, M<=10^5). The second line contains N integers, and the i-th integer is the weight of the i-th node in the tree (their absolute values are not larger than 10^4).
In next N-1 lines, there are two integers a and b (1<=a, b<=N), which means there exists an edge connecting node a and b.
The next M lines describe the operations you have to deal with. In each line the first integer is c (1<=c<=4), which indicates the type of operation.
If c = 1, there are four integers x, y, a, b (1<= x, y, a, b <=N) after c.
If c = 2, there are three integers a, b, x (1<= a, b<=N, |x|<=10^4) after c.
If c = 3, there are three integers a, b, d (1<= a, b<=N, |d|<=10^4) after c.
If c = 4 (it is a query operation), there are two integers a, b (1<= a, b<=N) after c.
All these parameters have the same meaning as described in problem description.
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).
题目大意:维护一棵树,每次删边加边、给一条路径的所有点赋一个值、给一条路径的所有点加上一个值,或询问一条路径上的第二大值及其在这条路径上的出现次数。
思路:算是LCT的模板题吧,维护每个区间的第一大值和第一大值的出现次数,第二大值和第二大值的出现次数。
PS:终于搞出了一个指针版,新模板出来啦~~~
犯过的错误:
1、LCT里splay的旋转和普通splay的旋转有所不同。
2、不能更新超级儿子 nil 的最值。
代码(2859MS):
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
#define FOR(i, n) for(int i = 0; i < n; ++i) const int MAXV = ;
const int MAXE = MAXV << ;
const int INF = 0x3f3f3f3f;
const int NINF = -INF; struct LCT {
struct Node {
Node *ch[], *fa;
int val, set, add;
int max[], cnt[], size;
bool rt, rev;
} statePool[MAXV], *nil;
int ncnt; int head[MAXV], val[MAXV], ecnt;
int to[MAXE], next[MAXE];
int n, m, T;
Node *ptr[MAXV]; LCT() {
ptr[] = nil = statePool;
nil->size = ;
FOR(k, ) nil->max[k] = NINF;
} void init() {
memset(head + , -, n * sizeof(int));
ncnt = ;
ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} Node* new_node(int val, Node *f) {
Node* x = &statePool[ncnt++];
x->ch[] = x->ch[] = nil; x->fa = f;
x->val = val; x->set = NINF; x->add = ;
x->max[] = val; x->cnt[] = ;
x->max[] = NINF;
x->size = ;
x->rt = true; x->rev = false;
return x;
} void dfs(int u, int f) {
ptr[u] = new_node(val[u], ptr[f]);
for(int p = head[u]; ~p; p = next[p]) {
int v = to[p];
if(v == f) continue;
dfs(v, u);
}
} void get_max(int &a, int &b, int c) {
if(a != c) {
if(b < c) swap(b, c);
if(a < b) swap(a, b);
}
} void cnt_max(int a, int &cnt, int b, int bcnt) {
if(a != NINF && a == b) cnt += bcnt;
} void update(Node *x) {
x->size = x->ch[]->size + x->ch[]->size + ; x->max[] = x->val; x->max[] = NINF;
FOR(i, ) FOR(j, )
get_max(x->max[], x->max[], x->ch[i]->max[j]); FOR(k, ) x->cnt[k] = ;
FOR(k, ) cnt_max(x->max[k], x->cnt[k], x->val, );
FOR(k, ) FOR(i, ) FOR(j, )
cnt_max(x->max[k], x->cnt[k], x->ch[i]->max[j], x->ch[i]->cnt[j]);
} void rotate(Node *x) {
Node *y = x->fa;
int t = (y->ch[] == x); if(y->rt) y->rt = false, x->rt = true;
else y->fa->ch[y->fa->ch[] == y] = x;
x->fa = y->fa; (y->ch[t] = x->ch[t ^ ])->fa = y;
(x->ch[t ^ ] = y)->fa = x;
update(y);
} void update_set(Node *x, int val) {
if(x == nil) return ;
x->add = ;
x->val = x->set = val;
x->max[] = val; x->cnt[] = x->size;
x->max[] = NINF;
} void update_add(Node *x, int val) {
if(x == nil) return ;
x->add += val;
x->val += val;
FOR(k, ) if(x->max[k] != NINF)
x->max[k] += val;
} void update_rev(Node *x) {
if(x == nil) return ;
x->rev = !x->rev;
swap(x->ch[], x->ch[]);
} void pushdown(Node *x) {
if(x->set != NINF) {
FOR(k, ) update_set(x->ch[k], x->set);
x->set = NINF;
}
if(x->add != ) {
FOR(k, ) update_add(x->ch[k], x->add);
x->add = ;
}
if(x->rev) {
FOR(k, ) update_rev(x->ch[k]);
x->rev = false;
}
} void push(Node *x) {
if(!x->rt) push(x->fa);
pushdown(x);
} void splay(Node *x) {
push(x);
while(!x->rt) {
Node *f = x->fa, *ff = f->fa;
if(!f->rt) rotate(((ff->ch[] == f) && (f->ch[] == x)) ? f : x);
rotate(x);
}
update(x);
} Node* access(Node *x) {
Node *y = nil;
while(x != nil) {
splay(x);
x->ch[]->rt = true;
(x->ch[] = y)->rt = false;
update(x);
y = x; x = x->fa;
}
return y;
} void be_root(Node *x) {
access(x);
splay(x);
update_rev(x);
} void link(Node *x, Node *y) {
be_root(x);
x->fa = y;
} void cut(Node *x, Node *y) {
be_root(x);
access(x);
splay(y);
y->fa = nil;
} void modify_add(Node *x, Node *y, int w) {
be_root(x);
update_add(access(y), w);
} void modify_set(Node *x, Node *y, int w) {
be_root(x);
update_set(access(y), w);
} void query(Node *x, Node *y) {
be_root(x);
Node *r = access(y);
if(r->max[] == NINF) puts("ALL SAME");
else printf("%d %d\n", r->max[], r->cnt[]);
} void work() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
init();
for(int i = ; i <= n; ++i) scanf("%d", &val[i]);
for(int i = , u, v; i < n; ++i) {
scanf("%d%d", &u, &v);
add_edge(u, v);
}
dfs(, );
printf("Case #%d:\n", t);
for(int i = , x, y, a, b, op; i < m; ++i) {
scanf("%d", &op);
if(op == ) {
scanf("%d%d%d%d", &x, &y, &a, &b);
cut(ptr[x], ptr[y]);
link(ptr[a], ptr[b]);
} else if(op == ) {
scanf("%d%d%d", &a, &b, &x);
modify_set(ptr[a], ptr[b], x);
} else if(op == ) {
scanf("%d%d%d", &a, &b, &x);
modify_add(ptr[a], ptr[b], x);
} else {
scanf("%d%d", &a, &b);
query(ptr[a], ptr[b]);
}
}
}
}
} S; int main() {
S.work();
}