QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#625121 | #8235. Top Cluster | bessie_goes_moo# | TL | 0ms | 18108kb | C++17 | 2.4kb | 2024-10-09 17:31:35 | 2024-10-09 17:31:36 |
Judging History
answer
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int read(){
int red=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){ if(ch=='-') f=-f;ch=getchar();}
while(ch>='0'&&ch<='9') red=red*10+ch-48,ch=getchar();
return red*f;
}
LL LLread(){
LL red=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){ if(ch=='-') f=-f;ch=getchar();}
while(ch>='0'&&ch<='9') red=red*10+ch-48,ch=getchar();
return red*f;
}
const int N=5e5+5,E=N<<1,inf=1<<30;
int n,q,a[N],f[N][21],dep[N],p1[N],p2[N];
LL S[N];
int son[E],nxt[E],lnk[N],tot,w[E];
void add_e(int x,int y,int z){
son[++tot]=y,nxt[tot]=lnk[x],lnk[x]=tot,w[tot]=z;
}
#define v son[j]
void dfs(int x,int fa=0){
f[x][0]=fa;
for(int i=1;i<=20;i++) f[x][i]=f[f[x][i-1]][i-1];
for(int j=lnk[x];j;j=nxt[j]) if(v^fa){
S[v]=S[x]+w[j];dep[v]=dep[x]+1;
dfs(v,x);
}
}
int LCA(int x,int y){
if(dep[x]>dep[y]) swap(x,y);
for(int i=20;~i;i--) if(dep[f[y][i]]>=dep[x]) y=f[y][i];
for(int i=20;~i;i--) if(f[x][i]^f[y][i]) x=f[x][i],y=f[y][i];
return x==y?x:f[x][0];
}
typedef pair<int,int> PII;
#define mp make_pair
map<PII,int>LC;
LL dis(int x,int y){
if(x>y) swap(x,y);
if(LC.count(mp(x,y))) return LC[mp(x,y)];
return LC[mp(x,y)]=S[x]+S[y]-2*S[LCA(x,y)];
}
int id[N],Mex;
map<int,bool>H;
bool cmp(int x,int y){return a[x]<a[y];}
bool check(int i,int x,LL k){
return dis(p1[i],x)<=k&&dis(p2[i],x)<=k;
}
int main(){
n=read(),q=read();
for(int i=1;i<=n;i++) a[i]=read(),H[a[i]]=1,id[i]=i;a[n+1]=inf,id[n+1]=n+1;
while(H[Mex]) Mex++;
for(int i=1;i<n;i++){
int x=read(),y=read(),z=read();
add_e(x,y,z),add_e(y,x,z);
}
dfs(1);
sort(id+1,id+1+n,cmp);p1[1]=id[1],p2[1]=id[1];
for(int i=2;i<=n;i++){
int x=id[i];
p1[i]=p1[i-1],p2[i]=p2[i-1];
LL now=dis(p1[i],p2[i]),Nxt;
Nxt=dis(x,p2[i-1]);
if(Nxt>now){
now=Nxt;
p1[i]=x;p2[i]=p2[i-1];
}
Nxt=dis(x,p1[i-1]);
if(Nxt>now){
now=Nxt;
p1[i]=p1[i-1];p2[i]=x;
}
}
while(q--){
int x=read();LL k=LLread();
int l=1,r=n,mid;
while(l<=r){
mid=l+r>>1;
if(check(mid,x,k)) l=mid+1;
else r=mid-1;
}
printf("%d\n",min(Mex,a[id[l]]));
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 18108kb
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...
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...