結果

問題 No.619 CardShuffle
ユーザー nebukuro09nebukuro09
提出日時 2018-01-12 17:37:58
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,139 ms / 3,000 ms
コード長 3,512 bytes
コンパイル時間 1,123 ms
コンパイル使用メモリ 106,728 KB
実行使用メモリ 85,248 KB
最終ジャッジ日時 2023-09-03 18:08:06
合計ジャッジ時間 18,215 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 954 ms
71,684 KB
testcase_17 AC 965 ms
71,108 KB
testcase_18 AC 935 ms
71,952 KB
testcase_19 AC 946 ms
70,864 KB
testcase_20 AC 459 ms
41,980 KB
testcase_21 AC 1,130 ms
85,248 KB
testcase_22 AC 871 ms
70,928 KB
testcase_23 AC 1,139 ms
85,012 KB
testcase_24 AC 869 ms
70,920 KB
testcase_25 AC 362 ms
34,700 KB
testcase_26 AC 847 ms
71,944 KB
testcase_27 AC 1,052 ms
71,176 KB
testcase_28 AC 837 ms
72,236 KB
testcase_29 AC 1,054 ms
71,452 KB
testcase_30 AC 525 ms
49,616 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 398 ms
37,536 KB
testcase_33 AC 703 ms
70,912 KB
testcase_34 AC 812 ms
71,972 KB
testcase_35 AC 178 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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+1, B.dup);
    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) {
            if (st.ops[a] == st.ops[b]) continue;
            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 {
            if (A[a] == A[b]) continue;
            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 && a2.length == 1) {
                ret = [tmp];
            } else if (a1.length == 1) {
                ret = tmp ~ a2[1..$];
            } else if (a2.length == 1) {
                ret = a1[0..$-1] ~ tmp;
            } else {
                foreach (x; a1[1..$-1]) (tmp += x) %= MOD;
                foreach (x; a2[1..$-1]) (tmp += x) %= MOD;
                ret = [a1.front, tmp, 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 {
            auto a = query(pl, pr, i*2+1, left, (left+right)/2);
            auto b = query(pl, pr, i*2+2, (left+right)/2+1, right);
            auto x =  merge((left + right) / 2, a, b);
            return x;
        }
    }
}
0