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. [dpdk] 读开发指南(2)(内容长期整理中)

    接续前节. 7 PMD (Poll Mode Driver) A Poll Mode Driver (PMD) consists of APIs, provided through the BSD d ...

  2. xcode 路径

    $(SRCROOT)宏和$(PROJECT_DIR)宏 XCode环境变量及路径设置 分类: Objective-C2013-03-11 12:30 41336人阅读 评论(1) 收藏 举报 一般我们 ...

  3. zepto源码--定义变量--学习笔记

    主要了解一下zepto定义的初始变量. 逐一以自己的理解解析,待到后面完全透彻理解之后,争取再写一遍zepto源码学习的文章. 其中的undefined确实不明白为什么定义这么个变量在这里. docu ...

  4. css“变形”效果

    <html <head> <title></title> <style> .test { margin-left:300px; margin-to ...

  5. Solr分页与高亮(使用SolrNet实现)

    Solr分页与高亮(使用SolrNet实现) 本节我们使用Asp.net MVC实现Solr客户端查询,建议使用SolrNet这个客户端,开源地址在:https://github.com/mausch ...

  6. 网页上的表格数据table

    格式: <table> <tr> <th> </th> </tr> <tr> <td> </td> &l ...

  7. BI 商业智能理解结构图

    本文章是在本人实习阶段对BI(商业智能 Business Intelligence)的理解:(如有不足之处请多指教,谢谢) BI 系统负责从多个数据源中搜集数据,并将这些数据进行必要的转换后存储到 ...

  8. Selenium2学习-010-WebUI自动化实战实例-008-Selenium 操作下拉列表实例-Select

    此文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,对下拉列表框 Select 的操作. 下拉列表是 Web UI 自动化测试过程中使用率非常高的,通常有两种形式的下拉列表,一 ...

  9. Keep Alive

    跳板机时经常出现连接被断开的情况.如果发生这种情况,请在客户端配置Keep Alive设置,具体方法参考如下: Windows: secureCRT:Properties -> Terminal ...

  10. imx6 MFG TOOL 分析

    之前分析过mfgtool的内容,最近从官网下载,返现新版的mfgtool工具将imx6各种版本的linux/android都使用一个工具进行烧录.所以从新分析一下. 新版与旧版的一个区别是烧写使用的u ...