library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: test/aoj/2563.test.cpp

Depends on

Code

// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2563

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../../vector/zip.cpp"
#include "../../vector/fusion.cpp"
#include "../../vector/compress.cpp"
#include "../../algorithm/parallelbinarysearch.cpp"
#include "../../segtree/basic/lazy.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  using ll = long long;

  int n,m,q;
  cin>>n>>m>>q;

  vector<int> as(m),bs(m),vs(m);
  for(int i=0;i<m;i++)
    cin>>as[i]>>bs[i]>>vs[i],as[i]--;

  {
    auto vt=zip(vs,as,bs);
    sort(vt.begin(),vt.end());
    for(int i=0;i<m;i++) tie(vs[i],as[i],bs[i])=vt[i];
  }

  vector<int> xs(q),ys(q);
  vector<ll> js(q);
  for(int i=0;i<q;i++)
    cin>>xs[i]>>ys[i]>>js[i],xs[i]--;

  auto ps=compress(fusion(as,bs,xs,ys,vector<int>({0,1000000007})));
  auto dc=dict(ps);

  using P = pair<ll, int>;
  auto f=[&](P a,P b){return P(a.first+b.first,a.second+b.second);};
  auto g=[&](P a,int b){return P(a.first+(ll)b*a.second,a.second);};
  auto h=[&](int a,int b){return a+b;};
  SegmentTree<P, int> seg(f,g,h,P(0,0),0);

  vector<P> vp(ps.size()-1);
  for(int i=0;i+1<(int)ps.size();i++) vp[i]=P(0,ps[i+1]-ps[i]);

  for(int &a:as) a=dc[a];
  for(int &b:bs) b=dc[b];
  for(int &x:xs) x=dc[x];
  for(int &y:ys) y=dc[y];

  auto init=[&](){
    seg.build(vp);
  };
  auto apply=[&](int i){
    seg.update(as[i],bs[i],1);
  };
  auto check=[&](int i){
    return seg.query(xs[i],ys[i]).first>=js[i];
  };

  auto ans=parallel_binary_search(q,m,init,apply,check);
  for(int i=0;i<q;i++) cout<<vs[ans[i]]<<"\n";
  cout<<flush;
  return 0;
}
#line 1 "test/aoj/2563.test.cpp"
// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2563

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#line 1 "vector/zip.cpp"

#line 3 "vector/zip.cpp"
using namespace std;
#endif

//BEGIN CUT HERE
template<typename ...Ts>
decltype(auto) zip(vector<Ts>... args){
  vector<decltype(make_tuple(args[0]...))> res;
  int n=min({args.size()...});
  res.reserve(n);
  for(int i=0;i<n;i++) res.emplace_back(args[i]...);
  return res;
}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 1 "vector/fusion.cpp"

#line 3 "vector/fusion.cpp"
using namespace std;
#endif

//BEGIN CUT HERE
template<typename T, typename ...Ts>
vector<T> fusion(vector<T> bs,Ts... ts){
  auto append=[&](auto vs){for(auto v:vs) bs.emplace_back(v);};
  initializer_list<int>{(void(append(ts)),0)...};
  return bs;
}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 1 "vector/compress.cpp"

#line 3 "vector/compress.cpp"
using namespace std;
#endif

//BEGIN CUT HERE
template<typename V>
V compress(V vs){
  sort(vs.begin(),vs.end());
  vs.erase(unique(vs.begin(),vs.end()),vs.end());
  return vs;
}
template<typename T>
map<T, int> dict(const vector<T> &vs){
  map<T, int> res;
  for(int i=0;i<(int)vs.size();i++)
    res[vs[i]]=i;
  return res;
}
map<char, int> dict(const string &s){
  return dict(vector<char>(s.begin(),s.end()));
}
template<typename T>
vector<T> compressed(vector<T> vs){
  auto dc=dict(compress(vs));
  for(auto &v:vs) v=dc[v];
  return vs;
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif
#line 1 "algorithm/parallelbinarysearch.cpp"

#line 3 "algorithm/parallelbinarysearch.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
// n questions, q operations
vector<int> parallel_binary_search(int n,int q,
                                   function<void(void)> init,
                                   function<void(int)> apply,
                                   function<bool(int)> check){
  vector< vector<int> > C(q);
  vector<int> L(n,-1),R(n,q);
  bool flg=1;
  while(flg){
    flg=0;
    init();
    for(int i=0;i<n;i++)
      if(L[i]+1<R[i]) C[(L[i]+R[i])>>1].emplace_back(i);
    for(int i=0;i<q;i++){
      flg|=!C[i].empty();
      apply(i);
      for(int j:C[i]){
        if(check(j)) R[j]=i;
        else L[j]=i;
      }
      C[i].clear();
    }
  }
  return R;
}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 1 "segtree/basic/lazy.cpp"

#line 3 "segtree/basic/lazy.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
template <typename T,typename E>
struct SegmentTree{
  using F = function<T(T,T)>;
  using G = function<T(T,E)>;
  using H = function<E(E,E)>;
  int n,height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(F f,G g,H h,T ti,E ei):
    f(f),g(g),h(h),ti(ti),ei(ei){}

  void init(int n_){
    n=1;height=0;
    while(n<n_) n<<=1,height++;
    dat.assign(2*n,ti);
    laz.assign(2*n,ei);
  }

  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]);
  }

  inline T reflect(int k){
    return laz[k]==ei?dat[k]:g(dat[k],laz[k]);
  }

  inline void propagate(int k){
    if(laz[k]==ei) return;
    laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
    laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
    dat[k]=reflect(k);
    laz[k]=ei;
  }

  inline void thrust(int k){
    for(int i=height;i;i--) propagate(k>>i);
  }

  inline void recalc(int k){
    while(k>>=1)
      dat[k]=f(reflect((k<<1)|0),reflect((k<<1)|1));
  }

  void update(int a,int b,E x){
    if(a>=b) return;
    thrust(a+=n);
    thrust(b+=n-1);
    for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
      if(l&1) laz[l]=h(laz[l],x),l++;
      if(r&1) --r,laz[r]=h(laz[r],x);
    }
    recalc(a);
    recalc(b);
  }

  void set_val(int a,T x){
    thrust(a+=n);
    dat[a]=x;laz[a]=ei;
    recalc(a);
  }

  T query(int a,int b){
    if(a>=b) return ti;
    thrust(a+=n);
    thrust(b+=n-1);
    T vl=ti,vr=ti;
    for(int l=a,r=b+1;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,reflect(l++));
      if(r&1) vr=f(reflect(--r),vr);
    }
    return f(vl,vr);
  }

  template<typename C>
  int find(int st,C &check,T &acc,int k,int l,int r){
    if(l+1==r){
      acc=f(acc,reflect(k));
      return check(acc)?k-n:-1;
    }
    propagate(k);
    int m=(l+r)>>1;
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);
    if(st<=l and !check(f(acc,dat[k]))){
      acc=f(acc,dat[k]);
      return -1;
    }
    int vl=find(st,check,acc,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(st,check,acc,(k<<1)|1,m,r);
  }

  template<typename C>
  int find(int st,C &check){
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }
};
//END CUT HERE
#ifndef call_from_test

