結果

問題 No.3436 [Cherry 8th Tune B] この夏に何が起こるかな?
コンテスト
ユーザー Taiki0715
提出日時 2026-01-23 22:22:03
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 2,323 ms / 4,000 ms
コード長 8,499 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,232 ms
コンパイル使用メモリ 364,152 KB
実行使用メモリ 13,460 KB
最終ジャッジ日時 2026-01-23 22:24:23
合計ジャッジ時間 51,335 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T1,typename T2,typename T3>istream &operator>>(istream &is,tuple<T1,T2,T3>&a){is>>std::get<0>(a)>>std::get<1>(a)>>std::get<2>(a);return is;}
template<typename T,size_t n>istream &operator>>(istream &is,array<T,n>&a){for(auto&i:a)is>>i;return is;}
template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}
#define overload3(_1,_2,_3,name,...) name
#define rep1(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define rep(...) overload3(__VA_ARGS__,rep2,rep1)(__VA_ARGS__)
#define reps(i,l,r) rep2(i,l,r)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcountll(x)
#define fin(x) return cout<<(x)<<'\n',static_cast<void>(0)
#define yn(x) cout<<((x)?"Yes\n":"No\n")
#define uniq(x) sort(all(x)),x.erase(unique(all(x)),x.end())
template<typename T>
inline int fkey(vector<T>&z,T key){return lower_bound(z.begin(),z.end(),key)-z.begin();}
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
  if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
  else return init;
}
#ifdef LOCAL
#include<debug.h>
#define SWITCH(a,b) (a)
#else
#define debug(...) static_cast<void>(0)
#define debugg(...) static_cast<void>(0)
#define SWITCH(a,b) (b)
template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}
#endif
struct Timer{
  clock_t start;
  Timer(){
    start=clock();
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout<<fixed<<setprecision(16);
  }
  inline double now(){return (double)(clock()-start)/1000;}
  #ifdef LOCAL
  ~Timer(){
    cerr<<"time:";
    cerr<<now();
    cerr<<"ms\n";
  }
  #endif
}timer;
void SOLVE();
int main(){
  int testcase=1;
  cin>>testcase;
  for(int i=0;i<testcase;i++){
    SOLVE();
  }
}
#include<type_traits>
#include<concepts>
template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>msb(T n){return n==0?-1:31-__builtin_clz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>msb(T n){return n==0?-1:63-__builtin_clzll(n);}

template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>lsb(T n){return n==0?-1:__builtin_ctz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>lsb(T n){return n==0?-1:__builtin_ctzll(n);}

template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>floor_pow2(T n){return n==0?0:T(1)<<msb(n);}

template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>ceil_pow2(T n){return n<=1?1:T(1)<<(msb(n-1)+1);}

template<std::integral T>
constexpr T safe_div(T a,T b){return a/b-(a%b&&(a^b)<0);}
template<std::integral T>
constexpr T safe_ceil(T a,T b){return a/b+(a%b&&(a^b)>0);}
template<typename M>
struct SegmentTree{
  using S=typename M::S;
private:
  int n,z;
  std::vector<S>dat;
public:
  SegmentTree():n(0),z(0),dat(){}
  explicit SegmentTree(int n):n(n),z(ceil_pow2(n)),dat(ceil_pow2(n)*2,M::e()){}
  explicit SegmentTree(const std::vector<S>&a):n(a.size()),z(ceil_pow2((int)a.size())){
    dat.resize(z*2,M::e());
    for(int i=0;i<n;i++)dat[i+z]=a[i];
    for(int i=z-1;i>=1;i--)dat[i]=M::op(dat[i*2],dat[i*2+1]);
  }
  SegmentTree(int n,S init):SegmentTree(std::vector<S>(n,init)){}
  inline S get(int i)const{return dat[i+z];}
  void set(int i,S x){
    assert(0<=i&&i<n);
    i+=z;
    dat[i]=x;
    i>>=1;
    while(i){
      dat[i]=M::op(dat[i*2],dat[i*2+1]);
      i>>=1;
    }
  }
  S prod(int l,int r)const{
    assert(0<=l&&l<=r&&r<=n);
    l+=z,r+=z;
    S left=M::e(),right=M::e();
    while(l<r){
      if(l&1)left=M::op(left,dat[l++]);
      if(r&1)right=M::op(dat[--r],right);
      l>>=1,r>>=1;
    }
    return M::op(left,right);
  }
  inline S all_prod()const{return dat[1];}
  template<typename Func>
  int max_right(int l,const Func&f)const{
    if(l==n)return n;
    l+=z;
    S now=M::e();
    do{
      while((~l)&1)l>>=1;
      S nxt=M::op(now,dat[l]);
      if(f(nxt))now=nxt,l++;
      else{
        while(l<z){
          l<<=1;
          nxt=M::op(now,dat[l]);
          if(f(nxt))now=nxt,l++;
        }
        return l-z;
      }
    }while(l!=(l&-l));
    return n;
  }
  template<typename Func>
  int min_left(int r,const Func&f)const{
    if(r==0)return 0;
    r+=z;
    S now=M::e();
    do{
      r--;
      while(r>1&&(r&1))r>>=1;
      S nxt=M::op(dat[r],now);
      if(f(nxt))now=nxt;
      else{
        while(r<z){
          r=(r<<1)+1;
          nxt=M::op(dat[r],now);
          if(f(nxt))now=nxt,r--;
        }
        return r-z+1;
      }
    }while(r!=(r&-r));
    return 0;
  }
  friend std::ostream &operator<<(std::ostream &os,const SegmentTree&seg){
    os<<"{";
    for(int i=0;i<seg.n;i++)os<<seg.dat[i+seg.z]<<",}"[i+1==seg.n];
    if(seg.n==0)os<<"}";
    return os;
  }
};
struct M{
  struct S{
    int sz;
    int idx;
    ll mx;
  };
  static S op(S x,S y){
    S res;
    res.sz=x.sz+y.sz;
    res.mx=max(x.mx,y.mx);
    if(res.mx==x.mx)res.idx=x.idx;
    else res.idx=y.idx;
    return res;
  }
  static S e(){return S{0,-1,-1};}
};
void SOLVE(){
  int n,m;
  int k;
  ll p;
  cin>>n>>m>>k>>p;
  vector<ll>a(n),c(m);
  vector<int>b(n),d(m);
  cin>>a>>b>>c>>d;
  b--,d--;
  vector<ll>s(k);
  cin>>s;
  vector<ll>z(c);
  uniq(z);
  vector<int>orda(n);
  iota(all(orda),0);
  sort(all(orda),[&](int lhs,int rhs){return b[lhs]<b[rhs];});
  vector<int>ordb(m);
  iota(all(ordb),0);
  sort(all(ordb),[&](int lhs,int rhs){return d[lhs]<d[rhs];});
  vector<vector<pair<ll,int>>>cdcol(k);
  rep(i,m)cdcol[d[i]].emplace_back(c[i],i);
  rep(i,k)sort(all(cdcol[i]));
  vector<int>keyc(m);
  rep(i,m)keyc[i]=fkey(z,c[i]);
  auto judge=[&](ll x)->tuple<int,int,ll> {
    ll res=0;
    ll mx=-1;
    int iv=-1,jv=-1;
    {
      SegmentTree<M>seg(z.size());
      int ptr=0;
      for(int i:orda){
        while(ptr<m&&d[ordb[ptr]]<b[i]){
          auto now=seg.get(keyc[ordb[ptr]]);
          now.sz++;
          now.mx=c[ordb[ptr]];
          now.idx=ordb[ptr];
          seg.set(keyc[ordb[ptr]],now);
          ptr++;
        }
        auto now=seg.prod(0,fkey(z,x-a[i]+1));
        if(now.sz){
          res+=now.sz;
          if(chmax(mx,now.mx+a[i])){
            iv=i;
            jv=now.idx;
          }
        }
      }
    }
    reverse(all(orda)),reverse(all(ordb));
    {
      SegmentTree<M>seg(z.size());
      int ptr=0;
      for(int i:orda){
        while(ptr<m&&d[ordb[ptr]]>b[i]){
          auto now=seg.get(keyc[ordb[ptr]]);
          now.sz++;
          now.mx=c[ordb[ptr]];
          now.idx=ordb[ptr];
          seg.set(keyc[ordb[ptr]],now);
          ptr++;
        }
        auto now=seg.prod(0,fkey(z,x-a[i]+1));
        if(now.sz){
          res+=now.sz;
          if(chmax(mx,now.mx+a[i])){
            iv=i;
            jv=now.idx;
          }
        }
      }
    }
    reverse(all(orda)),reverse(all(ordb));
    rep(i,n){
      int ncol=b[i];
      ll ns=s[ncol];
      //a[i]+c[i]-ns<=x
      //c[i]<=x+ns-a[i]
      auto itr=upper_bound(all(cdcol[ncol]),pair{x+ns-a[i],(int)1e9});
      if(itr!=cdcol[ncol].begin()){
        res+=itr-cdcol[ncol].begin();
        itr--;
        if(chmax(mx,itr->first+a[i]-ns)){
          iv=i;
          jv=itr->second;
        }
      }
    }
    return {iv,jv,res};
  };
  // {
  //   auto [iv,jv,res]=judge(126);
  //   debug(res,iv,jv);
  //   return;
  // }
  ll ok=1e10,ng=-1;
  while(ok-ng>1){
    ll mid=(ok+ng)/2;
    ll cnt=get<2>(judge(mid));
    if(cnt>=p)ok=mid;
    else ng=mid;
  }
  auto [ansi,ansj,ans]=judge(ok);
  debug(ans,ok);
  cout<<ansi+1<<' '<<ansj+1<<'\n';
}
0