結果

問題 No.230 Splarraay スプラレェーイ
ユーザー parukiparuki
提出日時 2016-08-12 03:49:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 249 ms / 5,000 ms
コード長 4,141 bytes
コンパイル時間 1,751 ms
コンパイル使用メモリ 178,336 KB
実行使用メモリ 7,820 KB
最終ジャッジ日時 2023-08-07 08:18:03
合計ジャッジ時間 4,406 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 13 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 116 ms
5,228 KB
testcase_10 AC 93 ms
4,544 KB
testcase_11 AC 71 ms
4,376 KB
testcase_12 AC 117 ms
5,608 KB
testcase_13 AC 20 ms
4,380 KB
testcase_14 AC 70 ms
7,820 KB
testcase_15 AC 210 ms
7,612 KB
testcase_16 AC 218 ms
7,576 KB
testcase_17 AC 249 ms
7,544 KB
testcase_18 AC 162 ms
7,580 KB
testcase_19 AC 162 ms
7,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

struct LazySegTree{
    int dataSize;
    vi val;
    vector<bool> lazyZero, lazyOne;

    LazySegTree(int n){
        dataSize = 1;
        while(dataSize < n)dataSize *= 2;
        int treeSize = 2 * dataSize - 1;
    
        val = vi(treeSize);
        lazyZero = lazyOne = vector<bool>(treeSize);
    }

    void propagate(int index, int curL, int curR){
        // 自分の値をlazyにもとづいて書き換えて自分のlazyをクリア。
        // 子のlazyを書き換える。
        if(lazyZero[index]){
            val[index] = 0;
        }
        
        if(lazyOne[index])val[index] = curR-curL;
        
        if(curR - curL > 1){
            int l = index * 2 + 1, r = index * 2 + 2;
            if(lazyZero[index]){
                lazyOne[l] = lazyOne[r] = false;
                lazyZero[l] = lazyZero[r] = true;
            } else if(lazyOne[index]){
                lazyOne[l] = lazyOne[r] = true;
                lazyZero[l] = lazyZero[r] = false;
            }
        }

        lazyZero[index] = lazyOne[index] = false;
    }

    // [curL, curR) を評価する
    void update(int index, int curL, int curR, int givenL, int givenR, bool isAdd){
        // 範囲外であろうとpropagateは必ず呼ぶ。そうでないと、親がうまく評価されない
        propagate(index, curL, curR);

        // 範囲外
        if(curR <= givenL || givenR <= curL)return;

        // 範囲内
        if(givenL <= curL && curR <= givenR){
            // 直接書き換えないでindexのlazyを書き換えてpropagateを呼ぶ
            if(!isAdd){
                lazyZero[index] = true;
                lazyOne[index] = false;
            }
            else{
                lazyOne[index] = true;
                lazyZero[index] = false;
            }
            propagate(index, curL, curR);
        } else{ // 範囲外と範囲内が含まれる(長さ2以上)
            int mid = (curL + curR) / 2;
            update(index * 2 + 1, curL, mid, givenL, givenR, isAdd);
            update(index * 2 + 2, mid, curR, givenL, givenR, isAdd);
            val[index] = val[index * 2 + 1] + val[index * 2 + 2];
        }
    }

    void update(int l, int r, bool isAdd){
        update(0, 0, dataSize, l, r, isAdd);
    }

    int query(int l, int r){
        return query(0, 0, dataSize, l, r);
    }

    int query(int index, int curL, int curR, int givenL, int givenR){
        // 範囲外
        if(curR <= givenL || givenR <= curL)return 0;

        propagate(index, curL, curR);

        if(givenL <= curL && curR <= givenR){
            return val[index];
        } else{
            int mid = (curL + curR) / 2;
            int res1 = query(index * 2 + 1, curL, mid, givenL, givenR);
            int res2 = query(index * 2 + 2, mid, curR, givenL, givenR);
            return res1 + res2;
        }
    }
};

const int MAX = (int)1e5 + 1;
int N, Q, X[MAX], L[MAX], R[MAX];

void solve(){
    vll ans(2);
    vector<LazySegTree> segTree(2, LazySegTree(N));

    rep(i, Q){
        R[i]++;
        X[i]--;
        if(X[i] == -1){
            vll val(2);
            rep(j, 2)val[j] = segTree[j].query(L[i], R[i]);
            if(val[0] > val[1])ans[0] += val[0];
            else if(val[1] > val[0])ans[1] += val[1];
        } else{
            segTree[X[i]].update(L[i], R[i], true);
            segTree[1-X[i]].update(L[i], R[i], false);
        }
    }
    
    rep(i, 2)ans[i] += segTree[i].query(0, N);

    cout << ans[0] << ' ' << ans[1] << endl;
}

int main(){
    while(cin >> N >> Q){
        rep(i, Q)scanf("%d%d%d", &X[i], &L[i], &R[i]);
        solve();
    }
}
0