QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#526793#4401. Prizebachbeo20070 0ms0kbC++232.3kb2024-08-21 20:26:382024-08-21 20:26:40

Judging History

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

  • [2024-08-21 20:26:40]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-08-21 20:26:38]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
const int maxl = 20;
int n,m,q;
struct Tree{
    int L[maxn],T,rt=-1;
    vector<int> edge[maxn];
    int dep[maxn],par[maxn][maxl];
    void dfs(int u,int p){
        par[u][0]=p,dep[u]=dep[p]+1;
        for(int i=1;i<18;i++) par[u][i]=par[par[u][i-1]][i-1];
        for(int v:edge[u]) dfs(v,u);
    }
    int lca(int u,int v){
        if(dep[u]>dep[v]) swap(u,v);
        for(int i=0;i<18;i++) if((dep[v]-dep[u])>>i&1) v=par[v][i];
        if(u==v) return u;
        for(int i=17;i>=0;i--) if(par[u][i]!=par[v][i]) u=par[u][i],v=par[v][i];
        return par[u][0];
    }

    int f[maxn],d[maxn];
    int findpar(int u){
        if(u==f[u]) return u;
        int x=f[u];f[u]=findpar(f[u]);
        d[u]+=d[x];
        return f[u];
    }
    void unions(int u,int v,int w){
        int pu=findpar(u),pv=findpar(v);
        if(pu==pv) return;
        f[pv]=pu;d[pv]+=d[u]-d[v]+w;
    }

    void build(){
        iota(f+1,f+n+1,1);
        for(int i=1;i<=n;i++){
            int p;cin >> p;
            if(p==-1) rt=i;
            else edge[p].push_back(i);
        }
        dfs(rt,0);
    }
}T1,T2;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin >> n >> m >> q >> q;
    T1.build();T2.build();
    vector<int> ord(n);
    iota(ord.begin(),ord.end(),1);
    sort(ord.begin(),ord.end(),[&](int x,int y){
        return T1.L[x]<T1.L[y];
    });
    while((int)ord.size()>m) ord.pop_back();
    sort(ord.begin(),ord.end(),[&](int x,int y){
        return T2.L[x]<T2.L[y];
    });
    for(int i=0;i<m;i++) cout << ord[i] << ' ';
    cout << endl;
    for(int i=1;i<m;i++){
        int u=ord[i-1],v=ord[i];
        cout << "? " << u << ' ' << v << endl;
        int x,y,a=T1.lca(u,v);cin >> x >> y;
        T1.unions(a,u,x);T1.unions(a,v,y);
        a=T2.lca(u,v);cin >> x >> y;
        T2.unions(a,u,x);T2.unions(a,v,y);
    }
    cout << "!" << endl;
    for(int i=1;i<=n;i++) T1.findpar(i),T2.findpar(i);
    for(int i=1;i<=q;i++){
        int u,v;cin >> u >> v;
        int a=T1.lca(u,v);
        cout << T1.d[u]+T1.d[v]-2*T1.d[a] << ' ';
        a=T2.lca(u,v);
        cout << T2.d[u]+T2.d[v]-2*T2.d[a] << endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

500000 64682 64681 100000
46115
470589
209303
2979
473162
343535
79503
299539
404621
102085
237721
279170
392890
165201
441593
456314
218991
358478
86614
410800
159785
169761
95368
285837
297549
370283
378974
26449
444381
39320
149913
404523
144109
174828
263837
49847
468694
478535
152644
216598
301...

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #13:

score: 0
Runtime Error

input:

500000 88721 177440 100000
30974
23891
211201
125199
180489
387190
218020
498838
230147
307989
484136
257785
353027
304420
311738
169842
334090
486070
126212
328609
174959
368840
238722
418092
488389
226349
427271
457322
332454
12958
197530
264474
355717
482774
221286
282148
216441
266659
213750
628...

output:


result:


Subtask #3:

score: 0
Runtime Error

Test #25:

score: 0
Runtime Error

input:

500000 200 199 40000
76296
130139
291501
292412
139543
433345
372726
451574
18315
465578
324564
477223
237354
81532
65170
465332
342130
9670
193303
193680
129668
149532
268907
89969
398275
356210
324593
433492
482232
466692
135343
433758
102545
287283
432859
351864
305769
489532
101532
450535
295762...

output:


result:


Subtask #4:

score: 0
Runtime Error

Test #37:

score: 0
Runtime Error

input:

1000000 1000 999 100000
678746
439069
32542
85937
936926
284219
461661
203235
533462
940676
230275
621140
780674
254931
562355
229273
201341
493976
358955
963527
880412
91220
474599
160086
698841
591551
718276
844558
39859
765917
34722
401724
219774
443004
682244
545401
968419
968020
354030
411187
1...

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%