QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#659711 | #8235. Top Cluster | 111111qqqqqq | WA | 911ms | 88432kb | C++23 | 2.7kb | 2024-10-19 21:28:23 | 2024-10-19 21:28:25 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define cmin(a,b) a=min(a,b)
#define cmax(a,b) a=max(a,b)
#define modd(a,b) a=a%b
#define pb push_back
#define db double
#define lowbit(x) x&(-x)
#define cerr(x) cout<<#x<<"="<<x<<endl
#define fi first
#define se second
const ll mod=1e9+7;
#define N 500010
int n,q;
int st[N][20],id=0,dfn[N];
int d1[N],d2[N],dis[N],dd[N];
int mp[N],pos;
int dep[N],mx=0,mxx=0,id1=0,id2=0;
vector<array<int,2>>g[N];
struct node{int id,val;}e[N];
void add(int a,int b,int c) {g[a].pb({b,c});}
int get(int x,int y) {return dfn[x]<dfn[y]?x:y;}
void dfs(int u,int fa) {
st[dfn[u]=++id][0]=fa;
for(auto [v,w]:g[u]) {
if(v==fa) continue;
dis[v]=dis[u]+w;
dfs(v,u);
}
}
int lca(int x,int y) {
if(x==y) return x;
if(dfn[x]>dfn[y]) swap(x,y);
int k=__lg(dfn[y]-dfn[x]);
return get(st[dfn[x]+1][k],st[dfn[y]-(1<<k)+1][k]);
}
int dist(int x,int y) {
return dis[x]+dis[y]-2*dis[lca(x,y)];
}
void solve() {
cin>>n>>q;
for(int i=1;i<=n;i++) {cin>>e[i].val,e[i].id=i;if(e[i].val<n) mp[e[i].val]=i;}
for(int i=1;i<n;i++) {
int a,b,c;cin>>a>>b>>c;
add(a,b,c),add(b,a,c);
}
dfs(1,0);
for(int j=1;j<=__lg(n);j++) {
for(int i=1;i+(1<<j)-1<=n;i++) {
st[i][j]=get(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
sort(e+1,e+n+1,[&](node a,node b) {return a.val<b.val;});
if(!mp[0]) {
pos=0;
}
else {
dd[0]=0;
d1[0]=d2[0]=mp[0];
}
for(int i=1;i<n;i++) {//求d1/d2
if(!mp[i]) {pos=i;break;}
int dis1=dd[i-1],dis2=dist(d1[i-1],mp[i]),dis3=dist(d2[i-1],mp[i]);
if(dis1>dis2 && dis1>dis3) d1[i]=d1[i-1],d2[i]=d2[i-1];
else if(dis2>dis3) d1[i]=d1[i-1],d2[i]=mp[i];
else d1[i]=mp[i],d2[i]=d2[i-1];
}
// for(int i=0;i<n;i++) cout<<d1[i]<<" "<<d2[i]<<endl;
while(q--) {
int x,k;cin>>x>>k;
int l=0,r=pos-1,mid,ans=-1;
auto ck=[&](int mid,int x)->bool {
return dist(d1[mid],x)<=k && dist(d2[mid],x)<=k;
};
while(l<=r) {
mid=l+r>>1;
if(ck(mid,x)) ans=mid,l=mid+1;
else r=mid-1;
}
cout<<ans+1<<endl;
}
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// int T;
// cin>>T;
// while(T--)
solve();
return 0;
}
/*
既然是求mex那肯定从0到x一个一个按照顺序升序加入到极小连通子图中去
一个树对于一个点的最远距离是这个树的直径的2个端点之一和这个点的距离
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13856kb
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
Wrong Answer
time: 911ms
memory: 88432kb
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:
wrong answer 50869th numbers differ - expected: '156', found: '249999'