結果

問題 No.619 CardShuffle
ユーザー nebukuro09nebukuro09
提出日時 2018-01-12 14:27:25
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 3,239 bytes
コンパイル時間 1,696 ms
コンパイル使用メモリ 106,124 KB
実行使用メモリ 121,284 KB
最終ジャッジ日時 2023-09-03 18:06:22
合計ジャッジ時間 29,490 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 WA -
testcase_17 AC 1,661 ms
120,656 KB
testcase_18 WA -
testcase_19 AC 1,614 ms
120,196 KB
testcase_20 AC 888 ms
50,892 KB
testcase_21 WA -
testcase_22 AC 2,131 ms
120,088 KB
testcase_23 WA -
testcase_24 AC 2,237 ms
120,396 KB
testcase_25 AC 1,068 ms
71,516 KB
testcase_26 WA -
testcase_27 AC 1,092 ms
79,328 KB
testcase_28 WA -
testcase_29 AC 1,082 ms
81,208 KB
testcase_30 AC 682 ms
51,208 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 944 ms
50,228 KB
testcase_33 AC 1,823 ms
120,680 KB
testcase_34 WA -
testcase_35 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto C = readln.split;
    auto A = iota(0, N, 2).map!(i => C[i].to!long).array;
    auto B = iota(1, N, 2).map!(i => C[i]).array;

    auto st = new SegmentTree(N/2, B);
    foreach (i; 0..N/2+1) st.update(i, A[i]);

    auto Q = readln.chomp.to!int;
    while (Q--) {
        auto q = readln.split;
        auto a = q[1].to!int - 1;
        auto b = q[2].to!int - 1;
        auto parity = a % 2;
        a /= 2;
        b /= 2;
        if (q[0] == "?") {
            st.query(a, b).writeln;
        } else if (parity) {
            swap(st.ops[a], st.ops[b]);
            st.update(a, A[a]);
            st.update(a+1, A[a+1]);
            st.update(b, A[b]);
            st.update(b+1, A[b+1]);
        } else {
            swap(A[a], A[b]);
            st.update(a, A[a]);
            st.update(b, A[b]);
        }
    }
}

class SegmentTree {
    long[][] table;
    string[] ops;
    int size;

    this(int n, string[] ops) {
        assert(bsr(n) < 29);
        size = 1 << (bsr(n) + 2);
        table = new long[][](size);
        this.ops = ops;
    }

    long[] merge(int mid, long[] a1, long[] a2) {
        long[] ret;
        if (a1 == [] || a2 == []) {
            ret = a1 ~ a2;
        } else if (ops[mid] == "+") {
            long tmp = 0;
            foreach (x; a1[1..$]) (tmp += x) %= MOD;
            foreach (x; a2[0..$-1]) (tmp += x) %= MOD;
            ret = [];
            ret ~= a1.front;
            if (tmp > 0) ret ~= tmp;
            ret ~= a2.back;
        } else {
            long tmp = a1.back * a2.front % MOD;
            if (a1.length > 1) foreach (x; a1[1..$-1]) (tmp += x) %= MOD;
            if (a2.length > 1) foreach (x; a2[1..$-1]) (tmp += x) %= MOD;
            ret = [];
            if (a1.length > 1) ret ~= a1.front;
            ret ~= tmp;
            if (a2.length > 1) ret ~= a2.back;
        }
        return ret;
    }
    
    void update(int pos, long num) {
        return update(pos, num, 0, 0, size/2-1);
    }

    void update(int pos, long num, int i, int left, int right) {
        if (left == right) {
            table[i] = [num];
            return;
        }
        
        auto mid = (left + right) / 2;
        if (pos <= mid)
            update(pos, num, i*2+1, left, mid);
        else
            update(pos, num, i*2+2, mid+1, right);

        table[i] = merge(mid, table[i*2+1], table[i*2+2]);
    }

    long query(int pl, int pr) {
        auto X = query(pl, pr, 0, 0, size/2-1);
        long ret = 0;
        foreach (x; X) (ret += x) %= MOD;
        return ret;
    }

    long[] query(int pl, int pr, int i, int left, int right) {
        if (pl > right || pr < left) {
            return [];
        } else if (pl <= left && right <= pr) {
            return table[i];
        } else {
            return merge((left + right) / 2,
                         query(pl, pr, i*2+1, left, (left+right)/2),
                         query(pl, pr, i*2+2, (left+right)/2+1, right));
        }
    }
}
0