結果

問題 No.1673 Lamps on a line
ユーザー Hideaki ItoHideaki Ito
提出日時 2021-09-11 20:16:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 1,636 bytes
コンパイル時間 1,465 ms
コンパイル使用メモリ 166,060 KB
実行使用メモリ 4,968 KB
最終ジャッジ日時 2023-09-05 08:58:49
合計ジャッジ時間 2,817 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 11 ms
4,376 KB
testcase_03 AC 11 ms
4,376 KB
testcase_04 AC 18 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 17 ms
4,380 KB
testcase_07 AC 33 ms
4,564 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 43 ms
4,376 KB
testcase_10 AC 45 ms
4,376 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 48 ms
4,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}

template< typename T >
struct BinaryIndexedTree {
  using U =
    typename std::conditional<std::is_same<T, long long>::value,
                              unsigned long long,
                              unsigned int>::type;

  BinaryIndexedTree(int sz) : _n(sz) {
    data.assign(++sz, 0);
  }

  T sum(int l, int r) {
    assert(0 <= l && l <= r && r <= _n);
    return sum(r) - sum(l);
  }

  void add(int p, T x) {
    assert(0 <= p && p < _n);
    p++;
    while(p <= _n) {
      data[p - 1] += U(x);
      p += p & -p;
    }
  }

private:
  vector< U > data;
  int _n;

  U sum(int r) {
    U ret = 0;
    while(r > 0) {
      ret += data[r - 1];
      r -= r & -r;
    }
    return ret;
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q; cin >> n >> q;
  BinaryIndexedTree<int> bit(n);
  rep(i, q) {
    int l, r; cin >> l >> r;
    l--;
    for(int j=l; j<r; j++) {
      if(bit.sum(j, j+1) == 1) bit.add(j, -1);
      else bit.add(j, 1);
    }
    cout << bit.sum(0, n) << endk;
  }
  return 0;
}
0