結果

問題 No.230 Splarraay スプラレェーイ
ユーザー mamekinmamekin
提出日時 2017-09-23 10:20:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 186 ms / 5,000 ms
コード長 4,798 bytes
コンパイル時間 1,176 ms
コンパイル使用メモリ 112,356 KB
実行使用メモリ 9,136 KB
最終ジャッジ日時 2023-08-08 23:08:33
合計ジャッジ時間 3,393 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 10 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 87 ms
6,236 KB
testcase_10 AC 88 ms
4,380 KB
testcase_11 AC 50 ms
4,764 KB
testcase_12 AC 89 ms
6,288 KB
testcase_13 AC 15 ms
4,376 KB
testcase_14 AC 88 ms
9,088 KB
testcase_15 AC 153 ms
9,068 KB
testcase_16 AC 160 ms
9,124 KB
testcase_17 AC 186 ms
9,116 KB
testcase_18 AC 129 ms
9,136 KB
testcase_19 AC 112 ms
9,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

class SegmentTree
{
public:
    typedef int T1;
    typedef int T2;

    // データの初期値、以下の条件を満たすこと
    //   uniteData(v, INIT_DATA) == v
    static const T1 INIT_DATA;

private:
    // 遅延の初期値、以下の条件を満たすこと
    //   updateData(prev, size, INIT_DELAY) == prev
    //   updateDelay(x, INIT_DELAY) == x
    static const T2 INIT_DELAY;

    // 長さがlenの区間における前回の計算結果prevに対して、
    // 区間の各要素にパラメータxを用いた更新処理を適用した後の計算結果を返す
    T1 updateData(T1 prev, int len, T2 x){
        if(x == -1)
            return prev;
        else if(x == 0)
            return 0;
        else
            return len;
    }
    // パラメータx1,x2による2つの更新処理を順に適用した場合に対して、
    // 同じ結果になるような1つの更新処理のパラメータを返す
    T2 updateDelay(T2 x1, T2 x2){
        if(x2 == -1)
            return x1;
        else
            return x2;
    }
    // 2つの区間の計算結果v1,v2に対して、
    // その2つの区間を統合した区間における計算結果を返す
    T1 uniteData(T1 v1, T1 v2){
        return v1 + v2;
    }

    int n;
    vector<T1> data;
    vector<T2> delay;
    void updateTree(int a, int b, int k, int l, int r, T2 x){
        if(a <= l && r <= b){
            data[k] = updateData(data[k], r - l + 1, x);
            delay[k] = updateDelay(delay[k], x);
        }
        else if(a <= r && l <= b){
            int len = (r - l + 1) / 2;
            for(int i=0; i<2; ++i){
                data[k*2+1+i] = updateData(data[k*2+1+i], len, delay[k]);
                delay[k*2+1+i] = updateDelay(delay[k*2+1+i], delay[k]);
            }
            delay[k] = INIT_DELAY;
            updateTree(a, b, k*2+1, l, (l+r)/2, x);
            updateTree(a, b, k*2+2, (l+r+1)/2, r, x);
            data[k] = uniteData(data[k*2+1], data[k*2+2]);
        }
    }
    T1 getValue(int a, int b, int k, int l, int r){
        if(a <= l && r <= b){
            return data[k];
        }
        else if(a <= r && l <= b){
            int len = (r - l + 1) / 2;
            for(int i=0; i<2; ++i){
                data[k*2+1+i] = updateData(data[k*2+1+i], len, delay[k]);
                delay[k*2+1+i] = updateDelay(delay[k*2+1+i], delay[k]);
            }
            delay[k] = INIT_DELAY;
            T1 v1 = getValue(a, b, k*2+1, l, (l+r)/2);
            T1 v2 = getValue(a, b, k*2+2, (l+r+1)/2, r);
            return uniteData(v1, v2);
        }
        else{
            return INIT_DATA;
        }
    }

public:
    SegmentTree(int n0){
        n = 1;
        while(n < n0)
            n *= 2;
        data.assign(2*n-1, INIT_DATA);
        delay.assign(2*n-1, INIT_DELAY);
    }
    SegmentTree(const vector<T1>& v) : SegmentTree((int)v.size()){
        for(unsigned i=0; i<v.size(); ++i)
            data[n-1+i] = v[i];
        for(int k=n-2; k>=0; --k)
            data[k] = uniteData(data[k*2+1], data[k*2+2]);
    }
    // 区間[a,b]の要素にパラメータxによる更新処理を適用
    void update(int a, int b, T2 x){
        updateTree(a, b, 0, 0, n-1, x);
    }
    // 区間[a,b]の計算結果を返す
    T1 get(int a, int b){
        return getValue(a, b, 0, 0, n-1);
    }
    T1 get(int a, int b, T1 leftPrev, T1 rightPrev){
        return uniteData(uniteData(leftPrev, get(a, b)), rightPrev);
    }
};
const SegmentTree::T1 SegmentTree::INIT_DATA = 0;
const SegmentTree::T2 SegmentTree::INIT_DELAY = -1;


int main()
{
    int n, q;
    cin >> n >> q;

    vector<SegmentTree> st(2, SegmentTree(n));
    vector<long long> score(2);
    while(--q >= 0){
        int x, l, r;
        cin >> x >> l >> r;
        if(x == 0){
            int a = st[0].get(l, r);
            int b = st[1].get(l, r);
            if(a > b)
                score[0] += a;
            else if(a < b)
                score[1] += b;
        }
        else if(x == 1){
            st[0].update(l, r, 1);
            st[1].update(l, r, 0);
        }
        else{
            st[0].update(l, r, 0);
            st[1].update(l, r, 1);
        }
    }

    score[0] += st[0].get(0, n-1);
    score[1] += st[1].get(0, n-1);
    cout << score[0] << ' ' << score[1] << endl;

    return 0;
}
0