結果

問題 No.1024 Children in a Row
ユーザー betrue12betrue12
提出日時 2020-04-10 23:13:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,761 bytes
コンパイル時間 2,700 ms
コンパイル使用メモリ 204,528 KB
実行使用メモリ 45,312 KB
最終ジャッジ日時 2023-10-14 04:45:17
合計ジャッジ時間 18,695 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,696 KB
testcase_01 AC 6 ms
12,876 KB
testcase_02 AC 6 ms
12,716 KB
testcase_03 WA -
testcase_04 AC 6 ms
12,808 KB
testcase_05 WA -
testcase_06 AC 7 ms
12,764 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 524 ms
31,036 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 537 ms
30,184 KB
testcase_14 AC 512 ms
29,908 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 565 ms
36,344 KB
testcase_21 WA -
testcase_22 AC 578 ms
36,208 KB
testcase_23 AC 549 ms
37,756 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 502 ms
45,312 KB
testcase_29 AC 460 ms
43,740 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