QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#356237 | #8235. Top Cluster | USTC_fish_touching_team# | TL | 9ms | 52708kb | C++14 | 2.5kb | 2024-03-17 16:57:01 | 2024-03-17 16:57:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=500005;
const long long inf=1e15+1;
#define ll long long
vector<ll >d[N],id[N];
int sz[N];
int w[N];
int ton[N];
int mexall=0;
int vis[N];
int ans[N];
long long dep[N],val[N];
vector<int >p[N],vp[N];
int rt,siz,zqdx;
void grt(int x,int fa){
sz[x]=1;
int szn=0;
for(int y:p[x])if(!vis[x]&&y!=fa){
grt(y,x);
sz[x]+=sz[y];
szn=max(szn,sz[y]);
}
szn=max(szn,zqdx-sz[x]);
if(siz>szn)siz=szn,rt=x;
}
void gx(int &x,int y){
x=min(x,y);
}
int t[N*60],ls[N*60],rs[N*60],wl,tot;
void add(int &now,ll l,ll r,ll x,int y){
if(!now)now=++tot,t[now]=1e9+1;
if(l==r){
t[now]=y;
return ;
}
int mid=l+r>>1;
if(x<=mid)add(ls[now],l,mid,x,y);
else add(rs[now],mid+1,r,x,y);
t[now]=min(t[now],y);
}
int qr(int now,ll l,ll r,ll x){
if(!now)return 1e9;
if(l>=x)return t[now];
int mid=l+r>>1,ans=1e9;
if(x<=mid)ans=min(ans,qr(ls[now],l,mid,x));
ans=min(ans,qr(rs[now],mid+1,r,x));
return ans;
}
void dfs(int x,int fa){
for(int i=0;i<p[x].size();i++)if(p[x][i]!=fa&&!vis[p[x][i]]){
int y=p[x][i];
dep[y]=dep[x]+vp[x][i];
dfs(y,x);
}
for(int i=0;i<d[x].size();i++){
gx(ans[id[x][i]],qr(wl,1,inf,d[x][i]-dep[x]+1));
}
}
void adfs(int x,int fa){
sz[x]=1;
for(int y:p[x])if(y!=fa&&!vis[y]){
adfs(y,x);sz[x]+=sz[y];
}
add(wl,1,inf,dep[x],w[x]);
}
void sol(int x,int dx){
if(vis[x])return ;
if(dx==1)return;
rt=0;
siz=1e9;
zqdx=dx;
grt(x,0);
x=rt;
while(tot)ls[tot]=rs[tot]=t[tot]=0,tot--;
dep[x]=0;tot=0;wl=0;
add(wl,1,inf,0,w[x]);
for(int i=0;i<p[x].size();i++)dep[p[x][i]]=dep[x]+vp[x][i],dfs(p[x][i],x),adfs(p[x][i],x);
while(tot)ls[tot]=rs[tot]=t[tot]=0,tot--;
dep[x]=0;tot=0;wl=0;
for(int i=(int)p[x].size()-1;i>=0;i--)dep[p[x][i]]=dep[x]+vp[x][i],dfs(p[x][i],x),adfs(p[x][i],x);
for(int i=0;i<d[x].size();i++){
gx(ans[id[x][i]],qr(wl,1,inf,d[x][i]-dep[x]+1));
}
vis[x]=1;
for(int y:p[x])sol(y,sz[y]);
}
int main(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
if(w[i]<=n)ton[w[i]]=1;
else w[i]=n+1;
}
while(ton[mexall])mexall++;
for(int i=1;i<n;i++){
int v,u,l;
scanf("%d%d%d",&u,&v,&l);
p[u].push_back(v);
vp[u].push_back(l);
p[v].push_back(u);
vp[v].push_back(l);
}
for(int i=1;i<=q;i++){
int x,k;
scanf("%d%d",&x,&k);
id[x].push_back(i);
d[x].push_back(k);
}
memset(ans,63,sizeof(ans));
sol(1,n);
for(int i=1;i<=q;i++){
printf("%d\n",min(mexall,ans[i]));
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 9ms
memory: 52708kb
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...