結果

問題 No.449 ゆきこーだーの雨と雪 (4)
ユーザー beetbeet
提出日時 2018-11-13 18:11:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 885 ms / 5,000 ms
コード長 5,532 bytes
コンパイル時間 3,168 ms
コンパイル使用メモリ 240,976 KB
実行使用メモリ 42,064 KB
最終ジャッジ日時 2023-10-23 16:08:57
合計ジャッジ時間 24,024 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 5 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 4 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 495 ms
10,504 KB
testcase_13 AC 594 ms
27,648 KB
testcase_14 AC 530 ms
25,252 KB
testcase_15 AC 603 ms
28,700 KB
testcase_16 AC 547 ms
26,840 KB
testcase_17 AC 565 ms
27,124 KB
testcase_18 AC 472 ms
10,504 KB
testcase_19 AC 528 ms
25,524 KB
testcase_20 AC 517 ms
25,004 KB
testcase_21 AC 679 ms
27,660 KB
testcase_22 AC 561 ms
27,936 KB
testcase_23 AC 472 ms
10,500 KB
testcase_24 AC 544 ms
26,584 KB
testcase_25 AC 449 ms
10,504 KB
testcase_26 AC 399 ms
10,240 KB
testcase_27 AC 574 ms
27,144 KB
testcase_28 AC 574 ms
27,648 KB
testcase_29 AC 545 ms
25,248 KB
testcase_30 AC 607 ms
28,188 KB
testcase_31 AC 595 ms
28,200 KB
testcase_32 AC 543 ms
24,204 KB
testcase_33 AC 559 ms
25,548 KB
testcase_34 AC 489 ms
10,768 KB
testcase_35 AC 612 ms
28,184 KB
testcase_36 AC 482 ms
10,772 KB
testcase_37 AC 885 ms
42,064 KB
testcase_38 AC 664 ms
26,256 KB
testcase_39 AC 509 ms
26,256 KB
testcase_40 AC 532 ms
26,256 KB
testcase_41 AC 710 ms
26,256 KB
testcase_42 AC 716 ms
26,256 KB
testcase_43 AC 304 ms
11,636 KB
testcase_44 AC 288 ms
12,384 KB
testcase_45 AC 401 ms
42,064 KB
権限があれば一括ダウンロードができます

ソースコード

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> 
struct BIT{
  Int n;
  vector<T> bit;
  //1-indexed
  BIT():n(-1){}
  BIT(Int n_,T d):n(n_),bit(n_+1,d){}
  
  T sum(Int i){
    T s=bit[0];
    for(Int x=i;x>0;x-=(x&-x))
      s+=bit[x];
    return s;
  }
  void add(Int i,T a){
    if(i==0) return;
    for(Int x=i;x<=n;x+=(x&-x))
      bit[x]+=a;
  }
  
  Int lower_bound(Int w){
    if(w<=0) return 0;
    Int x=0,r=1;
    while(r<n) r<<=1;
    for(Int k=r;k>0;k>>=1){
      if(x+k<=n&&bit[x+k]<w){
        w-=bit[x+k];
        x+=k;
      }
    }
    return x+1;
  }
  
  T sum0(Int i){
    return sum(i+1);
  }
  void add0(Int i,T a){
    add(i+1,a);
  }

  T query(Int l,Int r){
    return sum(r-1)-sum(l-1);
  }

  T query0(Int l,Int r){
    return sum(r)-sum(l);
  }
};


template<typename T>
vector<T> compress(vector<T> v){
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  return v;
}

template<typename T>
map<T, Int> dict(const vector<T> &v){
  map<T, Int> res;
  for(Int i=0;i<(Int)v.size();i++)
    res[v[i]]=i;
  return res;
}


template<typename T,size_t X>
struct BinaryTrie{
  struct Node{
    size_t cnt;
    Node *p,*l,*r;
    Node(Node* p):cnt(0),p(p){l=r=nullptr;}
  };
  
  T acc;
  Node *root;
  BinaryTrie():acc(0){root=emplace(nullptr);}

  void dfs(Node *a){
    if(!a) return;
    dfs(a->l);dfs(a->r);
    delete(a);
  }
  
  inline Node* emplace(Node* p){
    return new Node(p);
  }

  inline size_t count(Node* a){
    return a?a->cnt:0;
  }
  
  void add(const T b,size_t k=1){
    const T nb=b^acc;
    Node* a=root;
    for(int i=X-1;i>=0;i--){
      bool f=(nb>>i)&1;
      if(!f&&!a->l) a->l=emplace(a);
      if( f&&!a->r) a->r=emplace(a);
      a=f?a->r:a->l;
    }
    a->cnt+=k;    
    while((a=a->p)) a->cnt=count(a->l)+count(a->r);    
  }

  inline void update(const T b){acc^=b;}

