結果

問題 No.2292 Interval Union Find
ユーザー DaylightDaylight
提出日時 2023-05-06 10:30:28
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 484 ms / 5,000 ms
コード長 5,784 bytes
コンパイル時間 5,345 ms
コンパイル使用メモリ 276,164 KB
実行使用メモリ 30,088 KB
最終ジャッジ日時 2023-09-04 20:15:55
合計ジャッジ時間 29,480 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 256 ms
4,492 KB
testcase_05 AC 316 ms
4,516 KB
testcase_06 AC 258 ms
4,476 KB
testcase_07 AC 308 ms
4,476 KB
testcase_08 AC 313 ms
4,548 KB
testcase_09 AC 313 ms
4,544 KB
testcase_10 AC 294 ms
4,536 KB
testcase_11 AC 292 ms
4,492 KB
testcase_12 AC 321 ms
4,524 KB
testcase_13 AC 278 ms
4,504 KB
testcase_14 AC 283 ms
4,488 KB
testcase_15 AC 304 ms
4,552 KB
testcase_16 AC 294 ms
4,528 KB
testcase_17 AC 329 ms
4,504 KB
testcase_18 AC 312 ms
26,712 KB
testcase_19 AC 440 ms
26,732 KB
testcase_20 AC 484 ms
30,088 KB
testcase_21 AC 418 ms
16,148 KB
testcase_22 AC 425 ms
16,124 KB
testcase_23 AC 425 ms
16,216 KB
testcase_24 AC 437 ms
16,080 KB
testcase_25 AC 418 ms
16,208 KB
testcase_26 AC 424 ms
16,148 KB
testcase_27 AC 415 ms
16,152 KB
testcase_28 AC 422 ms
16,232 KB
testcase_29 AC 407 ms
16,112 KB
testcase_30 AC 413 ms
16,148 KB
testcase_31 AC 413 ms
16,092 KB
testcase_32 AC 409 ms
16,108 KB
testcase_33 AC 405 ms
16,204 KB
testcase_34 AC 407 ms
16,128 KB
testcase_35 AC 415 ms
16,160 KB
testcase_36 AC 419 ms
16,144 KB
testcase_37 AC 406 ms
16,076 KB
testcase_38 AC 398 ms
16,100 KB
testcase_39 AC 395 ms
16,196 KB
testcase_40 AC 398 ms
16,168 KB
testcase_41 AC 208 ms
4,488 KB
testcase_42 AC 217 ms
4,508 KB
testcase_43 AC 231 ms
4,472 KB
testcase_44 AC 244 ms
4,508 KB
testcase_45 AC 246 ms
4,536 KB
testcase_46 AC 253 ms
4,544 KB
testcase_47 AC 287 ms
4,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

public import std;

DList!string scan_buffer;
DList!char char_buffer;
static this() {
    scan_buffer = DList!(string)();
    char_buffer = DList!(char)();
}

void cin()() {

}

void cin(T, A...)(ref T a, ref A tail) {
    import std.traits : isArray;

    static if (typeof(a).stringof == "string") {
        if (!char_buffer.empty) {
            a = char_buffer.array.map!(x => x.to!string).join();
            char_buffer.clear();
        } else {
            while (scan_buffer.empty) {
                foreach (t; readln.split) {
                    if (t.length == 0) {
                        continue;
                    }
                    scan_buffer.insert(t);
                }
            }
            auto token = scan_buffer.front;
            scan_buffer.removeFront();
            a = token;
        }
    } else static if (typeof(a).stringof == "char") {
        if (!char_buffer.empty) {
            a = char_buffer.front();
            char_buffer.removeFront();
        } else {
            while (scan_buffer.empty) {
                foreach (t; readln.split) {
                    if (t.length == 0) {
                        continue;
                    }
                    scan_buffer.insert(t);
                }
            }
            auto token = scan_buffer.front;
            scan_buffer.removeFront();
            a = token[0];
            if (token.length > 1) {
                foreach (c; token[1 .. $]) {
                    char_buffer.insertBack(c);
                }
            }
        }
    } else static if (typeof(a).stringof == "char[]") {
        if (a.length == 0) {
            string token;
            cin(token);
            foreach (c; token) {
                a ~= c;
            }
        } else {
            foreach (ref v; a) {
                cin(v);
            }
        }
    } else static if (isArray!(typeof(a))) {
        foreach (ref v; a) {
            cin(v);
        }
    } else static if (isTuple!(typeof(a))) {
        foreach (i, _; a) {
            cin(a[i]);
        }
    } else {
        if (!char_buffer.empty) {
            writeln(char_buffer.array.map!(x => x.to!string));
            a = char_buffer.array.map!(x => x.to!string).join().to!T;
            char_buffer.clear();
        } else {
            while (scan_buffer.empty) {
                foreach (t; readln.split) {
                    if (t.length == 0) {
                        continue;
                    }
                    scan_buffer.insert(t);
                }
            }
            auto token = scan_buffer.front;
            scan_buffer.removeFront();
            a = token.to!T;
        }
    }
    cin(tail);
}

