結果
問題 |
No.3078 Difference Sum Query
|
ユーザー |
![]() |
提出日時 | 2025-04-02 09:06:20 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 226 ms / 2,000 ms |
コード長 | 1,696 bytes |
コンパイル時間 | 2,431 ms |
コンパイル使用メモリ | 208,248 KB |
実行使用メモリ | 11,068 KB |
最終ジャッジ日時 | 2025-04-02 09:06:32 |
合計ジャッジ時間 | 10,977 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Query { int L, R, idx; ll x; }; struct BIT { int n; vector<ll> bit; BIT(int n_) : n(n_), bit(n_+1, 0) {} void upd(int i, ll v) { for(; i<=n; i += i&-i) bit[i] += v; } ll qry(int i) { ll s = 0; for(; i; i-= i&-i) s += bit[i]; return s; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; vector<ll> a(n+1); for(int i=1; i<=n; i++) cin >> a[i]; vector<ll> pre(n+1, 0); for(int i=1; i<=n; i++) pre[i] = pre[i-1] + a[i]; vector<Query> qs(q); for(int i=0; i<q; i++){ int L, R; ll x; cin >> L >> R >> x; qs[i] = {L, R, i, x}; } sort(qs.begin(), qs.end(), [](const Query &p, const Query &q){ return p.x < q.x; }); vector<pair<ll,int>> arr; arr.reserve(n); for(int i=1; i<=n; i++) arr.push_back({a[i], i}); sort(arr.begin(), arr.end(), [](auto &p, auto &q){ return p.first < q.first; }); BIT bitc(n), bits(n); vector<ll> ans(q, 0); int pos = 0; for(auto &que : qs) { while(pos < n && arr[pos].first < que.x) { int idx = arr[pos].second; bitc.upd(idx, 1); bits.upd(idx, arr[pos].first); pos++; } ll cnt = bitc.qry(que.R) - bitc.qry(que.L-1); ll sum = bits.qry(que.R) - bits.qry(que.L-1); int len = que.R - que.L + 1; ll tot = pre[que.R] - pre[que.L-1]; ans[que.idx] = (que.x * cnt - sum) + ((tot - sum) - que.x * (len - cnt)); } for(auto a : ans) cout << a << endl; return 0; }