結果

問題 No.854 公平なりんご分配
ユーザー beetbeet
提出日時 2019-07-26 21:40:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,407 bytes
コンパイル時間 2,818 ms
コンパイル使用メモリ 229,504 KB
実行使用メモリ 47,716 KB
最終ジャッジ日時 2023-09-15 00:52:33
合計ジャッジ時間 17,147 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,948 KB
testcase_01 AC 3 ms
6,880 KB
testcase_02 AC 4 ms
6,932 KB
testcase_03 AC 3 ms
6,900 KB
testcase_04 AC 4 ms
7,004 KB
testcase_05 AC 3 ms
7,020 KB
testcase_06 AC 4 ms
7,064 KB
testcase_07 AC 4 ms
6,900 KB
testcase_08 AC 3 ms
6,956 KB
testcase_09 AC 3 ms
6,896 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 4 ms
6,944 KB
testcase_12 AC 4 ms
6,956 KB
testcase_13 AC 3 ms
6,884 KB
testcase_14 AC 3 ms
6,880 KB
testcase_15 AC 3 ms
7,204 KB
testcase_16 AC 3 ms
7,000 KB
testcase_17 AC 3 ms
7,004 KB
testcase_18 AC 3 ms
7,000 KB
testcase_19 AC 3 ms
7,072 KB
testcase_20 AC 4 ms
7,080 KB
testcase_21 AC 4 ms
6,900 KB
testcase_22 AC 6 ms
6,952 KB
testcase_23 AC 5 ms
7,200 KB
testcase_24 AC 7 ms
7,216 KB
testcase_25 AC 4 ms
7,328 KB
testcase_26 AC 7 ms
7,536 KB
testcase_27 AC 6 ms
7,272 KB
testcase_28 AC 5 ms
6,960 KB
testcase_29 AC 4 ms
6,952 KB
testcase_30 AC 5 ms
7,148 KB
testcase_31 AC 6 ms
7,268 KB
testcase_32 AC 40 ms
14,948 KB
testcase_33 AC 48 ms
11,888 KB
testcase_34 AC 90 ms
18,488 KB
testcase_35 AC 61 ms
15,764 KB
testcase_36 AC 46 ms
8,376 KB
testcase_37 AC 38 ms
14,236 KB
testcase_38 AC 29 ms
13,060 KB
testcase_39 AC 140 ms
19,576 KB
testcase_40 AC 57 ms
11,016 KB
testcase_41 AC 60 ms
14,448 KB
testcase_42 AC 77 ms
17,960 KB
testcase_43 AC 96 ms
15,108 KB
testcase_44 AC 101 ms
17,644 KB
testcase_45 AC 108 ms
10,272 KB
testcase_46 AC 134 ms
18,048 KB
testcase_47 AC 46 ms
12,900 KB
testcase_48 AC 71 ms
17,960 KB
testcase_49 AC 67 ms
18,172 KB
testcase_50 AC 34 ms
14,380 KB
testcase_51 AC 133 ms
18,220 KB
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 WA -
testcase_82 RE -
testcase_83 AC 286 ms
47,380 KB
testcase_84 AC 2,383 ms
47,516 KB
testcase_85 AC 276 ms
45,284 KB
testcase_86 AC 149 ms
45,588 KB
testcase_87 WA -
testcase_88 WA -
testcase_89 WA -
testcase_90 WA -
testcase_91 WA -
testcase_92 AC 461 ms
47,512 KB
testcase_93 AC 463 ms
47,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
struct Mo{
  using F = function<void(Int)>;
  vector<Int> ls,rs,ord;
  Int n,width,nl,nr,ptr;
  vector<bool> flg;
  F expand,shrink;

  Mo(Int n,Int width,F expand,F shrink):
    n(n),width(width),nl(0),nr(0),ptr(0),flg(n,0),
    expand(expand),shrink(shrink){}

  void add(Int l,Int r){
    ls.emplace_back(l);
    rs.emplace_back(r);
  }

  void build(){
    ord.resize(ls.size());
    iota(ord.begin(),ord.end(),0);
    sort(ord.begin(),ord.end(),
         [&](Int a,Int b){
           if(ls[a]/width!=ls[b]/width) return ls[a]<ls[b];
           return bool((rs[a]<rs[b])^((ls[a]/width)&1));
         });
  }

  Int process(){
    if(ptr==(Int)ord.size()) return -1;
    const Int idx=ord[ptr++];
    while(nl>ls[idx]) calc(--nl);
    while(nr<rs[idx]) calc(nr++);
    while(nl<ls[idx]) calc(nl++);
    while(nr>rs[idx]) calc(--nr);
    return idx;
  }

