結果

問題 No.318 学学学学学
ユーザー noriocnorioc
提出日時 2020-05-07 12:28:08
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 2,121 bytes
コンパイル時間 1,407 ms
コンパイル使用メモリ 172,672 KB
実行使用メモリ 36,024 KB
最終ジャッジ日時 2023-09-04 07:40:41
合計ジャッジ時間 7,049 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
22,164 KB
testcase_01 AC 33 ms
22,956 KB
testcase_02 AC 36 ms
23,220 KB
testcase_03 AC 30 ms
23,488 KB
testcase_04 AC 34 ms
23,236 KB
testcase_05 AC 134 ms
36,024 KB
testcase_06 AC 121 ms
33,216 KB
testcase_07 AC 106 ms
33,796 KB
testcase_08 AC 90 ms
28,928 KB
testcase_09 AC 86 ms
26,060 KB
testcase_10 AC 69 ms
24,812 KB
testcase_11 AC 135 ms
34,472 KB
testcase_12 AC 110 ms
34,048 KB
testcase_13 AC 101 ms
33,684 KB
testcase_14 AC 87 ms
26,908 KB
testcase_15 AC 85 ms
26,148 KB
testcase_16 AC 68 ms
24,752 KB
testcase_17 AC 99 ms
34,276 KB
testcase_18 AC 96 ms
32,884 KB
testcase_19 AC 97 ms
33,108 KB
testcase_20 AC 58 ms
24,032 KB
testcase_21 AC 18 ms
19,844 KB
testcase_22 AC 17 ms
19,848 KB
testcase_23 AC 17 ms
19,896 KB
testcase_24 AC 17 ms
19,896 KB
testcase_25 AC 18 ms
19,888 KB
testcase_26 AC 17 ms
19,920 KB
testcase_27 AC 17 ms
19,916 KB
testcase_28 AC 17 ms
19,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;


const segSize = 1 << 20;
int[segSize * 2] seg;
int[segSize * 2] segTime; // 更新された時刻

void update(int l, int r, int v, int time) {
    l += segSize;
    r += segSize;
    while (l < r) {
        if (l % 2 == 1) {
            seg[l] = v;
            segTime[l] = time;
            l++;
        }
        l /= 2;
        if (r % 2 == 1) {
            seg[r - 1] = v;
            segTime[r - 1] = time;
            r--;
        }
        r /= 2;
    }
}

int get(int ind) {
    ind += segSize;
    int v = seg[ind];
    int time = segTime[ind];
    while (true) {
        ind /= 2; // 上にのぼる
        if (ind == 0) break;
        if (time < segTime[ind]) { // 更新が新しい
            v = seg[ind];
            time = segTime[ind];
        }
    }
    return v;
}

// 親の更新を子に伝搬する(親は更新されない)
void propagate() {
    void rec(int ind) {
        if (ind * 2 >= seg.length) return;

        int lt = ind * 2;
        int rt = ind * 2 + 1;
        if (segTime[ind] > segTime[lt]) { // 左の子: 親の方が新しいなら子に伝搬
            seg[lt] = seg[ind];
            segTime[lt] = segTime[ind];
        }
        if (segTime[ind] > segTime[rt]) { // 右の子: 親の方が新しいなら子に伝搬
            seg[rt] = seg[ind];
            segTime[rt] = segTime[ind];
        }
        rec(lt);
        rec(rt);
    }

    rec(1);
}

void calc(int[] a) {
    int n = cast(int)a.length;

    int[int] first, last;
    foreach_reverse(i; 0..n) first[a[i]] = i;
    foreach (i; 0..n) last[a[i]] = i;

    int time = 1;
    foreach (k; first.keys.sort) {
        int l = first[k];
        int r = last[k];
        update(l, r + 1, k, time++);
    }

    propagate();
    auto ans = seg[segSize..$].take(n).map!text.join(' ');
    writeln(ans);
}

void main() {
    read;
    auto a = readints;
    calc(a);
}

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
0