QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#386911 | #8235. Top Cluster | sunkuangzheng | TL | 3849ms | 101260kb | C++23 | 2.8kb | 2024-04-11 21:23:32 | 2024-04-11 21:23:32 |
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,int op){
tp.emplace_back(a[u],d),dd[u] = d; if(op) ds.push_back(d);
for(auto i : uq[u]) qu.emplace_back(i);
ff dfs2(v,u,d+w,op);
}void solve(int u){
tp.clear(),qu.clear(),ds.clear(),dfs2(u,0,0,1); int f = 0; ds.push_back(1e14+5);
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,0);
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,0);
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: 2ms
memory: 3768kb
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: 3849ms
memory: 101260kb
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
Time Limit Exceeded
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 ...