結果

問題 No.448 ゆきこーだーの雨と雪 (3)
ユーザー beet
提出日時 2018-11-13 19:25:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 2,772 bytes
コンパイル時間 2,085 ms
コンパイル使用メモリ 203,648 KB
最終ジャッジ日時 2025-01-06 16:36:20
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


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 eval(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--) eval(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){
    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){
    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);
  }
};

//INSERT ABOVE HERE
signed main(){
  Int n,k;
  cin>>n>>k;
  vector<Int> t(n),d(n);
  for(Int i=0;i<n;i++) cin>>t[i]>>d[i];

  auto check=
    [&](Int x){
      Int lst=-k;
      for(Int i=0;i<n;i++){
        if(d[i]<=x) continue;
        if(lst+k>t[i]) return 0;
        lst=t[i];
      }
      return 1;
    };
  
  Int l=-1,r=1e9+100;
  while(l+1<r){
    Int m=(l+r)>>1;
    if(check(m)) r=m;
    else l=m;
  }
  cout<<r<<endl;
  const Int INF = 1e18;
  auto f=[](Int a,Int b){return min(a,b);};
  auto g=[](Int a,Int b){return a+b;};
  Int ti=INF,ei=0;
  SegmentTree<Int, Int> seg(f,g,g,ti,ei);
  vector<Int> dp(n+1,INF);
  dp[0]=0;
  seg.build(dp);

  Int p=-1;
  for(Int i=0;i<n;i++){
    Int x=upper_bound(t.begin(),t.end(),t[i]-k)-t.begin();

    
    dp[i]=seg.query(p+1,x+1);
    seg.set_val(i+1,dp[i]);
    
    seg.update(p+1,i+1,d[i]);
    
    if(d[i]>r) p=i;    
  }
  
  Int ans=seg.query(p+1,n+1);
  cout<<ans<<endl;
  return 0;
}
0