結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,880 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 AC 4 ms
6,932 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
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 AC 209 ms
47,380 KB
testcase_83 AC 212 ms
47,324 KB
testcase_84 AC 207 ms
47,496 KB
testcase_85 AC 201 ms
45,392 KB
testcase_86 AC 144 ms
45,256 KB
testcase_87 AC 363 ms
47,320 KB
testcase_88 AC 364 ms
47,372 KB
testcase_89 AC 361 ms
47,392 KB
testcase_90 AC 363 ms
47,368 KB
testcase_91 AC 369 ms
47,688 KB
testcase_92 WA -
testcase_93 WA -
権限があれば一括ダウンロードができます

ソースコード

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];
    if(acc[0][r]-acc[0][l]>=0){
      ans[k]=1;
      continue;
    }
    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