Submission #1228587


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;

struct AVLTree(T) {
    import std.math : abs;
    struct Node {
        int length, lv;
        Node* l, r;
        T v;
        this(in T v) {
            length = 1;
            this.v = v;
        }
        this(Node* l, Node* r) {
            this.l = l;
            this.r = r;
            length = l.length + r.length;
            lv = max(l.lv, r.lv) + 1;
            v = max(l.v, r.v);
        }
        void update() {
            length = l.length + r.length;
            v = max(l.v, r.v);
        }
        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));
                }
            }
            if (k < l.length) {
                l = l.insert(v, k);
                update();
                if (abs(l.lv - r.lv) <= 1) {
                    lv = max(l.lv, r.lv) + 1;
                    return &this;
                }
                if (lv - l.r.lv == 2 || lv - l.l.lv == 1) {
                    return new Node(l.l, new Node(l.r, r));
                }
                return new Node(new Node(l.l, l.r.l), new Node(l.r.r, r));
            } else {
                r = r.insert(v, k-l.length);
                update();
                if (abs(l.lv - r.lv) <= 1) {
                    lv = max(l.lv, r.lv) + 1;
                    return &this;
                }
                if (lv - r.l.lv == 2 || lv - r.r.lv == 1) {
                    return new Node(new Node(l, r.l), r.r);
                }
                return new Node(new Node(l, r.l.l), new Node(r.l.r, r.r));
            }
        }
        Node* removeAt(int k) {
            assert(0 <= k && k < length);
            if (length == 1) {
                return null;
            }
            if (k < l.length) {
                l = l.removeAt(k);
                if (l is null) return r;
                update();
                if (abs(l.lv - r.lv) <= 1) {
                    lv = max(l.lv, r.lv) + 1;
                    return &this;
                }
                if (r.l.lv == l.lv || r.l.lv == r.r.lv) {
                    return new Node(new Node(l, r.l), r.r);
                }
                return new Node(new Node(l, r.l.l), new Node(r.l.r, r.r));
            } else {
                r = r.removeAt(k-l.length);
                if (r is null) return l;
                update();
                if (abs(l.lv - r.lv) <= 1) {
                    lv = max(l.lv, r.lv) + 1;
                    return &this;
                }
                if (l.r.lv == r.lv || l.r.lv == l.l.lv) {
                    return new Node(l.l, new Node(l.r, r));
                }
                return new Node(new Node(l.l, l.r.l), new Node(l.r.r, r));
            }
        }
        int lowerBound(in T x) {
            if (length == 1) {
                if (x < v) return 0;
                return 1;
            }
            if (x < l.v) return l.lowerBound(x);
            return l.length + r.lowerBound(x);
        }
        T at(int k) {
            assert(0 <= k && k < length);
            if (length == 1) return v;
            if (k < l.length) return l.at(k);
            return r.at(k-l.length);
        }
        void allTest() {
            if (length == 1) {
                assert(l is null);
                assert(r is null);
                assert(lv == 0);
                return;
            }
            assert(l !is null);
            assert(r !is null);
            assert(lv == l.lv+1 || lv == l.lv+2);
            assert(lv == r.lv+1 || lv == r.lv+2);
            assert(lv != l.lv+2 || lv != r.lv+2);
            assert(length == l.length + r.length);
            l.allTest; r.allTest;
        }
    }
    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);
    }
    void insert(T v) {
        if (tr is null) {
            tr = new Node(v);
            return;
        }
        tr = tr.insert(v, tr.lowerBound(v));
    }
    T at(int k) {
        return tr.at(k);
    }
    void removeAt(int k) {
        tr = tr.removeAt(k);
    }
    void remove(T x) {
        tr = tr.removeAt(tr.lowerBound(x));
    }
    void allTest() {
        if (tr is null) return;
        tr.allTest;
    }
}

int main() {
    auto sc = new Scanner(stdin);
    int q;
    sc.read(q);
    AVLTree!int tr;
    foreach (i; 0..q) {
        int t, x;
        sc.read(t, x);
        if (t == 1) {
            tr.insert(x);
        } else {
            x--;
            writeln(tr.at(x));
            tr.removeAt(x);
        }
//        tr.allTest;
    }
    return 0;
}
/* 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 10334 Byte
Status AC
Exec Time 206 ms
Memory 15612 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 7 ms 1020 KB
subtask1_05.txt AC 14 ms 1404 KB
subtask1_06.txt AC 1 ms 256 KB
subtask1_07.txt AC 105 ms 8828 KB
subtask1_08.txt AC 96 ms 1916 KB
subtask1_09.txt AC 89 ms 1916 KB
subtask1_10.txt AC 171 ms 13052 KB
subtask1_11.txt AC 173 ms 13052 KB
subtask1_12.txt AC 206 ms 15612 KB
subtask1_13.txt AC 167 ms 11388 KB
subtask1_14.txt AC 167 ms 11644 KB
subtask1_15.txt AC 112 ms 13564 KB
subtask1_16.txt AC 137 ms 13052 KB