malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   const int MaxN = 300000;   using namespace std;   long long arr[MaxN + 5], a[MaxN + 5], n;   struct Segment {     int l, r, lx, mx, rx;     int Sign(long long x) { return x > 0 ? 1 : -1; }     void Update(Segment A, Segment B) {       lx = A.lx, rx = B.rx;       mx = max(A.mx, B.mx);       if (a[A.r] && a[B.l] && Sign(a[A.r]) >= Sign(a[B.l])) {         mx = max(mx, A.rx + B.lx);         if (A.mx == A.r - A.l + 1) lx = A.mx + B.lx;         if (B.mx == B.r - B.l + 1) rx = B.mx + A.rx;       }       mx = max(mx, max(lx, rx));       l = A.l, r = B.r;     }   } seg[MaxN * 4 + 5];   void Update(int l, int r, int t, int x, int w) {     if (l == r) {       a[x] += w;       if (a[x] != 0)         seg[t] = (Segment){l, r, 1, 1, 1};       else         seg[t] = (Segment){l, r, 0, 0, 0};       return;     }     int mid = (l + r) >> 1;     if (x <= mid)       Update(l, mid, t * 2, x, w);     else       Update(mid + 1, r, t * 2 + 1, x, w);     seg[t].Update(seg[t * 2], seg[t * 2 + 1]);   }   int main() {     while (~scanf( %d , &n)) {       for (int i = 1; i <= n; i++) scanf( %d , &arr[i]);       for (int i = 1; i <= n; i++) a[i] = arr[i + 1] - arr[i];       for (int i = 1; i < n; i++) Update(1, n - 1, 1, i, 0);       int m;       scanf( %d , &m);       for (int i = 1; i <= m; i++) {         int l, r, w;         scanf( %d%d%d , &l, &r, &w);         if (l - 1 >= 1) Update(1, n - 1, 1, l - 1, w);         if (r <= n - 1) Update(1, n - 1, 1, r, -w);         printf( %d n , seg[1].mx + 1);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2.5e5 + 10;   template <typename T>   inline T read() {     T ans = 0, flag = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) flag = -1;       c = getchar();     }     while (isdigit(c)) {       ans = ans * 10 + (c - 48);       c = getchar();     }     return ans * flag;   }   int n, k, p;   long long power_pow(long long base, int b, const long long MOD = p) {     long long ans = 1;     while (b) {       if (b & 1) (ans *= base) %= MOD;       (base *= base) %= MOD;       b >>= 1;     }     return ans;   }   int fac[maxn], Inv[maxn];   void init() {     const int MOD = p;     fac[0] = 1;     for (register int i(1), end_i(n); i <= end_i; i++)       fac[i] = 1ll * i * fac[i - 1] % MOD;     Inv[n] = power_pow(fac[n], MOD - 2);     for (register int i(n - 1), end_i(0); i >= end_i; i--)       Inv[i] = (i + 1ll) * Inv[i + 1] % MOD;   }   int f[26][maxn];   int dp(int x, int S) {     if (~f[x][S]) return f[x][S];     const int MOD = p;     if (!S) {       long long res = fac[n];       for (register int i(1), end_i(x); i <= end_i; i++)         res = res * (k - i + 1) % MOD;       return res;     }     long long res = 0;     int U = S & (S - 1);     for (int U = S & (S - 1), T = U;; T = (T - 1) & U) {       res = (res + 1ll * Inv[S - T] * dp(x + 1, T)) % MOD;       if (!T) break;     }     return f[x][S] = res;   }   int main() {     cin >> n >> k >> p;     long long ans = power_pow(k, n);     const int MOD = p;     if (n % 2 == 0) {       init();       memset(f, -1, sizeof f);       ans = (ans + MOD - dp(0, n)) % MOD;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     int aa[2010];     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) scanf( %d , &aa[i]);     sort(aa, aa + n);     int p = n - 1;     int ans = 0;     while (p >= 0) {       int temp = aa[p];       ans += (temp - 1) * 2;       p -= k;     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   int d[1000];   void Qsort(int, int);   bool Checked(int, int);   int main(int argc, char** argv) {     char a[10000];     std ::cin >> a;     int id = 0;     int k = 0;     while (a[k] !=   0 ) {       int num = 0;       while ((a[k] !=   0 ) && (a[k] !=  , )) {         num = (num * 10) + (a[k] -  0 );         k++;       }       k++;       if (Checked(num, id) == true) d[id++] = num;     }     bool flag = false;     Qsort(0, id - 1);     int cnt = 0;     int index = 0;     for (int i = 1; i <= id; i++) {       if (d[i - 1] + 1 == d[i]) {         if (cnt == 0) index = i - 1;         cnt++;       } else {         if (cnt != 0) {           if (flag == true) {             std ::cout <<  ,  << d[index] <<  -  << d[index + cnt];           } else {             std ::cout << d[index] <<  -  << d[index + cnt];             flag = true;           }           cnt = 0;         } else {           if (flag == true) {             std ::cout <<  ,  << d[i - 1];           } else {             std ::cout << d[i - 1];             flag = true;           }           cnt = 0;         }       }     }     if (flag == true) {       if (cnt != 0) std ::cout <<  ,  << d[index] <<  -  << d[index + cnt];     } else {       if (cnt != 0) std ::cout << d[index] <<  -  << d[index + cnt];     }     return 0;   }   void Qsort(int l, int r) {     if (l > r) return;     int i = l;     int j = r;     int m = d[(l + r) / 2];     while (i <= j) {       while (d[i] < m) i++;       while (d[j] > m) j--;       if (i <= j) {         int temp = d[i];         d[i] = d[j];         d[j] = temp;         i++;         j--;       }     }     Qsort(l, j);     Qsort(i, r);   }   bool Checked(int key, int n) {     for (int i = 0; i < n; i++) {       if (d[i] == key) return false;     }     return true;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10;   int n, vis[N];   int Log2[N], a[N], st[N][20], lim[N];   void pre() {     Log2[1] = 0, st[1][0] = a[1];     for (int i = 2; i <= n; ++i) {       Log2[i] = Log2[i - 1];       if (i == 1 << Log2[i - 1] + 1) Log2[i]++;       st[i][0] = a[i];     }     for (int i = n; i >= 1; --i)       for (int j = 1; i + (1 << j) - 1 <= n; ++j) {         st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);       }   }   int ask(int l, int r) {     int x = Log2[r - l + 1];     return max(st[l][x], st[r - (1 << x) + 1][x]);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     pre();     lim[n + 1] = n;     for (int i = n; i >= 1; i--) {       int x = a[i];       if (vis[x])         lim[i] = min(lim[i + 1], vis[x] - 1);       else         lim[i] = lim[i + 1];       vis[x] = i;     }     long long res = 0;     for (int i = 1; i <= n; i++) {       int j = i;       while (j <= lim[i]) {         int ma = ask(i, j);         if (ma == (j - i + 1))           res++, j++;         else           j = i + ma - 1;       }     }     printf( %lld , res);   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_SIZE = 1e6 + 10, INF = 2 * 1e9, MAX_N = 7e3 + 5, MAX_K = 3e4 + 5;   const long long SuperINF = 1e18;   const double eps = 0.5, PI = 20 / 7;   void files() {     freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);   }   int tests;   int main() {     std::ios::sync_with_stdio(0);     cin >> tests;     for (int tt = 0; tt < tests; tt++) {       int n, k;       cin >> n >> k;       string s;       cin >> s;       set<int> open, close;       for (int i = 0; i < s.size(); i++) {         if (s[i] ==  ( )           open.insert(i);         else           close.insert(i);       }       vector<pair<int, int> > indexes;       bool need_open = false;       int cnt = 2 * (k - 1);       int ost = (n - 2 * (k - 1)) / 2;       int now_ost = 0;       bool flag = false;       for (int i = 0; i < s.size(); i++) {         if (cnt > 0)           need_open ^= true;         else {           flag = true;           if (ost > 0) {             need_open = true;           } else             break;         }         if (need_open) {           if (s[i] !=  ( ) {             if (open.size() == 0) return 1;             int f = *open.begin();             close.insert(f);             close.erase(i);             if (open.find(f) == open.end()) return 1;             open.erase(f);             indexes.push_back(make_pair(i + 1, f + 1));             swap(s[i], s[f]);           } else {             if (open.find(i) == open.end()) return 1;             open.erase(i);           }         } else {           if (s[i] !=  ) ) {             if (close.size() == 0) return 1;             int f = *close.begin();             open.insert(f);             open.erase(i);             if (close.find(f) == close.end()) return 1;             close.erase(f);             indexes.push_back(make_pair(i + 1, f + 1));             swap(s[i], s[f]);           } else {             if (close.find(i) == close.end()) return 1;             close.erase(i);           }         }         cnt--;         if (flag) ost--;       }       cout << indexes.size() <<   n ;       for (auto x : indexes) cout << x.first <<     << x.second <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long n, k, d1, d2;       cin >> n >> k >> d1 >> d2;       if (n % 3 != 0) {         cout <<  no  << endl;         continue;       }       long long w1, w2, w3;       bool flag = true;       if (k - 2 * d1 - d2 < 0 || (k - 2 * d1 - d2) % 3 != 0) flag = false;       if (flag) {         w1 = (k - 2 * d1 - d2) / 3;         w2 = d1 + w1;         w3 = d1 + d2 + w1;         if (w1 > n / 3 || w2 > n / 3 || w3 > n / 3) flag = false;       }       if (!flag) {         flag = true;         if (k + 2 * d2 - d1 < 0 || (k + 2 * d2 - d1) % 3 != 0) flag = false;         if (flag) {           w3 = (k + 2 * d2 - d1) / 3;           w1 = w3 + d1 - d2;           w2 = w3 - d2;           if (w1 > n / 3 || w2 > n / 3 || w3 > n / 3 || w1 < 0 || w2 < 0)             flag = false;         }       }       if (!flag) {         flag = true;         if (k + d2 - 2 * d1 < 0 || (k + d2 - 2 * d1) % 3 != 0) flag = false;         if (flag) {           w1 = (k + d2 - 2 * d1) / 3;           w2 = d1 + w1;           w3 = d1 - d2 + w1;           if (w1 > n / 3 || w2 > n / 3 || w3 > n / 3 || w3 < 0) flag = false;         }       }       if (!flag) {         flag = true;         if (k - 2 * d2 - d1 < 0 || (k - 2 * d2 - d1) % 3 != 0) flag = false;         if (flag) {           w3 = (k - 2 * d2 - d1) / 3;           w1 = w3 + d1 + d2;           w2 = d2 + w3;           if (w3 > n / 3 || w2 > n / 3 || w1 > n / 3) flag = false;         }       }       if (flag)         cout <<  yes  << endl;       else         cout <<  no  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int q;   bool read() {     if (scanf( %d , &q) < 1) {       return 0;     }     return 1;   }   int get_dig(char c) {     if ( 0  <= c && c <=  9 ) {       return c -  0 ;     }     if ( a  <= c && c <=  f ) {       return c -  a  + 10;     }     return -1;   }   unsigned long long read_num() {     string res;     char c;     while (1) {       c = getchar();       int d = get_dig(c);       if (d == -1) {         if (!res.empty()) {           break;         }         continue;       }       res += c;     }     unsigned long long coef = 1;     unsigned long long ans = 0;     for (int i = int(((int)(res).size())) - 1; i >= 0; --i) {       int d = get_dig(res[i]);       ans += coef * d;       coef *= 16;     }     return ans;   }   bool bit(int x, int i) { return (x >> i) & 1; }   unsigned long long dp[20][2][2][2];   unsigned long long solve(unsigned long long r) {     if (!r) {       return 0;     }     unsigned long long old = r;     vector<int> R;     for (int it = 0; it < int(16); ++it) {       R.push_back(r % 16);       r /= 16;     }     reverse(begin(R), end(R));     r = old;     unsigned long long ans = 0;     for (int hb = 1; hb < 16; ++hb) {       memset(dp, 0, sizeof dp);       dp[0][0][0][0] = 1;       for (int i = 0; i < int(16); ++i)         for (int was16 = 0; was16 < int(2); ++was16)           for (int was2 = 0; was2 < int(2); ++was2)             for (int ls = 0; ls < int(2); ++ls) {               long long cur_dp = dp[i][was16][was2][ls];               if (!cur_dp) {                 continue;               }               for (int d = 0; d < int(16); ++d) {                 if (d > hb) {                   continue;                 }                 if (!ls && d > R[i]) {                   continue;                 }                 int nwas16 = was16 || d == hb;                 int nwas2 = was2;                 if (hb / 4 == 15 - i && bit(d, hb % 4)) {                   nwas2 = 1;                 }                 int nls = ls || d < R[i];                 dp[i + 1][nwas16][nwas2][nls] += cur_dp;               }             }       unsigned long long cur = 0;       cur += dp[16][1][1][0];       cur += dp[16][1][1][1];       ans += cur;     }     return ans;   }   void solve() {     for (int it = 0; it < int(q); ++it) {       unsigned long long l, r;       l = read_num();       r = read_num();       unsigned long long ans = solve(r);       if (l > 0) {         ans -= solve(l - 1);       }       printf( %llu n , ans);     }   }   int main() {     while (read()) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, sum = 0;     scanf( %d , &n);     int a[n];     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     sort(a, a + n);     for (i = 0; i < n; i++) {       sum += a[i];     }     printf( %d n , a[n - 1] * n - sum);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   int len(const T &c) {     return (int)c.size();   }   template <class T>   void cmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   void cmax(T &a, T b) {     if (b > a) a = b;   }   vector<pair<int, int> > v;   int main() {     int n, t1, t2;     cin >> n;     for (int i(0), _n(n); i < _n; ++i) {       cin >> t1 >> t2;       if (t1 > t2) swap(t1, t2);       v.push_back(make_pair(t1, t2));     }     vector<int> ans;     vector<pair<int, int> > tem;     bool b;     for (int i(0), _n(n); i < _n; ++i) {       b = 1;       tem.clear();       for (int j(0), _n(n); j < _n; ++j)         if (j != i) tem.push_back(v[j]);       sort(tem.begin(), tem.end());       for (int j(0), _n(n - 2); j < _n; ++j) {         if (tem[j].second > tem[j + 1].first) {           b = 0;           break;         }       }       if (b) ans.push_back(i + 1);     }     cout << len(ans) << endl;     for (int i(0), _n(len(ans)); i < _n; ++i) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   const int step[2] = {-1, 1}, mn = 101000;   int n, m, xs, ys, kx, ky, x, y, fx, fy, cnt;   long long ans;   string st;   set<int> T[mn];   void fix(int x, int y, int& fx, int& fy) {     if (x == 1)       fx = 1;     else if (x == n)       fx = 0;     if (y == 1)       fy = 1;     else if (y == m)       fy = 0;   }   int run(int& x, int& y, int& fx, int& fy) {     int l = n;     if (fx == 0)       l = min(l, x - 1);     else       l = min(l, n - x);     if (fy == 0)       l = min(l, y - 1);     else       l = min(l, m - y);     x += l * step[fx], y += l * step[fy];     fix(x, y, fx, fy);     return l;   }   int main() {     cin >> n >> m >> xs >> ys >> st;     kx = st[0] ==  D , ky = st[1] ==  R ;     fix(xs, ys, kx, ky);     x = xs, y = ys, fx = kx, fy = ky;     ans = cnt = 1;     T[x].insert(y);     for (;;) {       ans += run(x, y, fx, fy);       if (!T[x].count(y)) {         ++cnt;         T[x].insert(y);       }       if (cnt == n + m - 2) break;       if (x == xs && y == ys && fx == kx && fy == ky) break;     }     if (cnt == n + m - 2)       cout << ans << endl;     else       cout << -1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 10005;   bool dp[N][2];   char s[N];   set<string> ans;   inline bool ck(int x, int y) {     bool same = true;     for (int i = 0; x + i < y; ++i) {       same &= s[x + i] == s[y + i];     }     return !same;   }   int main() {     scanf( %s , s);     int n = strlen(s);     dp[n][0] = dp[n][1] = true;     for (int i = n - 2; i >= 5; --i) {       dp[i][0] = dp[i + 2][1] || (dp[i + 2][0] && ck(i, i + 2));       dp[i][1] = dp[i + 3][0] || (dp[i + 3][1] && ck(i, i + 3));       if (dp[i][0]) ans.insert(string(s + i, s + i + 2));       if (dp[i][1]) ans.insert(string(s + i, s + i + 3));     }     printf( %d n , ans.size());     for (auto &x : ans) {       printf( %s n , x.c_str());     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bitset<300000005> pr;   int uke[20000005];   int anti_uke(int l, int r) {     int ans = 0;     for (register int i = 5; i <= r; i += 4) {       if (i >= l && pr[i] == 0) ++ans;     }     if (l <= 2 && r >= 2) ++ans;     return ans;   }   int main() {     int l, r;     scanf( %d %d , &l, &r);     pr[0] = pr[1] = 1;     int cnt = 0;     for (register int i = 2; i <= r; i++) {       if (pr[i] == 0) uke[++cnt] = i;       for (register int j = 1; j <= cnt && i * uke[j] <= r; j++) {         pr[i * uke[j]] = 1;         if (i % uke[j] == 0) break;       }     }     printf( %d n , anti_uke(l, r));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int prim[100000];   int main() {     int a, b;     cin >> a >> b;     bool ok = false;     int x = 0;     for (int i = 3; i <= a; i++) {       for (int j = 2; j < i / 2; j++) {         if (i % j == 0) {           ok = true;           break;         }       }       if (ok == false) {         prim[x] = i;         x++;       }       ok = false;     }     int sum = 0;     int xx = 0;     for (int i = 0; i < x; i++) {       sum = prim[i] + prim[i + 1] + 1;       for (int j = 0; j < x; j++) {         if (sum == prim[j]) {           xx++;           break;         }       }     }     if (xx >= b) {       cout <<  YES ;       return 0;     } else {       cout <<  NO ;       return 0;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 5e5 + 10, mod = 1e9 + 9, base = 41, INF = 1e9 + 10, sq = 502;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     long long n, b;     cin >> n >> b;     long long a[n];     for (long long i = 0; i < (n); ++i) cin >> a[i];     long long maximum = b;     for (long long i = 0; i < (n); ++i) {       for (int j = i; j < n; j++) {         long long buy = b / a[i];         long long more = b % a[i];         long long sell = a[j] * buy + more;         if (sell > maximum) maximum = sell;       }     }     cout << maximum << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 2000000000;   const long long infLL = 9000000000000000000;   template <typename first, typename second>   ostream& operator<<(ostream& os, const pair<first, second>& p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T>   ostream& operator<<(ostream& os, const vector<T>& v) {     os <<  { ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  } ;   }   template <typename T>   ostream& operator<<(ostream& os, const set<T>& v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  , ;       os << *it;     }     return os <<  ] ;   }   template <typename T>   ostream& operator<<(ostream& os, const multiset<T>& v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  ] ;   }   template <typename first, typename second>   ostream& operator<<(ostream& os, const map<first, second>& v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << it->first <<   =   << it->second;     }     return os <<  ] ;   }   void faltu() { cerr <<   n ; }   template <typename T>   void faltu(T a[], int n) {     for (int i = 0; i < n; ++i) cerr << a[i] <<    ;     cerr <<   n ;   }   template <typename T, typename... hello>   void faltu(T arg, const hello&... rest) {     cerr << arg <<    ;     faltu(rest...);   }   int n, m;   vector<pair<int, int> > vec1[105], vec2[105];   int x[1005], y[1005];   int cnt1[105], cnt2[105];   vector<pair<int, int> > store[105];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> m;     for (int i = 1; i <= n; ++i) cnt1[i] = cnt2[i] = 1;     for (int i = 0; i < m; ++i) {       cin >> x[i] >> y[i];       if (x[i] > y[i]) swap(x[i], y[i]);       ++cnt1[x[i]];       ++cnt2[y[i]];     }     int r1 = 1e6, c1 = 1e6;     int r2 = r1 - 1, c2 = c1 + 1;     for (int i = 1; i <= n; ++i) {       for (int j = 0; j < cnt1[i]; ++j) {         vec1[i].push_back(make_pair(r1, c1));         store[i].push_back(make_pair(r1, c1));         ++r1;       }       for (int j = 0; j < cnt2[i]; ++j) {         vec2[i].push_back(make_pair(r2, c2));         store[i].push_back(make_pair(r2, c2));         ++c2;       }       reverse(vec1[i].begin(), vec1[i].end());       reverse(vec2[i].begin(), vec2[i].end());       store[i].push_back(make_pair(vec1[i].back().first, vec2[i].back().second));       vec1[i].pop_back();       vec2[i].pop_back();       --c1;       --r2;     }     for (int i = 0; i < m; ++i) {       assert(!vec1[x[i]].empty());       assert(!vec2[y[i]].empty());       store[x[i]].push_back(           make_pair(vec1[x[i]].back().first, vec2[y[i]].back().second));       vec1[x[i]].pop_back();       vec2[y[i]].pop_back();     }     for (int i = 1; i <= n; ++i) {       cout << (int)store[i].size() <<   n ;       for (auto x : store[i]) cout << x.first <<     << x.second <<   n ;     }   }