結果

問題 No.885 アマリクエリ
ユーザー beetbeet
提出日時 2019-09-13 21:35:24
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 1,931 bytes
コンパイル時間 2,265 ms
コンパイル使用メモリ 210,792 KB
実行使用メモリ 9,724 KB
最終ジャッジ日時 2023-09-17 07:00:09
合計ジャッジ時間 4,243 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
9,608 KB
testcase_01 AC 64 ms
9,660 KB
testcase_02 AC 77 ms
9,724 KB
testcase_03 AC 47 ms
9,460 KB
testcase_04 AC 47 ms
9,456 KB
testcase_05 AC 38 ms
9,460 KB
testcase_06 AC 31 ms
9,568 KB
testcase_07 AC 19 ms
9,524 KB
testcase_08 AC 21 ms
4,376 KB
testcase_09 AC 68 ms
9,464 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;



template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  Int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,T ti):f(f),ti(ti){}
  void init(Int n_){
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  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]);
  }
  void set_val(Int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }
  T query(Int a,Int b){
    T vl=ti,vr=ti;
    for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }

  void update(Int a,Int b,Int x,Int l,Int r,Int k){
    if(r<=a||b<=l||dat[k].first<x) return;
    if(a<=l&&r<=b&&l+1==r){
      dat[k].first%=x;
      dat[k].second%=x;
      return;
    }
    Int m=(l+r)>>1;
    update(a,b,x,l,m,(k<<1)|0);
    update(a,b,x,m,r,(k<<1)|1);
    dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }

  void update(Int a,Int b,Int x){
    update(a,b,x,0,n,1);
  }
};

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  vector<Int> as(n);
  for(Int i=0;i<n;i++) cin>>as[i];

  using P = pair<Int, Int>;
  auto f=[&](P a,P b){return P(max(a.first,b.first),a.second+b.second);};
  SegmentTree<P> seg(f,P(0,0));
  vector<P> zs;
  for(Int i=0;i<n;i++) zs.emplace_back(as[i],as[i]);
  seg.build(zs);

  Int q;
  cin>>q;
  for(Int i=0;i<q;i++){
    Int x;
    cin>>x;
    seg.update(0,n,x);
    cout<<seg.query(0,n).second<<"\n";
  }
  cout<<flush;
  return 0;
}
0