Hướng dẫn giải của Câu Cá Vạn Cân


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
#include <bits/stdc++.h>
using namespace std;
long long n,q,x,tv,w;
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>q;
    map<long long,long long> cnt;
    long long weight = 0;
    for (int i = 1;i<=n;i++){
        cin>>x;
        cnt[x]++;
        weight+=x;
    }
    while(q--){
        cin>>tv;
        if (tv == 1){
            cin>>x;
            cnt[x]++;
            weight+=x;
        }
        else if (tv == 2){
            cin>>x;
            cnt[x]--;
            weight-=x;
            if (!cnt[x]) cnt.erase(x);
        }
        else{
            cin>>w;
            auto it = cnt.lower_bound(w);
            vector<pair<long long,long long>> update;
            while(it != cnt.end()){
                update.emplace_back(*it);
                it++;
            }
            for (auto [f,s] : update){
                cnt.erase(f);
                weight-=f*s;
                cnt[f%w]+=s;
                weight+=(f%w)*s;
            }
        }
        cout<<weight<<"\n";
    }
}

Đang tải...