#include <bits/stdc++.h>   using namespace std;   struct tree {     int l, r, c, p;   };   int n, K, m, r;   long long res, a[10000011], b[10000011];   bool cmp1(tree a, tree b) { return a.l < b.l; }   bool cmp2(tree a, tree b) { return a.r < b.r; }   void update(int x, int l, int r, int c, int p) {     if (l == r) {       a[x] += c, b[x] += 1ll * l * c;       return;     }     int mid = (l + r) >> 1;     if (p <= mid)       update(x << 1, l, mid, c, p);     else       update(x << 1 | 1, mid + 1, r, c, p);     a[x] = a[x << 1] + a[x << 1 | 1], b[x] = b[x << 1] + b[x << 1 | 1];     return;   }   long long query(int x, int l, int r, int p) {     if (l == r) {       if (p <= a[x]) return 1ll * l * p;       return b[x];     }     int mid = (l + r) >> 1;     if (a[x << 1] >= p) return query(x << 1, l, mid, p);     return query(x << 1 | 1, mid + 1, r, p - a[x << 1]) + b[x << 1];   }   int main() {     scanf( %d%d%d , &n, &K, &m);     tree a[m + 1], b[m + 1];     for (int i = 1; i <= m; i++)       scanf( %d%d%d%d , &a[i].l, &a[i].r, &a[i].c, &a[i].p), b[i] = a[i];     for (int i = 1; i <= m; i++) r = max(r, a[i].p);     sort(a + 1, a + m + 1, cmp1);     sort(b + 1, b + m + 1, cmp2);     for (int i = 1, j = 1, k = 1; i <= n; i++) {       while (j <= m && a[j].l == i) update(1, 1, r, a[j].c, a[j].p), j++;       res += query(1, 1, r, K);       while (k <= m && b[k].r == i) update(1, 1, r, -b[k].c, b[k].p), k++;     }     printf( %lld , res);     return 0;   }   