QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#727349 | #8235. Top Cluster | proking | WA | 617ms | 154344kb | C++14 | 2.9kb | 2024-11-09 12:53:31 | 2024-11-09 12:53:31 |
Judging History
answer
#include <bits/stdc++.h>
#define F(i, a, b) for (int i = a; i <= b; i ++)
#define G(i, a, b) for (int i = a; i >= b; i --)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mx(a, b) ((a) = max(a, b))
#define mn(a, b) ((a) = min(a, b))
#define get getchar()
#define putc putchar
#define outarr(a, L, R) { printf(#a"[%d..%d] = ", L, R); F(i, L, R) W(a[i]), putc(' ') ; putc('\n'); }
#define out1(x) { printf(#x" = "), W(x), putc('\n'); }
#define out2(x, y) { printf(#x" = "), W(x), printf(" "#y" = "), W(y), putc('\n'); }
#define out3(x, y, z) { printf(#x" = "), W(x), printf(" "#y" = "), W(y), printf(" "#z" = "), W(z), putc('\n'); }
using namespace std;
template <typename Int>
void R(Int &x) {
char c = get; x = 0; Int t = 1;
for (; !isdigit(c); c = get) t = (c == '-' ? -1 : t);
for (; isdigit(c); x = (x << 3) + (x << 1) + c - '0', c = get); x *= t;
}
template <typename Int>
void W(Int x) {
if (x < 0) { putc('-'); x = - x; }
if (x > 9) W(x / 10); putc(x % 10 + '0');
}
#define pb push_back
#define ll long long
const int N = 1e6 + 10;
int n, q, w[N], u, v, l, x, k;
struct node {
int x, y;
friend bool operator < (node a, node b) { return a.x < b.x; }
} D[N], E[N];
struct edge {
int x, l;
} ; vector <edge> e[N];
int cnt, d[N], fir[N], dep[N], _[N], f[N][20], dis[N];
int Min(int x, int y) {
return dep[x] < dep[y] ? x : y;
}
int Lca(int x, int y) {
x = fir[x], y = fir[y]; if (x > y) swap(x, y);
int k = _[y - x + 1];
return Min(f[x][k], f[y - (1 << k) + 1][k]);
}
void Init() {
F(i, 2, cnt) _[i] = _[i >> 1] + 1;
F(i, 1, cnt) f[i][0] = d[i];
F(j, 1, 19) for (int i = 1; i + (1 << j) - 1 <= cnt; i ++)
f[i][j] = Min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
void Dfs(int k, int fa) {
d[++ cnt] = k, fir[k] = cnt, dep[k] = dep[fa] + 1;
for (auto [ x, l ] : e[k])
if (x ^ fa)
dis[x] = dis[k] + l, Dfs(x, k), d[++ cnt] = k;
}
int Dis(int x, int y) {
if (!x || !y) return 0;
return dis[x] + dis[y] - 2 * dis[Lca(x, y)];
}
int main() {
// freopen("data.in","r",stdin);
R(n), R(q);
F(i, 1, n) R(D[i].x), D[i].y = i;
F(i, 1, n - 1) R(u), R(v), R(l), e[u].pb({v, l}), e[v].pb({u, l});
Dfs(1, 0);
Init();
// outarr(dis, 1, n);
// out1(Dis(4,5));
sort(D + 1, D + n + 1);
int m = 0; D[0].x = - 1;
F(i, 1, n) {
if (D[i].x != D[i - 1].x + 1) break;
m = i;
E[i] = E[i - 1];
if (i == 1) E[i].x = D[i].y;
if (i == 2) E[i].y = D[i].y;
if (i > 2) {
int z1 = Dis(E[i].x, E[i].y);
int z2 = Dis(E[i].x, D[i].y);
int z3 = Dis(E[i].y, D[i].y);
if (z1 < max(z2, z3)) {
if (z2 > z3)
E[i].y = D[i].y;
else
E[i].x = D[i].y;
}
}
}
F(i, 1, q) {
R(x), R(k);
int l = 1, r = m, ans = 0;
while (l <= r) {
int md = (l + r) >> 1;
if (max(Dis(E[md].x, x), Dis(E[md].y, x)) <= k)
ans = md, l = md + 1;
else
r = md - 1;
}
W(ans), putc('\n');
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 40444kb
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: 0
Accepted
time: 504ms
memory: 153844kb
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:
ok 500000 numbers
Test #3:
score: -100
Wrong Answer
time: 617ms
memory: 154344kb
input:
500000 500000 416779 59604 366180 195604 4 30957 999969109 7476 352690 368624 121597 999960303 999933891 13 14 138579 294015 227392 106760 117837 208506 999997971 34770 40258 182765 65889 206246 233051 130491 182099 117381 241945 449750 155921 356191 999955435 2243 450904 242106 178163 148523 75648 ...
output:
59 0 250002 100311 94581 250002 250002 100311 59 0 0 94581 100311 250002 59 0 59 0 59 0 0 250002 250002 100311 100311 250002 250002 0 250002 0 0 94581 59 250002 0 250002 0 59 250002 0 0 250002 0 59 1018 250002 250002 0 59 250002 100311 94581 250002 250002 250002 250002 0 0 100311 0 0 0 611 250002 25...
result:
wrong answer 1st numbers differ - expected: '0', found: '59'