結果

問題 No.686 Uncertain LIS
ユーザー beetbeet
提出日時 2018-05-12 00:30:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,292 ms / 2,000 ms
コード長 4,617 bytes
コンパイル時間 1,772 ms
コンパイル使用メモリ 180,112 KB
実行使用メモリ 42,880 KB
最終ジャッジ日時 2024-05-08 00:31:08
合計ジャッジ時間 27,295 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
42,752 KB
testcase_01 AC 35 ms
42,752 KB
testcase_02 AC 36 ms
42,496 KB
testcase_03 AC 37 ms
42,752 KB
testcase_04 AC 36 ms
42,624 KB
testcase_05 AC 38 ms
42,752 KB
testcase_06 AC 35 ms
42,752 KB
testcase_07 AC 35 ms
42,624 KB
testcase_08 AC 37 ms
42,752 KB
testcase_09 AC 1,094 ms
42,880 KB
testcase_10 AC 1,058 ms
42,624 KB
testcase_11 AC 707 ms
42,752 KB
testcase_12 AC 70 ms
42,752 KB
testcase_13 AC 390 ms
42,752 KB
testcase_14 AC 453 ms
42,752 KB
testcase_15 AC 314 ms
42,752 KB
testcase_16 AC 774 ms
42,752 KB
testcase_17 AC 1,292 ms
42,752 KB
testcase_18 AC 1,259 ms
42,752 KB
testcase_19 AC 1,251 ms
42,752 KB
testcase_20 AC 1,087 ms
42,752 KB
testcase_21 AC 992 ms
42,752 KB
testcase_22 AC 599 ms
42,752 KB
testcase_23 AC 586 ms
42,880 KB
testcase_24 AC 1,153 ms
42,752 KB
testcase_25 AC 1,138 ms
42,752 KB
testcase_26 AC 691 ms
42,752 KB
testcase_27 AC 1,150 ms
42,752 KB
testcase_28 AC 1,156 ms
42,752 KB
testcase_29 AC 1,143 ms
42,624 KB
testcase_30 AC 1,173 ms
42,752 KB
testcase_31 AC 35 ms
42,624 KB
testcase_32 AC 1,004 ms
42,752 KB
testcase_33 AC 720 ms
42,624 KB
testcase_34 AC 1,225 ms
42,752 KB
testcase_35 AC 1,219 ms
42,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T,typename E>
struct RBST{
  using ll = long long;
  ll xor128(){
    static ll x = 123456789;
    static ll y = 362436069;
    static ll z = 521288629;
    static ll w = 88675123; 
    ll t;
   
    t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }
  using F = function<T(T,T)>;
  using G = function<T(T,E)>;
  using H = function<E(E,E)>;
  using P = function<E(E,size_t)>;

  F f;
  G g;
  H h;
  P p;
  T ti;
  E ei;
  
  struct Node{
    Node *l,*r;
    size_t cnt;
    T val,dat;
    E laz;
    Node():cnt(0){l=r=nullptr;}
    Node(T val,E laz):
      cnt(1),val(val),dat(val),laz(laz){l=r=nullptr;}
  };

  const size_t LIM = 1e6;
  vector<Node> pool;
  size_t ptr;

  RBST(F f,G g,H h,P p,T ti,E ei):
    f(f),g(g),h(h),p(p),ti(ti),ei(ei),pool(LIM),ptr(0){}

  Node* build(size_t l,size_t r,vector<T> &v){
    if(l+1==r) return create(v[l]);
    size_t m=(l+r)>>1;
    return merge(build(l,m,v),build(m,r,v));
  }
  
  Node* build(vector<T> &v){
    return build(0,v.size(),v);
  }
  
  inline Node* create(){
    return &pool[ptr++];
  }
  
  inline Node* create(T v){
    return &(pool[ptr++]=Node(v,ei));
  }
  
  inline size_t count(const Node* a){
    if(a==nullptr) return 0;
    return a->cnt;
  }

  inline Node* insert(Node* a,size_t pos,T v){
    Node* b=create(v);
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  inline Node* erase(Node* a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  inline T query(const Node *a){
    if(a==nullptr) return ti;
    return a->dat;
  }
  
  Node* eval(Node* a){
    if(a->laz!=ei){ 
      a->val=g(a->val,p(a->laz,1));
      if(a->l!=nullptr){
	a->l->laz=h(a->l->laz,a->laz);
	a->l->dat=g(a->l->dat,p(a->laz,count(a->l)));
      }
      if(a->r!=nullptr){
	a->r->laz=h(a->r->laz,a->laz);
	a->r->dat=g(a->r->dat,p(a->laz,count(a->r)));
      }
      a->laz=ei;
    }
    return update(a);
  }
  
  inline Node* update(Node* a){
    if(a==nullptr) return a;
    a->cnt=count(a->l)+count(a->r)+1;
    a->dat=f(a->val,f(query(a->l),query(a->r)));
    return a;
  }

  inline T query(Node *&a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=t.first;
    T res=query(u);
    a=merge(s.first,merge(t.first,t.second));
    return res;
  }
  
  inline void update(Node *&a,size_t l,size_t r,E v){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=t.first;
    u->laz=h(u->laz,v);
    a=merge(merge(s.first,eval(u)),t.second);
  }

  inline T get(Node *&a,size_t p){
    auto s=split(a,p);
    auto t=split(s.second,1);
    T res=t.first->val;
    a=merge(s.first,merge(t.first,t.second));
    return res;
  }
  
  void dump(Node* a,typename vector<T>::iterator it){
    if(!count(a)) return;
    a=eval(a);
    dump(a->l,it);
    *(it+count(a->l))=a->val;
    dump(a->r,it+count(a->l)+1);
  }
  
  vector<T> dump(Node* a){
    vector<T> v(count(a));
    dump(a,v.begin());
    return v;
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      return update(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    return update(b);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      auto s=split(a->l,k);
      a->l=s.second;
      return make_pair(s.first,update(a));
    }
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    return make_pair(update(a),s.second);
  } 
  
};


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){

  using P = pair<Int, Int>;
  RBST<Int, P>::F f=[](Int a,Int b){return max(a,b);};  
  RBST<Int, P>::G g=[](Int a,P b){return max(b.first,a+b.second);};
  RBST<Int, P>::H h=[](P a,P b){
    return P(max(a.first+b.second,b.first),a.second+b.second);};
  RBST<Int, P>::P p=[](P a,size_t b){++b;return a;};

  RBST<Int, P> rbst(f,g,h,p,-1,P(-1,0));  
  using Node =  RBST<Int, P>::Node;
  
  Int sz=1e5+100;
  vector<Int> v(sz,0);
  Node* r=rbst.build(v);
  
  Int n;
  cin>>n;
  for(Int i=0;i<n;i++){
    Int a,b;
    cin>>a>>b;
    
    r=rbst.erase(r,b);
    Int k=rbst.get(r,a-1);
    r=rbst.insert(r,a-1,k);
    rbst.update(r,a,b+1,P(0,1));
    
    k=rbst.get(r,b);
    rbst.update(r,b+1,sz,P(k,0));
  }
  
  cout<<rbst.query(r,0,sz)<<endl;
  return 0;
}
0