  inline void calc(Int idx){
    flg[idx].flip();
    if(flg[idx]) expand(idx);
    else         shrink(idx);
  }

};
//END CUT HERE

template<typename T>
map<T, Int> factorize(T x){
  map<T, Int> res;
  for(Int i=2;i*i<=x;i++){
    while(x%i==0){
      x/=i;
      res[i]++;
    }
  }
  if(x!=1) res[x]++;
  return res;
}


template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T>
vector<vector<T> > make_v(size_t a,size_t b){
  return vector<vector<T> >(a,make_v<T>(b));
}
template<typename T>
vector<vector<vector<T> > > make_v(size_t a,size_t b,size_t c){
  return vector<vector<vector<T> > > (a,make_v<T>(b,c));
}

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}


class EulerTourForEdge{
private:
  vector<Int> ds,us,dep,btm;

  void dfs(Int v,Int p,Int d){
    dep[v]=d;
    for(Int u:G[v]){
      if(u==p) continue;
      ds[u]=btm.size();
      btm.emplace_back(u);
      dfs(u,v,d+1);
      us[u]=btm.size();
      btm.emplace_back(u);
    }
  }
public:
  vector<vector<Int> > G;

  EulerTourForEdge(){}
  EulerTourForEdge(Int n):
    ds(n),us(n),dep(n),G(n){}

  void add_edge(Int u,Int v){
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  void build(Int r=0){
    btm.clear();
    ds[r]=btm.size();
    btm.emplace_back(r);
    dfs(r,-1,0);
    us[r]=btm.size();
    btm.emplace_back(r);
  }

  Int child(Int u,Int v){
    return dep[u]<dep[v]?v:u;
  }

  Int bottom(Int e){
    return btm[e];
  }

  // lca(u, v) must be u or v
  template<typename F>
  void query(Int u,Int v,F f){
    if(dep[u]>dep[v]) swap(u,v);
    f(ds[u]+1,ds[v]+1);
  }

  template<typename T,typename G>
  void update(Int v,T x,G g){
    g(ds[v], x);
    g(us[v],-x);
  }
};

//INSERT ABOVE HERE
signed DWANGO2017FINAL_B(){
  using ll = long long;
  Int n;
  cin>>n;
  vector<Int> x(n);
  for(Int i=0;i<n;i++) cin>>x[i];

  const Int RT = 40;
  auto acc=make_v<Int>(RT,n+1);
  fill_v(acc,0);

  using P = pair<Int, Int>;
  vector<vector<P> > v(n);
  for(Int i=0;i<n;i++){
    for(auto p:factorize(x[i])){
      if(p.first<RT) acc[p.first][i+1]+=p.second;
      else v[i].emplace_back(p);
    }
  }

  for(Int j=0;j<RT;j++)
    for(Int i=0;i<n;i++)
      acc[j][i+1]+=acc[j][i];

  const Int MAX = 5e5+100;
  vector<Int> cnt(MAX,0);

  auto expand=[&](Int idx){
                for(auto p:v[idx]){
                  cnt[p.first]+=p.second;
                }
              };
  auto shrink=[&](Int idx){
                for(auto p:v[idx]){
                  cnt[p.first]-=p.second;
                }
              };
  Mo mo(n,400,expand,shrink);

  Int q;
  cin>>q;
  vector<Int> ps(q);
  for(Int i=0;i<q;i++){
    Int l,r;
    cin>>ps[i]>>l>>r;
    l--;
    mo.add(l,r);
  }
  mo.build();

  vector<ll> ans(q,1);
  for(Int i=0;i<q;i++){
    Int k=mo.process();
    Int l=mo.ls[k],r=mo.rs[k];
    auto mp=factorize(ps[k]);
    for(auto p:mp){
      if(p.first<RT) ans[k]&=(acc[p.first][r]-acc[p.first][l])>=p.second;
      else ans[k]&=cnt[p.first]>=p.second;
    }
  }

  for(Int i=0;i<q;i++) cout<<(ans[i]?"Yes":"NO")<<"\n";
  cout<<flush;
  return 0;
}
/*
  verified on 2018/02/02
  https://beta.atcoder.jp/contests/dwacon2017-honsen/tasks/dwango2017final_b
*/



signed main(){
  DWANGO2017FINAL_B();
  //ABC133_F();
  return 0;
}
0