BZOJ_1146_[CTSC2008] Network Management Network_Chairman Tree + Tree Array

BZOJ_1146_[CTSC2008] Network Management Network_Chairman Tree

Description

  Company M is a very large multinational company with branches or departments in many countries. In order to make N distributed around the world
The departments work together, and the company builds a communication network that connects the entire company. The structure of the network consists of N routers and N-1 high-speed optical cables.
Each department has a dedicated router, and all machines in the department's local area network are connected to this router, and then communicate with other departments through this communication subnet.
door for communication. This network structure guarantees that a direct or indirect path exists between any two routers in the network for communication. high speed light
The data transmission speed of the cable is so fast that the delay time of transmission using the optical cable is negligible. But due to aging routers, on these routers
The exchange of data can introduce significant delays. The communication delay time between the two routers is the largest among all the routers in the communication path between the two routers.
exchange delay time. As an intern in the network department of M company, you are now asked to write a simple program to monitor the company's network conditions
. The program can update information on changes in network conditions (changes in router data exchange delay time) at any time, and gives two routers
The delay time of the router with the kth largest delay on the information path. [Task] Your program reads the connection information of N routers and N-1 optical cables from the input file
, the initial data exchange delay time Ti of each router, and Q pieces of inquiry (or state change) information. And process the Q pieces of inquiry information in turn, they
It may be: 1. The data exchange delay time of a router has changed due to the update of the device or a new failure of the device. 2. Check
Query the delay time of the router with the kth largest delay on the path between two routers a and b.

Input

The first line contains two integers N and Q, representing the total number of routers and the total number of queries, respectively.
The second line contains N integers, and the ith number represents the initial data delay time Ti of the router numbered i.
Following N-1 lines, each line contains two integers x and y. Indicates that there is an optical cable connecting router x and router y.
This is followed by Q lines, each with three integers k, a, b.
If k=0, it means that the state of router a has changed, and its data exchange delay time changes from Ta to b
If k>0, it means that the query delays in all routers (including a and b) traversed on the path from a to b
The delay time of the kth largest router.
Note that N, Q<=80000, any router satisfies the delay time less than 10^8 at any time.
Satisfy 0<=K<=N for all queries

Output

