結果

問題 No.1024 Children in a Row
ユーザー betrue12betrue12
提出日時 2020-04-10 23:17:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 608 ms / 2,000 ms
コード長 1,761 bytes
コンパイル時間 2,154 ms
コンパイル使用メモリ 205,220 KB
実行使用メモリ 45,352 KB
最終ジャッジ日時 2023-10-14 05:06:57
合計ジャッジ時間 17,842 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,840 KB
testcase_01 AC 6 ms
12,840 KB
testcase_02 AC 6 ms
12,692 KB
testcase_03 AC 7 ms
12,852 KB
testcase_04 AC 7 ms
12,868 KB
testcase_05 AC 7 ms
12,960 KB
testcase_06 AC 7 ms
12,876 KB
testcase_07 AC 7 ms
12,828 KB
testcase_08 AC 7 ms
12,768 KB
testcase_09 AC 7 ms
13,072 KB
testcase_10 AC 547 ms
31,256 KB
testcase_11 AC 541 ms
28,520 KB
testcase_12 AC 551 ms
29,712 KB
testcase_13 AC 558 ms
30,196 KB
testcase_14 AC 540 ms
29,864 KB
testcase_15 AC 555 ms
30,944 KB
testcase_16 AC 512 ms
30,676 KB
testcase_17 AC 502 ms
34,820 KB
testcase_18 AC 468 ms
29,668 KB
testcase_19 AC 599 ms
36,288 KB
testcase_20 AC 607 ms
36,220 KB
testcase_21 AC 605 ms
36,356 KB
testcase_22 AC 608 ms
36,360 KB
testcase_23 AC 561 ms
37,724 KB
testcase_24 AC 571 ms
37,896 KB
testcase_25 AC 560 ms
37,640 KB
testcase_26 AC 524 ms
40,504 KB
testcase_27 AC 496 ms
35,176 KB
testcase_28 AC 526 ms
45,352 KB
testcase_29 AC 471 ms
43,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    int N, M;
    cin >> N >> M;
    static vector<int> edges[400000];
    vector<int> V(N+M);
    vector<int> active(N), bef(N+M);
    vector<int> K(N+M);
    for(int i=0; i<N; i++){
        V[i] = i;
        active[i] = i;
        if(i) edges[i-1].push_back(i);
    }
    for(int i=N; i<N+M; i++){
        int a, b, k;
        cin >> a >> b >> k;
        a--; b--; k--;
        K[i] = k;
        edges[active[b]].push_back(i);
        V[i] = a;
        bef[i] = active[a];
        active[a] = i;
    }
    for(int i=0; i<N+M; i++) reverse(edges[i].begin(), edges[i].end());

    vector<int> order;
    vector<int> subnum(N+M), pos(N+M);
    auto dfs = [&](auto&& dfs, int i)->int{
        int res = 0;
        pos[i] = order.size();
        if(active[V[i]] == i){
            order.push_back(V[i]);
            res++;
        }
        for(int j : edges[i]) res += dfs(dfs, j);
        subnum[i] = res;
        return res;
    };

    dfs(dfs, 0);


    for(int i=N; i<N+M; i++){
        int posbef = pos[bef[i]];
        int posaft = pos[i];
        int num = subnum[i];
        int ans = 0;
        if(K[i] < min(posbef, posaft) || K[i] >= max(posbef, posaft+num)){
            ans = order[K[i]];
        }else if(posbef <= posaft){
            int R = K[i] - posbef;
            if(R < num){
                ans = order[posaft+R];
            }else{
                ans = order[K[i]-num];
            }
        }else{
            int R = K[i] - posaft;
            int mid = posbef-posaft-num;
            if(R < mid){
                ans = order[K[i]+num];
            }else{
                ans = order[posaft+R-mid];
            }
        }
        cout << ans+1 << endl;
    }
    return 0;
}
0