1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include <bits/stdc++.h>
using namespace std; using ll = long long; using pii = pair<int, int>;
const int MAXN = 2e5 + 10, MAXV = 1e6 + 10;
int n, k, m, to[MAXN]; vector<pii> edit[MAXV];
struct Query { int l, r, c, p; };
struct Lsh { vector<int> v; void add(int x) { v.push_back(x); } void build() { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int rnk(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; } int len() { return v.size(); } } L;
struct Node { ll sum, cnt, p; };
struct SegTree { Node dat[MAXN << 2], E = {0}; Node comb(const Node &dat1, const Node &dat2) { return {dat1.sum + dat2.sum, dat1.cnt + dat2.cnt}; } void build(int root, int l, int r) { if (l == r) { dat[root] = {0, 0, to[l]}; return; } int mid = l + r >> 1; build(root << 1, l, mid); build(root << 1 | 1, mid + 1, r); dat[root] = comb(dat[root << 1], dat[root << 1 | 1]); } void modify(int root, int l, int r, int pos, int val) { if (l == r) { dat[root].cnt += val; dat[root].sum += val * dat[root].p; return; } int mid = l + r >> 1; if (pos <= mid) { modify(root << 1, l, mid, pos, val); } else { modify(root << 1 | 1, mid + 1, r, pos, val); } dat[root] = comb(dat[root << 1], dat[root << 1 | 1]); } ll query(int root, int l, int r, int k) { if (l == r) return {min(k * dat[root].p, dat[root].sum)}; int mid = l + r >> 1; if (dat[root << 1].cnt >= k) { return query(root << 1, l, mid, k); } else { return dat[root << 1].sum + query(root << 1 | 1, mid + 1, r, k - dat[root << 1].cnt); } } } T;
vector<Query> Q;
int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> k >> m; for (int i = 1, l, r, c, p; i <= m; i++) { cin >> l >> r >> c >> p; Q.push_back({l, r, c, p}); L.add(p); } L.build(); int t = L.len(); for (Query &i : Q) { to[L.rnk(i.p)] = i.p, i.p = L.rnk(i.p); edit[i.l].push_back({i.c, i.p}); edit[i.r + 1].push_back({-i.c, i.p}); } T.build(1, 1, t); ll ans = 0; for (int i = 1; i <= n; i++) { for (pii &j : edit[i]) { T.modify(1, 1, t, j.second, j.first); } ans += T.query(1, 1, t, k); } cout << ans; return 0; }
|