Submission #1228649


Source Code Expand

/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.6.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;

int main() {
    auto sc = new Scanner(stdin);
    int q;
    sc.read(q);
    // Tree!(Engine, data type, op(data, data))
    // can change Engine ex AVLNode, AANode(ituka tukuru), RBSTNode(ituka tukuru), TreapNode(ituka), ...
    Tree!(AVLNode, int, max) tr;
    foreach (i; 0..q) {
        int t, x;
        sc.read(t, x);
        if (t == 1) {
            // binSearch!(pred), (y => y >= x) == auto pred(auto y) {return y >= x;}
            // find smallest i, s.t. pred(op(a1, a2, ..., ai)) == 1
            // find smallest {max(a1, a2, .., ai) >= x} == lowerbound
            tr.insert(x, tr.binSearch!(y => y >= x)(-1));
        } else {
            x--;
            // can access with []
            writeln(tr[x]);
            tr.removeAt(x);
        }
    }
    return 0;
}


import std.traits : isInstanceOf;

struct AVLNode(T, alias op) {
    alias Node = typeof(this);
    import std.math : abs;
    AVLNode*[2] ch;
    int length, lv;
    T v;
    this(in T v) {
        length = 1;
        this.v = v;
    }
    this(Node* l, Node* r) {
        ch = [l, r];
        update();
    }
    void update() {
        length = ch[0].length + ch[1].length;
        lv = max(ch[0].lv, ch[1].lv) + 1;
        v = op(ch[0].v, ch[1].v);
    }
    //type0 : ((a, b), c) -> (a, (b, c))
    //type1 : (a, (b, c)) -> ((a, b), c)
    Node* rot(int type) {
        assert(type == 0 || type == 1);
        auto buf = ch[type];
        ch[type] = buf.ch[1-type];
        buf.ch[1-type] = &this;
        update();
        buf.update();
        return buf;
    }
    Node* insert(in T v, int k) {
        assert(0 <= k && k <= length);
        if (length == 1) {
            if (k == 0) {
                return new Node(new Node(v), &this);
            } else {
                return new Node(&this, new Node(v));
            }
        }

        int type;
        if (k < ch[0].length) {
            ch[0] = ch[0].insert(v, k);
            type = 0;
        } else {
            ch[1] = ch[1].insert(v, k-ch[0].length);
            type = 1;
        }
        update();
        if (abs(ch[0].lv - ch[1].lv) <= 1) return &this;
        if (lv - ch[type].ch[1-type].lv == 2 && lv - ch[type].ch[type].lv == 3) {
            ch[type] = ch[type].rot(1-type);
            update();
        }
        return rot(type);
    }
    Node* removeAt(int k) {
        assert(0 <= k && k < length);
        if (length == 1) {
            return null;
        }
        int type;
        if (k < ch[0].length) {
            type = 0;
            ch[0] = ch[0].removeAt(k);
            if (ch[0] is null) return ch[1];
        } else {
            type = 1;
            ch[1] = ch[1].removeAt(k-ch[0].length);
            if (ch[1] is null) return ch[0];
        }
        update();
        if (abs(ch[0].lv - ch[1].lv) <= 1) return &this;
        if (lv - ch[1-type].ch[type].lv == 2 && lv - ch[1-type].ch[1-type].lv == 3) {
            ch[1-type] = ch[1-type].rot(type);
            update();
        }
        return rot(1-type);
    }
    //pred([a1, a2, ..., ai]) = 1 -> return i, search min i, assume pred([]) = 0 & assume pred(all+ex) = 1
    T at(int k) {
        assert(0 <= k && k < length);
        if (length == 1) return v;
        if (k < ch[0].length) return ch[0].at(k);
        return ch[1].at(k-ch[0].length);
    }    
}
int binSearch(alias pred, N : AVLNode!(E, op), E, alias op)(N* n, E sm) {
    with (n) {
        if (length == 1) {
            if (pred(op(sm, v))) return 0;
            return 1;
        }
        if (pred(op(sm, ch[0].v))) return ch[0].binSearch!pred(sm);
        return ch[0].length + ch[1].binSearch!pred(op(sm, ch[0].v));
    }
}

struct Tree(alias Engine, T, alias op) {
    import std.math : abs;
    alias Node = Engine!(T, op);
    Node* tr;
    @property int length() {
        return (!tr ? 0 : tr.length);
    }
    this(T v) {
        tr = new Node(v);
    }
    void insert(T v, int k) {
        if (tr is null) {
            tr = new Node(v);
            return;
        }
        tr = tr.insert(v, k);
    }
    T opIndex(int k) {
        return tr.at(k);
    }
    void removeAt(int k) {
        tr = tr.removeAt(k);
    }
}

