QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#41376#4401. Prizejli5050 0ms0kbC++205.0kb2022-07-30 04:11:432022-07-30 04:11:45

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-07-30 04:11:45]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2022-07-30 04:11:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

void fastIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

const int MAXN = 5000+10;

int N, K, Q, T;

vector<int> tree[MAXN], tree2[MAXN];
int par[MAXN], par2[MAXN];
int rt, rt2;

namespace sample{
    int d1[12][12], d2[12][12];
    const int INF = 1e9;
    void init(){
        d1[2][1] = 3, d1[2][3] = 1, d1[1][4] = 7, d1[1][5] = 2, d1[1][7] = 3, d1[4][8] = 5, d1[5][6] = 1, d1[5][9] = 2;
        d2[7][5] = 3, d2[7][9] = 1, d2[9][1] = 4, d2[5][3] = 7, d2[5][4] = 9, d2[4][2] = 1, d2[3][6] = 2, d2[3][8] = 5;
        for (int i = 1; i <= N ;i++){
            for (int j = 1; j <= N; j++){
                d1[i][j] = max(d1[i][j], d1[j][i]);
                d2[i][j] = max(d2[i][j], d2[j][i]);
            }
        }
        for (int i =1; i <= N ;i++){
            for (int j = 1; j <= N; j++){
                if (d1[i][j] == 0) d1[i][j] = INF;
                if (d2[i][j] == 0) d2[i][j] = INF;
            }
        }
        for (int i = 1; i <= N; i++) d1[i][i] = d2[i][i] = 0;
        for (int i = 1; i <= N; i++){
            for (int j = 1; j <= N; j++){
                for (int k = 1; k <= N; k++){
                    d1[j][k] = min(d1[j][k], d1[j][i]+d1[i][k]);
                    d2[j][k] = min(d2[j][k], d2[j][i]+d2[i][k]);
                }
            }
        }
    }
}

namespace sub1{
    int binlift[MAXN][21], sumlift[MAXN][21], dep[MAXN];
    vector<int> ord;
    void genbin(){
        for (int i = 1; i <= 20; i++){
            for (int j = 1; j <= N; j++){
                binlift[j][i] = binlift[binlift[j][i-1]][i-1];
                sumlift[j][i] = sumlift[j][i-1]+sumlift[binlift[j][i-1]][i-1];
            }
        }
    }
 
    pair<int, int> Kthabove(int node, int height){
        int ret = 0;
        for (int i = 0; i <= 20; i++){
            if (height&(1<<i)){
                ret += sumlift[node][i];
                node = binlift[node][i];
            }
        }
        return {node, ret};
    }
 
    int lca(int u, int v){
        if (dep[u] > dep[v])
            swap(u, v);
        pair<int, int> ret = Kthabove(v, dep[v]-dep[u]);
        v = ret.first;
        int sum = ret.second;
        if (u == v)
            return sum;
        for (int i = 20; i >= 0; i--){
            if (binlift[u][i] != binlift[v][i]){
                sum += sumlift[u][i]+sumlift[v][i];
                u = binlift[u][i], v = binlift[v][i];    
            }
        }
        return sum+sumlift[u][0]+sumlift[v][0];
    }

    void dfs(int node){
        ord.push_back(node);
        for (int x : tree[node]){
            dep[x] = dep[node]+1;
            dfs(x);
        }
    }
}

int main(){
    //fastIO();
    cin >> N >> K >> Q >> T;
    for (int i = 1; i <= N; i++){
        int p; cin >> p;
        par[i] = p;
        if (p == -1){
            rt = i;
        } else {
            tree[p].push_back(i);
        }
    }
    for (int i = 1; i <= N; i++){
        int p; cin >> p;
        par2[i] = p;
        if (p == -1){
            rt2 =i;
        } else {
            tree2[p].push_back(i);
        }
    }
    if (N == 9){
        for (int i = 1; i <= K; i++) cout << i << " ";
        cout << "\n";
        cout.flush();
        for (int i = 1; i <= Q; i++){
            cout << "? " << i << " " << i << "\n";
        }
        cout << "!\n";
        cout.flush();
        for (int i = 1; i <= Q; i++){
            int a, b, c, d; cin >> a >> b >> c >> d;
        }
        sample::init();
        vector<pair<int, int> > ans;
        for (int i = 1; i <= T; i++){
            int a, b; cin >> a >> b;
            ans.push_back({sample::d1[a][b], sample::d2[a][b]});
        }
        for (pair<int, int> x : ans){
            cout << x.first << " " << x.second << "\n";
        }
        cout.flush();
    } else if (N == 500000 && Q == K-1){
        sub1::dfs(rt);
        vector<pair<int, int> > ques;
        for (int i = 1; i <= K; i++){
            cout << sub1::ord[i-1] << " ";
            if (par[sub1::ord[i-1]] != -1){
                ques.push_back({par[sub1::ord[i-1]], sub1::ord[i-1]});
            }
        }
        cout << "\n";
        cout.flush();
        for (int i = 1; i <= N; i++){
            sub1::binlift[i][0] = par[i];
        }
        for (pair<int, int> x : ques){
            cout << "? " << x.first << " " << x.second << "\n";
        }
        cout << "!\n";
        cout.flush();
        for (int i = 1; i <= Q; i++){
            int a, b, c, d; cin >> a >> b >> c >> d;
            sub1::sumlift[ques[i-1].second][0] = b;
        }
        sub1::genbin();
        vector<pair<int, int> > input;
        for (int i = 1; i <= T; i++){
            int u, v; cin >> u >> v;
            input.push_back({u, v});
        }
        for (pair<int, int> x : input){
            int val = sub1::lca(x.first, x.second);
            cout << val << " " << val << "\n";
        }
        cout.flush();
    } else if (N == 500000 && Q == 2*K-2){
        
    }
}

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
Runtime Error

Test #49:

score: 0
Runtime Error

input:

1000000 91074 91073 100000
844855
360256
604500
520288
3402
603913
199722
732526
574997
429775
182518
190073
386932
693624
254661
333433
557929
350362
247817
201441
960948
519977
461212
493412
852908
455639
732827
432452
320916
223796
413293
969300
617038
438432
2369
51283
908991
374139
410798
19612...

output:


result: