結果
| 問題 |
No.1673 Lamps on a line
|
| コンテスト | |
| ユーザー |
ocvret_
|
| 提出日時 | 2021-09-14 13:55:31 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 304 ms / 2,000 ms |
| コード長 | 2,545 bytes |
| コンパイル時間 | 2,178 ms |
| コンパイル使用メモリ | 181,900 KB |
| 実行使用メモリ | 15,488 KB |
| 最終ジャッジ日時 | 2024-06-27 03:46:08 |
| 合計ジャッジ時間 | 5,062 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 11 |
ソースコード
#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;
}
ocvret_