malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   #define ll long long   #define db long double   #define x first   #define y second   #define mp make_pair   #define pb push_back   #define all(a) a.begin(), a.end()      using namespace std;      struct Pair {     int x;     int y;     int def_stack;   };      bool cmp(Pair &a, Pair &b) {     return (a.x < b.x);   }      const int INF = 1e9;      pair<int, int> check(set<pair<int, int> > &st, Pair &p, Pair &prev, int cost, int t) {     if (!st.size()) return mp(-1, -1);     auto it = st.lower_bound(mp(p.y, -1));     if (it == st.end()) return mp(-1, -1);        int C = ((*it).y) + cost + t;     return mp(prev.y, C);      }      void Ins(set<pair<int, int> > &st, pair<int, int> &p, int t) {     p.y -= t;        if (st.size()) {          auto it = st.lower_bound(p);       if (it != st.end()) {         if ((*it).y <= p.y) return;       }        }        while (st.size()) {       auto it = st.lower_bound(p);       if (it == st.begin()) break;       it--;          if ((*it).y <= p.y) break;       st.erase(it);     }        st.insert(p);      }      int main(){   #ifdef LOCAL    freopen( D_input.txt ,  r , stdin);    //freopen( D_output.txt ,  w , stdout);   #endif    ios_base::sync_with_stdio(0);    cin.tie(0);        int n;     cin >> n;        vector<Pair> v(n);     for (auto &x : v) {       cin >> x.x >> x.y;       x.def_stack = 0;       if (x.x > x.y) {         swap(x.x, x.y);         x.def_stack = 1;         if (x.x > n) {           cout << -1;           exit(0);         }       }       else if (x.x > n) {         cout << -1;         exit(0);       }     }        sort(all(v), cmp);        int fadd = 0, sadd = 0;        set<pair<int, int> > first_stack, second_stack;     int D = v[0].def_stack;        first_stack.insert(mp(INF, 1-D));     second_stack.insert(mp(INF, D));        for (int i = 1; i < n; ++i) {       D = v[i].def_stack;              auto A = check(first_stack, v[i], v[i-1], D, fadd);       auto B = check(second_stack, v[i], v[i-1], 1-D, sadd);          if (v[i].y < v[i-1].y) {         if (D==0) fadd++;         else sadd++;       }       else {         fadd = 0;         sadd = 0;         first_stack.clear();         second_stack.clear();       }         // cout << A.x <<     << B.x << endl;          if (A.x != -1) {         Ins(second_stack, A, sadd);       }       if (B.x != -1) {         Ins(first_stack, B, fadd);       }     }        int ans = INF;     if (first_stack.size()) {       ans = min(ans, (*first_stack.begin()).y + fadd);     }     if (second_stack.size()) {       ans = min(ans, (*second_stack.begin()).y + sadd);     }        if (ans == INF) cout << -1;     else cout << ans;            }
#include <bits/stdc++.h>   using namespace std;   bool const TO_FILE = false;   string const FILENAME =   ;   void prepare() {     if (TO_FILE) {       freopen((FILENAME +  .in ).c_str(),  r , stdin);       freopen((FILENAME +  .out ).c_str(),  w , stdout);     }   }   void solve() {     int n, m, k;     cin >> n >> m >> k;     map<int, pair<int, int>> apps;     map<pair<int, int>, int> apps2;     int curScr = 0;     int curPos = 0;     for (int i = 0; i < n; i++) {       int app;       cin >> app;       apps[app] = make_pair(curScr, curPos);       apps2[make_pair(curScr, curPos)] = app;       if (++curPos == k) {         curPos = 0;         curScr++;       }     }     long long res = m;     for (int i = 0; i < m; i++) {       int curApp, prevApp;       cin >> curApp;       int curScr = apps[curApp].first;       int curPos = apps[curApp].second;       int prevScr, prevPos;       if (curScr == 0 && curPos == 0)         continue;       else if (curPos == 0) {         prevScr = curScr - 1;         prevPos = k - 1;         prevApp = apps2[make_pair(prevScr, prevPos)];       } else {         prevScr = curScr;         prevPos = curPos - 1;         prevApp = apps2[make_pair(prevScr, prevPos)];       }       apps[curApp] = make_pair(prevScr, prevPos);       apps2[make_pair(prevScr, prevPos)] = curApp;       apps[prevApp] = make_pair(curScr, curPos);       apps2[make_pair(curScr, curPos)] = prevApp;       res += curScr;     }     cout << res;   }   int main() {     prepare();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long int;   using P = pair<int, int>;   constexpr int INF = 1 << 30;   template <typename Monoid>   struct SegmentTree {    private:     using F = function<Monoid(Monoid, Monoid)>;     int N;     vector<Monoid> node;     F f;     Monoid e;       public:     SegmentTree() {}     SegmentTree(F f, Monoid e) : f(f), e(e) {}     void init(int sz) {       N = 1;       while (N < sz) N <<= 1;       node.assign(2 * N - 1, e);     }     void build(vector<Monoid>& v) {       int sz = int(v.size());       init(sz);       for (int i = 0; i < sz; i++) {         node[i + N - 1] = v[i];       }       for (int i = N - 2; i >= 0; i--) {         node[i] = f(node[i * 2 + 1], node[i * 2 + 2]);       }     }     void update(int k, Monoid x) {       k += N - 1;       node[k] = x;       while (k > 0) {         k = (k - 1) / 2;         node[k] = f(node[2 * k + 1], node[2 * k + 2]);       }     }     Monoid query(int a, int b) { return query(a, b, 0, 0, N); }     Monoid query(int a, int b, int k, int l, int r) {       if (b <= l || r <= a) return e;       if (a <= l && r <= b) return node[k];       Monoid vl, vr;       vl = query(a, b, 2 * k + 1, l, (l + r) / 2);       vr = query(a, b, 2 * k + 2, (l + r) / 2, r);       return f(vl, vr);     }   };   SegmentTree<int> seg([](int a, int b) { return max(a, b); }, -INF);   vector<vector<int> > g;   vector<int> a;   int main() {     int n;     scanf( %d , &n);     g.resize(n);     a.resize(n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d %d , &u, &v);       u--;       v--;       g[u].push_back(v);       g[v].push_back(u);     }     seg.build(a);     int ans = INF;     for (int i = 0; i < n; i++) {       int maxi = a[i];       g[i].push_back(i);       sort(g[i].begin(), g[i].end());       int pre = 0;       for (auto j : g[i]) {         if (j != i) maxi = max(maxi, a[j] + 1);         maxi = max(maxi, seg.query(pre, j) + 2);         pre = j + 1;       }       maxi = max(maxi, seg.query(pre, n) + 2);       ans = min(ans, maxi);     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int dx[] = {0, 1, -1, 0, 1, -1, -1, 1};   const long long int dy[] = {-1, 0, 0, 1, 1, 1, -1, -1};   long long int gcd(long long int x, long long int y) {     return (y != 0 ? gcd(y, x % y) : x);   }   long long int lcm(long long int x, long long int y) {     return (x / gcd(x, y) * y);   }   struct edge {     int viz;     int cap;     int cost;     int dual;     edge() {}     edge(int viz, int cap, int cost, int dual)         : viz(viz), cap(cap), cost(cost), dual(dual) {}   };   int pro[3054];   int spo[3054];   vector<edge> g[3054];   int d[3054];   int p[3054];   int p_edge[3054];   int color[3054];   int flow;   int fcost;   void add_edge(int x, int y, int cap, int cost) {     g[x].push_back(edge(y, cap, cost, (int)g[y].size()));     g[y].push_back(edge(x, 0, -cost, (int)g[x].size() - 1));   }   int SPFA(int s, int t) {     queue<int> fila;     for (int i = 0; i <= t; i++) {       d[i] = 1000000000;       color[i] = 0;     }     d[s] = 0;     fila.push(s);     while (!fila.empty()) {       int next = fila.front();       color[next] = 0;       fila.pop();       for (int i = 0; i < (int)g[next].size(); i++) {         int viz = g[next][i].viz;         int cap = g[next][i].cap;         int cost = d[next] + g[next][i].cost;         if (cap > 0 && d[viz] > cost) {           d[viz] = cost;           p_edge[viz] = i;           p[viz] = next;           if (color[viz] == 0) {             fila.push(viz);             color[viz] = 1;           }         }       }     }     return d[t] != 1000000000;   }   void mcmf(int s, int t) {     while (SPFA(s, t)) {       int augment = 1000000000;       for (int v = t; v != s; v = p[v]) {         int idx = p_edge[v];         augment = min(augment, g[p[v]][idx].cap);       }       for (int v = t; v != s; v = p[v]) {         int idx = p_edge[v];         int dual = g[p[v]][idx].dual;         g[p[v]][idx].cap -= augment;         g[v][dual].cap += augment;       }       flow += augment;       fcost += d[t] * augment;     }     return;   }   int main(void) {     int src = 3054 - 1;     int sink = 3054 - 2;     int sports = 3054 - 3;     int programming = 3054 - 4;     int n, pp, ss;     cin >> n >> pp >> ss;     for (int i = 0; i < n; i++) {       add_edge(src, i, 1, 0);       cin >> pro[i];       add_edge(i, programming, 1, -pro[i]);     }     for (int i = 0; i < n; i++) {       cin >> spo[i];       add_edge(i, sports, 1, -spo[i]);     }     add_edge(programming, sink, pp, 0);     add_edge(sports, sink, ss, 0);     mcmf(src, sink);     cout << -fcost << endl;     vector<int> saida1, saida2;     for (int i = 0; i < n; i++) {       if (g[i][1].cap == 0) {         saida1.push_back(i + 1);       }       if (g[i][2].cap == 0) {         saida2.push_back(i + 1);       }     }     for (int i = 0; i < saida1.size(); i++) {       cout << saida1[i] <<    ;     }     cout << endl;     for (int i = 0; i < saida2.size(); i++) {       cout << saida2[i] <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int a[30], b[30];   char s[1000005];   struct st {     long long int x, y;   } ara[30];   bool comp(struct st x1, struct st x2) { return x1.y < x2.y; };   int main() {     long long int i, j, k, l, m, n, ans = 1LL, t;     scanf( %lld %lld , &n, &k);     scanf( %s , s);     m = strlen(s);     for (i = 1; i <= m; i++) {       j = s[i - 1] -  a ;       l = (((ans - a[j]) % 1000000007) + 1000000007) % 1000000007;       ans = (ans + l) % 1000000007;       a[j] = (a[j] + l) % 1000000007;       b[j] = i;     }     for (i = 0; i < k; i++) ara[i] = {a[i], b[i]};     sort(ara, ara + k, comp);     for (i = 0; i < k; i++) a[i] = ara[i].x;     for (i = 1; i <= n; i++) {       l = (((ans - a[0]) % 1000000007) + 1000000007) % 1000000007;       ans = (ans + l) % 1000000007;       a[0] = (a[0] + l) % 1000000007;       t = a[0];       for (j = 0; j <= k - 2; j++) a[j] = a[j + 1];       a[j] = t;     }     printf( %lld n , ((ans % 1000000007) + 1000000007) % 1000000007);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T gcd(T a, T b) {     while (b) b ^= a ^= b ^= a %= b;     return a;   }   template <class T>   inline T lcm(T a, T b) {     a = abs(a);     b = abs(b);     return (a / gcd(a, b)) * b;   }   template <class T>   inline T my_strrev(T array) {     int N = strlen(array);     for (int i = 0; i < N / 2; ++i) swap(array[i], array[N - i - 1]);   }   template <class T>   inline T my_revara(T array, int N) {     for (int i = 0; i < N / 2; ++i) swap(array[i], array[N - i - 1]);   }   int fx[8] = {0, -1, 1, 1, 1, 0, -1, -1};   int fy[8] = {1, 0, 1, -1, 0, -1, -1, 1};   int main() {     string str1, str2;     cin >> str1;     cin >> str2;     int len = str1.length();     int flag = 0;     for (int i = 0; i < len; i++) {       if (flag == 1) {         if (str2[i] !=  a ) {           str2[i] = str2[i] - 1;           cout << str2 << endl;           return 0;         }         if (str1[i] !=  z ) {           str1[i] = str1[i] + 1;           cout << str1 << endl;           return 0;         }       }       if (str2[i] > str1[i]) {         if (str2[i] - str1[i] > 1) {           str1[i] = str1[i] + 1;           cout << str1 << endl;           return 0;         }         flag = 1;       }     }     printf( No such string n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double a, ay, b, by, dx, dy, x[100011], y[100011], before[100011],       after[100011], r = 1e18, dist;   int n, i, pos;   int main() {     cin >> a >> ay >> b >> by >> dx >> dy >> n;     for (i = 1; i <= n; i++) {       cin >> x[i] >> y[i];       dist += 2 * sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i]));     }     for (i = 1; i <= n; i++)       r = min(r, dist +                      sqrt((a - x[i]) * (a - x[i]) + (ay - y[i]) * (ay - y[i])) -                      sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i])));     for (i = 1; i <= n; i++)       r = min(r, dist +                      sqrt((b - x[i]) * (b - x[i]) + (by - y[i]) * (by - y[i])) -                      sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i])));     for (i = 1; i <= n; i++)       before[i] = after[i] =           sqrt((b - x[i]) * (b - x[i]) + (by - y[i]) * (by - y[i])) -           sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i]));     for (i = 2; i <= n; ++i) before[i] = min(before[i], before[i - 1]);     for (i = n - 1; i >= 1; --i) after[i] = min(after[i], after[i + 1]);     before[0] = after[n + 1] = 1e18;     for (i = 1; i <= n; ++i)       r = min(r, dist +                      sqrt((a - x[i]) * (a - x[i]) + (ay - y[i]) * (ay - y[i])) -                      sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i])) +                      min(before[i - 1], after[i + 1]));     cout.precision(15);     cout << r;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int n = 1e7;   int main() {     long long a, b, c, ans = 0;     cin >> a >> b >> c;     long long mx = max(a, max(b, c));     long long sum = (a + b + c) - mx;     if (sum * 2 <= mx)       cout << sum << endl;     else       cout << (a + b + c) / 3 << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 2000005;   int n, t;   long long sum[MaxN];   int nums[MaxN];   int main() {     scanf( %d , &n);     t = 0;     for (int i = 1; i <= n; ++i) {       long long ns;       scanf( %lld , &ns);       int nn = 1;       while (t && ns * nums[t] <= sum[t] * nn) {         ns += sum[t];         nn += nums[t];         --t;       }       nums[++t] = nn;       sum[t] = ns;     }     for (int i = 1; i <= t; ++i) {       double val = (double)sum[i] / nums[i];       for (int j = 1; j <= nums[i]; ++j) printf( %.10lf n , val);     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   using namespace std;   int q, a, b, c;   vector<array<int, 3>> v;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     for (int a = 1; a <= 10e4; ++a) {       for (int b = a; b <= 10e4; b += a) {         for (int c = b; c <= 10e4; c += b) v.push_back({a, b, c});       }     }     cin >> q;     while (q--) {       cin >> a >> b >> c;       array<int, 4> ans = {30000, 0, 0, 0};       for (auto x : v) {         int ma = abs(a - x[0]) + abs(b - x[1]) + abs(c - x[2]);         if (ma < ans[0]) ans = {ma, x[0], x[1], x[2]};       }       cout << ans[0] <<   n  << ans[1] <<     << ans[2] <<     << ans[3] <<   n ;     }   }
#include <bits/stdc++.h>   const int N = 3200;   using namespace std;   int n, l;   int a[N][3], b[N][3];   int dp[N][105][3];   void add(int &a, int &b) {     a += b;     if (a >= 1000000007) a -= 1000000007;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> l;     for (int i = 1; i <= n; i++) {       cin >> a[i][0] >> b[i][0];       a[i][1] = b[i][0];       b[i][1] = a[i][0];     }     for (int i = 1; i <= n; i++) {       dp[a[i][0]][i][0] += 1;       if (a[i][0] != b[i][0]) dp[b[i][0]][i][1] += 1;     }     for (int i = 1; i <= l; i++) {       for (int cur = 1; cur <= n; cur++) {         for (int rot1 = 0; rot1 <= 1; rot1++) {           for (int to = 1; to <= n; to++) {             if (to != cur)               for (int rot2 = 0; rot2 <= 1; rot2++) {                 if (a[to][0] == b[to][0] && rot2 == 1) continue;                 if (b[cur][rot1] == a[to][rot2])                   add(dp[i + a[to][rot2]][to][rot2], dp[i][cur][rot1]);               }           }         }       }     }     int ans = 0;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= 1; j++) {         add(ans, dp[l][i][j]);       }     }     cout << ans << endl;     cin.get();     cin.get();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 3e3 + 3;   int p[MAX], cycle[MAX], nocycles, n, m;   void dfs(int v) {     cycle[v] = nocycles;     if (cycle[p[v]] == 0) dfs(p[v]);   }   void gen() {     nocycles = 1;     memset(cycle, 0, sizeof cycle);     for (int i = 1; i < n + 1; i++)       if (not cycle[i]) dfs(i), nocycles++;   }   vector<pair<int, int> > ans;   int main() {     scanf( %d , &n);     for (int i = 1; i < n + 1; i++) {       scanf( %d , p + i);     }     cin >> m;     gen();     int cur = n - nocycles + 1;     if (cur < m) {       int idx = 2;       while (cur != m) {         int comp = cycle[p[idx]];         if (comp == cycle[p[1]]) {           idx++;           continue;         }         ans.push_back(pair<int, int>(1, idx));         for (int i = idx; i < n + 1; i++)           if (cycle[i] == comp) cycle[i] = cycle[p[1]];         cur++, idx++;       }     } else if (cur > m) {       int idx = 1, loop = 1;       while (cur != m) {         if (p[idx] == idx) {           idx++;           continue;         }         for (int i = idx + 1; i < n + 1; i++)           if (cycle[p[i]] == cycle[p[idx]]) {             ans.push_back(pair<int, int>(idx, i));             swap(p[idx], p[i]);             break;           }         cur--;         gen();         loop++;         assert(loop <= n + 10);       }     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) {       printf( %d %d  , ans[i].first, ans[i].second);     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long fuck = 4e17;   long long n, arr[100010];   vector<long long> ans;   bool chk(long long x) {     vector<int> diffs;     for (int i = 0; i + 1 < n; i++) {       long long next = arr[i + 1];       if (arr[i] <= x) next = min(arr[i + 1], x);       long long d = next - arr[i];       if (diffs.size() == 0)         diffs.push_back(d);       else if (diffs[0] != d)         return false;     }     return true;   }   int main() {     scanf( %lld , &n);     for (int i = 0; i < n; i++) scanf( %lld , &arr[i]);     sort(arr, arr + n);     if (n == 1) {       printf( -1 );       return 0;     }     if (n == 2) {       long long d = arr[1] - arr[0];       ans.push_back(arr[0] - d);       ans.push_back(arr[1] + d);       if (!(d & 1)) ans.push_back(arr[1] - d / 2);     } else {       long long prev = arr[1] - arr[0], another = fuck, pos;       for (int i = 2; i < n; i++) {         long long d = arr[i] - arr[i - 1];         if (d != prev && d != another && another != fuck) {           printf( 0 );           return 0;         }         if (d != prev) {           pos = i;           another = arr[i] - arr[i - 1];         }       }       if (another == fuck) {         ans.push_back(arr[0] - prev);         ans.push_back(arr[n - 1] + prev);       } else {         if (prev == 2ll * another)           ans.push_back(arr[1] - another);         else if (another == prev * 2ll)           ans.push_back(arr[pos] - prev);       }     }     set<long long> res;     for (int i = 0; i < ans.size(); i++)       if (chk(ans[i])) res.insert(ans[i]);     printf( %d n , res.size());     for (set<long long>::iterator it = res.begin(); it != res.end(); it++)       printf( %lld  , *it);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int SEED = chrono::steady_clock::now().time_since_epoch().count();   mt19937 rng(SEED);   const int B = 3000;   struct Query {     int l, r, t, idx, bl, br;     Query(int l, int r, int t, int idx)         : l(l), r(r), t(t), idx(idx), bl(l / B), br(r / B) {}     bool operator<(const Query &q) const {       return tuple<int, int, int>(bl, br, t) <              tuple<int, int, int>(q.bl, q.br, q.t);     }   };   const int MAX = 100005;   int cnt[MAX], v[MAX], ocur[2 * MAX];   vector<pair<int, int> > updateForward, updateBackward;   void add(int pos) {     int x = v[pos];     cnt[ocur[x]]--;     cnt[++ocur[x]]++;   }   void remove(int pos) {     int x = v[pos];     cnt[ocur[x]]--;     cnt[--ocur[x]]++;   }   void applyUpdate(pair<int, int> upd, int l, int r) {     auto [pos, val] = upd;     if (l <= pos && pos <= r) remove(pos);     v[pos] = val;     if (l <= pos && pos <= r) add(pos);   }   int getAns() {     int res = 0;     while (cnt[res]) res++;     return res;   }   vector<int> MO(vector<Query> queries) {     cnt[0] = 0x3f3f3f3f;     int l = 0, r = -1, t = 0;     vector<int> ans(queries.size());     sort(queries.begin(), queries.end());     for (auto q : queries) {       while (t < q.t) applyUpdate(updateForward[t++], l, r);       while (q.t < t) applyUpdate(updateBackward[--t], l, r);       while (q.l < l) add(--l);       while (r < q.r) add(++r);       while (l < q.l) remove(l++);       while (q.r < r) remove(r--);       ans[q.idx] = getAns();     }     return ans;   }   void create_queries(vector<pair<int, int> > updates) {     int n = updates.size();     for (int i = 0; i < n; i++) {       auto [pos, val] = updates[i];       updateForward.emplace_back(pos, val);       updateBackward.emplace_back(pos, v[pos]);       v[pos] = val;     }     for (int i = n - 1; i >= 0; i--) {       auto [pos, val] = updateBackward[i];       v[pos] = val;     }   }   map<int, int> mp;   int compress(int x) {     if (!mp.count(x)) mp[x] = mp.size();     return mp[x];   }   void test_case() {     int n, q, type, l, r, x;     cin >> n >> q;     for (int i = 0; i < n; i++) {       cin >> x;       v[i] = compress(x);     }     vector<pair<int, int> > updates;     vector<Query> queries;     for (int i = 0; i < q; i++) {       cin >> type >> l >> r;       if (type == 1)         queries.emplace_back(l - 1, r - 1, updates.size(), queries.size());       if (type == 2) updates.emplace_back(l - 1, compress(r));     }     create_queries(updates);     vector<int> res = MO(queries);     for (auto x : res) cout << x << endl;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t = 1;     for (int i = 1; i <= t; i++) {       test_case();     }     return 0;   }