結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー tubuanntubuann
提出日時 2019-08-09 12:03:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 809 ms / 5,000 ms
コード長 4,970 bytes
コンパイル時間 3,057 ms
コンパイル使用メモリ 219,972 KB
実行使用メモリ 24,308 KB
最終ジャッジ日時 2023-10-24 02:36:30
合計ジャッジ時間 21,816 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 4 ms
4,348 KB
testcase_02 AC 5 ms
4,348 KB
testcase_03 AC 5 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 4 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 5 ms
4,348 KB
testcase_11 AC 743 ms
23,252 KB
testcase_12 AC 723 ms
23,516 KB
testcase_13 AC 523 ms
23,252 KB
testcase_14 AC 732 ms
24,308 KB
testcase_15 AC 773 ms
24,308 KB
testcase_16 AC 809 ms
24,308 KB
testcase_17 AC 644 ms
24,308 KB
testcase_18 AC 639 ms
24,308 KB
testcase_19 AC 550 ms
24,308 KB
testcase_20 AC 586 ms
24,308 KB
testcase_21 AC 574 ms
24,044 KB
testcase_22 AC 567 ms
24,308 KB
testcase_23 AC 609 ms
24,044 KB
testcase_24 AC 513 ms
24,308 KB
testcase_25 AC 534 ms
24,308 KB
testcase_26 AC 546 ms
24,044 KB
testcase_27 AC 526 ms
24,308 KB
testcase_28 AC 550 ms
24,044 KB
testcase_29 AC 724 ms
24,308 KB
testcase_30 AC 758 ms
24,308 KB
testcase_31 AC 807 ms
24,308 KB
testcase_32 AC 198 ms
24,308 KB
testcase_33 AC 537 ms
24,308 KB
testcase_34 AC 569 ms
24,308 KB
testcase_35 AC 536 ms
24,308 KB
testcase_36 AC 539 ms
24,308 KB
testcase_37 AC 544 ms
24,308 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;}
    }
  }
};

#endif /*SEGMENT_TREE*/


struct Data{
  ll mx,mi,lcm,sum,num;
};

inline ll gcd(ll a,ll b){
  while(b!=0){a%=b; swap(a,b);}
  return a;
}

const ll MX=1e18;


int main(){
  auto df=[](Data &a,Data &b){
            a.mx=max(a.mx,b.mx);
            a.mi=min(a.mi,b.mi);
            a.sum+=b.sum;
            a.lcm=a.lcm/gcd(a.lcm,b.lcm);
            a.num+=b.num;
            if(MX/a.lcm>=b.lcm && MX>=a.lcm*b.lcm){a.lcm*=b.lcm;}
            else{a.lcm=MX;}
          };
  auto mf=[](pll &a,pll &b){a=(b.F==0?a:b);};
  auto cf=[](Data &a,pll &b){
            if(b.F){a={b.S,b.S,b.S,b.S*a.num,a.num};}
          };
  Data de={1,1,1,0,0};
  pll me={0,0};

  ll n,q;
  cin>>n>>q;
  vector<ll> A(n);
  cin>>A;
  vector<Data> arr(n);
  for(int i=0;i<n;i++){
    arr[i]={A[i],A[i],A[i],A[i],1};
  }
  SegTree<Data,pll> 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){
      cin>>x;
      Tr.update(l,r+1,{1,x});
    }
    else if(c==2){
      cin>>x;
      auto qr=[&](Data &d,pll &m,int &l,int &r)->bool{
                if(d.mx==d.mi){
                  ll g=gcd(d.mx,x);
                  d={g,g,g,d.num*g,d.num};
                  m={1,g};
                  return true;
                }
                else if(x%d.lcm==0){
                  return true;
                }
                else{
                  return false;
                }
              };
      Tr.update(l,r+1,qr);
    }
    else if(c==3){
      auto ret=Tr.query(l,r+1);
      cout<<ret.mx<<endl;
    }
    else{
      auto ret=Tr.query(l,r+1);
      cout<<ret.sum<<endl;
    }
  }

  
  return 0;
}
0