int binSearch(alias pred, T : Tree!(Engine, E, op), E, alias Engine, alias op)(T t, E e) {
    if (t.tr is null) return 0;
    return t.tr.binSearch!pred(e);
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }
    unittest {
        import std.stdio;
        auto l = [1, 2, 3, 4, 5];
        assert(l.fold!"a+b"(10) == 25);
    }
}
version (X86) static if (__VERSION__ < 2071) {
    int bsf(ulong v) {
        foreach (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;
    }
    int bsr(ulong v) {
        foreach_reverse (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;   
    }
    int popcnt(ulong v) {
        int c = 0;
        foreach (i; 0..64) {
            if (v & (1UL << i)) c++;
        }
        return c;
    }
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray; 
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            if (f.eof) return false;
            line = lineBuf[];
            f.readln(line);
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                //string or char[10] etc
                //todo optimize
                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else {
                auto buf = line.split.map!(to!E).array;
                static if (isStaticArray!T) {
                    //static
                    assert(buf.length == T.length);
                }
                x = buf;
                line.length = 0;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}



unittest {
    import std.path : buildPath;
    import std.file : tempDir;
    import std.algorithm : equal;
    import std.stdio : File;
    string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
    auto fout = File(fileName, "w");
    fout.writeln("1 2 3");
    fout.writeln("ab cde");
    fout.writeln("1.0 1.0 2.0");
    fout.close;
    Scanner sc = new Scanner(File(fileName, "r"));
    int a;
    int[2] b;
    char[2] c;
    string d;
    double e;
    double[] f;
    sc.read(a, b, c, d, e, f);
    assert(a == 1);
    assert(equal(b[], [2, 3]));
    assert(equal(c[], "ab"));
    assert(equal(d, "cde"));
    assert(e == 1.0);
    assert(equal(f, [1.0, 2.0]));
}

unittest {
    import std.path : buildPath;
    import std.file : tempDir;
    import std.algorithm : equal;
    import std.stdio : File, writeln;
    import std.datetime;
    string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
    auto fout = File(fileName, "w");
    foreach (i; 0..1_000_000) {
        fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
    }
    fout.close;
    writeln("Scanner Speed Test(3*1,000,000 int)");
    StopWatch sw;
    sw.start;
    Scanner sc = new Scanner(File(fileName, "r"));
    foreach (i; 0..500_000) {
        int a, b, c;
        sc.read(a, b, c);
        assert(a == 3*i);
        assert(b == 3*i+1);
        assert(c == 3*i+2);
    }
    foreach (i; 500_000..700_000) {
        int[3] d;
        sc.read(d);
        int a = d[0], b = d[1], c = d[2];
        assert(a == 3*i);
        assert(b == 3*i+1);
        assert(c == 3*i+2);
    }
    foreach (i; 700_000..1_000_000) {
        int[] d;
        sc.read(d);
        assert(d.length == 3);
        int a = d[0], b = d[1], c = d[2];
        assert(a == 3*i);
        assert(b == 3*i+1);
        assert(c == 3*i+2);
    }
    writeln(sw.peek.msecs, "ms");
}

Submission Info

Submission Time
Task C - データ構造
User yosupo
Language D (LDC 0.17.0)
Score 100
Code Size 9726 Byte
Status AC
Exec Time 200 ms
Memory 13436 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 18
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
subtask1_01.txt AC 1 ms 256 KB
subtask1_02.txt AC 1 ms 256 KB
subtask1_03.txt AC 1 ms 256 KB
subtask1_04.txt AC 6 ms 764 KB
subtask1_05.txt AC 13 ms 1276 KB
subtask1_06.txt AC 1 ms 256 KB
subtask1_07.txt AC 100 ms 5628 KB
subtask1_08.txt AC 84 ms 1916 KB
subtask1_09.txt AC 79 ms 1916 KB
subtask1_10.txt AC 170 ms 10108 KB
subtask1_11.txt AC 165 ms 9212 KB
subtask1_12.txt AC 200 ms 13436 KB
subtask1_13.txt AC 158 ms 7548 KB
subtask1_14.txt AC 160 ms 9340 KB
subtask1_15.txt AC 104 ms 7548 KB
subtask1_16.txt AC 126 ms 8572 KB