For each second query (k > 0), print one line. Contains an integer for the corresponding delay time.
If there are less than k routers on the path, the message "invalidrequest!" will be output
(all lowercase without quotes, with 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!

 Generally speaking, when the chairman tree maintains the information on the tree, each point maintains the information on the path from the point to the root.
But this is still on the tree, and the modification is not very convenient, so it is put on the sequence.
Create a stacking and stacking sequence, each point is +1 at the place where it is pushed, and -1 at the place where it is popped.
The prefix sum is the information on the path from this point to the root, which is transformed into a sequence problem.
Then these operations can be done with tree arrays.
There are hidden dangers in what I wrote here, and it cannot be considered that the last node +1 in the subtree is the position where the stack is popped.
Because if you maintain some other information, you need to modify the information of that node, so the information maintained by the pop-up position of the previous point will also be modified.
And the space has to be doubled.
 
(kth largest)
 
Code:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 80050
#define maxn 100000000
int n,m,head[N],to[N<<1],nxt[N<<1],val[N],dfn[N],cnt,out[N];
int fa[N],top[N],son[N],size[N],dep[N],tot,A[N],B[N],C[N],D[N];
int siz[N*250],ls[N*250],rs[N*250],root[N];
inline void add(int u,int v) {
	to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;
}
void insert(int &y,int l,int r,int v,int c) {
	if(!y) y=++tot; siz[y]+=c;
	if(l==r) return ;
	int mid=(l+r)>>1;
	if(v<=mid) insert(ls[y],l,mid,v,c);
	else insert(rs[y],mid+1,r,v,c);
}
void dfs1(int x,int y) {
	int i; dep[x]=dep[y]+1; fa[x]=y; size[x]=1; dfn[x]=++dfn[0];
	for(i=dfn[x];i<=n;i+=i&(-i)) insert(root[i],0,maxn,val[x],1);
	for(i=head[x];i;i=nxt[i]) if(to[i]!=y) {
		dfs1(to[i],x); size[x]+=size[to[i]]; if(size[to[i]]>size[son[x]]) son[x]=to[i];
	}
	for(i=dfn[0]+1;i<=n;i+=i&(-i)) insert(root[i],0,maxn,val[x],-1);
	out[x]=dfn[0]+1;
}
void dfs2(int x,int t) {
	top[x]=t; if(son[x]) dfs2(son[x],t); int i;
	for(i=head[x];i;i=nxt[i]) if(to[i]!=fa[x]&&to[i]!=son[x]) dfs2(to[i],to[i]);
}
int lca(int x,int y) {
	while(top[x]!=top[y]) {
		if(dep[top[x]]>dep[top[y]]) swap(x,y);
		y=fa[top[y]];
	}
	return dep[x]<dep[y]?x:y;
}
int query(int l,int r,int k) {
	if(l==r) return l;
	int mid=(l+r)>>1,i,sizls=0;
	for(i=1;i<=A[0];i++) sizls+=siz[ls[A[i]]];
	for(i=1;i<=B[0];i++) sizls+=siz[ls[B[i]]];
	for(i=1;i<=C[0];i++) sizls-=siz[ls[C[i]]];
	for(i=1;i<=D[0];i++) sizls-=siz[ls[D[i]]];
	if(k<=sizls) {
		for(i=1;i<=A[0];i++) A[i]=ls[A[i]];
		for(i=1;i<=B[0];i++) B[i]=ls[B[i]];
		for(i=1;i<=C[0];i++) C[i]=ls[C[i]];
		for(i=1;i<=D[0];i++) D[i]=ls[D[i]];
		return query(l,mid,k);
	}else {
		for(i=1;i<=A[0];i++) A[i]=rs[A[i]];
		for(i=1;i<=B[0];i++) B[i]=rs[B[i]];
		for(i=1;i<=C[0];i++) C[i]=rs[C[i]];
		for(i=1;i<=D[0];i++) D[i]=rs[D[i]];
		return query(mid+1,r,k-sizls);
	}
}
int main() {
	scanf("%d%d",&n,&m);
	int i,x,y,k;
	for(i=1;i<=n;i++) scanf("%d",&val[i]);
	for(i=1;i<n;i++) {
		scanf("%d%d",&x,&y); add(x,y); add(y,x);
	}
	dfs1(1,0); dfs2(1,1);
	while(m--) {
		scanf("%d%d%d",&k,&x,&y);
		if(!k) {
			for(i=dfn[x];i<=n;i+=i&(-i)) insert(root[i],0,maxn,val[x],-1);
			for(i=out[x];i<=n;i+=i&(-i)) insert(root[i],0,maxn,val[x],1);
			val[x]=y;
			for(i=dfn[x];i<=n;i+=i&(-i)) insert(root[i],0,maxn,val[x],1);
			for(i=out[x];i<=n;i+=i&(-i)) insert(root[i],0,maxn,val[x],-1);
		}else {
			A[0]=B[0]=C[0]=D[0]=0;
			int l=lca(x,y),len=dep[x]+dep[y]-dep[l]-dep[fa[l]];
			if(len<k) puts("invalid request!");
			else {
				k = len-k + 1;
				for(i=dfn[x];i;i-=i&(-i)) A[++A[0]]=root[i];
				for(i=dfn[y];i;i-=i&(-i)) B[++B[0]]=root[i];
				for(i=dfn[l];i;i-=i&(-i)) C[++C[0]]=root[i];
				if(fa[l]) for(i=dfn[fa[l]];i;i-=i&(-i)) D[++D[0]]=root[i];
				printf("%d\n",query(0,maxn,k));
			}
		}
	}
}
/*
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
*/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025643&siteId=291194637