signed CFR569_C(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,m;
  cin>>n>>m;
  vector<int> as(n),bs(m);
  for(int i=0;i<n;i++) cin>>as[i];
  for(int i=0;i<m;i++) cin>>bs[i];

  auto f=[](int a,int b){return max(a,b);};
  auto g=[](int a,int b){return a+b;};
  int ti=0,ei=0;
  SegmentTree<int, int> seg(f,g,g,ti,ei);

  const int sz = 1<<20;
  seg.build(vector<int>(sz,0));

  for(int i=0;i<n;i++) seg.update(sz-as[i],sz,+1);
  for(int i=0;i<m;i++) seg.update(sz-bs[i],sz,-1);

  int q;
  cin>>q;
  auto check=[](int d){return d>0;};
  for(int i=0;i<q;i++){
    int t,k,v;
    cin>>t>>k>>v;
    k--;
    if(t==1){
      seg.update(sz-as[k],sz,-1);
      as[k]=v;
      seg.update(sz-as[k],sz,+1);
    }
    if(t==2){
      seg.update(sz-bs[k],sz,+1);
      bs[k]=v;
      seg.update(sz-bs[k],sz,-1);
    }
    int pos=seg.find(0,check);
    cout<<(pos<0?pos:sz-pos)<<"\n";
  }
  cout<<flush;
  return 0;
}
/*
  verified on 2019/10/28
  https://codeforces.com/contest/1179/problem/C
*/

signed main(){
  CFR569_C();
  return 0;
}
#endif
#line 12 "test/aoj/2563.test.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  using ll = long long;

  int n,m,q;
  cin>>n>>m>>q;

  vector<int> as(m),bs(m),vs(m);
  for(int i=0;i<m;i++)
    cin>>as[i]>>bs[i]>>vs[i],as[i]--;

  {
    auto vt=zip(vs,as,bs);
    sort(vt.begin(),vt.end());
    for(int i=0;i<m;i++) tie(vs[i],as[i],bs[i])=vt[i];
  }

  vector<int> xs(q),ys(q);
  vector<ll> js(q);
  for(int i=0;i<q;i++)
    cin>>xs[i]>>ys[i]>>js[i],xs[i]--;

  auto ps=compress(fusion(as,bs,xs,ys,vector<int>({0,1000000007})));
  auto dc=dict(ps);

  using P = pair<ll, int>;
  auto f=[&](P a,P b){return P(a.first+b.first,a.second+b.second);};
  auto g=[&](P a,int b){return P(a.first+(ll)b*a.second,a.second);};
  auto h=[&](int a,int b){return a+b;};
  SegmentTree<P, int> seg(f,g,h,P(0,0),0);

  vector<P> vp(ps.size()-1);
  for(int i=0;i+1<(int)ps.size();i++) vp[i]=P(0,ps[i+1]-ps[i]);

  for(int &a:as) a=dc[a];
  for(int &b:bs) b=dc[b];
  for(int &x:xs) x=dc[x];
  for(int &y:ys) y=dc[y];

  auto init=[&](){
    seg.build(vp);
  };
  auto apply=[&](int i){
    seg.update(as[i],bs[i],1);
  };
  auto check=[&](int i){
    return seg.query(xs[i],ys[i]).first>=js[i];
  };

  auto ans=parallel_binary_search(q,m,init,apply,check);
  for(int i=0;i<q;i++) cout<<vs[ans[i]]<<"\n";
  cout<<flush;
  return 0;
}
Back to top page