QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#771754 | #8235. Top Cluster | billf | WA | 445ms | 124784kb | C++14 | 3.3kb | 2024-11-22 15:27:36 | 2024-11-22 15:27:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 5e5 + 10, L = 20;
int n, q, fth[N], a[N], b[N], dep[N], stg[N], lm, in[N], ord[N * 2];
int tot, to[N * 2], nxt[N * 2], fst[N], wei[N * 2], up;
LL dis[N];
void add(int ui, int vi, int wi)
{
to[++tot] = vi, wei[tot] = wi, nxt[tot] = fst[ui];
fst[ui] = tot;
}
struct ST
{
int f[L][N * 2], Lg[N * 2], *ay;
void build(int *ary, int ln)
{
ay = ary;
Lg[0] = -1;
for (int i = 1; i <= ln; i++)
Lg[i] = Lg[i >> 1] + 1;
int p1, p2;
iota(f[0] + 1, f[0] + ln + 1, 1);
for (int j = 1; 1 << j <= ln; j++)
for (int i = 1; i + (1 << j) - 1 <= ln; i++)
{
p1 = f[j - 1][i], p2 = f[j - 1][i + (1 << j - 1)];
if (ary[p1] > ary[p2])
f[j][i] = p2;
else
f[j][i] = p1;
}
}
int ask(int l, int r)
{
if (l > r)
return 0;
int ln = Lg[r - l + 1], p1 = f[ln][l], p2 = f[ln][r - (1 << ln) + 1];
if (ay[p1] > ay[p2])
return p2;
else
return p1;
}
} rq;
void dfs(int x, int fa)
{
dep[x] = dep[fth[x]] + 1;
in[x] = ++tot;
ord[tot] = x;
fth[x] = fa;
for (int i = fst[x]; i; i = nxt[i])
{
int y = to[i], z = wei[i];
if (y == fa)
continue;
dis[y] = dis[x] + z;
dfs(y, x);
ord[++tot] = x;
}
}
LL dist(int x, int y)
{
if (in[x] > in[y])
swap(x, y);
return dis[x] + dis[y] - 2 * dis[ord[rq.ask(in[x], in[y])]];
}
struct chain
{
int x, y;
LL d;
LL dit()
{
return d = dist(x, y);
}
friend chain operator+(const chain a, const int b)
{
int mx;
chain c, d, e;
c = d = e = a;
c.y = b;
d.x = b;
mx = max(c.dit(), max(d.dit(), e.dit()));
if (c.d == mx)
return c;
if (d.d == mx)
return d;
return e;
}
} pre[N];
int main()
{
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
scanf("%d", a + i);
if (a[i] > n)
continue;
b[a[i]] = i;
}
for (int i = 0; i <= n; i++)
if (!b[i])
{
up = i;
break;
}
if (!up)
{
while (q--)
puts("0");
return 0;
}
for (int i = 1; i < n; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w), add(v, u, w);
}
tot = 0;
dfs(1, 0);
for (int i = 1; i <= tot; i++)
stg[i] = dep[ord[i]];
rq.build(stg, tot);
pre[0].x = pre[0].y = b[0];
pre[0].d = 0;
for (int i = 1; i < up; i++)
pre[i] = pre[i - 1] + b[i];
while (q--)
{
int x, l = 0, r = up - 1, mid;
LL y;
scanf("%d%lld", &x, &y);
while (l < r)
{
mid = l + r >> 1;
if (max(dist(x, pre[mid].x), dist(x, pre[mid].y)) > y)
r = mid;
else
l = mid + 1;
}
if (max(dist(x, pre[l].x), dist(x, pre[l].y)) <= y)
l++;
printf("%d\n", l);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 28448kb
input:
5 4 3 9 0 1 2 1 2 10 3 1 4 3 4 3 3 5 2 3 0 1 0 4 6 4 7
output:
1 0 3 4
result:
ok 4 number(s): "1 0 3 4"
Test #2:
score: -100
Wrong Answer
time: 445ms
memory: 124784kb
input:
500000 500000 350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...
output:
0 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 2499...
result:
wrong answer 6721st numbers differ - expected: '0', found: '249999'