HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)

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 = 100010;
 10 const int MAXE = MAXV << 1;
 11 const int INF = 0x3f3f3f3f;
 12 const int NINF = -INF;
 13
 14 struct LCT {
 15     struct Node {
 16         Node *ch[2], *fa;
 17         int val, set, add;
 18         int max[2], cnt[2], 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[0] = nil = statePool;
 30         nil->size = 0;
 31         FOR(k, 2) nil->max[k] = NINF;
 32     }
 33
 34     void init() {
 35         memset(head + 1, -1, n * sizeof(int));
 36         ncnt = 1;
 37         ecnt = 0;
 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[0] = x->ch[1] = nil; x->fa = f;
 48         x->val = val; x->set = NINF; x->add = 0;
 49         x->max[0] = val; x->cnt[0] = 1;
 50         x->max[1] = NINF;
 51         x->size = 1;
 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[0]->size + x->ch[1]->size + 1;
 78
 79         x->max[0] = x->val; x->max[1] = NINF;
 80         FOR(i, 2) FOR(j, 2)
 81             get_max(x->max[0], x->max[1], x->ch[i]->max[j]);
 82
 83         FOR(k, 2) x->cnt[k] = 0;
 84         FOR(k, 2) cnt_max(x->max[k], x->cnt[k], x->val, 1);
 85         FOR(k, 2) FOR(i, 2) FOR(j, 2)
 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[1] == x);
 92
 93         if(y->rt) y->rt = false, x->rt = true;
 94         else y->fa->ch[y->fa->ch[1] == y] = x;
 95         x->fa = y->fa;
 96
 97         (y->ch[t] = x->ch[t ^ 1])->fa = y;
 98         (x->ch[t ^ 1] = y)->fa = x;
 99         update(y);
100     }
101
102     void update_set(Node *x, int val) {
103         if(x == nil) return ;
104         x->add = 0;
105         x->val = x->set = val;
106         x->max[0] = val; x->cnt[0] = x->size;
107         x->max[1] = 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, 2) 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[0], x->ch[1]);
122     }
123
124     void pushdown(Node *x) {
125         if(x->set != NINF) {
126             FOR(k, 2) update_set(x->ch[k], x->set);
127             x->set = NINF;
128         }
129         if(x->add != 0) {
130             FOR(k, 2) update_add(x->ch[k], x->add);
131             x->add = 0;
132         }
133         if(x->rev) {
134             FOR(k, 2) 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[1] == f) && (f->ch[1] == 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[1]->rt = true;
159             (x->ch[1] = 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[1] == NINF) puts("ALL SAME");
198         else printf("%d %d\n", r->max[1], r->cnt[1]);
199     }
200
201     void work() {
202         scanf("%d", &T);
203         for(int t = 1; t <= T; ++t) {
204             scanf("%d%d", &n, &m);
205             init();
206             for(int i = 1; i <= n; ++i) scanf("%d", &val[i]);
207             for(int i = 1, u, v; i < n; ++i) {
208                 scanf("%d%d", &u, &v);
209                 add_edge(u, v);
210             }
211             dfs(1, 0);
212             printf("Case #%d:\n", t);
213             for(int i = 0, x, y, a, b, op; i < m; ++i) {
214                 scanf("%d", &op);
215                 if(op == 1) {
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 == 2) {
220                     scanf("%d%d%d", &a, &b, &x);
221                     modify_set(ptr[a], ptr[b], x);
222                 } else if(op == 3) {
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 }

时间: 2024-10-06 23:50:34

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

HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP

Clone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submission(s) : 8   Accepted Submission(s) : 5 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description After eating food from Chernobyl,

HDU 5000 Clone 规律+dp 2014 ACM/ICPC Asia Regional Anshan Online

每只羊有n个属性 下面n个数字表示每个属性的值范围为[ 0, T[i] ] 对于羊圈里的a羊和b羊,若a羊的每个属性都>=b羊,则a羊会杀死b羊. 问羊圈里最多存活多少只羊. 规律1:sum相同的羊不会互相杀死. 因为若2个羊的属性都相同,a羊某个属性要增加1,则a羊另一个属性要减少1,这样ab一定能共存. 规律2: sum不同的羊不会重合. 我们设a羊sum = x,b羊sum = y,若a,b羊能共存,但不会把ab同时放到羊圈里. 因为一定存在一只羊c ,sum = x,且c和b不能共存,既

HDU 5003 Osu! 水题 2014 ACM/ICPC Asia Regional Anshan Online

水.. #include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <vector> #include <algorithm> #include <set> using namespace std; #define N 100 double a[N]; bool cmp(double x, double y){ re

HDU 5001 Walk 求从任意点出发任意走不经过某个点的概率 概率dp 2014 ACM/ICPC Asia Regional Anshan Online

题意: 给定n个点m条边的无向图 问: 从任意点出发任意走d步,从不经过某个点的概率 dp[i][j]表示从不经过i点的前提下,走了d步到达j点的概率. #include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <vector> #include <algorithm> #include <set> using n

HDU 4998 Rotate 计算几何 2014 ACM/ICPC Asia Regional Anshan Online

题意: 有一个平面放在一个二维坐标轴上 给定n个操作 (x,y) p 表示把平面绕着(x,y) 逆时针转p弧度. 最后的结果相当于平面绕着(X, Y) 逆时针旋转了P弧度. 求:X,Y,P 思路: 任取一个三角形ABC,然后根据n个操作得到A'B'C', 然后求外心. 旋转的角度就是相加..==为啥我也不大清楚,不是本弱写的. #include <cstdio> #include <algorithm> #include <iostream> #include <

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 right now! He used this power for several times. He found out that this power was not as perfect as he wanted. For example, some of the cloned objects

poj 5001 Walk &amp;&amp;2014 ACM/ICPC Asia Regional Anshan Online 1005(dp)

http://acm.hdu.edu.cn/showproblem.php?pid=5001 思路:dp计算出途径每个点的总概率,1-x即为所求解. dp题,先介绍下dp[i][j]为第j步走在第i个点的概率,那么dp[i][j]=dp[x1][j-1]+dp[x2][j-1]+...,x1,x2为i 的相邻节点.上一步在相邻节点这一步才能走到该点嘛. 每个点概率要一个一个的算,当算到第ii个点时(没打错,ii个点与代码对应),从起点推起,起点嘛,算是第0步吧,每个点被选中几率1.0/n,直接计

2014 ACM/ICPC Asia Regional Anshan Online

已经确定了的... B Rotate 1 /* 2 ID:esxgx1 3 LANG:C++ 4 PROG:B 5 */ 6 #include <cmath> 7 #include <cstdio> 8 #include <cstring> 9 #include <iostream> 10 #include <algorithm> 11 using namespace std; 12 13 struct p { 14 double x,y; 15

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que