QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#499789 | #8235. Top Cluster | cz_yxx | TL | 0ms | 63152kb | C++20 | 3.0kb | 2024-07-31 19:17:28 | 2024-07-31 19:17:28 |
Judging History
answer
// sis puella oier
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
ll read(){
ll xx = 0, f = 1; char ch = getchar();
for (;!isdigit(ch); ch = getchar())
f = (ch == '-' ? -1 : 1);
for (; isdigit(ch); ch = getchar())
xx = (xx << 3) + (xx << 1) + ch - '0';
return xx * f;
}
const int N = 500100;
int n, q;
struct edge{
int v, w;
};
vector <edge> e[N];
int a[N], dis[N], fa[19][N], dep[N];
void dfs(int x, int fath){
fa[0][x] = fath; for (int i = 1; i <= 18; ++i)fa[i][x] = fa[i - 1][fa[i - 1][x]];
for (edge v : e[x])if (v.v != fath)
dep[v.v] = dep[x] + 1, dis[v.v] = dis[x] + v.w, dfs(v.v, x);
}
int lca(int x, int y){
if (dep[x] < dep[y])swap(x, y);
for (int i = 18; i >= 0; --i)if (dep[fa[i][x]] >= dep[y])x = fa[i][x];
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 Dis(int x, int y){
return dis[x] + dis[y] - 2 * dis[lca(x, y)];
}
edge qu[N];
int id[N], idl[N], idr[N], ans[N];
int dd[2][N];
void solve(int l, int r, int nl, int nr){
if (nl > nr)return ;
// cout<<l<<" "<<r<<endl;
if (l == r){
for (int i = nl; i <= nr; ++i)ans[id[i]] = l;
return ;
}
int mid = (l + r) >> 1, cntl = 0, cntr = 0;
// cout<<mid<<" :: "<<dd[0][mid]<<" "<<dd[1][mid]<<" : "<<qu[id[nl]].v<<endl;
// cout<<max(dis(dd[0][mid], qu[id[nl]].v), dis(dd[1][mid], qu[id[nl]].v))<<endl;
for (int i = nl; i <= nr; ++i)if (max(Dis(dd[0][mid], qu[id[i]].v), Dis(dd[1][mid], qu[id[i]].v)) > qu[id[i]].w)
idl[++cntl] = id[i];
else idr[++cntr] = id[i];
for (int i = 1; i <= cntl; ++i)id[nl + i - 1] = idl[i];
for (int i = 1; i <= cntr; ++i)id[nl + cntl + i - 1] = idr[i];
solve(l, mid, nl, nl + cntl - 1), solve(mid + 1, r, nl + cntl, nr);
}
int vis[N];
signed main(){
n = read(), q = read();
for (int i = 1; i <= n; ++i){
a[i] = read(); if (a[i] < N)vis[a[i]] = i;
}
// for (int i = 0; i <= n; ++i)cout<<vis[i]<<" ";cout<<endl;
int mx = 0; while(vis[mx])++mx;
for (int i = 1, u, v, w; i < n; ++i)u = read(), v = read(), w = read(),
e[u].push_back(edge{v, w}), e[v].push_back(edge{u, w});
dis[1] = dep[1] = 0, dfs(1, 0);
dd[0][0] = vis[0], dd[1][0] = vis[0];
for (int i = 1; i < mx; ++i){
int xx = Dis(vis[i], dd[0][i - 1]), yy = Dis(vis[i], dd[1][i - 1]),
zz = Dis(dd[0][i - 1], dd[1][i - 1]);
if (xx >= yy && xx >= zz)dd[0][i] = vis[i], dd[1][i] = dd[0][i - 1];
if (yy >= xx && yy >= zz)dd[0][i] = vis[i], dd[1][i] = dd[1][i - 1];
if (zz >= xx && zz >= yy)dd[0][i] = dd[0][i - 1], dd[1][i] = dd[1][i - 1];
}
// for (int i = 0; i < mx; ++i)cout<<dd[0][i]<<" "<<dd[1][i]<<endl; cout<<endl;
for (int i = 1; i <= q; ++i)qu[i].v = read(), qu[i].w = read(), id[i] = i;
solve(0, mx, 1, q);
for (int i = 1; i <= q; ++i)printf("%lld\n", ans[i]);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 63152kb
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
Time Limit Exceeded
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...