Problem Description
You are given a tree with N nodes which are numbered by integers 1..N. Each node is associated with an integer as the weight.

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.

 
Input
The first line contains an integer T (T<=3), which means there are T test cases in the input.

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.

 
Output
For each test case, first output "Case #x:"" (x means case 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).

题目大意:维护一棵树,每次删边加边、给一条路径的所有点赋一个值、给一条路径的所有点加上一个值,或询问一条路径上的第二大值及其在这条路径上的出现次数。

思路:算是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();
}

HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)的更多相关文章

  1. HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP

    Clone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submiss ...

  2. HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...

  3. 2014 ACM/ICPC Asia Regional Anshan Online

    默默的签到 Osu! http://acm.hdu.edu.cn/showproblem.php?pid=5003 #include<cstdio> #include<algorit ...

  4. hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi'an Online)

    Mart Master II Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  5. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  6. HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...

  7. HDU 5052 Yaoge’s maximum profit 光秃秃的树链拆分 2014 ACM/ICPC Asia Regional Shanghai Online

    意甲冠军: 特定n小点的树权. 以下n每一行给出了正确的一点点来表达一个销售点每只鸡价格的格 以下n-1行给出了树的侧 以下Q操作 Q行 u, v, val 从u走v,程中能够买一个鸡腿,然后到后面卖 ...

  8. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  9. HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)

    思路:广搜, 因为空格加上动物最多只有32个那么对这32个进行编号,就能可以用一个数字来表示状态了,因为只有 ‘P’   'S' 'M' '.' 那么就可以用4进制刚好可以用64位表示. 接下去每次就 ...

随机推荐

  1. Selenium2学习-037-WebUI自动化实战实例-IE浏览器显示比例问题:org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Browser zoom level was set to 94%. It should be set to 100%

    好久没有写博文了,今天在给部门新人演示 Selenium WebDriver 启动其支持的各种浏览器时,启动 IE 时总是无法打开对应的百度网址,页面如下所示:

  2. sell -- 解码16进制unicode

    1. //System.out.println("decodeUnicode:" + decodeUnicode("0049"));//I public sta ...

  3. zero3- JPA http://www.cnblogs.com/holbrook/archive/2012/12/30/2839842.html

    1.很好的博客:http://www.cnblogs.com/holbrook/archive/2012/12/30/2839842.html 2. 最新的搬到github : http://holb ...

  4. 在CentOS下企图整合Apache和Tomcat依然失败

    环境: 64位CentOS  Linux version 2.6.32-431.el6.x86_64 CentOS release 6.5 (Final) Apache/2.2.15,mod_jk/1 ...

  5. JS-面向对象-封装

    --参考文献: --http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html --js面 ...

  6. IntelliJ IDEA大小写转换快捷键

    IntelliJ IDEA大小写转换快捷键 Ctr + Shift + u

  7. python笔记 - day6

    python笔记 - day6 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 大纲: 利用递归,实现阶乘: Python反射 pyt ...

  8. jquery中奖实例代码

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. mobiscroll.js 使用

    使用较为详情的参考网址:http://www.lanrenmaku.com/jMobile/2014_1231_1357.html

  10. JavaScript:通过id来进行元素的取得

    每一个HTML元素都使用id来进行一个标注,随后可以通过document.getElementById(“ID名称”)取得指定的ID元素对象,取得元素对象之后就可以对其进行操作. 但是document ...