結果

問題 No.924 紲星
ユーザー shinchanshinchan
提出日時 2023-09-12 18:20:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,737 ms / 4,000 ms
コード長 3,215 bytes
コンパイル時間 3,110 ms
コンパイル使用メモリ 229,752 KB
実行使用メモリ 49,388 KB
最終ジャッジ日時 2023-09-12 18:21:32
合計ジャッジ時間 20,773 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 7 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 8 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3,601 ms
46,856 KB
testcase_09 AC 3,575 ms
48,848 KB
testcase_10 AC 3,568 ms
49,308 KB
testcase_11 AC 3,737 ms
48,756 KB
testcase_12 AC 3,648 ms
49,388 KB
testcase_13 AC 1,742 ms
27,072 KB
testcase_14 AC 1,859 ms
34,676 KB
testcase_15 AC 1,612 ms
24,504 KB
testcase_16 AC 1,055 ms
23,396 KB
testcase_17 AC 2,825 ms
42,700 KB
testcase_18 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

struct SegmentTree {
private:
    ll n;
    vector<ll> node;
    ll ID = 0; // min -> INF, max, sum -> 0
    inline ll func(ll a, ll b){return a + b;} /////////

public:
    SegmentTree(vector<ll> v) {
        int sz = v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, ID);
        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = func(node[2*i+1], node[2*i+2]);
    }

    void add(ll x, ll val) {
        x += (n - 1);
        node[x] += val;
        while(x > 0) {
            x = (x - 1) / 2;
            node[x] = func(node[2*x+1], node[2*x+2]);
        }
    }

    ll get(ll a, ll b, ll k=0, ll l=0, ll r=-1) {
        if(r < 0) r = n;
        if(r <= a || b <= l) return ID;
        if(a <= l && r <= b) return node[k];

        ll vl = get(a, b, 2*k+1, l, (l+r)/2);
        ll vr = get(a, b, 2*k+2, (l+r)/2, r);
        return func(vl, vr);
    }
};

struct node {
    ll a, b, c, d, e, f;
    bool operator<( const node& right ) const {
        return a < right.a;
    }
};
using T = tuple<ll, ll, ll, ll, ll, ll>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n, q;
    cin >> n >> q;
    vector<pair<ll, ll>> v;
    SegmentTree seg_(vector<ll> (n, 0));
    rep(i, n) {
        ll a;
        cin >> a;
        seg_.add(i, a);
        v.push_back({a, i});
    }
    sort(all(v));
    vector<node> task;
    rep(i, q) {
        ll le, ri;
        cin >> le >> ri;
        task.push_back({0LL, -1LL << 30, 1LL << 30, le - 1, ri, i});
    } 
    sort(all(task));

    bool f = true;
    vector<tuple<ll, ll, ll, ll>> query; 
    while(f) {
        vector<node> nx;
        int id = 0;
        f = false;
        SegmentTree seg(vector<ll> (n, 0));
        for(auto [mid, Min, Max, le, ri, i] : task) {
            while(id < n and v[id].first <= mid) {
                seg.add(v[id].second, 1);
                id ++;
            }
            if(seg.get(le, ri) * 2 >= (ri - le)) Max = mid;
            else Min = mid;
            ll m = (Max + Min) / 2;
            if(Max - Min == 1LL) {
                query.push_back({Max, le, ri, i});
                continue;
            }
            f = true;
            nx.push_back({m, Min, Max, le, ri, i});
        }
        swap(nx, task);
        sort(all(task));
    }

    sort(all(query));

    vector<ll> ans(q);
    {
        int id = 0;
        SegmentTree seg(vector<ll> (n, 0)), seg_cnt(vector<ll> (n, 0));
        for(auto [m, le, ri, i] : query) {
            while(id < n and v[id].first <= m) {
                seg_cnt.add(v[id].second, 1);
                seg.add(v[id].second, - v[id].first);
                id ++;
            }
            ans[i] = seg_.get(le, ri) + seg.get(le, ri) * 2 - m * (ri - le) + m * 2 * seg_cnt.get(le, ri);
        }
    }

    rep(i, q) {
        cout << ans[i] << endl;
    }

    return 0;
}
0