結果
| 問題 |
No.3078 Difference Sum Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-03-29 01:05:19 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 93 ms / 2,000 ms |
| コード長 | 1,071 bytes |
| コンパイル時間 | 2,714 ms |
| コンパイル使用メモリ | 215,792 KB |
| 実行使用メモリ | 11,136 KB |
| 最終ジャッジ日時 | 2025-03-29 01:05:27 |
| 合計ジャッジ時間 | 6,829 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 26 |
ソースコード
#include <atcoder/fenwicktree>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
void solve() {
ll n, q;
cin >> n >> q;
vector<ll> a(n);
rep(i, n) cin >> a[i];
vector<tuple<ll, ll, ll, ll>> xlrs(q);
rep(qi, q) {
ll l, r, x;
cin >> l >> r >> x;
xlrs[qi] = {x, l, r, qi};
}
sort(xlrs.begin(), xlrs.end());
vector<pair<ll, ll>> que(n);
rep(i, n) que[i] = {a[i], i};
sort(que.rbegin(), que.rend());
atcoder::fenwick_tree<ll> bit1(n), bit2(n);
rep(i, n) bit1.add(i, a[i]), bit2.add(i, -1);
vector<ll> ans(q);
for (const auto &[x, l, r, qi] : xlrs) {
while (!que.empty() && que.back().first < x) {
auto [v, i] = que.back();
que.pop_back();
bit1.add(i, -v * 2), bit2.add(i, 2);
}
ans[qi] = bit1.sum(l - 1, r) + x * bit2.sum(l - 1, r);
}
rep(qi, q) cout << ans[qi] << '\n';
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int T = 1;
for (int t = 0; t < T; t++) {
solve();
}
return 0;
}