QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#713740 | #8235. Top Cluster | ucup-team902 | TL | 3964ms | 654516kb | C++20 | 3.4kb | 2024-11-05 20:25:30 | 2024-11-05 20:25:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<22|1;
char ibuf[RLEN],*ib,*ob;
inline char gc(){
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline ll readll(){
char ch=gc();
ll res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<typename tp>inline void chemx(tp &a,tp b){(a<b)?(a=b):0;}
template<typename tp>inline void chemn(tp &a,tp b){(a>b)?(a=b):0;}
cs int N=500005;
int n,q,maxn,rt;
int w[N];
int sz[N],son[N],vs[N];
struct F{
int f,pf;
ll d;
F(int _f=0,int _p=0,ll _d=0):f(_f),pf(_p),d(_d){}
};vector<F>fa[N];
vector<pii> e[N];
struct node{
int w,p;
ll d;
node(int _f=0,int _p=0,ll _d=0):w(_f),p(_p),d(_d){}
friend bool operator <(cs node &a,cs node &b){
return a.d<b.d;
}
};
vector<node> d[N];
struct info{
pii x,y;
void init(node c){
x=pii(c.w,c.p);
y=pii(2e9,-2);
}
};
info merge(info x,node c){
pii y=pii(c.w,c.p);
if(y.fi<x.x.fi){
if(y.se==x.x.se){
x.x=y;
}
else{
x.y=x.x,x.x=y;
}
}
else if(y.fi<x.y.fi){
if(y.se!=x.x.se)x.y=y;
}
return x;
}
vector<info> f1[N];
void getrt(int u,int f){
sz[u]=1,son[u]=0;
for(pii x:e[u])if(x.fi!=f&&!vs[x.fi]){
getrt(x.fi,u),sz[u]+=sz[x.fi];
chemx(son[u],sz[x.fi]);
}
son[u]=max(son[u],maxn-sz[u]);
if(son[u]<son[rt])rt=u;
}
//fa root,pf,dep
//d w,pf,dep
void givefa(int u,int f,ll dep,int pf,int ff){
fa[u].pb(F(ff,pf,dep));
d[ff].pb(node(w[u],(u==ff)?-1:pf,dep));
for(int i=0;i<e[u].size();i++){
int v=e[u][i].fi;
if(v==f||vs[v])continue;
givefa(v,u,dep+e[u][i].se,(u==ff)?v:pf,ff);
}
}
void init(int u){
givefa(u,0,0,0,u);
sort(d[u].begin(),d[u].end());
int nn=d[u].size();
f1[u].resize(nn);
f1[u][nn-1].init(d[u].back());
for(int i=nn-2;i>=0;i--)
f1[u][i]=merge(f1[u][i+1],d[u][i]);
}
void solve(int u,int f){
vs[u]=1;
init(u);
for(pii x:e[u])if(!vs[x.fi]){
int v=x.fi;
maxn=sz[v]>sz[u]?sz[f]-sz[u]:sz[v];
getrt(v,rt=0);
solve(rt,u);
}
}
//fa root,pf,dep
//d w,pf,dep
int calc(int u,int pf,ll dis){
int l=0,r=f1[u].size()-1,res=-1;
while(l<=r){
int mid=(l+r)/2;
if(d[u][mid].d>dis)res=mid,r=mid-1;
else l=mid+1;
}
if(res==-1)return 2e9;
if(f1[u][res].x.se==pf&&f1[u][res].y.se==pf)return 2e9;
if(f1[u][res].x.se==pf)return f1[u][res].y.fi;
return f1[u][res].x.fi;
}
int query(int u,ll k){
int res=2e9;
for(int i=fa[u].size()-1;~i;i--){
F x=fa[u][i];
res=min(res,calc(x.f,x.pf,k-x.d));
}
return res;
}
int main(){
#ifdef Stargazer
freopen("1.in","r",stdin);
#endif
n=read(),q=read();
map<int,int>visit;
int mnans=0;
for(int i=1;i<=n;i++)w[i]=read(),visit[w[i]]=1;
for(int i=0;i<=n+1;i++)if(!visit[i]){
mnans=i;break;
}
for(int i=1;i<n;i++){
int u=read(),v=read(),w=read();
e[u].pb(pii(v,w));
e[v].pb(pii(u,w));
}
sz[0]=son[0]=maxn=n;
getrt(1,rt=0);
solve(rt,0);
while(q--){
int x=read();ll k=readll();
cout<<min(query(x,k),mnans)<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9764kb
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: 2741ms
memory: 491776kb
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: 0
Accepted
time: 2859ms
memory: 491156kb
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:
0 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 2500...
result:
ok 500000 numbers
Test #4:
score: 0
Accepted
time: 3964ms
memory: 654288kb
input:
500000 500000 161260 258036 999901390 280191 21975 193647 397811 362276 206096 421117 336278 416815 31870 999934957 999977591 15 999991550 235871 999916756 19 241647 466194 218880 352702 84559 479034 24903 124981 339271 153019 157132 333035 124326 464181 219609 94164 999933701 440055 118732 2285 175...
output:
0 250000 250000 69 10 250000 250000 0 5 250000 250000 250000 1 33 69 10 250000 250000 250000 10 0 250000 250000 250000 1 250000 250000 0 250000 10 5 0 33 10 250000 1 66 250000 1 250000 1 250000 250000 250000 16 1 1 0 5 10 250000 250000 0 250000 250000 250000 250000 250000 250000 250000 5 1 250000 1 ...
result:
ok 500000 numbers
Test #5:
score: 0
Accepted
time: 3894ms
memory: 654516kb
input:
500000 500000 205699 141313 291987 999929528 111195 248994 999960789 391023 323134 92686 432395 316257 188889 219187 315056 39556 999959131 279778 30709 419259 242325 169515 137848 999905250 130641 245510 26 52987 999900052 144308 180276 162429 405263 130502 101937 108999 259339 137566 414011 304915...
output:
0 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 0 250000 3 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 0 250000 250000 250000 250000 250000 2 250...
result:
ok 500000 numbers
Test #6:
score: -100
Time Limit Exceeded
input:
500000 500000 142817 198136 999910358 236637 238445 325614 22961 999909937 354352 999984540 189454 221148 208300 169118 999945163 256203 999945857 276295 175580 481676 114118 119423 999962365 999904418 250723 333971 311979 384475 386372 11544 999956892 999960490 999936512 292089 42395 5450 326202 26...
output:
0 250004 0 4 6 5 250004 6 250004 250004 0 0 250004 250004 250004 250004 0 0 250004 250004 250004 0 0 0 0 0 6 0 0 0 0 6 0 250004 4 250004 250004 250004 250004 250004 0 0 6 250004 6 250004 0 0 6 250004 4 250004 0 6 250004 250004 4 250004 0 6 0 250004 0 0 0 250004 4 0 0 6 0 0 0 6 0 250004 4 250004 5 25...