bool chmin(T)(ref T a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

bool chmax(T)(ref T a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

alias PQueue(T = long, alias less = "a<b") = BinaryHeap!(Array!T, less);
import std;

class IntervalUnionFind {
private:
    alias P = Tuple!(long, "l", long, "r");
    const long INF = (long.max - 100);
public:
    RedBlackTree!(P, "a.r!=b.r?a.r<b.r:a<b") set;
    this() {
        set = new RedBlackTree!(P, "a.r!=b.r?a.r<b.r:a<b")();
    }

    P merge(long l, long r) {
        auto it = set.upperBound(P(-INF, l));
        auto vec = new P[](0);
        foreach (p; it) {
            if (max(l, p.l) > min(r, p.r)) {
                break;
            }
            l.chmin(p.l);
            r.chmax(p.r);
            vec ~= p;
        }
        foreach (p; vec) {
            set.removeKey(p);
        }
        set.insert(P(l, r));
        return P(l, r);
    }

    void unmerge(long l, long r) {
        auto it = set.upperBound(P(-INF, l));
        auto vec = new P[](0);
        auto vec2 = new P[](0);
        foreach (p; it) {
            if (max(l, p.l) > min(r, p.r)) {
                break;
            }
            if (p.l < l) {
                vec2 ~= (P(p.l, l));
            }
            if (r < p.r) {
                vec2 ~= (P(r, p.r));
            }
            vec ~= p;
        }
        foreach (p; vec) {
            set.removeKey(p);
        }
        foreach (p; vec2) {
            set.insert(p);
        }
    }

    Nullable!P getInterval(long i) {
        auto it = set.upperBound(P(-INF, i));
        if (it.empty) {
            return Nullable!P();
        } else if (it.front.l <= i && i <= it.front.r) {
            return it.front.nullable;
        } else {
            return Nullable!P();
        }
    }

    bool opBinary(string op)(long i) {
        return !getInterval(i).isNull();
    }
}

void main() {
    int N, Q;
    cin(N, Q);
    auto dsu = new IntervalUnionFind();
    foreach (_; 0 .. Q) {
        int k;
        cin(k);
        if (k == 1) {
            int l, r;
            cin(l, r);
            dsu.merge(l, r);
        } else if (k == 2) {
            int l, r;
            cin(l, r);
            dsu.unmerge(l, r);
        } else if (k == 3) {
            int u, v;
            cin(u, v);
            auto p = dsu.getInterval(u);
            if (p.isNull()) {
                if (u == v) {
                    1.writeln;
                } else {
                    0.writeln;
                }
            } else {
                auto pp = p.get();
                if (pp.l <= v && v <= pp.r) {
                    1.writeln;
                } else {
                    0.writeln;
                }
            }
        } else if (k == 4) {
            int u;
            cin(u);
            auto p = dsu.getInterval(u);
            if (p.isNull()) {
                1.writeln;
            } else {
                auto pp = p.get();
                (pp.r - pp.l + 1).writeln;
            }

        }
    }
}
0