Description
Given a point weight tree of size n, a pair of points (u,v) are asked each time, and whether three point weights can be taken on the simple path from u to v, and a triangle is formed with these three weights as the side length. It also supports single point modification.
Sample Input
5 5
1 2 3 4 5
1 2
2 3
3 4
1 5
0 1 3
0 4 5
1 1 4
0 2 5
0 2 3
Sample Output
N
Y
Y
N
After you sort the series, it must be the adjacent items that make up the triangle.
You find that the worst-case situation can only be a Fibonacci sequence, and you find that almost 50 items will explode, so if there are 50 points or more between the two points, you can directly YES, otherwise you can use the first nature of violence.
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; int _min(int x, int y) {return x < y ? x : y;} int _max(int x, int y) {return x > y ? x : y;} int read() { int s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();} while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * f; } struct edge { int x, y, next; } e[210000]; int len, last[110000]; int a[110000], dep[110000], fa[20][110000]; LL tp, sta[51]; void ins(int x, int y) { e[++len].x = x, e[len].y = y; e[len].next = last[x], last[x] = len; } void dfs(int x) { for(int i = 1; (1 << i) <= dep[x]; i++) fa[i][x] = fa[i - 1][fa[i - 1][x]]; for(int k = last[x]; k; k = e[k].next) { int y = e[k].y; if(y != fa[0][x]) fa[0][y] = x, dep[y] = dep[x] + 1, dfs(y); } } int LCA(int x, int y) { if(dep[x] > dep[y]) swap(x, y); for(int i = 18; i >= 0; i--) if(dep[y] - dep[x] >= (1 << i)){ y = fa[i][y]; } if(x == y) return x; for(int i = 18; i >= 0; i--) if(fa[i][x] != fa[i][y]){ x = fa[i][x], y = fa[i][y]; } return fa[0][x]; } int main() { int n = read(), q = read(); for(int i = 1; i <= n; i++) a[i] = read(); for(int i = 1; i < n; i++) { int x = read(), y = read(); ins(x, y), ins(y, x); } dfs(1); for(int i = 1; i <= q; i++) { int opt = read(), x = read(), y = read(); if(opt == 1) a[x] = y; else { int lca = LCA(x, y); int s = dep[x] + dep[y] - dep[lca] - dep[fa[0][lca]]; if(s >= 50) puts("Y"); else { tp = 0; while(x != lca) sta[++tp] = a[x], x = fa[0][x]; while(y != lca) sta[++tp] = a[y], y = fa[0][y]; sta[++tp] = a[lca]; sort(sta + 1, sta + tp + 1); bool bk = 0; for(int i = 3; i <= tp; i++) { if(sta[i] < sta[i - 1] + sta[i - 2]) {bk = 1; break;} } if(bk) puts("Y"); else puts("N"); } } } return 0; }