結果

問題 No.1705 Mode of long array
ユーザー harurun
提出日時 2021-09-19 18:23:11
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 635 ms / 3,000 ms
コード長 2,114 bytes
コンパイル時間 1,941 ms
コンパイル使用メモリ 119,108 KB
最終ジャッジ日時 2025-01-24 15:53:59
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

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++){
        max_S.insert(0);
        add(i,A.at(i));
      }
    }
    void add(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)));
      S.at(place.at(now+y)).insert(x);
      max_S.insert(place.at(now+y));
      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)));
      S.at(place.at(now-y)).insert(x);
      max_S.insert(place.at(now-y));
      count.at(x)-=y;
    }
    ll get(){
      ll max_place=*max_S.rbegin();
      return *S.at(max_place).rbegin()+1;
    }
};


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());
  unordered_map<ll,ll> place;
  place.insert(make_pair(b.at(0),0));
  ll cnt=1;
  for(ll i=1;i<b.size();i++){
    if(b.at(i-1)!=b.at(i)){
      place.insert(make_pair(b.at(i),cnt));
      cnt++;
    }
  }
  Mode mode(M,place,a);
  for(ll i=0;i<Q;i++){
    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;
    }
  }
  return 0;
}
0