結果

問題 No.879 Range Mod 2 Query
ユーザー tubuanntubuann
提出日時 2019-08-09 11:07:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 5,883 bytes
コンパイル時間 2,913 ms
コンパイル使用メモリ 223,844 KB
実行使用メモリ 29,680 KB
最終ジャッジ日時 2023-09-07 03:31:23
合計ジャッジ時間 18,827 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 1,306 ms
29,220 KB
testcase_20 AC 1,300 ms
29,276 KB
testcase_21 AC 1,390 ms
29,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;

template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,const vector<T> &A){ll i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}
template<typename T>vector<T> & cset(vector<T> &A,T e=T()){for(auto &I:A){I=e;} return A;}

#ifndef SEGMENT_TREE
#define SEGMENT_TREE
 
template<typename Data,typename Mono>
class SegTree{
private:
  typedef function<void(Data&,Data&)> MergeData;
  typedef function<void(Mono&,Mono&)> MergeMono;
  typedef function<void(Data&,Mono&)> Culc;
  typedef function<bool(Data&,Mono&,int&,int&)> UpdateQuery; //data,mono,left,right true::Data,Monoに作用する false::子に降りる
 
 
  int size,high;
  vector<int> left,right;
  vector<Data> dv;
  vector<Mono> mv;
   
  Data de;
  Mono me;
  MergeData df;
  MergeMono mf;
  Culc cf;
   
 
  inline bool nx(int &idx){
    while(idx&1){idx>>=1; if(idx){reculc(idx);}}
    return idx?idx|=1:idx;
  }
 
  inline void reculc(int idx){
    dv[idx]=dv[idx<<1];
    df(dv[idx],dv[(idx<<1)|1]);
    cf(dv[idx],mv[idx]);
  }
 
  inline void down(int idx){
    cf(dv[idx<<1],mv[idx]);
    mf(mv[idx<<1],mv[idx]);
    cf(dv[(idx<<1)|1],mv[idx]);
    mf(mv[(idx<<1)|1],mv[idx]);
    mv[idx]=me;
  }
 
public:
  SegTree(Data de,Mono me,MergeData df,MergeMono mf,Culc cf,int n=0):de(de),me(me),df(df),mf(mf),cf(cf){init(n);}
 
  void init(int n){
    size=1;
    high=0;
    while(size<n){size<<=1; high++;}
    dv.assign(size<<1,de);
    mv.assign(size<<1,me);
    left.resize(size<<1);
    right.resize(size<<1);
    for(int i=0;i<size;i++){left[size|i]=i; right[size|i]=i+1;}
    for(int i=size-1;i>0;i--){left[i]=left[i<<1]; right[i]=right[(i<<1)|1];}
  }
 
  void build(const vector<Data> &ary){
    int n=ary.size();
    init(n);
    for(int i=0;i<n;i++){dv[size|i]=ary[i];}
    for(int i=size-1;i>0;i--){dv[i]=dv[i<<1]; df(dv[i],dv[(i<<1)|1]);}
  }
 
  //[lf,rg)
  Data query(int lf,int rg){
    Data ret=de;
    int idx=1;
    bool j=true;
    while(j){
      if(right[idx]<=lf || rg<=left[idx]){j&=nx(idx);}
      else if(lf<=left[idx] && right[idx]<=rg){df(ret,dv[idx]); j&=nx(idx);}
      else{down(idx); idx<<=1;}
    }
    return ret;
  }
 
  void update(int lf,int rg,Mono m){
    int idx=1;
    bool j=true;
    while(j){
      if(right[idx]<=lf || rg<=left[idx]){j&=nx(idx);}
      else if(lf<=left[idx] && right[idx]<=rg){
        cf(dv[idx],m); mf(mv[idx],m); j&=nx(idx);
      }
      else{down(idx); idx<<=1;}
    }
  }
 
  void update(int lf,int rg,UpdateQuery br){
    int idx=1;
    bool j=true;
    while(j){
      if(right[idx]<=lf || rg<=left[idx]){j&=nx(idx);}
      else if(lf<=left[idx] && right[idx]<=rg){
        if(br(dv[idx],mv[idx],left[idx],right[idx])){j&=nx(idx);}
        else{down(idx); idx<<=1;}
      }
      else{down(idx); idx<<=1;}
    }
  }
 
  void DEBUG(){cout<<dv<<endl;}
};
 
#endif /*SEGMENT_TREE*/

int main(){
  typedef pair<pll,pll> P;//odd(number,sum),even(number,sum)
  typedef pair<pll,pair<pll,pll>> P2;
  P de={{0,0},{0,0}};
  P2 me={{0,0},{{0,0},{0,0}}}; //0::nothing  1::mod2  2::add
  auto df=[](P &a,P &b){
            a.F.F+=b.F.F;
            a.F.S+=b.F.S;
            a.S.F+=b.S.F;
            a.S.S+=b.S.S;
          };
  auto mf=[&](P2 &a,P2 &b){
            queue<pll> A;
            if(a.F.F!=0){A.push(a.F);}
            if(a.S.F.F!=0){A.push(a.S.F);}
            if(a.S.S.F!=0){A.push(a.S.S);}
            if(b.F.F!=0){A.push(b.F);}
            if(b.S.F.F!=0){A.push(b.S.F);}
            if(b.S.S.F!=0){A.push(b.S.S);}
            deque<pll> B;
            function<void(pll)> cul=[&](pll w){
                       if(B.empty()){B.push_back(w);}
                       else{
                         auto &I=B.back();
                         if(I.F==w.F){I.S+=w.S;}
                         else if(B.size()>=2 && B[B.size()-2].F==1){
                           B[B.size()-2]=I;
                           I=w;
                         }
                         else{B.push_back(w);}
                       }
                     };
            
            while(!A.empty()){
              pll w=A.front(); A.pop();
              cul(w);
            }
            a=me;
            if(B.size()>=1){a.F=B[0];}
            if(B.size()>=2){a.S.F=B[1];}
            if(B.size()>=3){a.S.S=B[2];}
          };
  auto cf=[](P &a,P2 &b){
            auto cul=[&](pll w){
                       if(w.F==1){
                         a.F.S=a.F.F;
                         a.S.S=0;
                       }
                       else if(w.F==2){
                         a.F.S+=a.F.F*w.S;
                         a.S.S+=a.S.F*w.S;
                         if(w.S&1){swap(a.F,a.S);}
                       }
                     };
            cul(b.F);
            cul(b.S.F);
            cul(b.S.S);
          };
  
  ll n,q;
  cin>>n>>q;
  vector<ll> A(n);
  cin>>A;
  vector<P> arr(n,de);
  for(int i=0;i<n;i++){
    if(A[i]&1){arr[i]={{1,A[i]},{0,0}};}
    else{arr[i]={{0,0},{1,A[i]}};}
  }
  SegTree<P,P2> Tr(de,me,df,mf,cf);
  Tr.build(arr);
  for(int i=0;i<q;i++){
    int c,l,r,x;
    cin>>c>>l>>r;
    l--; r--;
    if(c==1){
      Tr.update(l,r+1,{{1,0},{{0,0},{0,0}}});
    }
    else if(c==2){
      cin>>x;
      Tr.update(l,r+1,{{2,x},{{0,0},{0,0}}});
    }
    else{
      auto I=Tr.query(l,r+1);
      cout<<I.F.S+I.S.S<<endl;
    }
  }
  
  

  return 0;
}
0