func_code_string: #include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int MOD = (int)998244353;   const int MAXN = (int)1e6 + 5;   template <typename T>   class BIT {    public:     BIT(T* _a, int _n) {       a = _a;       n = _n;       bit = new T[n + 1];       for (int i = 1; i <= n; ++i) bit[i] = 0;       for (int i = 1; i <= n; ++i) upd_(i, a[i]);     }     void upd(int i, T val) {       T diff = val - a[i];       a[i] = val;       while (i <= n) {         bit[i] = bit[i] + diff;         i += i & (-i);       }     }     T get(int i) {       T ret = 0;       while (i > 0) {         ret = ret + bit[i];         i -= i & (-i);       }       return ret;     }       private:     void upd_(int i, T val) {       while (i <= n) {         bit[i] = bit[i] + val;         i += i & (-i);       }     }     T *bit, *a;     int n;   };   ll n, k, m, a1[MAXN], a2[MAXN], a3[MAXN];   struct Segment {     Segment(int _l, int _r, int _c, int _p) : l(_l), r(_r), c(_c), p(_p) {       end = 0;     };     int l, r, c, p;     bool end;     int pos;     int get_coord() {       if (end) return r;       return l;     }   };   bool cmp1(Segment& lhs, Segment& rhs) { return lhs.p < rhs.p; }   bool cmp2(Segment& lhs, Segment& rhs) {     int lhsx = (lhs.end == 1) ? lhs.r : lhs.l;     int rhsx = (rhs.end == 1) ? rhs.r : rhs.l;     if (lhsx == rhsx) return lhs.end < rhs.end;     return lhsx < rhsx;   }   vector<Segment> segs;   bool can(BIT<ll>& bit1, BIT<ll>& bit2, int pos) { return (bit1.get(pos) >= k); }   int main() {     cin >> n >> k >> m;     for (int i = 0; i < m; ++i) {       int l, r, c, p;       scanf( %d%d%d%d , &l, &r, &c, &p);       Segment seg(l, r, c, p);       segs.push_back(seg);     }     sort(segs.begin(), segs.end(), cmp1);     int sz = segs.size();     for (int i = 1; i <= sz; ++i) {       segs[i - 1].pos = i;       a3[i] = segs[i - 1].p;       Segment seg = segs[i - 1];       seg.end = 1;       segs.push_back(seg);     }     sort(segs.begin(), segs.end(), cmp2);     ll sol = 0;     BIT<ll> bit1(a1, m), bit2(a2, m);     int segpos = 0;     for (int i = 1; i <= n; ++i) {       while (segpos < 2 * m &&              (segs[segpos].get_coord() < i ||               (segs[segpos].get_coord() == i && segs[segpos].end == 0))) {         int pos = segs[segpos].pos;         if (segs[segpos].end == 0) {           bit1.upd(pos, segs[segpos].c);           bit2.upd(pos, 1LL * segs[segpos].c * segs[segpos].p);         } else {           bit1.upd(pos, 0);           bit2.upd(pos, 0);         }         segpos++;       }       ll solc = m;       int l = 1, r = m;       while (l <= r) {         int mid = (l + r) / 2;         if (can(bit1, bit2, mid)) {           solc = min(solc, (ll)mid);           r = mid - 1;         } else           l = mid + 1;       }       if (bit1.get(m) < k)         sol += bit2.get(m);       else {         ll diff = bit1.get(solc) - k;         sol += bit2.get(solc) - diff * a3[solc];       }     }     cout << sol;     return 0;   }   