QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#670047 | #8235. Top Cluster | -Ofast | TL | 3ms | 28336kb | C++20 | 3.0kb | 2024-10-23 20:19:58 | 2024-10-23 20:19:58 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+10,inf=1e18;
int n,q,a[N],u,v,w,x,k,ans[N],s[N],root,nowmin=-1,rt,now;
vector <array<int,2>> Q[N];
vector <array<int,2>> edge[N];
bool vis[N];
void init(int u,int fa){
s[u]=1;
for(int i=0;i<edge[u].size();i++){
int v=edge[u][i][0];
if(v==fa||vis[v])continue;
init(v,u);s[u]+=s[v];
}
}
void find(int u,int fa){
if(fa==0)nowmin=1e9,init(u,fa),rt=u;
int MAX=-1;
for(int i=0;i<edge[u].size();i++){
int v=edge[u][i][0];
if(v==fa||vis[v])continue;
find(v,u);MAX=max(MAX,s[v]);
}
MAX=max(MAX,s[rt]-s[u]);
if(nowmin>MAX){
nowmin=MAX;
root=u;
}
}
int tree[N<<2];
void pushup(int rt){tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);}
void update(int rt,int left,int right,int x,int y){
if(left==right){tree[rt]=y;return;}
int mid=(left+right)>>1;
if(x<=mid)update(rt<<1,left,mid,x,y);
else update(rt<<1|1,mid+1,right,x,y);
pushup(rt);
}
int query(int rt,int left,int right,int x){
if(left==right)return left;
int mid=(left+right)>>1;
if(tree[rt<<1]>x)return query(rt<<1,left,mid,x);
else return query(rt<<1|1,mid+1,right,x);
}
void upd(int u,int fa,int op,int dep){
if(a[u]<=n){
if(op==1)update(1,0,n,a[u],dep);
else update(1,0,n,a[u],-inf);
}
for(auto it:edge[u]){
int v=it[0],w=it[1];
if(vis[v]||v==fa)continue;
upd(v,u,op,dep+w);
}
}
void calc(int u,int fa,int dep){
for(auto it:Q[u]){
int k=it[0],id=it[1];
ans[id]=min(ans[id],query(1,0,n,k-dep));
}
for(auto it:edge[u]){
int v=it[0],w=it[1];
if(vis[v]||v==fa)continue;
calc(v,u,dep+w);
}
}
void solve(int u){
vis[u]=true;
for(int i=0;i<edge[u].size();i++){
int v=edge[u][i][0];int w=edge[u][i][1];
if(vis[v])continue;
upd(v,0,1,w);
}
for(auto it:Q[u]){
int k=it[0],id=it[1];
ans[id]=min(ans[id],query(1,0,n,k));
}
if(a[u]<=n)update(1,0,n,a[u],0);
for(int i=0;i<edge[u].size();i++){
int v=edge[u][i][0];int w=edge[u][i][1];
if(vis[v])continue;
upd(v,0,-1,w);
calc(v,0,w);
upd(v,0,1,w);
}
if(a[u]<=n)update(1,0,n,a[u],-inf);
for(int i=0;i<edge[u].size();i++){
int v=edge[u][i][0];int w=edge[u][i][1];
if(vis[v])continue;
upd(v,0,-1,w);
}
for(int i=0;i<edge[u].size();i++){
int v=edge[u][i][0];
if(vis[v])continue;
find(v,0);solve(root);
}
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
memset(tree,-0x3f,sizeof(tree));
memset(ans,0x3f,sizeof(ans));
cin>>n>>q;
vector <int> p;
for(int i=1;i<=n;i++)cin>>a[i],p.push_back(a[i]);
sort(p.begin(),p.end());
if(p[0]==0){
p.push_back(2e9);
for(int i=0;i+1<p.size();i++){
if(p[i]+1!=p[i+1]){
for(int j=1;j<=q;j++)
ans[j]=p[i]+1;
break;
}
}
}else memset(ans,0,sizeof(ans));
for(int i=1;i<n;i++){
cin>>u>>v>>w;
edge[u].push_back({v,w});
edge[v].push_back({u,w});
}
for(int i=1;i<=q;i++){
cin>>x>>k;
Q[x].push_back({k,i});
}
find(1,0);
solve(root);
for(int i=1;i<=q;i++)
cout<<ans[i]<<"\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 28336kb
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...