結果

問題 No.3078 Difference Sum Query
ユーザー NortGlG
提出日時 2025-04-20 03:52:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 708 ms / 2,000 ms
コード長 1,745 bytes
コンパイル時間 2,472 ms
コンパイル使用メモリ 205,356 KB
実行使用メモリ 64,352 KB
最終ジャッジ日時 2025-04-20 03:52:25
合計ジャッジ時間 16,299 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100100;
// fat node fenwick tree
struct PersistentFenwick {
    vector<pair<int, ll>> fw[N]; // value, version
    int n;
    void init(int _n) {
        n = _n;
        for (int i = 1;i <= n;i++) fw[i].push_back({ 0, 0ll });
    }
    void update(int v, ll w, int ver) {
        for (int i = v;i <= n;i += i & -i) {
            fw[i].push_back({ ver, fw[i].back().second + w });
        }
    }
    ll query(int ver, int v) {
        ll ans = 0;
        for (int i = v;i > 0;i -= i & -i) {
            ans += (--upper_bound(fw[i].begin(), fw[i].end()
                , pair<int, ll>(ver, LLONG_MAX)))->second;
        }
        return ans;
    }
} fw, cnt;
ll a[N], qs[N];
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<ll> c;
    for (int i = 1;i <= n;i++) {
        cin >> a[i];
        qs[i] = qs[i - 1] + a[i];
        c.push_back(a[i]);
    }
    sort(c.begin(), c.end());
    c.resize(unique(c.begin(), c.end()) - c.begin());
    int m = c.size();
    fw.init(m), cnt.init(m);
    for (int i = 1;i <= n;i++) {
        int idx = upper_bound(c.begin(), c.end(), a[i]) - c.begin();
        fw.update(idx, a[i], i);
        cnt.update(idx, 1, i);
    }
    while (q--) {
        int l, r;
        ll x;
        cin >> l >> r >> x;
        int idx = lower_bound(c.begin(), c.end(), x) - c.begin() + 1;
        ll suml = fw.query(r, idx - 1) - fw.query(l - 1, idx - 1);
        ll cntl = cnt.query(r, idx - 1) - cnt.query(l - 1, idx - 1);
        ll sumr = (qs[r] - qs[l - 1]) - suml;
        ll cntr = (r - l + 1) - cntl;
        cout << (x * cntl - suml) + (sumr - x * cntr) << '\n';
    }
    return 0;
}
0