"bzoj1146" [CTSC2008] network management networks multiplication Lca+dfs sequence + Tree array + Chairman Tree

Source: Internet
Author: User

Title Description

M Corporation is a very large multinational company, which has its subordinate branches or departments in many countries. In order to work together across N departments around the world, the company has built a communications network that connects the entire company. The structure of the network consists of n routers and N-1 high-speed optical cable. Each department has a dedicated router, all the machines in the departmental LAN are connected to the router, and then communicate with other departments through this communication subnet. This network structure ensures that there is a direct or indirect path between any two routers in the network for communication. High-speed optical cable data transmission is very fast, so that the use of cable transmission delay time can be ignored. However, due to the aging of routers, there is a great delay in exchanging data on these routers. The communication latency between the two routers is related to the maximum switching latency in all routers on the two routers ' communication paths. As an intern in the network division of M Company, you are now asked to write a simple program to monitor the company's network situation. The program is able to update the change information of the network condition at any time (change of delay time of the router data interchange), and according to the query give the delay time of the router on the two router communication path delay of the big K. "Task" your program reads the connection information of n routers and N-1 cables from the input file, and the initial data exchange delay time for each router is TI, and the Q Bar asks (or states change) information. And then processing the Q-bar query information, they may be: 1. A router's data exchange delay time has changed due to a newer device, or a new failure of the device. 2. Query the delay time of a router with a delay of k on a path between two routers A and B.

Input

The first behavior is two integers n and q, respectively, representing the total number of routers and queries. The second line has n integers, and the number of I represents the initial data delay time of the router with the number I. ti. Immediately following the N-1 line, each line contains two integers x and y. Indicates that there is an optical cable connecting router x and Router Y. followed by Q line, three integers per line k, a, B. If k=0, it indicates that the state of router A has changed, and its data interchange delay time is changed from TA to B. If k>0, indicates the delay time of the router that is delaying the K-large in all routers (including A and b) that are on the path from A to B. Note that n,q<=80000, any router satisfies a delay time of less than 10^8 at any moment. For all inquiries satisfying 0<=k<=n

Output

For each of the second queries (k>0), output one line. Contains an integer for the corresponding delay time. Output message "Invalid request!" if the router on the path is less than K (All lowercase characters do not contain quotation marks, and there is a space between two words).

Sample input

5 5
5 1 2) 3 4
3 1
2 1
4 3
5 3
2 4 5
0 1 2
2 2 3
2 1 4
3 3 5

Sample output

3
2
2
Invalid request!

Exercises

Multiply Lca+dfs sequence + Tree array + Chairman Tree

Maintain a DFS into the stack out of the stack, build the Chairman tree, put the weights position +1 in the stack position, and the position of the stack will be weighted position-1.

For each inquiry, convert to two chains of X->lca and Y->lca, which translates into 4 paths: 1~x + 1~y-1~lca-1~fa[lca].

Because the topic is modified, you need to use a tree array to maintain the "prefix and" of the chairman tree. Then you can have no brain code.

Note that the point is the card space, so the chairman of the tree insert process can not create a new node each time, if there are nodes to modify, no Add.

