結果

問題 No.905 Sorted?
ユーザー nebukuro09nebukuro09
提出日時 2019-10-11 21:38:49
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 2,025 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 110,756 KB
実行使用メモリ 23,136 KB
最終ジャッジ日時 2023-09-04 03:01:20
合計ジャッジ時間 4,490 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 6 ms
5,040 KB
testcase_08 AC 222 ms
16,388 KB
testcase_09 AC 141 ms
12,252 KB
testcase_10 AC 209 ms
19,304 KB
testcase_11 AC 187 ms
17,664 KB
testcase_12 AC 215 ms
19,748 KB
testcase_13 AC 203 ms
22,816 KB
testcase_14 AC 239 ms
22,736 KB
testcase_15 AC 194 ms
23,036 KB
testcase_16 AC 245 ms
23,100 KB
testcase_17 AC 243 ms
23,136 KB
testcase_18 AC 180 ms
17,572 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

module bbb;import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;


void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto M = readln.chomp.to!int;
    auto Q = iota(M).map!(_ => readln.split.map!(to!int).array).array;

    int[] solve() {
        auto ans = new int[](M);
        auto st = new SegmentTree!(int, (a,b)=>(a+b), 0)(N);
        foreach (i; 0..N-1) st.assign(i, A[i] <= A[i+1]);
        foreach (i, q; Q) {
            int l = q[0];
            int r = q[1];
            if (l == r) {
                ans[i] = 1;
            } else {
                ans[i] = st.query(l, r-1) == r - l;
            }
        }
        return ans;
    }

    auto ans1 = solve;
    foreach (i; 0..N) A[i] *= -1;
    auto ans2 = solve;

    foreach (i; 0..M) {
        writeln(ans1[i], " ", ans2[i]);
    }
}

class SegmentTree(T, alias op, T e) {
    T[] table;
    int size;
    int offset;

    this(int n) {
        size = 1;
        while (size <= n) size <<= 1;
        size <<= 1;
        table = new T[](size);
        fill(table, e);
        offset = size / 2;
    }

    void assign(int pos, T val) {
        pos += offset;
        table[pos] = val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    void add(int pos, T val) {
        pos += offset;
        table[pos] += val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    T query(int l, int r) {
        return query(l, r, 1, 0, offset-1);
    }

    T query(int l, int r, int i, int a, int b) {
        if (b < l || r < a) {
            return e;
        } else if (l <= a && b <= r) {
            return table[i];
        } else {
            return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
        }
    }
}
0