Problem Description
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.
 
Input
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.
 
Output
For each question k>0, print a line to answer the latency time. Once there are less than k routers in the way, print "invalid request!" instead.
 
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!
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3071 3070 3072 3073 3074 
 
 
一开始以为要选择第K大的这需要什么东西维护
结果就是排序  (打扰了)
 

题意:单case,一棵无根树,

输入点数和操作数,下面一行n个值代表每个点的权。下面n-1行是树边

操作分为

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

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

板子随便改一改就过了

 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + ;
int _pow[maxn], dep[maxn], dis[maxn], vis[maxn], ver[maxn];
int tot, head[maxn], dp[maxn * ][], k, first[maxn], path[maxn], val[maxn], fa[maxn];
struct node {
int u, v, w, nxt;
} edge[maxn << ];
void init() {
tot = ;
mem(head, -);
}
void add(int u, int v, int w) {
edge[tot].v = v, edge[tot].u = u;
edge[tot].w = w, edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int DEP, int pre) {
vis[u] = ;
ver[++k] = u;
first[u] = k;
dep[k] = DEP;
fa[u] = pre;
for (int i = head[u]; ~i; i = edge[i].nxt) {
if (vis[edge[i].v]) continue;
int v = edge[i].v, w = edge[i].w;
dis[v] = dis[u] + w;
dfs(v, DEP + , u);
ver[++k] = u;
dep[k] = DEP;
}
}
void ST(int len) {
int K = (int)(log((double)len) / log(2.0));
for (int i = ; i <= len ; i++) dp[i][] = i;
for (int j = ; j <= K ; j++) {
for (int i = ; i + _pow[j] - <= len ; i++) {
int a = dp[i][j - ], b = dp[i + _pow[j - ]][j - ];
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 + )) / log(2.0));
int a = dp[x][K], b = dp[y - _pow[K] + ][K];
if (dep[a] < dep[b]) return a;
else return b;
}
int LCA(int u, int v) {
int x = first[u], y = first[v];
if (x > y) swap(x, y);
int ret = RMQ(x, y);
return ver[ret];
}
int cnt;
void solve(int s, int t) {
while(s != t) {
path[cnt++] = val[s];
s = fa[s];
// printf("cnt=%d\n",cnt);
}
path[cnt++] = val[t];
}
int cmp(int a, int b) {
return a > b;
}
int main() {
for (int i = ; i < ; i++) _pow[i] = ( << i);
int n, m;
while(~sff(n, m)) {
init();
mem(vis, );
mem(fa, );
for (int i = ; i <= n ; i++) sf(val[i]);
for (int i = ; i < n - ; i++) {
int u, v;
sff(u, v);
add(u, v, );
add(v, u, );
}
k = , dis[] = ;
dfs(, , -);
ST( * n - );
while(m--) {
int op, u, v;
sfff(op,u,v);
if (op == ) val[u] = v;
else {
int lca = LCA(u, v);
// printf("u=%d v=%d lca=%d\n",u,v,lca);
cnt = ;
solve(u, lca);
solve(v, lca);
cnt--;
// printf("cnt=%d\n", cnt);
if (op > cnt) printf("invalid request!\n");
else {
sort(path, path + cnt, cmp);
printf("%d\n", path[op - ]);
}
}
}
}
return ;
}

最新文章

  1. WPF感悟
  2. printf背后的故事
  3. COGS 2434 暗之链锁 题解
  4. [转载]tail No space left on device
  5. [Effective JavaScript 笔记]第31条:使用Object.getPrototypeOf函数而不要使用__proto__属性
  6. Java设计模式之工厂设计模式
  7. 【转】关系映射文件***.hbm.xml详解
  8. IE浏览器在虚拟机中无法正常显示字符
  9. UVa 557 (概率 递推) Burger
  10. Kail安装Parallels tools
  11. 在ios开发中有多少常用的加密解密方式(备用)
  12. Python实战:美女图片下载器,海量图片任你下载
  13. Django-Rest-Framework 教程: 快速入门
  14. OpenRisc-45-or1200的ID模块分析
  15. K - K.Bro Sorting
  16. Android makefile 组织结构
  17. Java并发基础:进程和线程之由来
  18. kali中的webshell
  19. AJAX的简洁写法
  20. web-day2

热门文章

  1. 在Arch上安装VSCode的方法
  2. Python数据分析基础——Numpy tutorial
  3. 在页面使用echarts的地图(解决地图不完整)
  4. java报错:Exception in thread &quot;main&quot; java.lang.NoSuchFieldError: INSTANCE
  5. spring学习(一)——控制反转简单例子
  6. 数据库索引(结合B-树和B+树)
  7. python学习笔记03:python的核心数据类型
  8. LintCode-88.最近公共祖先
  9. 访问方式由http改为https curl:(51)
  10. 理解BitSet