QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#659742#8235. Top Cluster111111qqqqqqWA 512ms88416kbC++232.8kb2024-10-19 21:44:082024-10-19 21:44:08

Judging History

你现在查看的是最新测评结果

  • [2024-10-19 21:44:08]
  • 评测
  • 测评结果:WA
  • 用时:512ms
  • 内存:88416kb
  • [2024-10-19 21:44:08]
  • 提交

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;});
    bool f=0;
    if(!mp[0]) {
        pos=0;f=1;
    }
    else {
        dd[0]=0;
        d1[0]=d2[0]=mp[0];
    }
    for(int i=1;i<n;i++) {//求d1/d2
        if(f) break;
        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]);
        int mx=max({dis1,dis2,dis3});
        dd[i]=mx;
        if(mx==dis1) d1[i]=d1[i-1],d2[i]=d2[i-1];
        else if(mx==dis2) 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]<<" "<<dd[i]<<endl;
    // cout<<pos<<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个端点之一和这个点的距离
*/

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 13876kb

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: 512ms
memory: 88416kb

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: -100
Wrong Answer
time: 507ms
memory: 88384kb

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:

59
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
250...

result:

wrong answer 1st numbers differ - expected: '0', found: '59'