結果

問題 No.1705 Mode of long array
ユーザー harurunharurun
提出日時 2021-09-19 14:31:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 563 ms / 3,000 ms
コード長 3,208 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 121,224 KB
実行使用メモリ 35,168 KB
最終ジャッジ日時 2023-09-30 09:36:45
合計ジャッジ時間 18,581 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 12 ms
4,380 KB
testcase_07 AC 15 ms
4,376 KB
testcase_08 AC 14 ms
4,380 KB
testcase_09 AC 12 ms
4,380 KB
testcase_10 AC 11 ms
4,380 KB
testcase_11 AC 13 ms
4,380 KB
testcase_12 AC 13 ms
4,380 KB
testcase_13 AC 431 ms
31,212 KB
testcase_14 AC 267 ms
22,096 KB
testcase_15 AC 204 ms
14,828 KB
testcase_16 AC 268 ms
17,568 KB
testcase_17 AC 182 ms
13,852 KB
testcase_18 AC 154 ms
12,664 KB
testcase_19 AC 335 ms
22,640 KB
testcase_20 AC 187 ms
15,548 KB
testcase_21 AC 319 ms
26,900 KB
testcase_22 AC 525 ms
35,168 KB
testcase_23 AC 126 ms
6,320 KB
testcase_24 AC 126 ms
6,364 KB
testcase_25 AC 130 ms
6,484 KB
testcase_26 AC 126 ms
6,452 KB
testcase_27 AC 126 ms
6,324 KB
testcase_28 AC 126 ms
6,464 KB
testcase_29 AC 126 ms
6,456 KB
testcase_30 AC 127 ms
6,328 KB
testcase_31 AC 128 ms
6,276 KB
testcase_32 AC 127 ms
6,328 KB
testcase_33 AC 553 ms
31,452 KB
testcase_34 AC 563 ms
31,388 KB
testcase_35 AC 551 ms
31,484 KB
testcase_36 AC 545 ms
31,636 KB
testcase_37 AC 555 ms
31,584 KB
testcase_38 AC 545 ms
31,568 KB
testcase_39 AC 538 ms
31,752 KB
testcase_40 AC 547 ms
31,708 KB
testcase_41 AC 549 ms
31,508 KB
testcase_42 AC 554 ms
31,504 KB
testcase_43 AC 320 ms
17,140 KB
testcase_44 AC 316 ms
17,136 KB
testcase_45 AC 315 ms
17,060 KB
testcase_46 AC 317 ms
17,120 KB
testcase_47 AC 317 ms
17,060 KB
testcase_48 AC 322 ms
17,200 KB
testcase_49 AC 371 ms
22,600 KB
testcase_50 AC 372 ms
22,740 KB
testcase_51 AC 369 ms
22,736 KB
testcase_52 AC 383 ms
22,912 KB
testcase_53 AC 391 ms
22,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <utility>
using namespace std;
using ll=long long;

struct Queries{
  ll T,X,Y;
};

class Mode{
  private:
    unordered_map<ll,ll> place;
    vector<ll> count;
    vector<set<ll>> S;
    multiset<ll> max_S;
  public:
    Mode(ll M,unordered_map<ll,ll>& um,vector<ll>& A){
      place=um;
      S.resize(um.size());
      count.resize(M,0);
      for(ll i=0;i<M;i++){
        // i が A[i] 個ある
        max_S.insert(0);
        add(i,A.at(i));
        //max_S.insert(place.at(A.at(i)));
        //cerr<<"insert max_S: "<<A.at(i)<<" "<<place.at(A.at(i))<<endl;
        //cerr<<"init add: "<<i<<" "<<A.at(i)<<endl;
      }
    }
    void add(ll x,ll y){
      // x:0-indexed
      ll now=count.at(x);
      //cerr<<"x: "<<x<<", y: "<<y<<", now: "<<now<<endl;
      S.at(place.at(now)).erase(x);
      max_S.erase(max_S.find(place.at(now)));
      //cerr<<"erase max_S: "<<now<<" "<<place.at(now)<<endl;
      S.at(place.at(now+y)).insert(x);
      max_S.insert(place.at(now+y));
      //cerr<<"insert max_S: "<<now+y<<" "<<place.at(now+y)<<endl;
      count.at(x)+=y;
    }

    void erase(ll x,ll y){
      ll now=count.at(x);
      S.at(place.at(now)).erase(x);
      max_S.erase(max_S.find(place.at(now)));
      //cerr<<"erase max_S: "<<now<<" "<<place.at(now)<<endl;
      S.at(place.at(now-y)).insert(x);
      max_S.insert(place.at(now-y));
      //cerr<<"insert max_S: "<<now-y<<" "<<place.at(now-y)<<endl;
      count.at(x)-=y;
    }
    ll get(){
      //cerr<<"get"<<endl;
      ll max_place=*max_S.rbegin();
      //cerr<<"max_place: "<<max_place<<endl;
      return *S.at(max_place).rbegin()+1;
    }

    void debug_conut(){
      for(ll i=0;i<count.size();i++){
        cerr<<count.at(i)<<" ";
      }
      cerr<<endl;
    }

    void debug_max_S(){
      cerr<<"max_S: ";
      for(ll i:max_S){
        cerr<<i<<" ";
      }
      cerr<<endl;
    }

};


int main(){
  ll N,M;
  cin>>N>>M;
  vector<ll> a(M);
  for(ll i=0;i<M;i++){
    cin>>a.at(i);
  }
  vector<ll> b=a;
  ll Q;
  cin>>Q;
  vector<Queries> query(Q);
  // クエリ先読みして座圧する
  for(ll i=0;i<Q;i++){
    cin>>query.at(i).T>>query.at(i).X>>query.at(i).Y;
    if(query.at(i).T==1){
      b.push_back(b.at(query.at(i).X-1));
      b.at(query.at(i).X-1)+=query.at(i).Y;
    }else if(query.at(i).T==2){
      b.push_back(b.at(query.at(i).X-1));
      b.at(query.at(i).X-1)-=query.at(i).Y;
    }
  }
  b.push_back(0);
  sort(b.begin(),b.end());
  //cerr<<"sort end"<<endl;
  unordered_map<ll,ll> place;
  place.insert(make_pair(b.at(0),0));
  ll cnt=1;
  //cerr<<"place: 0 ";
  for(ll i=1;i<b.size();i++){
    if(b.at(i-1)!=b.at(i)){
      //cerr<<b.at(i)<<" ";
      place.insert(make_pair(b.at(i),cnt));
      cnt++;
    }
  }
  //cerr<<endl;
  Mode mode(M,place,a);
  //cerr<<"query start"<<endl;
  for(ll i=0;i<Q;i++){
    //mode.debug_max_S();
    if(query.at(i).T==1){
      mode.add(query.at(i).X-1,query.at(i).Y);
    }else if(query.at(i).T==2){
      mode.erase(query.at(i).X-1,query.at(i).Y);
    }else{
      cout<<mode.get()<<endl;
    }
    //cerr<<"i: "<<i<<" end"<<endl;
  }
  return 0;
}
0