#include <cstdio> #include <algorithm> #define N 80010using namespace Std;int w[n], head[n], to[n << 1] , Next[n << 1], CNT, fa[n][18], deep[n], log[n], lp[n], rp[n], num;int ls[n * [], rs[n * +], Si[n * 200 ], Root[n << 1], tot, a[40], b[40], c[40], d[40], TA, TB, TC, Td;int v[n << 1], TV, Vk[n], Vx[n] , vy[n];void Add (int x, int y) {to[++cnt] = y, next[cnt] = head[x], head[x] = cnt;} void Dfs (int x) {int i;lp[x] = ++num;for (i = 1; (1 << i) <= deep[x]; i + +) fa[x][i] = fa[fa[x][i-1]][i-1];for (i = head[x]; i; i = Next[i]) if (to[i]! = fa[x][0]) fa[to[i]][0] = x, deep[t O[i]] = deep[x] + 1, DFS (To[i]); rp[x] = ++num;}  int LCA (int x, int y) {int i;if (deep[x] < deep[y]) swap (x, y); for (i = log[deep[x]-deep[y]]; ~i; i--) if (Deep[x]- Deep[y] >= (1 << i) x = fa[x][i];if (x = = y) return x;for (i = log[deep[x]]; ~i; i--) if (Deep[x] >= (1 <& Lt i) && fa[x][i]! = Fa[y][i]) x = fa[x][i], y= Fa[y][i];return fa[x][0];} void Update (int p, int A, int l, int r, int &x) {if (!x) x = ++tot;si[x] + = a;if (L = = r) Return;int mid = (L + r) &G T;> 1;if (P <= mid) update (p, a, L, Mid, ls[x]); Else update (P, a, mid + 1, R, Rs[x]);}  int query (int k, int l, int r) {if (L = = r) Return l;int mid = (L + r) >> 1, sum = 0, i;for (i = 1; I <= ta; i + +) sum + = si[rs[a[i]]];for (i = 1; I <= TB; i + +) sum + = si[rs[b[i]]];for (i = 1; I <= TC; i + +) sum-= Si[rs [C[i]]; for (i = 1; I <= TD; I + +) sum-= si[rs[d[i]]];if (k <= sum) {for (i = 1; I <= ta; i + +) a[i] = Rs[a[i]];for (i = 1; I <= TB; i + +) b[i] = rs[b[i]];for (i = 1; I <= TC; i + +) c[i] = rs[c[i]];for (i = 1; I <= TD; I + +) d[i] = Rs[d[i]];ret Urn Query (k, mid + 1, r);} Else{for (i = 1; I <= ta; i + +) a[i] = ls[a[i]];for (i = 1; I <= TB; i + +) b[i] = ls[b[i]];for (i = 1; I <= t C i + +) c[i] = ls[c[i]];for (i = 1; I <= TD; I + +) d[i] = Ls[d[i]];return querY (K-sum, L, mid);}} int main () {int n, q, I, J, X, Y, f;scanf ("%d%d", &n, &q); for (i = 1; I <= n; i + +) scanf ("%d", &  W[i]), V[++TV] = w[i];for (i = 2; I <= n; i + +) scanf ("%d%d", &x, &y), add (x, y), add (y, x), log[i] = Log[i >> 1] + 1;DFS (1); for (i = 1; I <= q; i + +) {scanf ("%d%d%d", &vk[i], &vx[i], &vy[i]); if (!VK [i]) v[++tv] = Vy[i];}  Sort (v + 1, v + TV + 1); for (TV = 0, i = 1; I <= n + q; i + +) if (v[i]! = V[i-1]) V[++TV] = v[i];for (i = 1; I <= n; i + +) {W[i] = Lower_bound (v + 1, v + TV + 1, w[i])-v;for (j = lp[i]; j <= Num; j + = J &-j) Update (W[i], 1, 1, TV, Root[j]); for (j = rp[i]; j <= Num; j + = J &-j) Update (W[i],-1, 1, TV, Root[j]);}  for (i = 1; I <= q; i + +) {if (Vk[i]) {f = LCA (Vx[i], vy[i]), if (Deep[vx[i]] + deep[vy[i] [2 * deep[f] + 1 < vk[i]) Puts ("Invalid request!"); Else{ta = TB = TC = TD = 0;for (j = lp[vx[i]; j; J-= J &-j) A[++ta] = Root[j];for(j = Lp[vy[i]]; j; J-= J &-j) B[++TB] = root[j];for (j = lp[f]; j; J-= J &-j) C[++TC] = root[j];for (j = lp[fa[f][0]]; j; J-= J &-j) d[++t D] = Root[j];p rintf ("%d\n", V[query (Vk[i], 1, TV)]);}} Else{vy[i] = Lower_bound (v + 1, v + TV + 1, vy[i])-v;for (j = Lp[vx[i]]; j <= Num; j + = J &-j) update (W[vx[i] ],-1, 1, TV, Root[j]), update (Vy[i], 1, 1, TV, Root[j]); for (j = rp[vx[i]; j <= Num; j + = J &-j) Updat E (W[vx[i]], 1, 1, TV, Root[j]), update (Vy[i], 1, 1, TV, Root[j]); W[vx[i]] = Vy[i];}} return 0;}

"bzoj1146" [CTSC2008] network management networks multiplication Lca+dfs sequence + Tree array + Chairman Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.