2014
03-01

# Network

The ALPC company is now working on his own network system, which is connecting all N ALPC department. To economize on spending, the backbone network has only one router for each department, and N-1 optical fiber in total to connect all routers.
The usual way to measure connecting speed is lag, or network latency, referring the time taken for a sent packet of data to be received at the other end.
Now the network is on trial, and new photonic crystal fibers designed by ALPC42 is trying out, the lag on fibers can be ignored. That means, lag happened when message transport through the router. ALPC42 is trying to change routers to make the network faster, now he want to know that, which router, in any exactly time, between any pair of nodes, the K-th high latency is. He needs your help.

There are only one test case in input file.
Your program is able to get the information of N routers and N-1 fiber connections from input, and Q questions for two condition: 1. For some reason, the latency of one router changed. 2. Querying the K-th longest lag router between two routers.
For each data case, two integers N and Q for first line. 0<=N<=80000, 0<=Q<=30000.
Then n integers in second line refer to the latency of each router in the very beginning.
Then N-1 lines followed, contains two integers x and y for each, telling there is a fiber connect router x and router y.
Then q lines followed to describe questions, three numbers k, a, b for each line. If k=0, Telling the latency of router a, Ta changed to b; if k>0, asking the latency of the k-th longest lag router between a and b (include router a and b). 0<=b<100000000.
A blank line follows after each case.

There are only one test case in input file.
Your program is able to get the information of N routers and N-1 fiber connections from input, and Q questions for two condition: 1. For some reason, the latency of one router changed. 2. Querying the K-th longest lag router between two routers.
For each data case, two integers N and Q for first line. 0<=N<=80000, 0<=Q<=30000.
Then n integers in second line refer to the latency of each router in the very beginning.
Then N-1 lines followed, contains two integers x and y for each, telling there is a fiber connect router x and router y.
Then q lines followed to describe questions, three numbers k, a, b for each line. If k=0, Telling the latency of router a, Ta changed to b; if k>0, asking the latency of the k-th longest lag router between a and b (include router a and b). 0<=b<100000000.
A blank line follows after each case.

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

3
2
2
invalid request!

LCA

0 x w ，表示把点x的权改为w

k a b ， 求出，从a到b的路径中，第k大的点权

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define N 80010

int __pow[25];
int fa[N],val[N],p[N];
int node[2*N],first[N],dep[2*N],dp[2*N][25];
bool vis[N];
vector<int>e[N];

void dfs(int &index , int u ,int d , int par)
{
++index; vis[u] = true;
first[u] = index; node[index] = u; dep[index] = d; fa[u] = par;
for(int i=0; i<e[u].size(); i++)
if(!vis[e[u][i]])
{
dfs(index , e[u][i] , d+1 , u);
++index;
node[index] = u; dep[index] = d;
}
}

void ST(int n)
{
int K = (int)(log((double)n) / log(2.0));
for(int i=1; i<=n; i++) dp[i][0] = i;
for(int j=1; j<=K; j++)
for(int i=1; i+__pow[j]-1 <= n ; i++)
{
int a = dp[i][j-1];
int b = dp[i+__pow[j-1]][j-1];
if(dep[a] < dep[b]) dp[i][j] = a;
else                dp[i][j] = b;
}
}

int RMQ(int x ,int y)
{
int K = (int)(log((double)(y-x+1)) / log(2.0));
int a = dp[x][K];
int b = dp[y-__pow[K]+1][K];
if(dep[a] < dep[b]) return a;
else                return b;
}

int LCA(int u ,int v)
{
int x = first[u];
int y = first[v];
if(x > y) swap(x,y);
int index = RMQ(x,y);
return node[index];
}

bool cmp(int a, int b)
{
return a > b;
}

void path(int &index , int s , int t)
{
while(s != t)
{
p[index++] = val[s];
s = fa[s];
}
p[index++] = val[t];
}

void solve(int kth , int u,int v)
{
int lca = LCA(u,v);
int tot = 0;
path(tot,u,lca);
path(tot,v,lca);
tot--;
if(kth > tot)
{
printf("invalid request!\n");
return ;
}
sort(p,p+tot,cmp);
printf("%d\n",p[kth-1]);
}

int main()
{
for(int i=0; i<25; i++) __pow[i] = 1 << i;

int n,q;
scanf("%d%d",&n,&q);
for(int i=1; i<=n; i++) scanf("%d",&val[i]);
for(int i=1; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}

int tot = 0;
memset(vis,false,sizeof(vis));
dfs(tot,1,1,-1);

ST(tot);
while(q--)
{
int op;
scanf("%d",&op);
if(op == 0)
{
int x,w;
scanf("%d%d",&x,&w);
val[x] = w;
}
else
{
int u,v;
scanf("%d%d",&u,&v);
solve(op,u,v);
}
}
return 0;
}