結果
| 問題 |
No.230 Splarraay スプラレェーイ
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2018-11-09 13:36:01 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 305 ms / 5,000 ms |
| コード長 | 2,719 bytes |
| コンパイル時間 | 2,400 ms |
| コンパイル使用メモリ | 206,156 KB |
| 最終ジャッジ日時 | 2025-01-06 15:54:38 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
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,typename E>
struct SegmentTree{
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;
SegmentTree(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(2*n,ti);
laz.assign(2*n,ei);
}
void build(const vector<T> &v){
Int n_=v.size();
init(n_);
for(Int i=0;i<n_;i++) dat[n+i]=v[i];
for(Int i=n-1;i;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;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 query(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);
}
};
//INSERT ABOVE HERE
signed main(){
Int n,q;
cin>>n>>q;
using P = pair<Int, Int>;
auto f=[](P a,P b){return P(a.first+b.first,a.second+b.second);};
auto g=[](P a,Int b){return P(b*a.second,a.second);};
auto h=[](Int a,Int b){a++;return b;};
P ti(0,0);
Int ei=-1;
SegmentTree<P, Int> segA(f,g,h,ti,ei);
SegmentTree<P, Int> segB(f,g,h,ti,ei);
vector<P> vp(n,P(0,1));
segA.build(vp);
segB.build(vp);
Int sa=0,sb=0;
for(Int i=0;i<q;i++){
Int x,l,r;
cin>>x>>l>>r;
r++;
if(x==0){
Int a=segA.query(l,r).first;
Int b=segB.query(l,r).first;
if(a<b) sb+=b;
if(a>b) sa+=a;
}
if(x==1){
segA.update(l,r,1);
segB.update(l,r,0);
}
if(x==2){
segA.update(l,r,0);
segB.update(l,r,1);
}
}
sa+=segA.query(0,n).first;
sb+=segB.query(0,n).first;
cout<<sa<<" "<<sb<<endl;
return 0;
}
beet