結果

問題 No.924 紲星
ユーザー milanis48663220milanis48663220
提出日時 2023-08-11 00:05:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,555 bytes
コンパイル時間 1,616 ms
コンパイル使用メモリ 133,828 KB
実行使用メモリ 22,556 KB
最終ジャッジ日時 2023-08-11 00:05:48
合計ジャッジ時間 9,444 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

template<typename T>
class Mo{
    public:
    vector<int> l, r;
    int bucket_size;
    Mo(int bucket_size): l(vector<int>()), r(vector<int>()), bucket_size(bucket_size) {}
    void add_query(int l_, int r_){
        l.push_back(l_);
        r.push_back(r_);
    }
    template<typename ADD, typename DEL, typename GET_ANS>
    vector<T> solve(ADD add, DEL del, GET_ANS get_ans){
        int sz = l.size();
        vector<int> ord(sz);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](const int a, const int b){
            const int c = l[a] / bucket_size, d = l[b] / bucket_size;
            return (c == d) ? ((c & 1) ? (r[b] < r[a]) : (r[a] < r[b])) : (c < d);
        });
        int li = 0, ri = 0;
        vector<T> ans(sz);
        for(const int i : ord){
            while(li > l[i]) add(--li);
            while(ri < r[i]) add(ri++);
            while(li < l[i]) del(li++);
            while(ri > r[i]) del(--ri);
            ans[i] = get_ans();
        }
        return ans;
    }
};

class MedianTracker{
    public:
    ll sum_l = 0;
    ll sum_r = 0;
    int size = 0;
    ll size_l(){
        return stl.size();
    }
    ll size_r(){
        return str.size();
    }
    void push(ll x){
        if(str.empty()){
            str.insert(x);
            sum_r += x;
        }else{
            if(median() > x) {
                stl.insert(x);
                sum_l += x;
            }else{
                str.insert(x);
                sum_r += x;
            }
        }
        size++;
        balance();
    }
    void pop(ll x){
        if(stl.find(x) != stl.end()) {
            stl.erase(stl.find(x));
            sum_l -= x;
        }else{
            str.erase(str.find(x));
            sum_r -= x;
        }
        size--;
        balance();
    }
    ll median(){
        return *str.begin();
    }
    private:
    void balance(){
        while(!str.empty() && !stl.empty() && *str.begin() < *stl.begin()){
            ll x = *stl.begin();
            stl.erase(stl.begin());
            str.insert(x);
            sum_l -= x;
            sum_r += x;
        }
        while(2*str.size() < size){
            ll x = *stl.begin();
            stl.erase(stl.begin());
            str.insert(x);
            sum_l -= x;
            sum_r += x;
        }
        while(2*str.size() > size+1){
            ll x = *str.begin();
            str.erase(str.begin());
            stl.insert(x);
            sum_l += x;
            sum_r -= x;
        }
    }
    multiset<ll, greater<ll>> stl;
    multiset<ll> str;
};

using P = pair<ll, ll>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, q; cin >> n >> q;
    vector<ll> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    auto mt = MedianTracker();
    auto add = [&](int i){
        mt.push(a[i]);
    };
    auto del = [&](int i){
        mt.pop(a[i]);
    };
    auto get_ans = [&](){
        ll sl = mt.size_l();
        ll sr = mt.size_r();
        ll sum_l = mt.sum_l;
        ll sum_r = mt.sum_r;
        ll x = mt.median();
        return (sum_r-sum_l) -x*(sr-sl);
    };
    auto mo = Mo<ll>(sqrt(n));
    for(int i = 0; i < q; i++){
        int l, r; cin >> l >> r; l--;
        mo.add_query(l, r);
    }
    auto ans = mo.solve(add, del, get_ans);
    print_vector(ans, '\n');
}
0