[hihocoder#1065] Full map transfer

Source: Internet
Author: User

[hihocoder#1065] Full map transfer

Question Description

Prophet Fario is a hero in the Dota series. Strong maneuverability, first-class tower capability, fast money-playing, transfer skills so that the prophet can support the whole map. In the late competition through the strong line control ability to make the other side of the line has been difficult to cross the river, and has a certain late DPS ability.

What we have to consider today is the transfer of skills. Suppose the battlefield is a tree-shaped area, each node has a weight vi. When you transfer to a point u , you can support all nodes within the RADIUS R of the point u . For the following query "u r", the answer is centered on the u node, radius r within the node, the maximum weight of the node number is. If there are multiple nodes, returns the smallest number.

Input

The first line contains an integer n (1≤ n ≤105) that represents the total number of nodes.

The next line, containing n digits, represents the weight vi (1≤ VI ≤109) of each node. The next n-1 lines, three integers per line (ai, bi,wi), represent an edge of the AI, bi node, and the edge length is wi (1≤ ai, bin, 1≤ wi ≤104).

The next line contains an integer Q, which indicates the total number of queries (1≤ q ≤105). Next Q line, each line contains two integers u, r(1≤ un, 0≤ R ≤109), which indicates an inquiry to u node is the center, the radius r within the node, the maximum weight of the node number is. If there is more than one solution returns the smallest number.

Output

For each set of queries, the output line represents the answer.

Input example

71 2 3 4 5 6 71 2 12 3 12 4 11 5 15 6 15 7 141 11 22 12 2

Output example

5 7 4 5

Data size and conventions

See " Input "

Exercises

Consider the offline processing of each operation, the corresponding node of the operation is logged to the node information, consider the use of Point division. Obviously recursive every time to reduce the size of the problem, consider the cross-midpoint of the part, we DFS and record each node depth and weight, according to the depth of the keyword sorting, processing the prefix maximum, for a query, we two minutes to update the answer.

#include <cstdio> #include <algorithm> #include <cmath> #include <cctype> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL        ine Char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin);    Tail = (Head = buffer) + L; } return *head++;}    int read () {int x = 0, f = 1; char c = GetChar ();    while (!isdigit (c)) {if (c = = '-') f =-1; c = GetChar ();}    while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 100010#define maxm 200010#define oo 2147483647int N, M, HEAD[MAXN], NXT[MAXM], TO[MAXM], DIST[MAXM], VAL[MAXN  ], ans[maxn];void addedge (int A, int b, int c) {To[++m] = b; dist[m] = c; nxt[m] = Head[a]; Head[a] = M;swap (A, b); To[++m] = b; DIST[M] = c; NXT[M] = Head[a]; Head[a] = M;return;} int Q, M2, H2[maxn], n2[maxn];struct Que {int u, r; Que () {}que (int _, int __): U (_), R (__) {}bool operator < (const que& t) const {return R < T.R;}} Qs[maxn];bool vis[maxn];int Root, F[maxn], SIZ[MAXN], size;void getroot (int u, int fa) {Siz[u] = 1; F[u] = 0;for (int e = h Ead[u]; E e = Nxt[e]) if (to[e]! = FA &&!vis[to[e]) {getroot (to[e], u); siz[u] + = Siz[to[e]];f[u] = max (F[u], siz[to[e]]);} F[u] = max (F[u], size-siz[u]), if (F[u] < f[root]) root = U;return;} int ToT, NS[MAXN]; Que a[maxn];void dfs (int u, int d, int fa) {A[++tot] = Que (U, D); for (int e = head[u]; e; e = nxt[e]) if (to[e]! = FA && amp;!vis[to[e]]) DFS (To[e], D + dist[e], u); return;} void solve (int u) {//printf ("U:%d\n", u); vis[u] = 1; A[tot = 1] = Que (u, 0), for (int e = Head[u], e; e = nxt[e]) if (!vis[to[e]]) DFS (To[e], dist[e], u); sort (A + 1, a + ToT + 1); for (int i = 1; I <= tot; i++) printf ("%d%d\n", a[i].u, A[I].R); for (int i = 1; I <= ToT; i++) ns[i] = A[i].u;for (i NT i = 2; I <= ToT; i++) IF (val[a[i-1].u] > val[a[i].u]) a[i].u = A[i-1].u;else if (val[a[i-1].u] = = val[a[i].u] && a[i-1].u < A[I].U) A[I].U = a[i-1].u;for (int i = 1; I <= ToT; i++) for (int u = h2[ns[i] [u; u = n2[u]) {//printf ("%d Q[u]:%d\n", ns[i], u ); que& Que = qs[u];if (QUE.R < A[I].R) Continue;int x = Upper_bound (A + 1, a + ToT + 1, Que (0, QUE.R-A[I].R))-a;if (x <= ToT && a[x].r <= que.r-a[i].r) x++;x--;if (ans[u] = oo) ans[u] = A[x].u;else if (Val[ans[u]] < Val [A[X].U]) Ans[u] = A[x].u;else if (val[ans[u] [= val[a[x].u] && ans[u] > a[x].u) ans[u] = a[x].u;} for (int e = head[u]; e; e = nxt[e]) if (!vis[to[e]]) {root = 0; F[0] = n + 1; size = Siz[to[e]]; Getroot (to[e], u); Solve (ro OT);} return;} int main () {n = read (), for (int i = 1; I <= n; i++) Val[i] = read (); for (int i = 1; i < n; i++) {int a = read (), B = r EAD (), C = Read (); Addedge (A, B, c);} Q = Read (); for (int i = 1; I <= Q; i++) {int u = Read (), r = Read (), qs[i] = Que (U, r); n2[++m2] = H2[u]; h2[u] = m2;} for (int i = 1; I <= Q; i++) Ans[i] = Oo;root = 0; F[0] = n + 1; size = N; Getroot (1, 0), solve (root), for (int i = 1; I <= Q; i++) printf ("%d\n", Ans[i]); return 0;}

[hihocoder#1065] Full map transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.