結果

問題 No.686 Uncertain LIS
ユーザー beetbeet
提出日時 2018-05-12 00:29:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,617 bytes
コンパイル時間 3,000 ms
コンパイル使用メモリ 179,112 KB
実行使用メモリ 11,564 KB
最終ジャッジ日時 2023-09-10 18:08:42
合計ジャッジ時間 34,680 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
11,224 KB
testcase_01 AC 12 ms
11,384 KB
testcase_02 AC 12 ms
11,216 KB
testcase_03 AC 13 ms
11,208 KB
testcase_04 AC 13 ms
11,080 KB
testcase_05 AC 14 ms
11,188 KB
testcase_06 AC 13 ms
11,124 KB
testcase_07 AC 13 ms
11,232 KB
testcase_08 AC 13 ms
11,080 KB
testcase_09 AC 1,246 ms
11,148 KB
testcase_10 AC 1,182 ms
11,328 KB
testcase_11 AC 790 ms
11,240 KB
testcase_12 AC 50 ms
11,248 KB
testcase_13 AC 427 ms
11,312 KB
testcase_14 AC 478 ms
11,128 KB
testcase_15 AC 322 ms
11,128 KB
testcase_16 AC 817 ms
11,136 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 13 ms
11,196 KB
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
権限があれば一括ダウンロードができます

ソースコード

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 = 2e5;
  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