标签:mat oid open color get def change some i++
题意:单case,一棵无根树,
输入点数和操作数,下面一行n个值代表每个点的权。下面n-1行是树边
操作分为
0 x w ,表示把点x的权改为w
k a b , 求出,从a到b的路径中,第k大的点权
板子随便改一改就过了
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <cmath> 5 #include <algorithm> 6 #include <set> 7 #include <iostream> 8 #include <map> 9 #include <stack> 10 #include <string> 11 #include <vector> 12 #define pi acos(-1.0) 13 #define eps 1e-6 14 #define fi first 15 #define se second 16 #define lson l,m,rt<<1 17 #define rson m+1,r,rt<<1|1 18 #define bug printf("******\n") 19 #define mem(a,b) memset(a,b,sizeof(a)) 20 #define fuck(x) cout<<"["<<x<<"]"<<endl 21 #define f(a) a*a 22 #define sf(n) scanf("%d", &n) 23 #define sff(a,b) scanf("%d %d", &a, &b) 24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) 25 #define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d) 26 #define pf printf 27 #define FRE(i,a,b) for(i = a; i <= b; i++) 28 #define FREE(i,a,b) for(i = a; i >= b; i--) 29 #define FRL(i,a,b) for(i = a; i < b; i++) 30 #define FRLL(i,a,b) for(i = a; i > b; i--) 31 #define FIN freopen("DATA.txt","r",stdin) 32 #define gcd(a,b) __gcd(a,b) 33 #define lowbit(x) x&-x 34 #pragma comment (linker,"/STACK:102400000,102400000") 35 using namespace std; 36 typedef long long LL; 37 typedef unsigned long long ULL; 38 const int maxn = 2e5 + 10; 39 int _pow[maxn], dep[maxn], dis[maxn], vis[maxn], ver[maxn]; 40 int tot, head[maxn], dp[maxn * 2][30], k, first[maxn], path[maxn], val[maxn], fa[maxn]; 41 struct node { 42 int u, v, w, nxt; 43 } edge[maxn << 2]; 44 void init() { 45 tot = 0; 46 mem(head, -1); 47 } 48 void add(int u, int v, int w) { 49 edge[tot].v = v, edge[tot].u = u; 50 edge[tot].w = w, edge[tot].nxt = head[u]; 51 head[u] = tot++; 52 } 53 void dfs(int u, int DEP, int pre) { 54 vis[u] = 1; 55 ver[++k] = u; 56 first[u] = k; 57 dep[k] = DEP; 58 fa[u] = pre; 59 for (int i = head[u]; ~i; i = edge[i].nxt) { 60 if (vis[edge[i].v]) continue; 61 int v = edge[i].v, w = edge[i].w; 62 dis[v] = dis[u] + w; 63 dfs(v, DEP + 1, u); 64 ver[++k] = u; 65 dep[k] = DEP; 66 } 67 } 68 void ST(int len) { 69 int K = (int)(log((double)len) / log(2.0)); 70 for (int i = 1 ; i <= len ; i++) dp[i][0] = i; 71 for (int j = 1 ; j <= K ; j++) { 72 for (int i = 1 ; i + _pow[j] - 1 <= len ; i++) { 73 int a = dp[i][j - 1], b = dp[i + _pow[j - 1]][j - 1]; 74 if (dep[a] < dep[b]) dp[i][j] = a; 75 else dp[i][j] = b; 76 } 77 } 78 } 79 int RMQ(int x, int y) { 80 int K = (int)(log((double)(y - x + 1)) / log(2.0)); 81 int a = dp[x][K], b = dp[y - _pow[K] + 1][K]; 82 if (dep[a] < dep[b]) return a; 83 else return b; 84 } 85 int LCA(int u, int v) { 86 int x = first[u], y = first[v]; 87 if (x > y) swap(x, y); 88 int ret = RMQ(x, y); 89 return ver[ret]; 90 } 91 int cnt; 92 void solve(int s, int t) { 93 while(s != t) { 94 path[cnt++] = val[s]; 95 s = fa[s]; 96 // printf("cnt=%d\n",cnt); 97 } 98 path[cnt++] = val[t]; 99 } 100 int cmp(int a, int b) { 101 return a > b; 102 } 103 int main() { 104 for (int i = 0 ; i < 40 ; i++) _pow[i] = (1 << i); 105 int n, m; 106 while(~sff(n, m)) { 107 init(); 108 mem(vis, 0); 109 mem(fa, 0); 110 for (int i = 1 ; i <= n ; i++) sf(val[i]); 111 for (int i = 0 ; i < n - 1 ; i++) { 112 int u, v; 113 sff(u, v); 114 add(u, v, 0); 115 add(v, u, 0); 116 } 117 k = 0, dis[1] = 0; 118 dfs(1, 1, -1); 119 ST(2 * n - 1); 120 while(m--) { 121 int op, u, v; 122 sfff(op,u,v); 123 if (op == 0) val[u] = v; 124 else { 125 int lca = LCA(u, v); 126 // printf("u=%d v=%d lca=%d\n",u,v,lca); 127 cnt = 0; 128 solve(u, lca); 129 solve(v, lca); 130 cnt--; 131 // printf("cnt=%d\n", cnt); 132 if (op > cnt) printf("invalid request!\n"); 133 else { 134 sort(path, path + cnt, cmp); 135 printf("%d\n", path[op - 1]); 136 } 137 } 138 } 139 } 140 return 0; 141 }
标签:mat oid open color get def change some i++
原文地址:https://www.cnblogs.com/qldabiaoge/p/9448191.html