結果

問題 No.1673 Lamps on a line
ユーザー abc3abc3
提出日時 2021-09-10 21:38:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 2,276 bytes
コンパイル時間 6,085 ms
コンパイル使用メモリ 204,028 KB
実行使用メモリ 13,144 KB
最終ジャッジ日時 2023-09-02 16:21:11
合計ジャッジ時間 4,494 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 75 ms
4,380 KB
testcase_03 AC 72 ms
4,380 KB
testcase_04 AC 135 ms
4,380 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 113 ms
4,384 KB
testcase_07 AC 131 ms
12,796 KB
testcase_08 AC 9 ms
12,484 KB
testcase_09 AC 169 ms
5,464 KB
testcase_10 AC 185 ms
7,912 KB
testcase_11 AC 21 ms
7,912 KB
testcase_12 AC 181 ms
13,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

    #include <bits/stdc++.h>
    using namespace std;
    #include <math.h>
    #include <iomanip>
    #include <cstdint>
    #include <string>
    #include <sstream>
    template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
    #define rep(i,n) for (int i = 0; i < (n); ++i)
    typedef long long ll;
    typedef long double ld;
    const int INF=1001001001;
    const int mod=998244353;
    
    int N,q;
    
struct LazySegmentTree {
private:
    int n;
    vector<int> node, lazy;

public:
    LazySegmentTree(vector<int> v) {
        int sz = (int)v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1);
        lazy.resize(2*n-1);

        for(int i=0; i<sz; i++) node[i+n-1] = 0;
        for(int i=n-2; i>=0; i--) node[i] = node[i*2+1] + node[i*2+2];
    }

    void eval(int k, int l, int r) {
        if(lazy[k]) {
            node[k] = r-l-node[k];
            if(r - l > 1) {
                lazy[k*2+1] ^= 1;
                lazy[k*2+2] ^= 1;
            }
            lazy[k] = false;
        }
    }

    void update(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy[k] ^= 1;
            eval(k, l, r);
        }
        else {
            update(a, b, 2*k+1, l, (l+r)/2);
            update(a, b, 2*k+2, (l+r)/2, r);
            node[k] = node[2*k+1] + node[2*k+2];
        }
    }

    int get(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a) return 0;
        if(a <= l && r <= b) return node[k];
        int vl = get(a, b, 2*k+1, l, (l+r)/2);
        int vr = get(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
};

    void solve(){
        cin>>N>>q;
        LazySegmentTree seg(vector<int>(N,0));
        rep(i,q){
            int l,r;
            cin>>l>>r;
            l--;
            seg.update(l,r);
            cout<<seg.get(0,N)<<endl;
        }
    }

    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
		solve();
        
        return 0;
    }
0