結果

問題 No.1673 Lamps on a line
ユーザー ocvret_ocvret_
提出日時 2021-09-14 13:55:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 2,545 bytes
コンパイル時間 1,845 ms
コンパイル使用メモリ 180,800 KB
実行使用メモリ 15,192 KB
最終ジャッジ日時 2023-09-09 10:38:21
合計ジャッジ時間 5,450 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 93 ms
4,376 KB
testcase_03 AC 91 ms
4,380 KB
testcase_04 AC 162 ms
4,376 KB
testcase_05 AC 10 ms
4,376 KB
testcase_06 AC 141 ms
4,376 KB
testcase_07 AC 189 ms
14,232 KB
testcase_08 AC 10 ms
13,352 KB
testcase_09 AC 250 ms
5,636 KB
testcase_10 AC 266 ms
9,016 KB
testcase_11 AC 28 ms
9,280 KB
testcase_12 AC 300 ms
15,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007
// #define MOD 998244353

// 2020/12/06 rechecked
template<typename T,typename E>
class LazySegtree{
  public:
  using F = function<T(T,T)>;
  using G = function<T(T,E)>;
  using H = function<E(E,E)>;
  int n,height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  LazySegtree(F f,G g,H h,T ti,E ei):
    f(f),g(g),h(h),ti(ti),ei(ei){}

  void init(int N){
    n = 1;height = 0;
    while(n < N)n <<= 1,height++;
    dat.assign(n << 1,ti);
    laz.assign(n << 1,ei);
  }
  void build(const vector<T> &v){
    int N = v.size();
    init(N);
    for(int i = 0;i < N;i++)dat[i+n] = v[i];
    for(int i = n-1;i > 0;i--)dat[i] = f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  inline T reflect(int k){
    return (laz[k] == ei ? dat[k] : g(dat[k],laz[k]));
  }
  inline void eval(int k){
    if(laz[k] == ei)return;
    laz[(k<<1)|0] = h(laz[(k<<1)|0],laz[k]);
    laz[(k<<1)|1] = h(laz[(k<<1)|1],laz[k]);
    dat[k] = reflect(k);
    laz[k] = ei;
  }
  inline void thrust(int k){
    for(int i = height;i > 0;i--)eval(k >> i);
  }
  inline void recalc(int k){
    while(k >>= 1)dat[k] = f(reflect((k << 1)|0),reflect((k << 1)|1));
  }
  void update(int a,int b,E x){
    thrust(a+=n);
    thrust(b+=n-1);
    for(int l = a,r = b+1;l < r;l >>= 1,r >>= 1){
      if(l & 1)laz[l] = h(laz[l],x),l++;
      if(r & 1)r--,laz[r] = h(laz[r],x);
    }
    recalc(a);
    recalc(b);
  }
  void set_val(int a,T x){
    thrust(a+=n);
    dat[a] = x;laz[a] = ei;
    recalc(a);
  }
  T get(int a,int b){
    thrust(a+=n);
    thrust(b+=n-1);
    T vl = ti,vr = ti;
    for(int l = a,r = b+1;l < r;l >>= 1,r >>= 1){
      if(l & 1)vl = f(vl,reflect(l++));
      if(r & 1)vr = f(reflect(--r),vr);
    }
    return f(vl,vr);
  }
};


int main(){
  
  int n,Q;
  cin >> n >> Q;
  auto f = [](P a,P b){return P{a.first+b.first,a.second+b.second};};
  auto g = [](P a,bool is){return (is ? P{a.second-a.first,a.second} : a);};
  auto h = [](bool a,bool b){return a != b;};
  LazySegtree<P,bool> seg(f,g,h,{0,0},false);
  seg.build(vector<P>(n,{0,1}));
  while(Q--){
    int l,r;cin >> l >> r;
    seg.update(l-1,r,1);
    cout << seg.get(0,n).first << "\n";
  }

  return 0;
}
0