QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#386845 | #8235. Top Cluster | sunkuangzheng | RE | 0ms | 3720kb | C++23 | 2.8kb | 2024-04-11 20:40:05 | 2024-04-11 20:40:05 |
Judging History
answer
/**
* author: sunkuangzheng
* created: 11.04.2024 20:01:39
**/
#include<bits/stdc++.h>
#ifdef DEBUG_LOCAL
#include <mydebug/debug.h>
#endif
using ll = long long;
const int N = 5e5+5;
using namespace std;
int T,n,m,a[N],ok[N],u,v,w,siz[N],tt,rt,vis[N],au[N],t[N],ans[N],re = 1e9; vector<pair<int,int>> g[N]; vector<int> b;
vector<pair<int,ll>> tp,qu,uq[N]; ll k,dd[N]; vector<ll> ds;
#define ff for(auto [v,w] : g[u]) if(!vis[v] && v != f)
void upd(int x,int k){for(;x;x -= x & -x) t[x] = min(t[x],k);}
int qry(int x){for(re = 1e9;x <= n;x += x & -x) re = min(re,t[x]); return re;}
void clr(int x){for(;x;x -= x & -x) t[x] = 1e9;}
void dfs1(int u,int f,int sz){
siz[u] = 1; int mx = 0;
ff dfs1(v,u,sz),siz[u] += siz[v],mx = max(mx,siz[v]);
if(mx = max(mx,sz - siz[u]),mx < tt) tt = mx,rt = u;
}void dfs2(int u,int f,ll d){
tp.emplace_back(a[u],d),dd[u] = d,ds.push_back(d);
for(auto i : uq[u]) qu.emplace_back(i);
ff dfs2(v,u,d+w);
}void solve(int u){
tp.clear(),qu.clear(),ds.clear(),dfs2(u,0,0); int f = 0;
sort(ds.begin(),ds.end()),ds.erase(unique(ds.begin(),ds.end()));
for(auto &[x,k] : tp) if(k) k = lower_bound(ds.begin(),ds.end(),k) - ds.begin() + 1,upd(k,x);
qu.clear(); for(auto i : uq[u]) qu.push_back(i);
for(auto &[x,k] : qu) k = lower_bound(ds.begin(),ds.end(),k+1) - ds.begin() + 1,ans[x] = min(ans[x],qry(k));
for(int i = 1;i <= ds.size();i ++) t[i] = 1e9;upd(1,a[u]);
ff{
tp.clear(),qu.clear(),dfs2(v,u,w);
for(auto &[x,k] : qu) k = lower_bound(ds.begin(),ds.end(),k+1-dd[au[x]]) - ds.begin() + 1,ans[x] = min(ans[x],qry(k));
for(auto &[x,k] : tp) k = lower_bound(ds.begin(),ds.end(),k) - ds.begin() + 1,upd(k,x);
}for(int i = 1;i <= ds.size();i ++) t[i] = 1e9;
reverse(g[u].begin(),g[u].end());
ff{
tp.clear(),qu.clear(),dfs2(v,u,w);
for(auto &[x,k] : qu) k = lower_bound(ds.begin(),ds.end(),k+1-dd[au[x]]) - ds.begin() + 1,ans[x] = min(ans[x],qry(k));
for(auto &[x,k] : tp) k = lower_bound(ds.begin(),ds.end(),k) - ds.begin() + 1,upd(k,x);
}for(int i = 1;i <= ds.size();i ++) t[i] = 1e9;
reverse(g[u].begin(),g[u].end());
}void dfs2(int u,int f){solve(u),vis[u] = 1; ff tt = 1e9,dfs1(v,u,siz[v]),dfs2(rt,0);}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin >> n >> m;
for(int i = 1;i <= n;i ++)
if(cin >> a[i],b.push_back(a[i]),a[i] <= n) ok[a[i]] = 1;
int mex = 0; while(ok[mex]) mex ++;
fill(ans+1,ans+m+1,mex),fill(t+1,t+n+1,1e9);
for(int i = 1;i < n;i ++) cin >> u >> v >> w,g[u].emplace_back(v,w),g[v].emplace_back(u,w);
for(int i = 1;i <= m;i ++) cin >> u >> k,au[i] = u,uq[u].emplace_back(i,k);
tt = 1e9,dfs1(1,0,n),dfs2(rt,0);
for(int i = 1;i <= m;i ++) cout << ans[i] << "\n";
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3720kb
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
Runtime Error
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...