結果

問題 No.674 n連勤
ユーザー nanaenanae
提出日時 2018-04-30 11:40:54
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 3,014 bytes
コンパイル時間 788 ms
コンパイル使用メモリ 103,536 KB
実行使用メモリ 11,620 KB
最終ジャッジ日時 2023-09-03 20:06:27
合計ジャッジ時間 2,685 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 14 ms
4,376 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 38 ms
6,948 KB
testcase_14 AC 111 ms
11,620 KB
testcase_15 AC 75 ms
10,344 KB
testcase_16 AC 99 ms
10,408 KB
testcase_17 AC 96 ms
9,776 KB
testcase_18 AC 64 ms
10,560 KB
testcase_19 AC 144 ms
9,572 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(28): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`

ソースコード

diff #

import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons;
import std.math, std.numeric;

alias Seg = Tuple!(long, "l", long, "r");

void main() {
    long d; int q; scan(d, q);

    long[] a;
    Seg[]  ss = new Seg[](q);

    foreach (i ; 0 .. q) {
        long li, ri;
        scan(li, ri); ri++;
        ss[i] = Seg(li, ri);
        a ~= li; a ~= ri;
    }

    a = a.sort().uniq().array;

    debug {
        writeln("a:", a);
    }

    int[long] map;
    long[] rmap = new long[](2*q);
    foreach (int i, ai; a) {
        map[ai] = i;
        rmap[i] = ai;
    }

    debug {
        writeln("map:", map);
        writeln("rmap:", rmap);
    }

    auto sd = SqrtDecomp(2*q);

    long ans;

    foreach (i ; 0 .. q) {
        int l = min(map[ss[i].l], sd.query(map[ss[i].l]).l.to!int);
        int r = max(map[ss[i].r], sd.query(map[ss[i].r]).r.to!int);
        ans = max(ans, rmap[r] - rmap[l]);
        sd.update(l, r + 1, Seg(l, r));
        writeln(ans);

        debug {
            writefln("(%d, %d)", l, r);
            writeln(sd.bucket);
            writeln(sd.data);
        }
    }
}



struct SqrtDecomp {
    private {
        enum root = 512;
        enum inf  = 10^^9;
        Seg[] data; int N;
        Seg[] bucket; int K;
    }

    this(int n) {
        K = (n + root - 1) / root;
        N = K * root;
        data = new Seg[](N);
        data[] = Seg(inf, -inf);
        bucket = new Seg[](K);
        bucket[] = Seg(inf, -inf);
    }

    Seg query(int x) {
        int k = x / root;
        if (bucket[k].l == inf) {
            return data[x];
        }
        else {
            foreach (i ; k * root .. (k + 1) * root) {
                data[i] = bucket[k];
            }
            bucket[k] = Seg(inf, -inf);
            return data[x];
        }
    }

    void update(int l, int r, Seg s) {
        foreach (k ; 0 .. K) {
            int lb = k * root, rb = (k + 1) * root;
            if (r <= lb || rb <= l) continue;
            if (l <= lb && rb <= r) {
                bucket[k] = s;
            }
            else {
                if (bucket[k].l != inf) {
                    foreach (i ; k*root .. (k+1)*root) {
                        data[i] = bucket[k];
                    }
                    bucket[k] = Seg(inf, -inf);
                }
                foreach (i ; max(l, lb) .. min(r, rb)) {
                    data[i] = s;
                }
            }
        }
    }
}






void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
0