malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 5001;   bool dp1[N][N], dp2[N][N];   int main() {     {       ios_base::sync_with_stdio(false);       cin.tie(NULL);     }     string s;     memset(dp1, false, sizeof dp1);     memset(dp2, false, sizeof dp2);     cin >> s;     long long n = s.length();     for (long long i = 0; i < n; i++) {       long long curr = 0;       for (long long j = i; j < n; j++) {         if (s[j] ==  ) )           curr--;         else           curr++;         if (curr >= 0)           dp1[i][j] = true;         else           break;       }     }     for (long long i = n - 1; i >= 0; i--) {       long long curr = 0;       for (long long j = i; j >= 0; j--) {         if (s[j] ==  ( )           curr--;         else           curr++;         if (curr >= 0)           dp2[j][i] = true;         else           break;       }     }     long long ans = 0;     for (long long i = 0; i < n; i++) {       for (long long j = i; j < n; j++) {         if (dp1[i][j] && dp2[i][j] && (j - i + 1) % 2 == 0) ans++;       }     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   int gcd(int a, int b) {     if (b == 0) return a;     a %= b;     return gcd(b, a);   }   bool sortbysec(const pair<char, int> &a, const pair<char, int> &b) {     return (a.second < b.second);   }   bool checkParity(long long int a, long long int b) {     if (a % 2 == 0 && b % 2 == 0 || a % 2 == 1 && b % 2 == 1) return true;     return false;   }   bool isPrime(int n) {     if (n <= 1) return false;     for (int i = 2; i < n; i++)       if (n % i == 0) return false;     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cerr.tie(NULL);     ;     int n;     cin >> n;     string s;     cin >> s;     string p =   ;     for (long long int i = 0; i < s.size(); i++) p += s[i];     int count = 0;     for (long long int i = 0; i < s.size() - 1; i++) {       if (s[i] == s[i + 1]) {         if (s[i] ==  R ) {           if (i + 2 <= s.size() - 1) {             if (s[i + 2] ==  B ) {               count++;               s[i + 1] =  G ;             } else if (s[i + 2] ==  G ) {               count++;               s[i + 1] =  B ;             } else               s[i + 1] =  G ;           } else             s[i + 1] =  G ;         } else if (s[i] ==  G ) {           if (i + 2 <= s.size() - 1) {             if (s[i + 2] ==  B ) {               count++;               s[i + 1] =  R ;             } else if (s[i + 2] ==  R ) {               count++;               s[i + 1] =  B ;             } else               s[i + 1] =  R ;           } else             s[i + 1] =  R ;         } else {           if (i + 2 <= s.size() - 1) {             if (s[i + 2] ==  G ) {               count++;               s[i + 1] =  R ;             } else if (s[i + 2] ==  R ) {               count++;               s[i + 1] =  G ;             } else               s[i + 1] =  R ;           } else             s[i + 1] =  R ;         }       }     }     int l = 0;     for (long long int i = 0; i < n; i++)       if (p[i] != s[i]) l++;     cout << l << endl;     cout << s;   }
#include <bits/stdc++.h>   using namespace std;   int Max() { return -1000000007; }   int Min() { return 1000000007; }   template <typename... Args>   int Max(int a, Args... args) {     return max(a, Max(args...));   }   template <typename... Args>   int Min(int a, Args... args) {     return min(a, Min(args...));   }   long long a[7][7];   int main() {     for (int i = (1); i <= (5); ++i) {       for (int j = (1); j <= (5); ++j) {         scanf( %lld , &a[i][j]);       }     }     vector<long long> vc;     for (int i = (1); i <= (5); ++i) vc.push_back((long long)i);     long long ans = 0LL;     do       ans = max(ans, (a[vc[0]][vc[1]] + a[vc[1]][vc[0]]) +                          (a[vc[2]][vc[3]] + a[vc[3]][vc[2]]) +                          (a[vc[1]][vc[2]] + a[vc[2]][vc[1]]) +                          (a[vc[3]][vc[4]] + a[vc[4]][vc[3]]) +                          (a[vc[2]][vc[3]] + a[vc[3]][vc[2]]) +                          (a[vc[3]][vc[4]] + a[vc[4]][vc[3]]));     while (next_permutation(vc.begin(), vc.end()));     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> ans[1000];   int main() {     cin >> n;     int k = 2;     while ((k * (k + 1)) / 2 <= n) k++;     int cnt = 1;     for (int i = 1; i <= k; i++)       for (int j = 1; j < i; j++) {         ans[i].push_back(cnt);         ans[j].push_back(cnt);         cnt++;       }     cout << k << endl;     for (int i = 1; i <= k; i++) {       for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void openfile() {     freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);   }   const int nmax = 1 << 16;   int n, k;   int a[nmax];   vector<string> name;   map<string, int> num;   vector<vector<int> > g;   int main() {     cin >> n >> k;     g.resize(n);     name.resize(n);     for (int i = 0; i < n; i++) {       cin >> name[i];       num[name[i]] = i;     }     for (int i = 0; i < k; i++) {       string s1, s2;       cin >> s1 >> s2;       int v1 = num[s1], v2 = num[s2];       g[v1].push_back(v2);       g[v2].push_back(v1);     }     int ans, cans = 0;     for (int i = 0; i < nmax; i++) {       int cur = 0;       for (int j = 0; j < n; j++) {         if (i & (1 << j)) cur++;       }       for (int j = 0; j < n; j++) {         if (i & (1 << j)) {           for (int q = 0; q < ((int)((g[j]).size())); q++) {             if (i & (1 << g[j][q])) {               cur = 0;               break;             }           }         }       }       if (cur > cans) {         ans = i;         cans = cur;       }     }     cout << cans << endl;     set<string> b;     for (int i = 0; i < n; i++) {       if (ans & (1 << i)) {         b.insert(name[i]);       }     }     for (set<string>::iterator it = b.begin(); it != b.end(); it++) {       cout << *it << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     char s[400005];     int n, k;     char c =  a ;     int T[26];     memset(T, 0, sizeof(T));     cin >> n >> k;     for (int i = 0; i < n; i++) {       scanf(  %c , &s[i]);       T[s[i] -  a ]++;     }     int maxx;     for (int i = 0; i < 26; i++) {       if (k >= T[i])         k -= T[i];       else {         maxx = i, T[i] = k;         break;       }     }     for (int i = 0; i < n; i++) {       if (s[i] -  a  <= maxx && T[s[i] -  a ] > 0)         T[s[i] -  a ]--, s[i] =  * ;       else         printf( %c , s[i]);     }     printf(  n );   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int sum[1010][1010];   char mp[1010][1010];   int getsum(int x1, int y1, int x2, int y2) {     return sum[x1][y1] - sum[x2 + 1][y1] - sum[x1][y2 + 1] + sum[x2 + 1][y2 + 1];   }   bool check(int a, int b, int x, int y) {     while (1) {       if (x + a - 1 > n || y + b - 1 > m) return 0;       if (getsum(x, y, x + a - 1, y + b - 1) != a * b) return 0;       if (getsum(x + a, y, n, y) > 0 && getsum(x, y + b, x, m) > 0) return 0;       if (sum[x + a][y] == 0 && sum[x][y + b] == 0) break;       if (getsum(x + a, y, n, y) > 0) {         x++;         if (getsum(x + a - 1, y, x + a - 1, y + b - 1) > 0 &&             getsum(x + a - 1, y, x + a - 1, y + b - 1) != b)           return 0;       } else {         y++;         if (getsum(x, y + b - 1, x + a - 1, y + b - 1) > 0 &&             getsum(x, y + b - 1, x + a - 1, y + b - 1) != a)           return 0;       }     }     return 1;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %s , mp[i] + 1);     memset(sum, 0, sizeof(sum));     int x0, y0;     x0 = y0 = 10000;     for (int i = n; i >= 1; i--)       for (int j = m; j >= 1; j--) {         sum[i][j] =             sum[i + 1][j] + sum[i][j + 1] - sum[i + 1][j + 1] + (mp[i][j] ==  X );         if (mp[i][j] ==  X ) {           x0 = min(x0, i);           y0 = min(y0, j);         }       }     int ans = n * m + 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         if (i * j >= ans) continue;         if (check(i, j, x0, y0)) ans = i * j;       }     if (ans > n * m)       printf( -1 n );     else       printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int c = 1, flag = 0, flag1 = 0, flag2 = 0, flag3 = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  U ) {         if (flag1 == 1) {           c++;           flag2 = 0, flag3 = 0, flag1 = 0;         }         flag = 1;       }       if (s[i] ==  D ) {         if (flag == 1) {           c++;           flag2 = 0, flag3 = 0, flag = 0;         }         flag1 = 1;       }       if (s[i] ==  L ) {         if (flag2 == 1) {           flag = 0, flag1 = 0, flag2 = 0;           c++;         }         flag3 = 1;       }       if (s[i] ==  R ) {         if (flag3 == 1) {           c++;           flag1 = 0, flag = 0, flag3 = 0;         }         flag2 = 1;       }     }     cout << c;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class __Ty>   bool minimize(__Ty& a, __Ty b) {     if (a > b) {       a = b;       return true;     }     return false;   }   template <class __Ty>   bool maximize(__Ty& a, __Ty b) {     if (a < b) {       a = b;       return true;     }     return false;   }   const int N = 2003;   const int mod = 1e9 + 7;   int f[N][N] = {0};   int n, k;   vector<int> g[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= i; ++j)         if (i % j == 0) g[i].push_back(j);     }     for (int j = 1; j <= n; ++j) f[1][j] = 1;     for (int i = 2; i <= k; ++i)       for (int j = 1; j <= n; ++j) {         for (int v : g[j]) {           f[i][j] = (f[i][j] + f[i - 1][v]) % mod;         }       }     int ans = 0;     for (int i = 1; i <= n; ++i) ans = (ans + f[k][i]) % mod;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   struct Matrix {     int n;     vector<vector<long long>> a;     Matrix(int _n) {       n = _n;       a = vector<vector<long long>>(n, vector<long long>(n, 0));     }     Matrix operator+(Matrix o) {       Matrix ans(n);       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) ans[i][j] = a[i][j] + o[i][j];       ans.modulate();       return ans;     }     Matrix operator*(Matrix o) {       Matrix ans(n);       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++)           for (int k = 0; k < n; k++) ans[i][j] += a[i][k] * o[k][j];       ans.modulate();       return ans;     }     vector<long long>& operator[](int index) { return a[index]; }     void modulate() {       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) a[i][j] %= MOD;     }     void eye() {       a = vector<vector<long long>>(n, vector<long long>(n, 0));       for (int i = 0; i < n; i++) a[i][i] = 1;     }   };   Matrix mod_pow(Matrix a, long long e) {     Matrix ans(2);     ans.eye();     while (e) {       if (e & 1) ans = ans * a;       a = a * a;       e >>= 1;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     long long A, B, n, x;     cin >> A >> B >> n >> x;     Matrix ans(2);     ans[0][0] = A;     ans[0][1] = B;     ans[1][1] = 1;     ans = mod_pow(ans, n);     cout << (ans[0][0] * x + ans[0][1]) % MOD <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[1000010];   int main() {     char str;     int i, n, x, maxn = 0, num = 0;     cin >> n;     while (n--) {       cin >> str >> x;       if (str ==  + ) {         num++;         a[x] = 1;         maxn = max(maxn, num);       } else {         if (a[x]) {           num--;           a[x] = 0;         } else {           maxn++;         }       }     }     cout << maxn << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void chkmin(T &a, T b) {     if (a > b) a = b;   }   template <class T>   inline void chkmax(T &a, T b) {     if (a < b) a = b;   }   int n;   double q0, a0, b0;   double y_best[6000], x[6000], chain[6000];   double quad_min(double a, double b, double c) { return -b / (2.0 * a); }   double bound(double h, double v) { return max(1.0 + h, min(q0, v)); }   void pull_chain(int j0, double h, double a, double b, double c,                   double &res_yopt, int &res_j) {     for (int j = j0; j >= 0; j--) {       double yi_nat = bound(h, quad_min(a, b, c));       if (y_best[j] < yi_nat - h - b0) {         double ny = x[j] + h + b0;         chain[j] = b0;         h += b0;         a += 1.0;         b += -2.0 * ny;         c += ny * ny;       } else if (y_best[j] > yi_nat - h - a0) {         double ny = x[j] + h + a0;         chain[j] = a0;         h += a0;         a += 1.0;         b += -2.0 * ny;         c += ny * ny;       } else {         res_yopt = yi_nat;         res_j = j;         return;       }     }     res_yopt = bound(h, quad_min(a, b, c));     res_j = -1;   }   void fill_in_y(int i) {     while (i >= 0) {       double anything;       int j;       pull_chain(i - 1, 0.0, 1.0, -2.0 * x[i], x[i] * x[i], anything, j);       for (int k = i - 1; k > j; k--) {         y_best[k] = y_best[k + 1] - chain[k];       }       i = j;     }   }   int main() {     scanf( %d%lf%lf%lf , &n, &q0, &a0, &b0);     for (int i = 0; i < n; i++) scanf( %lf , &x[i]);     for (int i = 0; i < n; i++) y_best[i] = x[i];     for (int i = 0; i < n; i++) chain[i] = 0.0;     for (int i = 1; i < n; i++) {       double yopt;       int j;       pull_chain(i - 1, 0.0, 1.0, -2.0 * x[i], x[i] * x[i], yopt, j);       y_best[i] = yopt;     }     fill_in_y(n - 1);     double total = 0.0;     for (int i = 0; i < n; i++) {       total = total + (y_best[i] - x[i]) * (y_best[i] - x[i]);     }     for (int i = 0; i < n; i++) {       printf( %.8f  , y_best[i]);     }     printf(  n%.8lf n , total);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long f(long long n) {     if (n < 10) return n;     long long last_digit = n % 10;     long long ret = n / 10 + 9;     long long tmp = n;     while (tmp >= 10) tmp = tmp / 10;     if (tmp > last_digit) ret--;     return ret;   }   int main() {     long long i, j, k, n, m, d, a, b;     while (cin >> a >> b) {       cout << f(b) - f(a - 1) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   char s[100005];   long long dp[100005], bit[100005], ans[100005], bit2[100005], gbit2[100005],       B[100005], GB[100005], gbit[100005], T, len, i, j, A, o;   struct node {     int x, y, z;   } a[100005];   int cmp(node i, node j) { return i.y < j.y; }   int quick(int a, int b) {     int ans = 1;     while (b) {       if (b & 1) ans = 1ll * ans * a % MOD;       a = 1ll * a * a % MOD;       b >>= 1;     }     return ans;   }   long long C(int x, int y) { return B[x] * GB[x - y] % MOD * GB[y] % MOD; }   int main() {     bit[0] = 1;     B[0] = 1;     for (i = 1; i <= 100001; i++)       bit[i] = bit[i - 1] * 25 % MOD, B[i] = B[i - 1] * i % MOD;     for (i = 0; i <= 100001; i++)       gbit[i] = quick(bit[i], MOD - 2), GB[i] = quick(B[i], MOD - 2);     bit2[0] = 1;     for (i = 1; i <= 100001; i++) bit2[i] = bit2[i - 1] * 26 % MOD;     for (i = 0; i <= 100001; i++) gbit2[i] = quick(bit2[i], MOD - 2);     scanf( %d , &T);     scanf( %s , s);     len = strlen(s);     for (i = 1; i <= T; i++) {       scanf( %d , &A);       if (A == 1) {         scanf( %s , s);         len = strlen(s);       } else {         scanf( %d , &A);         o++;         a[o].x = A;         a[o].y = len;         a[o].z = o;       }     }     sort(a + 1, a + o + 1, cmp);     dp[0] = 0;     for (i = 1; i <= o; i++) {       if (a[i].y != a[i - 1].y) {         for (j = 1; j < a[i].y; j++) dp[j] = 0;         for (j = a[i].y; j <= 100000; j++)           dp[j] = C(j - 1, a[i].y - 1) * bit[j - a[i].y] % MOD;         for (j = 1; j <= 100000; j++)           dp[j] = (dp[j - 1] + dp[j] * bit2[100000 - j]) % MOD;       }       ans[a[i].z] = dp[a[i].x] * gbit2[100000 - a[i].x] % MOD;     }     for (i = 1; i <= o; i++) printf( %d n , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 400010, inf = 1e18;   long long q, lastans, cnt = 1, w[maxn];   long long fa[maxn][21], sum[maxn][21];   void add(long long x, long long v) {     w[++cnt] = v;     if (w[cnt] <= w[x])       fa[cnt][0] = x;     else {       int y = x;       for (int i = 20; i >= 0; i--) {         if (w[fa[y][i]] < w[cnt]) y = fa[y][i];       }       fa[cnt][0] = fa[y][0];     }     if (fa[cnt][0] == 0)       sum[cnt][0] = inf;     else       sum[cnt][0] = w[fa[cnt][0]];     for (int i = 1; i <= 20; i++) {       fa[cnt][i] = fa[fa[cnt][i - 1]][i - 1];       if (fa[cnt][i] == 0)         sum[cnt][i] = inf;       else         sum[cnt][i] = sum[cnt][i - 1] + sum[fa[cnt][i - 1]][i - 1];     }   }   long long query(long long x, long long v) {     if (w[x] > v) return 0;     v -= w[x];     long long res = 1;     for (int i = 20; i >= 0; i--) {       if (v >= sum[x][i]) {         v -= sum[x][i];         res += (1 << i);         x = fa[x][i];       }     }     return res;   }   int main() {     w[0] = inf;     for (int i = 0; i <= 20; i++) sum[1][i] = inf;     scanf( %lld , &q);     while (q--) {       int id;       long long a, b;       scanf( %d , &id);       scanf( %lld%lld , &a, &b);       a ^= lastans;       b ^= lastans;       if (id == 1)         add(a, b);       else         printf( %lld n , lastans = query(a, b));     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<bool> sub(n, false);     int c = 0;     for (int i = 0; i < n; i++) {       int t;       cin >> t;       if (t < 0) {         sub[i] = true;         c++;       }     }     if (c > k) {       cout <<  -1 n ;       return 0;     }     bool f = c;     k -= c;     vector<int> r(n + 1, 0);     c = 0;     int p = -1;     bool s = true;     int last = -1;     for (int i = 0; i < n; i++) {       if (sub[i]) {         if (s) {           c++;           s = false;           if (p != -1) {             r[i - p]++;           }         }       } else {         if (!s) {           p = i;           s = true;           c++;         }       }     }     if (f && !sub[n - 1]) last = n - p;     for (int i = 1; i < n + 1; i++) {       while (k - i >= 0 && r[i]) {         c -= 2;         k -= i;         r[i] -= 1;       }       if (k - i < 0) {         if (last != -1 && k - last >= 0) {           c--;           last = -1;         }         break;       }     }     if (last != -1 && k - last >= 0) c--;     cout << c <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d %d , &n, &m);     char s[n + 5];     scanf( %s , s);     vector<int>* vp[30];     for (int i = 0; i <= 25; i++) vp[i] = new vector<int>();     for (int i = 0; s[i]; i++) {       int now = s[i] -  a ;       vp[now]->push_back(i);     }     while (m--) {       getchar();       char x, y;       scanf( %c %c , &x, &y);       int id1 = x -  a ;       int id2 = y -  a ;       swap(vp[id1], vp[id2]);     }     for (int i = 0; i <= 25; i++) {       for (vector<int>::iterator it = vp[i]->begin(); it != vp[i]->end(); it++) {         s[*it] = i +  a ;       }     }     printf( %s n , s);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   constexpr long long INF = (sizeof(long long) == 4 ? 1e9 : 2e18) + 1e5;   constexpr long long MOD = 1e9 + 7;   constexpr long long MAXN = 2e5 + 3;   void solve() {     long long n;     cin >> n;     string s;     cin >> s;     long long w = 0;     queue<long long> arr;     for (long long i = 0; i < n; ++i) {       if (i < n && s[i] == s[i + 1]) {         arr.emplace(i + 1);       }     }     long long ans = 0;     long long l = 0;     while (l < n) {       ++ans;       while (arr.size() && arr.front() < l) arr.pop();       if (arr.size() && arr.front() >= l) {         arr.pop();       } else {         while (l + 1 < n && s[l] == s[l + 1]) ++l;         ++l;       }       while (l + 1 < n && s[l] == s[l + 1]) ++l;       ++l;     }     cout << ans <<   n ;   }   signed main() {     const string FILE_NAME =   ;     ios_base::sync_with_stdio(false);     cin.tie(nullptr), cout.tie(nullptr);     long long t = 1;     cin >> t;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   struct info {     int d, f, t, cst;   } inp[100006];   int forw[1000005];   int forw2[1000005];   long long cforw[1000005];   long long cforw2[1000005];   int ok[1000006];   int ok2[1000006];   bool cmp(info a, info b) { return a.d < b.d; }   void process1(int prev, int day) {     while (prev < day) {       cforw[prev + 1] = cforw[prev];       forw[prev + 1] = forw[prev];       prev++;     }   }   void process2(int prev, int day) {     while (prev > day) {       cforw2[prev - 1] = cforw2[prev];       forw2[prev - 1] = forw2[prev];       prev--;     }   }   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     for (int i = 1; i <= m; i++) {       scanf( %d %d %d %d , &inp[i].d, &inp[i].f, &inp[i].t, &inp[i].cst);     }     sort(inp + 1, inp + m + 1, cmp);     int prev = 0;     for (int i = 1; i <= m; i++) {       int day = inp[i].d;       process1(prev, day);       prev = day;       if (inp[i].f != 0) {         int pos = inp[i].f;         if (ok[pos] == 0) {           ok[pos] = inp[i].cst;           cforw[day] += inp[i].cst;           forw[day]++;         } else if (ok[pos] > inp[i].cst) {           cforw[day] += (-ok[pos] + inp[i].cst);           ok[pos] = inp[i].cst;         }       }     }     process1(prev, 1000000);     prev = n + 1;     for (int i = m; i >= 1; i--) {       if (inp[i].f != 0) continue;       int pos = inp[i].t;       int day = inp[i].d;       process2(prev, day);       prev = day;       if (ok2[pos] == 0) {         ok2[pos] = inp[i].cst;         cforw2[day] += inp[i].cst;         forw2[day]++;       } else if (ok2[pos] > inp[i].cst) {         cforw2[day] += (-ok2[pos] + inp[i].cst);         ok2[pos] = inp[i].cst;       }     }     process2(prev, 1);     long long ans = -1;     for (int i = 1; i <= 1000000 - k - 1; i++) {       int t = i + k + 1;       if (forw[i] != n || (forw2[t] != n)) continue;       long long tot = cforw[i] + cforw2[t];       if (ans == -1)         ans = tot;       else         ans = min(ans, tot);     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000005;   struct tr {     int l, r, c, p;   } a[maxn], b[maxn];   int n, k, m, maxr = 0;   long long ans = 0, num[maxn << 2], sum[maxn << 2];   bool cmpl(tr a, tr b) { return a.l < b.l; }   bool cmpr(tr a, tr b) { return a.r < b.r; }   void update(int o, int l, int r, int c, int p) {     if (l == r) {       num[o] += c;       sum[o] += 1LL * c * l;       return;     }     int mid = (l + r) >> 1;     if (p <= mid)       update(o << 1, l, mid, c, p);     else       update(o << 1 | 1, mid + 1, r, c, p);     num[o] = num[o << 1] + num[o << 1 | 1];     sum[o] = sum[o << 1] + sum[o << 1 | 1];   }   long long query(int o, int l, int r, int p) {     if (l == r) {       if (p <= num[o]) return 1LL * l * p;       return sum[o];     }     int mid = (l + r) >> 1;     if (num[o << 1] >= p) return query(o << 1, l, mid, p);     return query(o << 1 | 1, mid + 1, r, p - num[o << 1]) + sum[o << 1];   }   int main() {     scanf( %d%d%d , &n, &k, &m);     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++) maxr = max(maxr, a[i].p);     sort(a + 1, a + m + 1, cmpl);     sort(b + 1, b + m + 1, cmpr);     for (int i = 1, j = 1, o = 1; i <= n; i++) {       while (j <= m && a[j].l == i) update(1, 1, maxr, a[j].c, a[j].p), j++;       ans += query(1, 1, maxr, k);       while (o <= m && b[o].r == i) update(1, 1, maxr, -b[o].c, b[o].p), o++;     }     printf( %lld , ans);     return 0;   }