結果

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

ソースコード

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();
  }
}
template<typename I,typename M>
struct DynamicSegmentTree{
  using S=typename M::S;
private:
  struct node{
    I key;
    S v,sum;
    node *par,*left,*right;
    node(I key_,S v_):key(key_),v(v_),sum(v_),par(nullptr),left(nullptr),right(nullptr){}
    inline void update(){
      sum=v;
      if(left)sum=M::op(left->sum,sum);
      if(right)sum=M::op(sum,right->sum);
    }
    void splay(){
      while(this->par){
        node *p=this->par;
        if(!this->par->par){
          if(p->left==this){
            if(this->right)this->right->par=p;
            p->left=this->right;
            this->right=p;
            this->par=nullptr;
            p->par=this;
          }
          else{
            if(this->left)this->left->par=p;
            p->right=this->left;
            this->left=p;
            this->par=nullptr;
            p->par=this;
          }
          this->sum=p->sum;
          p->update();
        }
        else{
          node *pp=p->par;
          if(p->left==this){
            if(pp->left==p){
              if(this->right)this->right->par=p;
              if(p->right)p->right->par=pp;
              p->left=this->right;
              this->right=p;
              pp->left=p->right;
              p->right=pp;
              if(pp->par){
                if(pp->par->left==pp)pp->par->left=this;
                else pp->par->right=this;
              }
              this->par=pp->par;
              p->par=this;
              pp->par=p;
            }
            else{
              if(this->left)this->left->par=pp;
              if(this->right)this->right->par=p;
              pp->right=this->left;
              p->left=this->right;
              if(pp->par){
                if(pp->par->left==pp)pp->par->left=this;
                else pp->par->right=this;
              }
              this->par=pp->par;
              pp->par=this;
              p->par=this;
              this->left=pp;
              this->right=p;
            }
          }
          else{
            if(pp->left==p){
              if(this->left)this->left->par=p;
              if(this->right)this->right->par=pp;
              p->right=this->left;
              pp->left=this->right;
              if(pp->par){
                if(pp->par->left==pp)pp->par->left=this;
                else pp->par->right=this;
              }
              this->par=pp->par;
              p->par=this;
              pp->par=this;
              this->left=p;
              this->right=pp;
            }
            else{
              if(this->left)this->left->par=p;
              if(p->left)p->left->par=pp;
              p->right=this->left;
              this->left=p;
              pp->right=p->left;
              p->left=pp;
              if(pp->par){
                if(pp->par->left==pp)pp->par->left=this;
                else pp->par->right=this;
              }
              this->par=pp->par;
              p->par=this;
              pp->par=p;
            }
          }
          this->sum=pp->sum;
          pp->update();
          p->update();
        }
      }
    }
  };
  bool lower_bound(node*&nd,I key){
    while(nd){
      if(nd->key==key){
        nd->splay();
        return true;
      }
      else if(nd->key>key){
        if(nd->left)nd=nd->left;
        else{
          nd->splay();
          return true;
        }
      }
      else{
        if(nd->right)nd=nd->right;
        else{
          nd->splay();
          if(nd->right){
            nd=nd->right;
            while(nd->left)nd=nd->left;
            nd->splay();
            return true;
          }
          return false;
        }
      }
    }
    return false;
  }
  node *root;
public:
  DynamicSegmentTree():root(nullptr){}
  void set(I key,S val){
    if(!root)root=new node(key,val);
    else{
      while(true){
        if(root->key==key){
          root->splay();
          root->v=val;
          root->update();
          break;
        }
        else if(root->key>key){
          if(root->left)root=root->left;
          else{
            root->splay();
            node *nd=new node(key,val);
            nd->left=root->left;
            if(nd->left)nd->left->par=nd;
            nd->right=root;
            root->par=nd;
            root->left=nullptr;
            root->update();
            nd->update();
            root=nd;
            break;
          }
        }
        else{
          if(root->right)root=root->right;
          else{
            root->splay();
            node *nd=new node(key,val);
            nd->right=root->right;
            if(nd->right)nd->right->par=nd;
            nd->left=root;
            root->par=nd;
            root->right=nullptr;
            root->update();
            nd->update();
            root=nd;
            break;
          }
        }
      }
    }
  }
  S get(I key){
    if(!root)return M::e();
    else{
      while(true){
        if(root->key==key){
          root->splay();
          return root->v;
        }
        else if(root->key>key){
          if(root->left)root=root->left;
          else break;
        }
        else{
          if(root->right)root=root->right;
          else break;
        }
      }
      root->splay();
      return M::e();
    }
  }
  S prod(I l,I r){
    if(!root)return M::e();
    if(!lower_bound(root,l))return M::e();
    node *nd=root->left;
    root->left=nullptr;
    root->update();
    node *pre=root;
    if(!lower_bound(root,r)){
      S ret=root->sum;
      root=pre;
      root->splay();
      root->left=nd;
      if(nd)nd->par=root;
      root->update();
      return ret;
    }
    S ret=root->left?root->left->sum:M::e();
    root=pre;
    root->splay();
    root->left=nd;
    if(nd)nd->par=root;
    root->update();
    return ret;
  }
  S all_prod()const{return root?root->sum:M::e();}
};
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<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]));
  auto judge=[&](ll x)->tuple<int,int,ll> {
    ll res=0;
    ll mx=-1;
    int iv=-1,jv=-1;
    {
      DynamicSegmentTree<ll,M>seg;
      int ptr=0;
      for(int i:orda){
        while(ptr<m&&d[ordb[ptr]]<b[i]){
          auto now=seg.get(c[ordb[ptr]]);
          now.sz++;
          now.mx=c[ordb[ptr]];
          now.idx=ordb[ptr];
          seg.set(c[ordb[ptr]],now);
          ptr++;
        }
        auto now=seg.prod(-2e9,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));
    {
      DynamicSegmentTree<ll,M>seg;
      int ptr=0;
      for(int i:orda){
        while(ptr<m&&d[ordb[ptr]]>b[i]){
          auto now=seg.get(c[ordb[ptr]]);
          now.sz++;
          now.mx=c[ordb[ptr]];
          now.idx=ordb[ptr];
          seg.set(c[ordb[ptr]],now);
          ptr++;
        }
        auto now=seg.prod(-2e9,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