  Node* find(const T b){
    const T nb=b^acc;
    Node* a=root;
    for(int i=X-1;i>=0;i--){
      bool f=(nb>>i)&1;
      a=f?a->r:a->l;
      if(!a) return a;
    }
    return a;
  }

  Node* check(Node *a){
    if(!a||count(a)) return a;
    delete(a);
    return nullptr;
  }

  void sub(Node* a,size_t k=1){
    assert(a&&a->cnt>=k);
    a->cnt-=k;
    while((a=a->p)){
      a->l=check(a->l);
      a->r=check(a->r);
      a->cnt=count(a->l)+count(a->r);
    }
  }
  
  Node* xmax(const T b){
    assert(count(root));
    const T nb=b^acc;
    Node* a=root;    
    for(int i=X-1;i>=0;i--){
      bool f=(nb>>i)&1;
      if(!a->l||!a->r) a=a->l?a->l:a->r;
      else a=f?a->l:a->r;
    }
    return a;
  }

  Node* xmin(const T b){
    return xmax(~b&((T(1)<<X)-1));
  }

  Node* ge(Node *a,int i){
    if(!a) return a;
    Node *l=a->l,*r=a->r;
    if((acc>>i)&1) swap(l,r);
    if(l||r) return ge(l?l:r,i+1);
    return a;
  }
  
  Node* next(Node* a,int i){
    if(!(a->p)) return nullptr;
    Node *l=a->p->l,*r=a->p->r;
    if((acc>>(i+1))&1) swap(l,r);
    if(a==l&&r) return ge(r,i);
    return next(a->p,i+1);
  }
  
  Node* lower_bound(const T b){
    const T nb=b^acc;
    Node* a=root;
    for(int i=X-1;i>=0;i--){
      bool f=(nb>>i)&1;
      if(!f&&a->l){a=a->l;continue;}
      if( f&&a->r){a=a->r;continue;}
      if((b>>i)&1) return next(a,i);
      return ge(a,i);
    }
    return a;
  }

  Node* upper_bound(const T b){
    return lower_bound(b+1);
  }
  
  T val(Node* a){
    T res(0);
    for(int i=0;i<(int)X;i++){
      assert(a->p);
      res|=(T(a==a->p->r)<<i);
      a=a->p;
    }
    return res^acc;
  }

  Node* find_by_order(size_t k){
    Node *a=root;
    if(count(a)<=k) return nullptr;
    for(int i=X-1;i>=0;i--){
      bool f=(acc>>i)&1;
      if(count(f?a->r:a->l)<=k){
        k-=count(f?a->r:a->l);
        a=f?a->l:a->r;
      }else{
        a=f?a->r:a->l;
      }
    }
    return a;
  }
  
  size_t order_of_key(const T b){
    Node *a=root;
    size_t res=0;
    for(int i=X-1;i>=0;i--){      
      Node *l=a->l,*r=a->r;
      if((acc>>i)&1) swap(l,r);
      bool f=(b>>i)&1;
      if(f) res+=count(l); 
      a=f?r:l;
      if(!a) break;
    }
    return res;
  }
};

//INSERT ABOVE HERE
signed main(){
  int n;
  cin>>n;
  vector<int> l(n);
  for(int i=0;i<n;i++) cin>>l[i];
  
  vector<int> cnt(n,0);
  auto sc=[&](int x){return 50*l[x]+(500*l[x]/(8+2*++cnt[x]));};

  int q;
  cin>>q;
  vector<string> ss;
  vector<char> ps;
  map<string, int> lst,sum;

  vector<int> vs;
  for(int i=0;i<q;i++){
    string s;
    char p;    
    cin>>s>>p;
    if(isalpha(p)){
      lst[s]=i;
      sum[s]+=sc(p-'A');
    }
    vs.emplace_back(sum[s]);
    ss.emplace_back(s);
    ps.emplace_back(p);
  }
  
  vs.emplace_back(0);
  vs.emplace_back(1e9);
  vs=compress(vs);
  auto dc=dict(vs);

  int m=lst.size();
  BIT<int> bit(vs.size(),0);
  using BT = BinaryTrie<int, 20>;
  vector<BT> ords(vs.size());
  bit.add0(dc[0],m);
  
  cnt.assign(n,0);
  lst.clear();
  sum.clear();  
  for(int i=0;i<q;i++){
    string s=ss[i];
    char p=ps[i];
    int &val=sum[s];
    if(isalpha(p)){
      bit.add0(dc[val],-1);
      if(lst.count(s))
        ords[dc[val]].sub(ords[dc[val]].find(lst[s]));
      lst[s]=i;
      val+=sc(p-'A');
      bit.add0(dc[val],1);      
      ords[dc[val]].add(lst[s]);
    }else{
      int cnt=m-bit.sum0(dc[val]);
      cnt+=ords[dc[val]].order_of_key(lst[s]);
      cout<<cnt+1<<endl;
    }
  }
  
  return 0;
}
0