結果

問題 No.230 Splarraay スプラレェーイ
ユーザー nebukuro09nebukuro09
提出日時 2017-04-07 16:12:45
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 3,417 bytes
コンパイル時間 712 ms
コンパイル使用メモリ 117,356 KB
実行使用メモリ 9,076 KB
最終ジャッジ日時 2024-06-12 18:37:55
合計ジャッジ時間 2,902 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 1 ms
6,948 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 86 ms
7,180 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 100 ms
7,288 KB
testcase_19 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;

void main() {
    auto N = readln.chomp.to!int;
    auto Q = readln.chomp.to!int;

    auto st = new LazySegmentTree!(int, (a,b)=>a+b)(N);
    auto st2 = new LazySegmentTree!(int, (a,b)=>a+b)(N);
    long score1 = 0;
    long score2 = 0;
    
    foreach (q; 0..Q) {
        auto s = readln.split.map!(to!int);
        auto x = s[0];
        auto l = s[1];
        auto r = s[2];
        
        if (x == 0) {
            auto used = st2.getVal(l, r);
            auto m = st.getVal(l, r);
            auto a = (used + m) / 2;
            auto b =  used - a;
            if (m > 0) score1 += a;
            if (m < 0) score2 += b;
        }
        else if (x == 1) {
            st.assign(l, r, 1);
            st2.assign(l, r, 1);
        }
        else if (x == 2) {
            st.assign(l, r, -1);
            st2.assign(l, r, 1);
        }

    }

    auto used = st2.getVal(0, N-1);
    auto m = st.getVal(0, N-1);
    auto a = (used + m) / 2;
    auto b =  used - a;
    score1 += a;
    score2 += b;
    
    writeln(score1, " ", score2);
}


class LazySegmentTree(T, alias op) {
    T[] table;
    T[] lazy_;
    int size;
 
    this(int n) {
        assert(bsr(n) < 29);
        size = 1 << (bsr(n) + 2);
        table = new T[](size);
        lazy_ = new T[](size);
        fill(table, 0);
        fill(lazy_, 0);
    }

    void assign(int left, int right, T num) {
        return assign(left, right, num, 0, 0, size/2-1);
    }
 
    void assign(int left, int right, T num, int i, int seg_left, int seg_right) {
        if (left > seg_right || right < seg_left) {
            return;
        }

        if (lazy_[i] == num) return;
        
        if (left <= seg_left && seg_right <= right) {
            lazy_[i] = num;
            return;
        }
        else {
            if (lazy_[i] != 0) {
                lazy_[i*2+1] = lazy_[i];
                lazy_[i*2+2] = lazy_[i];
                lazy_[i] = 0;
            }
            assign(left, right, num, i*2+1, seg_left, (seg_left+seg_right)/2);
            assign(left, right, num, i*2+2, (seg_left+seg_right)/2+1, seg_right);
        }
    }
 
    T getVal(int left, int right) {
        return getVal(left, right, 0, 0, size/2-1);
    }
 
    T getVal(int left, int right, int i, int seg_left, int seg_right) {
        if (left > seg_right || right < seg_left) {
            return 0;
        }
        else if (seg_left == seg_right) {
            if (lazy_[i] != 0) {
                table[i] = lazy_[i];
                lazy_[i] = 0;
            }
            return table[i];
        }
        else if (left <= seg_left && seg_right <= right) {
            if (lazy_[i] != 0) {
                lazy_[i*2+1] = lazy_[i]; 
                lazy_[i*2+2] = lazy_[i];
                table[i] = lazy_[i] * (seg_right - seg_left + 1);
                lazy_[i] = 0;
            }
            return table[i];
        }

        if (lazy_[i] != 0) {
            lazy_[i*2+1] = lazy_[i];
            lazy_[i*2+2] = lazy_[i];
            lazy_[i] = 0;
        }
        table[i] = op(getVal(left, right, i*2+1, seg_left, (seg_left+seg_right)/2),
                      getVal(left, right, i*2+2, (seg_left+seg_right)/2+1, seg_right));
        return table[i];
    }
}
0