malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native )   using namespace std;   const long long inf = 1e9 + 5;   struct Node {     Node *l = 0, *r = 0;     long long lo, hi, mset = inf, madd = 0, val = 0, smol = inf;     Node(int lo, int hi) : lo(lo), hi(hi) {}     Node(vector<long long>& v, int lo, int hi) : lo(lo), hi(hi) {       if (lo + 1 < hi) {         int mid = lo + (hi - lo) / 2;         l = new Node(v, lo, mid);         r = new Node(v, mid, hi);         val = l->val + r->val;         smol = min(l->smol, r->smol);       } else         val = v[lo], smol = v[lo];     }     long long query(int L, int R) {       if (R <= lo || hi <= L) return 0;       if (L <= lo && hi <= R) return val;       push();       return l->query(L, R) + r->query(L, R);     }     int find(long long v) {       if (smol > v) return hi;       if (lo + 1 == hi) return lo;       if (mset <= v) return lo;       if (l->smol <= v) return l->find(v);       return r->find(v);     }     void set(int L, int R, int x) {       if (R <= lo || hi <= L) return;       if (L <= lo && hi <= R)         mset = x, val = (hi - lo) * x, smol = x, madd = 0;       else {         push(), l->set(L, R, x), r->set(L, R, x);         val = l->val + r->val;         smol = min(l->smol, r->smol);       }     }     void push() {       if (!l) {         int mid = lo + (hi - lo) / 2;         l = new Node(lo, mid);         r = new Node(mid, hi);       }       if (mset != inf) l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;     }   };   int n, q;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> q;     vector<long long> l(n, 0);     for (int i = 0; i < n; i++) {       long long v;       cin >> v;       l[i] = v;     }     Node seg = Node(l, 0, n);     for (int it = 0; it < q; it++) {       long long y;       int t, x;       cin >> t >> x >> y;       x -= 1;       if (t == 1) {         int left = seg.find(y);         if (x + 1 > left) {           seg.set(left, x + 1, y);         }       } else {         int out = 0;         x = max(x, seg.find(y));         while (x < n) {           long long dist = y / seg.query(x, x + 1);           int lo = min((long long)n - 1, x + dist - 1);           y -= seg.query(x, lo + 1);           out += (lo - x + 1);           x = max(lo + 1, seg.find(y));         }         cout << out <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   double EPS = 1e-9;   int conversion(string p) {     int o;     o = atoi(p.c_str());     return o;   }   string toString(int h) {     stringstream ss;     ss << h;     return ss.str();   }   long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }   long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }   long long toi(string p) {     long long x;     istringstream in(p);     in >> x;     return x;   }   long long square(long long n) { return n * n; }   long long fastexp(long long base, long long power) {     if (power == 0)       return 1LL;     else if (power % 2 == 0)       return square(fastexp(base, power / 2));     else       return base * (fastexp(base, power - 1));   }   bool is_prime(int n) {     for (int i = 2; i * i <= n; i++)       if (n % i == 0) return 0;     return 1;   }   int logba(int a, int b) { return log(a) / log(b); }   int rd(double d) {     int x = (int)((double)d + 0.5);     return x;   }   vector<pair<int, int> > W, B, Q;   char lab[8][8];   bool go(int x, int y, int id) {     if (id == 0) {       for (int i = x - 1; i >= 0; i--) {         if (lab[i][y] !=  . ) return false;       }     } else {       for (int i = x + 1; i < 8; i++) {         if (lab[i][y] !=  . ) return false;       }     }     return true;   }   int main() {     int res = 0, x, y;     for (int i = 0; i < 8; i++) {       for (int j = 0; j < 8; j++) {         cin >> lab[i][j];         if (lab[i][j] ==  W ) W.push_back(make_pair(i, j));         if (lab[i][j] ==  B ) B.push_back(make_pair(i, j));       }     }     for (int i = 0; i < W.size(); i++) {       x = W[i].first;       y = W[i].second;       if (go(x, y, 0)) Q.push_back(make_pair(x, 0));     }     for (int i = 0; i < B.size(); i++) {       x = B[i].first;       y = B[i].second;       if (go(x, y, 1)) Q.push_back(make_pair(7 - x, 1));     }     sort(Q.begin(), Q.end());     if (Q[0].second == 0)       cout <<  A  << endl;     else       cout <<  B  << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 16;   int n, m;   int d[2][N];   int get_n(int x, int n) {     int tmp = 3 << (n * 2);     tmp = x & tmp;     tmp >>= n * 2;     return tmp;   }   int set_n(int x, int n, int y) {     int tmp = 3 << (n * 2);     x = x & (~tmp);     y <<= (n * 2);     x |= y;     return x;   }   void update(int &x, int y) {     if (x == -1 || x < y) x = y;   }   int solve() {     int i, j, k;     int t = 0;     memset(d, -1, sizeof(d));     d[0][0] = 0;     int tot = 1 << (n * 2 + 2);     for (i = 0; i < m; ++i) {       for (j = 0; j < n; ++j) {         memset(d[1 - t], -1, sizeof(d[1 - t]));         for (k = 0; k < tot; ++k) {           if (d[t][k] == -1) continue;           int t0 = get_n(k, j);           int t1 = get_n(k, j + 1);           int st = set_n(k, j, 1);           st = set_n(st, j + 1, 1);           update(d[1 - t][st], d[t][k]);           if (t0 > 0) {             st = set_n(k, j - 1, 2);             st = set_n(st, j, 0);             st = set_n(st, j + 1, 0);             update(d[1 - t][st], d[t][k] + 1);             if (t0 == 1) {               st = set_n(k, j - 1, 0);               st = set_n(st, j, 2);               st = set_n(st, j + 1, 2);               update(d[1 - t][st], d[t][k] + 1);             }           }           if (t1 > 0) {             st = set_n(k, j, 0);             st = set_n(st, j + 1, 0);             update(d[1 - t][st], d[t][k] + 1);             if (t1 == 1) {               st = set_n(k, j, 2);               st = set_n(st, j + 1, 2);               update(d[1 - t][st], d[t][k] + 1);             }           }           if (t0 == 1 && t1 == 1) {             st = set_n(k, j - 1, 0);             st = set_n(st, j, 2);             st = set_n(st, j + 1, 2);             update(d[1 - t][st], d[t][k] + 2);           }         }         t = 1 - t;       }       memset(d[1 - t], -1, sizeof(d[1 - t]));       for (k = 0; k < tot; ++k) {         int st = set_n(k, n, 0);         st <<= 2;         update(d[1 - t][st], d[t][k]);       }       t = 1 - t;     }     int ans = -1;     for (k = 0; k < tot; ++k) {       update(ans, d[t][k]);     }     return ans;   }   int main() {     int i, j, k, t, nc = 0;     while (scanf( %d%d , &n, &m) != EOF) {       if (n > m) swap(n, m);       int ans = solve();       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > g;   vector<long double> ans;   vector<int> sum;   void dfs0(int x) {     sum[x] = 1;     for (int to : g[x]) {       dfs0(to);       sum[x] += sum[to];     }   }   void dfs(int x, long double f) {     ans[x] = f;     int s = sum[x] - 1;     for (int to : g[x]) {       dfs(to, ((s - sum[to]) / 2.0) + f + 1.0);     }   }   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     g.resize(n);     ans.resize(n);     sum.resize(n);     for (int i = 1; i < n; i++) {       int p;       cin >> p;       p--;       g[p].push_back(i);     }     dfs0(0);     dfs(0, 1.0);     cout << fixed << setprecision(8);     for (long double x : ans) cout << x <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   long long k;   long long f(int a[], int l, int r) {     long long m = 0;     for (int i = 0; i < n; i++)       if (a[i] < l)         m += l - a[i];       else if (a[i] > r)         m += a[i] - r;     return m;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     cin >> n >> k;     int a[n];     for (int& i : a) cin >> i;     int l = 0, r = 1e9;     while (l <= r) {       int m = (l + r) / 2;       int lt = 0, rt = 1e9;       while (rt - lt > 1) {         int mt = (lt + rt) / 2;         if (f(a, mt, mt + m) < f(a, mt + 1, mt + 1 + m))           rt = mt;         else           lt = mt;       }       if (f(a, rt, rt + m) <= k)         r = m - 1;       else         l = m + 1;     }     cout << l <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   struct tree {     int C;     int root;     int number_of_branches;     int *branches;   };   int main() {     int c, p, n, i, j, r = 0;     int start, D[100000] = {0}, k = 0;     struct tree A[100000];     cin >> n;     for (i = 0; i < n; i++) {       A[i].branches = new int[1];       A[i].number_of_branches = 0;     }     for (i = 0; i < n; i++) {       cin >> p >> c;       if (p == -1) {         start = i;       } else {         A[i].C = c;         A[i].root = p - 1;         A[p - 1].branches[A[p - 1].number_of_branches] = i;         A[p - 1].number_of_branches++;         A[p - 1].branches =             (int *)realloc(A[p - 1].branches,                            (A[p - 1].number_of_branches) * sizeof(struct tree));       }     }     for (i = 0; i < n; i++) {       if (A[i].C == 1) {         k = 0;         for (j = 0; j < A[i].number_of_branches; j++) {           if (A[A[i].branches[j]].C == 1) {             k++;           }         }         if ((k == A[i].number_of_branches) ||             (k == 0 && k == A[i].number_of_branches)) {           cout << i + 1 <<    ;           r = 1;         }       }     }     if (r == 0) {       cout << -1;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int N = 2e5 + 5;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cout << name <<   :   << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   long long int n, k;   long long int a[N];   long long int month[2 * N];   long long int sum(long long int days) { return (days * (days + 1) / 2); }   void solve() {     cin >> n >> k;     for (long long int i = 0; i < n; i++) {       cin >> a[i];       month[i] = a[i];       month[n + i] = a[i];     }     long long int j = 0;     long long int hugs = 0, days = 0, best = 0;     for (long long int i = 0; i < 2 * n; i++) {       hugs += sum(month[i]);       days += month[i];       while (days > k) {         days -= month[j];         hugs -= sum(month[j]);         j++;       }       long long int curr = hugs;       if (j > 0) {         long long int skips = month[j - 1] + days - k;         curr += sum(month[j - 1]) - sum(skips);       }       best = max(best, curr);     }     cout << best <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     solve();     return 0;   }
#include <bits/stdc++.h>   #define ll long long   #define ld long double   #define pb push_back   #define VI vector<int>   #define VL vector<ll>   #define MX vector<VI >   #define all(x) x.begin(), x.end()   #define IOS cin.tie(0)->sync_with_stdio(0)      using namespace std;      ll gcd(ll a, ll b){    if(!b) return a;    return gcd(b, a%b);   }      int main(){       int n, m;    cin >> n >> m;       VL a(n), b(m);    for(ll &i : a) cin >> i;    for(ll &i : b) cin >> i;       sort(all(a));       ll t = 0;    for(int i=1; i<n; i++) t = gcd(a[i]-a[i-1], t);       for(ll &i : b) cout << gcd(i+a[0], t) <<    ;    cout <<   n ;       return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int toInt(string s) {     int v;     istringstream sin(s);     sin >> v;     return v;   }   template <class T>   inline string toString(T x) {     ostringstream sout;     sout << x;     return sout.str();   }   inline int readInt() {     int x;     scanf( %d , &x);     return x;   }   int a[55];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < (int)n; ++i) scanf( %d , &a[i]);     sort(a, a + n);     if (a[0] == a[n / 2])       printf( Bob n );     else       printf( Alice n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   const int INF = 0x7fffffff;   const double PI = acos(-1.0);   const int MAX = 100005;   const int MOD = 1000000007;   inline bool isEq(double a, double b) { return abs(a - b) < EPS; }   inline bool isEq(double a, double b, double eps) { return abs(a - b) < eps; }   inline double toRad(double deg) { return deg * PI / 180; }   inline double toDeg(double rad) { return rad * 180 / PI; }   class Timer {     clock_t start;     string name;       public:     Timer() {       name =   ;       start = clock();     }     Timer(string s) {       name = s;       start = clock();     }     ~Timer() {       fprintf(stderr,  %s: %.3gs n , name.c_str(),               1.0 * (clock() - start) / CLOCKS_PER_SEC);     }   };   double cross(complex<double> &a, complex<double> &b) {     return imag((conj(a) * b));   }   double perpendicularDist(double x1, double y1, double x2, double y2, double x3,                            double y3) {     complex<double> A = complex<double>(x2 - x1, y2 - y1);     complex<double> B = complex<double>(x3 - x1, y3 - y1);     return cross(A, B) / abs(A);   }   double getYaxiyIntersection(complex<double> start, complex<double> a) {     double lo = 0, hi = 1000000000, dist = 0;     int cnt = 300;     a = a / abs(a);     complex<double> en;     while (cnt--) {       dist = (lo + hi) / 2;       en = start + a * dist;       if (start.real() * en.real() > 0)         lo = dist;       else         hi = dist;     }     return en.imag();   }   int main(int argc, char *argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     double y1, y2, yw, xb, yb, r;     cin >> y1 >> y2 >> yw >> xb >> yb >> r;     double lowxw = 0, highxw = xb, xw, y, rxb;     int cnt = 300;     double yweff = yw - r;     bool found = false;     complex<double> A;     while (cnt--) {       xw = (lowxw + highxw) / 2;       rxb = xw - (xb - xw);       A = complex<double>(xw - rxb, yweff - yb);       if (rxb > 0) A = -A;       y = getYaxiyIntersection(complex<double>(rxb, yb), A);       if (y - r >= y1 && y2 >= y + r &&           perpendicularDist(0, y, xw, yweff, 0, y2) >= r) {         found = true;         break;       } else {         if (y - r < y1)           highxw = xw;         else           lowxw = xw;       }     }     if (found)       cout << fixed << setprecision(10) << xw << endl;     else       cout <<  -1  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long i, n, m, a[200000], q, l, p, k, c[200000], b[200000], j, ans;   string s[200000], ss, s1;   int main() {     ios_base::sync_with_stdio(0);     ans = 1000000007;     cin >> n;     for (i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + 1 + n);     for (i = 1; i <= n / 2; i++) {       ans = min(ans, a[n / 2 + i] - a[i]);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<int> a(n);     for (int &i : a) {       cin >> i;     }     vector<vector<bool> > match(n + 1, vector<bool>(n + 1, false));     for (int i = 0, x, y; i < m; i++) {       cin >> x >> y;       x--;       y--;       match[x][y] = true;       match[y][x] = true;     }     int ans = INT_MAX;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         for (int z = j + 1; z < n; z++) {           if (match[i][j] && match[j][z] && match[z][i]) {             ans = min(ans, a[i] + a[j] + a[z]);           }         }       }     }     if (ans == INT_MAX) ans = -1;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, x, ans;   priority_queue<long long> q;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x;       q.push(x);       if (q.top() > x) {         ans += q.top() - x;         q.pop();         q.push(x);       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int *p = new int[100010];     fill(p, p + 100010, -1000000000);     for (int i = 0; i < n; ++i) {       int x, y;       int res = 0;       scanf( %d%d , &x, &y);       int q = (int)sqrt((double)x);       for (int j = 1; j <= q; ++j) {         if (x % j == 0) {           if (p[j] < i - y) ++res;           p[j] = i;           if (p[x / j] < i - y) ++res;           p[x / j] = i;         }       }       cout << res << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int power(long long int x, long long int y) {     if (y == 0) return 1;     long long int d = power(x, y / 2) % 1000000007;     if (y % 2 == 0)       return (d * d) % 1000000007;     else       return (((x * d) % 1000000007) * d) % 1000000007;   }   long long int divmod(long long int num, long long int den) {     return (num * (power(den, 1000000007 - 2))) % 1000000007;   }   int mobius[100001];   int mob(int x) {     int cnt = 0;     for (int i = 2; i * i <= x; i++) {       if (x % i == 0) {         x = x / i;         cnt++;       }       if (x % i == 0) {         return 0;       }     }     if (x > 1) {       cnt++;     }     if (cnt & 1) return -1;     return 1;   }   long long int nmod(long long int x) {     return (x % 1000000007 + 1000000007) % 1000000007;   }   long long int fact[100001];   long long int ncr(long long int n, long long int r) {     long long int num = fact[n];     long long int den = fact[r] * fact[n - r];     den %= 1000000007;     return divmod(num, den);   }   long long int calc(long long int n, long long int k, long long int x) {     n = n / x;     n = n - k;     if (n < 0) return 0;     if (n == 0) return 1;     return ncr(n + k - 1, k - 1);   }   void solve() {     int n, k;     scanf( %d , &n);     scanf( %d , &k);     vector<int> f;     for (int i = 1; i * i <= n; i++) {       if (i * i == n) {         f.push_back(i);         break;       }       if (n % i == 0) {         f.push_back(i);         f.push_back(n / i);       }     }     long long int ans = 0;     for (int i = 0; i < f.size(); i++) {       if (mobius[f[i]] == 0) continue;       ans = ans + mobius[f[i]] * calc(n, k, f[i]);     }     ans = nmod(ans);     printf( %lld n , ans);   }   int main() {     fact[0] = 1;     for (int i = 1; i <= 100000; i++) fact[i] = (i * fact[i - 1]) % 1000000007;     mobius[1] = 1;     for (int i = 2; i <= 100000; i++) {       mobius[i] = mob(i);     }     int q;     scanf( %d , &q);     for (int i = 0; i < q; i++) solve();   }
#include <bits/stdc++.h>   using namespace std;   int read() {     int x = 0, f = 1, ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + (ch -  0 );       ch = getchar();     }     return f * x;   }   const int maxn = 1e5 + 10, mod = 1e9 + 7, limit = 1e7;   int n, num[2][maxn], k[2];   int f[2][maxn], cnt[2][maxn], ans[2][maxn];   queue<pair<int, int> > q;   int main() {     n = read();     memset(ans, 127, sizeof(ans));     k[0] = read();     for (int i = 1; i <= k[0]; i++) num[0][i - 1] = read();     k[1] = read();     for (int i = 1; i <= k[1]; i++) num[1][i - 1] = read();     ans[0][0] = ans[1][0] = 0;     q.push(make_pair(0, 0));     q.push(make_pair(1, 0));     while (q.size()) {       while (q.size()) {         int x = q.front().first, y = q.front().second;         q.pop();         for (int i = 0; i < k[x ^ 1]; i++) {           int x1 = x ^ 1, y1 = (y - num[x1][i] + n) % n;           if (!ans[x1][y1] || ans[x1][y1] == 1) continue;           ans[x1][y1] = 1;           for (int j = 0; j < k[x]; j++) {             int y2 = (y1 - num[x][j] + n) % n;             ++cnt[x][y2];           }         }       }       for (int i = 0; i < 2; i++)         for (int j = 0; j < n; j++) {           if (!ans[i][j] || ans[i][j] == 1) continue;           if (cnt[i][j] == k[i]) {             ans[i][j] = 0;             q.push(make_pair(i, j));           }         }     }     for (int i = 0; i < 2; i++) {       for (int j = 1; j < n; j++) {         if (ans[i][j] == 0)           printf( Lose  );         else if (ans[i][j] == 1)           printf( Win  );         else           printf( Loop  );       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     while (cin >> n) {       stack<int> st;       bool ov = true;       int ovs = 0;       int res = 0;       int spd = 0;       for (int i = 0; i < n; i++) {         scanf( %d , &(a));         if (a == 1) {           scanf( %d , &(b));           spd = b;         } else if (a == 2) {           res += ovs;           ovs = 0;         } else if (a == 3) {           scanf( %d , &(b));           st.push(b);         } else if (a == 4) {           ovs = 0;         } else if (a == 5) {           while (!st.empty()) st.pop();         } else if (a == 6) {           ovs++;         }         while (!st.empty() && st.top() < spd) {           st.pop();           res++;         }       }       cout << res << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int cmax = 1e5 + 8;   int n;   int cnt[cmax];   vector<pair<int, int> > l(cmax);   long long psum[cmax];   int d[202];   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> l[i].first, cnt[l[i].first]++;     for (int i = 1; i <= n; i++) cin >> l[i].second;     sort(l.begin() + 1, l.begin() + n + 1);     for (int i = 1; i <= n; i++) psum[i] = psum[i - 1] + l[i].second;     long long ans = 284488937875784;     for (int i = 1; i <= n; i++) {       int p = i;       while (i < n and l[i].first == l[i + 1].first) i++;       long long t = psum[n] - psum[i];       int x = max(0, i - 2 * cnt[l[i].first] + 1);       for (int j = 1; j <= 200 and x > 0; j++) {         int u = min(x, d[j]);         x -= u;         t += (long long)u * j;       }       ans = min(ans, t);       for (int k = p; k <= i; k++) d[l[k].second]++;     }     cout << ans <<   n ;     return 0;   }