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):

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. using namespace std;
  6. typedef long long LL;
  7. #define FOR(i, n) for(int i = 0; i < n; ++i)
  8.  
  9. const int MAXV = ;
  10. const int MAXE = MAXV << ;
  11. const int INF = 0x3f3f3f3f;
  12. const int NINF = -INF;
  13.  
  14. struct LCT {
  15. struct Node {
  16. Node *ch[], *fa;
  17. int val, set, add;
  18. int max[], cnt[], size;
  19. bool rt, rev;
  20. } statePool[MAXV], *nil;
  21. int ncnt;
  22.  
  23. int head[MAXV], val[MAXV], ecnt;
  24. int to[MAXE], next[MAXE];
  25. int n, m, T;
  26. Node *ptr[MAXV];
  27.  
  28. LCT() {
  29. ptr[] = nil = statePool;
  30. nil->size = ;
  31. FOR(k, ) nil->max[k] = NINF;
  32. }
  33.  
  34. void init() {
  35. memset(head + , -, n * sizeof(int));
  36. ncnt = ;
  37. ecnt = ;
  38. }
  39.  
  40. void add_edge(int u, int v) {
  41. to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
  42. to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
  43. }
  44.  
  45. Node* new_node(int val, Node *f) {
  46. Node* x = &statePool[ncnt++];
  47. x->ch[] = x->ch[] = nil; x->fa = f;
  48. x->val = val; x->set = NINF; x->add = ;
  49. x->max[] = val; x->cnt[] = ;
  50. x->max[] = NINF;
  51. x->size = ;
  52. x->rt = true; x->rev = false;
  53. return x;
  54. }
  55.  
  56. void dfs(int u, int f) {
  57. ptr[u] = new_node(val[u], ptr[f]);
  58. for(int p = head[u]; ~p; p = next[p]) {
  59. int v = to[p];
  60. if(v == f) continue;
  61. dfs(v, u);
  62. }
  63. }
  64.  
  65. void get_max(int &a, int &b, int c) {
  66. if(a != c) {
  67. if(b < c) swap(b, c);
  68. if(a < b) swap(a, b);
  69. }
  70. }
  71.  
  72. void cnt_max(int a, int &cnt, int b, int bcnt) {
  73. if(a != NINF && a == b) cnt += bcnt;
  74. }
  75.  
  76. void update(Node *x) {
  77. x->size = x->ch[]->size + x->ch[]->size + ;
  78.  
  79. x->max[] = x->val; x->max[] = NINF;
  80. FOR(i, ) FOR(j, )
  81. get_max(x->max[], x->max[], x->ch[i]->max[j]);
  82.  
  83. FOR(k, ) x->cnt[k] = ;
  84. FOR(k, ) cnt_max(x->max[k], x->cnt[k], x->val, );
  85. FOR(k, ) FOR(i, ) FOR(j, )
  86. cnt_max(x->max[k], x->cnt[k], x->ch[i]->max[j], x->ch[i]->cnt[j]);
  87. }
  88.  
  89. void rotate(Node *x) {
  90. Node *y = x->fa;
  91. int t = (y->ch[] == x);
  92.  
  93. if(y->rt) y->rt = false, x->rt = true;
  94. else y->fa->ch[y->fa->ch[] == y] = x;
  95. x->fa = y->fa;
  96.  
  97. (y->ch[t] = x->ch[t ^ ])->fa = y;
  98. (x->ch[t ^ ] = y)->fa = x;
  99. update(y);
  100. }
  101.  
  102. void update_set(Node *x, int val) {
  103. if(x == nil) return ;
  104. x->add = ;
  105. x->val = x->set = val;
  106. x->max[] = val; x->cnt[] = x->size;
  107. x->max[] = NINF;
  108. }
  109.  
  110. void update_add(Node *x, int val) {
  111. if(x == nil) return ;
  112. x->add += val;
  113. x->val += val;
  114. FOR(k, ) if(x->max[k] != NINF)
  115. x->max[k] += val;
  116. }
  117.  
  118. void update_rev(Node *x) {
  119. if(x == nil) return ;
  120. x->rev = !x->rev;
  121. swap(x->ch[], x->ch[]);
  122. }
  123.  
  124. void pushdown(Node *x) {
  125. if(x->set != NINF) {
  126. FOR(k, ) update_set(x->ch[k], x->set);
  127. x->set = NINF;
  128. }
  129. if(x->add != ) {
  130. FOR(k, ) update_add(x->ch[k], x->add);
  131. x->add = ;
  132. }
  133. if(x->rev) {
  134. FOR(k, ) update_rev(x->ch[k]);
  135. x->rev = false;
  136. }
  137. }
  138.  
  139. void push(Node *x) {
  140. if(!x->rt) push(x->fa);
  141. pushdown(x);
  142. }
  143.  
  144. void splay(Node *x) {
  145. push(x);
  146. while(!x->rt) {
  147. Node *f = x->fa, *ff = f->fa;
  148. if(!f->rt) rotate(((ff->ch[] == f) && (f->ch[] == x)) ? f : x);
  149. rotate(x);
  150. }
  151. update(x);
  152. }
  153.  
  154. Node* access(Node *x) {
  155. Node *y = nil;
  156. while(x != nil) {
  157. splay(x);
  158. x->ch[]->rt = true;
  159. (x->ch[] = y)->rt = false;
  160. update(x);
  161. y = x; x = x->fa;
  162. }
  163. return y;
  164. }
  165.  
  166. void be_root(Node *x) {
  167. access(x);
  168. splay(x);
  169. update_rev(x);
  170. }
  171.  
  172. void link(Node *x, Node *y) {
  173. be_root(x);
  174. x->fa = y;
  175. }
  176.  
  177. void cut(Node *x, Node *y) {
  178. be_root(x);
  179. access(x);
  180. splay(y);
  181. y->fa = nil;
  182. }
  183.  
  184. void modify_add(Node *x, Node *y, int w) {
  185. be_root(x);
  186. update_add(access(y), w);
  187. }
  188.  
  189. void modify_set(Node *x, Node *y, int w) {
  190. be_root(x);
  191. update_set(access(y), w);
  192. }
  193.  
  194. void query(Node *x, Node *y) {
  195. be_root(x);
  196. Node *r = access(y);
  197. if(r->max[] == NINF) puts("ALL SAME");
  198. else printf("%d %d\n", r->max[], r->cnt[]);
  199. }
  200.  
  201. void work() {
  202. scanf("%d", &T);
  203. for(int t = ; t <= T; ++t) {
  204. scanf("%d%d", &n, &m);
  205. init();
  206. for(int i = ; i <= n; ++i) scanf("%d", &val[i]);
  207. for(int i = , u, v; i < n; ++i) {
  208. scanf("%d%d", &u, &v);
  209. add_edge(u, v);
  210. }
  211. dfs(, );
  212. printf("Case #%d:\n", t);
  213. for(int i = , x, y, a, b, op; i < m; ++i) {
  214. scanf("%d", &op);
  215. if(op == ) {
  216. scanf("%d%d%d%d", &x, &y, &a, &b);
  217. cut(ptr[x], ptr[y]);
  218. link(ptr[a], ptr[b]);
  219. } else if(op == ) {
  220. scanf("%d%d%d", &a, &b, &x);
  221. modify_set(ptr[a], ptr[b], x);
  222. } else if(op == ) {
  223. scanf("%d%d%d", &a, &b, &x);
  224. modify_add(ptr[a], ptr[b], x);
  225. } else {
  226. scanf("%d%d", &a, &b);
  227. query(ptr[a], ptr[b]);
  228. }
  229. }
  230. }
  231. }
  232. } S;
  233.  
  234. int main() {
  235. S.work();
  236. }

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. nRF51822之WDT浅析

    看门狗定时器 NRF51822 的看门狗定时器是倒计数器, 当计数值减少到 0 时产生 TIMEOUT 事件. 通过 START task 来启动看门狗定时器. 看门狗定时器启动时,如没有其他 32. ...

  2. WCF TCP 错误代码 10061: 由于目标计算机积极拒绝

    表象是连不上服务端,本质原因多种多样,网络硬件问题导致的网络不通.服务本身问题或没有启动.或者防火墙阻隔等等不一而足. 1.ping看服务端能否ping通: 2.telnet ip地址 端口 ,看看是 ...

  3. C中文件操作说明

    r 以只读方式打开文件,该文件必须存在. r+ 以读/写方式打开文件,该文件必须存在. rb+ 以读/写方式打开一个二进制文件,只允许读/写数据. rt+ 以读/写方式打开一个文本文件,允许读和写. ...

  4. php——n维数组的遍历——递归

    <?php /**** ****/function digui($arr){    foreach($arr as $key => $value)    {        if(is_ar ...

  5. Nodejs路由之间的数据传递

    实例是模拟登录页面提交表单,然后根据信息判断是否登录成功 login.js var express =require('express'); var router =express.Router(); ...

  6. 20145211 《Java程序设计》第九周学习总结——垂死病中惊坐起

    教材学习内容总结 JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层数据库驱动程序的差异性 JDBC标准分为两个部分:J ...

  7. Select Statement Syntax [AX 2012]

    Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 ...

  8. Java学习-024-获取当前类名或方法名二三文

    今天,看朋友编写程序,打印日志时,需要记录当前类的类名以及当前方法的方法名,我发现 TA 将类名或者方法名直接写死在了代码中...虽说这样可以实现记录类名和方法名,但是当有特殊情况需要修改类名或者方法 ...

  9. asp.net操作xml

    下面是xml文档内容: <content width="368" height="450" bgcolor="cccccc" load ...

  10. Java日期时间处理常用方法

    虽然是老生常谈,但整理出来还是有点用. 1.由字符串时间得到Date类型时间 // 由字符串时间得到Date类型时间 public static Date getDateFrom(String str ...