|
| 1 | +/** |
| 2 | + * @file 696681L.cpp |
| 3 | + * @author Macesuted (i@macesuted.moe) |
| 4 | + * @date 2026-06-06 |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2026 |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <bits/stdc++.h> |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +#ifndef LOCAL |
| 14 | +#define endl '\n' |
| 15 | +#endif |
| 16 | + |
| 17 | +bool mem1; |
| 18 | + |
| 19 | +#define maxn 200005 |
| 20 | + |
| 21 | +using pll = pair<int64_t, int64_t>; |
| 22 | + |
| 23 | +class SegmentTree { |
| 24 | + private: |
| 25 | + pll a[maxn << 2]; |
| 26 | + int n; |
| 27 | + |
| 28 | + pll merge(const pll& a, const pll& b) { return {a.first + b.first + a.second * b.second, a.second + b.second}; } |
| 29 | + void build(int p, int l, int r, int v[]) { |
| 30 | + if (l == r) return a[p] = {0, v[l]}, void(); |
| 31 | + int mid = (l + r) >> 1; |
| 32 | + build(p << 1, l, mid, v), build(p << 1 | 1, mid + 1, r, v); |
| 33 | + return a[p] = merge(a[p << 1], a[p << 1 | 1]), void(); |
| 34 | + } |
| 35 | + pll query(int p, int l, int r, int ql, int qr) { |
| 36 | + if (ql <= l && r <= qr) return a[p]; |
| 37 | + int mid = (l + r) >> 1; |
| 38 | + if (qr <= mid) return query(p << 1, l, mid, ql, qr); |
| 39 | + if (ql > mid) return query(p << 1 | 1, mid + 1, r, ql, qr); |
| 40 | + return merge(query(p << 1, l, mid, ql, qr), query(p << 1 | 1, mid + 1, r, ql, qr)); |
| 41 | + } |
| 42 | + |
| 43 | + public: |
| 44 | + void resize(int _n) { return n = _n, void(); } |
| 45 | + void build(int v[]) { return build(1, 1, n, v); } |
| 46 | + pll query(int l, int r) { return query(1, 1, n, l, r); } |
| 47 | +}; |
| 48 | + |
| 49 | +int v[maxn]; |
| 50 | + |
| 51 | +void solve(void) { |
| 52 | + int n, q; |
| 53 | + cin >> n >> q; |
| 54 | + for (int i = 1; i <= n; i++) cin >> v[i]; |
| 55 | + |
| 56 | + SegmentTree SGT; |
| 57 | + SGT.resize(n), SGT.build(v); |
| 58 | + |
| 59 | + while (q--) { |
| 60 | + int l, r, S; |
| 61 | + cin >> l >> r >> S; |
| 62 | + pll res = SGT.query(l, r); |
| 63 | + cout << (res.second > S ? -1 : res.first + res.second * (S - res.second)) << endl; |
| 64 | + } |
| 65 | + |
| 66 | + return; |
| 67 | +} |
| 68 | + |
| 69 | +bool mem2; |
| 70 | + |
| 71 | +int main() { |
| 72 | + ios::sync_with_stdio(false), cin.tie(nullptr); |
| 73 | +#ifdef LOCAL |
| 74 | + cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl; |
| 75 | +#endif |
| 76 | + |
| 77 | + int _ = 1; |
| 78 | + while (_--) solve(); |
| 79 | + |
| 80 | +#ifdef LOCAL |
| 81 | + cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl; |
| 82 | +#endif |
| 83 | + return 0; |
| 84 | +} |
0 commit comments