malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   int n;   struct vec {     long long x, y, idx;   } a, b;   long double pos_ang_with_x_axis(vec a) {     long double res = atan2(a.y, a.x) * 180 / acos(-1);     if (res < 0) res += 360;     return res;   }   int dot(vec a, vec b) { return a.x * b.x + a.y * b.y; }   long double norm(vec a) { return (long double)sqrt(a.x * a.x + a.y * a.y); }   long double angle(vec a, vec b) {     long double ang1 = pos_ang_with_x_axis(a);     long double ang2 = pos_ang_with_x_axis(b);     return fabs(ang1 - ang2);   }   vector<vec> v;   bool cmp(vec a, vec b) {     vec c;     c.x = c.y = 0;     long double ang1 = pos_ang_with_x_axis(a);     long double ang2 = pos_ang_with_x_axis(b);     return ang1 < ang2;   }   int main() {     ios::sync_with_stdio(0);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a.x >> a.y;       a.idx = i + 1;       v.push_back(a);     }     sort(v.begin(), v.end(), cmp);     int idx1, idx2;     long double mn = 1e18;     v.push_back(v[0]);     for (int i = 1; i <= n; i++) {       long double d = angle(v[i], v[i - 1]);       d = min(d, (long double)360 - d);       if (d < mn) mn = d, idx1 = v[i - 1].idx, idx2 = v[i].idx;     }     cout << idx1 <<     << idx2 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long last = -1;   void dfs(long long s, vector<vector<pair<long long, long long>>> grf,            vector<long long> &vis, long long &cost) {     vis[s] = 1;     last = s;     for (auto x : grf[s]) {       long long v = x.first, d = x.second;       if (vis[v] == 0) {         cost += d;         dfs(v, grf, vis, cost);       }     }   }   signed main() {     long long n;     cin >> n;     vector<vector<pair<long long, long long>>> grf(n + 1);     long long sum = 0;     for (long long i = 0; i < n; i++) {       long long u, v, w;       cin >> u >> v >> w;       grf[u].push_back({v, w});       grf[v].push_back({u, 0});       sum += w;     }     long long cost = 0;     vector<long long> vis(n + 1, 0);     dfs(1, grf, vis, cost);     if (last != 1) {       for (auto x : grf[last]) {         if (x.first == 1) cost += x.second;       }     }     cout << min(cost, sum - cost);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> primes = {2,  4,  3,  9,  5,  25, 7,  49, 11, 13,                           17, 19, 23, 29, 31, 37, 41, 43, 47};     int n = 0;     for (int i = 0; i < primes.size(); i++) {       cout << primes[i] << endl;       string s;       cin >> s;       if (s ==  yes ) n++;       if (n > 1) return cout <<  composite , 0;     }     cout <<  prime ;   }
#include <bits/stdc++.h>   using namespace std;   long long int n, k, nb, a[1005], b[2005], t = -10000, pos = 0, p, d;   long long int save[1005][2005];   long long int time(int i, int j) {     if (save[i][j] != -1) return save[i][j];     if (i == n) return 0;     if (j == k) return 3e9;     return (save[i][j] = min(time(i, j + 1), max(abs(a[i] - b[j]) + abs(p - b[j]),                                                  time(i + 1, j + 1))));   }   int main() {     long long int i, j;     cin >> n >> k >> p;     vector<long long int> v;     v.clear();     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     for (i = 0; i < k; i++) cin >> b[i];     sort(b, b + k);     memset(save, -1, sizeof save);     t = time(0, 0);     cout << t;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   int n, m, t;   vector<int> e[maxn];   int ecnt[maxn];   queue<int> q;   bool vis[maxn];   int dep[maxn];   int ec[maxn];   bool flag;   int f[maxn];   int g[maxn];   int cnt[maxn];   int read() {     int x;     scanf( %d , &x);     return x;   }   int dfs(int x, int fa) {     int y;     if (!ec[dep[x]]) {       ec[dep[x]] = e[x].size();     } else {       if (ec[dep[x]] != e[x].size()) {         flag = false;         return 0;       }     }     for (auto y : e[x]) {       if (y == fa) continue;       dep[y] = dep[x] + 1;       dfs(y, x);       if (!flag) {         return 0;       }     }     return 0;   }   bool check(int x) {     memset(ec, 0, sizeof(ec));     memset(dep, 0, sizeof(dep));     flag = true;     dep[x] = 1;     dfs(x, -1);     return flag;   }   int solve() {     int i, j;     int x, y;     vector<int> ans;     memset(vis, 0, sizeof(vis));     for (i = 1; i <= n; i++) {       ecnt[i] = e[i].size();       if (e[i].size() == 1) {         vis[i] = true;         q.push(i);         f[i] = 1;         g[i] = i;         cnt[i] = 1;       }     }     while (!q.empty()) {       x = q.front();       q.pop();       for (auto y : e[x]) {         if (vis[y]) continue;         ecnt[y]--;         if (ecnt[y] == 1) {           q.push(y);           vis[y] = true;         }       }       for (auto y : e[x]) {         if (!vis[y]) continue;         if (!f[y]) continue;         if (f[x]) {           if (f[y] + 1 != f[x]) {             if (cnt[x] < cnt[y]) {               ans.push_back(g[x]);             } else if (cnt[x] > cnt[y]) {               ans.push_back(g[y]);             } else {               ans.push_back(g[x]);               ans.push_back(g[y]);             }             break;           } else {             cnt[x] += cnt[y];           }         } else {           f[x] = f[y] + 1;           g[x] = g[y];           cnt[x] += cnt[y];         }       }       if (q.empty() and ans.size() == 0) {         for (auto y : e[x]) {           if (!vis[y]) continue;           if (!f[y]) continue;           if (cnt[y] == 1) {             ans.push_back(g[y]);           }         }       }       if (ans.size()) break;     }     for (auto x : ans) {       if (check(x)) {         printf( %d n , x);         return 0;       }     }     printf( -1 n );     return 0;   }   int main() {     int i, j;     int x = 1, y;     n = read();     for (i = 1; i < n; i++) {       x = read();       y = read();       e[x].push_back(y);       e[y].push_back(x);       ecnt[x]++;       ecnt[y]++;     }     for (i = 1; i <= n; i++) {       if (e[i].size() == 1) {         vis[i] = true;         q.push(i);       }     }     while (!q.empty()) {       x = q.front();       q.pop();       for (auto y : e[x]) {         if (vis[y]) continue;         ecnt[y]--;         if (ecnt[y] == 1) {           q.push(y);           vis[y] = true;         }       }     }     if (check(x)) {       printf( %d n , x);     } else {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 200001;   const long long K = 5;   const long long INF = 1e16 + 7;   long long n, k, q;   long long a[MAXN][K];   long long tree[MAXN << 2][1 << K];   void upd(long long v, long long tl, long long tr, long long p) {     if (tl == tr) {       for (long long i = 0; i < k; ++i) {         cin >> a[tl][i];       }       for (long long mask = 0; mask < (1 << k); ++mask) {         long long cost = 0;         for (long long i = 0; i < k; ++i) {           if ((mask >> i) & 1) {             cost += a[tl][i];           } else {             cost -= a[tl][i];           }         }         tree[v][mask] = cost;       }       return;     }     long long tm = (tl + tr) >> 1;     if (p <= tm) {       upd(v * 2 + 1, tl, tm, p);     } else {       upd(v * 2 + 2, tm + 1, tr, p);     }     for (long long mask = 0; mask < (1 << k); ++mask) {       tree[v][mask] = max(tree[v * 2 + 1][mask], tree[v * 2 + 2][mask]);     }   }   long long getmax(long long v, long long tl, long long tr, long long l,                    long long r, long long mask) {     if (tr < l || r < tl) {       return -INF;     }     if (l <= tl && tr <= r) {       return tree[v][mask];     }     long long tm = (tl + tr) >> 1;     return max(getmax(v * 2 + 1, tl, tm, l, r, mask),                getmax(v * 2 + 2, tm + 1, tr, l, r, mask));   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     for (long long i = 0; i < (MAXN << 2); ++i) {       for (long long mask = 0; mask < (1 << K); ++mask) {         tree[i][mask] = -INF;       }     }     cin >> n >> k;     for (long long i = 0; i < n; ++i) {       upd(0, 0, n - 1, i);     }     cin >> q;     for (long long i = 0; i < q; ++i) {       long long t;       cin >> t;       if (t == 1) {         long long p;         cin >> p;         --p;         upd(0, 0, n - 1, p);       } else {         long long l, r;         cin >> l >> r;         --l;         --r;         long long ans = -INF;         for (long long mask = 0; mask < (1 << (k - 1)); ++mask) {           ans = max(ans, getmax(0, 0, n - 1, l, r, mask) +                              getmax(0, 0, n - 1, l, r, mask ^ ((1 << k) - 1)));         }         cout << ans <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool cmp(pair<long long, long long> a, pair<long long, long long> b) {     return (a.first - a.second) > (b.first - b.second);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<long long> hand(n);     vector<long long> pile(n);     unordered_map<long long, long long> hand_map(n);     unordered_map<long long, long long> pile_map(n);     for (long long i = 0; i < n; i++) {       cin >> hand[i];       hand_map[hand[i]]++;     }     for (long long i = 0; i < n; i++) {       cin >> pile[i];       pile_map[pile[i]]++;     }     long long flag = 0;     long long last = pile[n - 1];     long long index;     for (long long i = n - 1; i > 0; i--) {       if (pile[i] == 1) {         index = i;         break;       }       if (pile[i] - 1 == pile[i - 1] && pile[i] != 0) {       } else {         flag = 1;         break;       }     }     long long shift = 0;     if (flag == 1) {       for (long long i = 0; i < n; i++) {         if (pile[i] != 0) shift = max(shift, i + 2 - pile[i]);       }       cout << shift + n;     } else {       long long last = pile[n - 1];       for (long long i = 0; i < n; i++) {         if (pile[i] == 1) {           break;         }         if (pile[i] != 0) {           shift = max(shift, i + 2 - (pile[i] - pile[n - 1]));         }       }       if (shift == 0) {         cout << shift + n - pile[n - 1];       } else {         cout << index + 1 + n;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     if (0) return;     cout << name <<   :   << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     if (0) return;     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   void get_it_done() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   const long long N = 1e5 + 5;   long long n, m, k, s;   vector<long long> adjList[N], goods[N], cost[N];   long long arr[N], dp[105][N];   bool visited[N];   int32_t main() {     get_it_done();     long long t = 1;     while (t--) {       cin >> n >> m >> k >> s;       for (long long i = 1; i < n + 1; ++i)         cin >> arr[i], goods[arr[i]].push_back(i);       for (long long i = 0; i < m; ++i) {         long long u, v;         cin >> u >> v;         adjList[u].push_back(v);         adjList[v].push_back(u);       }       if (s == 1) {         for (long long i = 0; i < n; ++i) {           cout << 0 <<    ;         }         cout <<   n ;         continue;       }       for (long long i = 1; i < k + 1; ++i) {         memset(visited, 0, sizeof(visited));         queue<long long> q;         for (auto x : goods[i]) {           visited[x] = 1;           q.push(x);         }         while (!q.empty()) {           long long node = q.front();           q.pop();           for (auto x : adjList[node]) {             if (!visited[x]) {               dp[i][x] = dp[i][node] + 1;               visited[x] = 1;               q.push(x);             }           }         }         for (long long j = 1; j < n + 1; ++j) {           cost[j].push_back(dp[i][j]);         }       }       for (long long i = 1; i < n + 1; ++i) {         sort(cost[i].begin(), cost[i].end());         long long ans = 0;         for (long long j = 0; j < s; ++j) {           ans += cost[i][j];         }         cout << ans <<    ;       }       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, x, dd[270000];   int main() {     cin >> n >> x;     int limit = (1 << n) - 1;     vector<int> Q;     Q.push_back(0);     for (int i = 1; i <= limit; i++) {       if (i == x) continue;       if (dd[i]) continue;       Q.push_back(i);       dd[(i ^ x)] = 1;     }     cout << Q.size() - 1 << endl;     for (int i = 1; i < Q.size(); i++) {       cout << (Q[i] ^ Q[i - 1]) <<    ;     }   }
#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     int r = n + n - 1;     int a = 0;     int b = 0;     int i;     for (i = 0; i < n; i++) {       scanf( %d , &a);       if (a >= b) {         r = r + (a - b);       } else {         r = r + (b - a);       }       b = a;     }     printf( %d , r);   }
#include <bits/stdc++.h>   using namespace std;   int n, m, p, len;   int pos[500020];   int Left[500020];   int Right[500020];   string str;   void Work() {     int t = p;     char op;     int rt;     for (int i = 0; i < m; i++) {       cin >> op;       if (op ==  L )         p = Left[p];       else if (op ==  R )         p = Right[p];       else {         rt = pos[p];         if (rt > p) swap(rt, p);         Right[Left[rt]] = Right[p];         Left[Right[p]] = Left[rt];         if (Right[p] > len)           p = Left[rt];         else           p = Right[p];       }     }     for (int i = 0; i <= len;) {       if (i) cout << str[i - 1];       i = Right[i];     }     cout << endl;   }   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     while (cin >> n >> m >> p) {       stack<int> s;       cin >> str;       len = str.size();       Right[0] = 1;       Left[1] = 0;       Right[len] = len + 1;       Left[len + 1] = len;       for (int i = 1; i <= len; i++) {         Right[i] = i + 1;         Left[i] = i - 1;         if (str[i - 1] ==  ( )           s.push(i);         else {           pos[i] = s.top();           pos[s.top()] = i;           s.pop();         }       }       Work();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   unsigned long long F(unsigned long long n) {     if (n < 10) return n;     unsigned long long nn = n;     int firstDig = 0;     while (n) {       firstDig = n % 10;       n /= 10;     }     return (nn / 10) + 9 - (firstDig > (nn % 10));   }   int main() {     unsigned long long x, y;     while (cin >> x >> y) cout << F(y) - F(x - 1) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   char p[2010], tp[2010];   char q[2010];   vector<int> Res;   void Rev(int L) {     int i;     Res.push_back(L);     for (i = n - L; i < n; i++) {       tp[n - 1 - i] = p[i];     }     for (i = 0; i < n - L; i++) {       tp[i + L] = p[i];     }     for (i = 0; i < n; i++) p[i] = tp[i];   }   int C[30];   int main() {     int i, j;     scanf( %d , &n);     scanf( %s , p);     scanf( %s , q);     for (i = 0; q[i]; i++) {       C[p[i] -  a ]++;       C[q[i] -  a ]--;     }     for (i = 0; i < 26; i++) {       if (C[i] != 0) {         printf( -1 n );         return 0;       }     }     for (i = 0; i < n - 1; i += 2) {       int p1 = -1, p2 = -1;       for (j = i; j < n; j++) {         if (p[j] == q[i]) p1 = j;       }       for (j = i; j < n; j++) {         if (p[j] == q[i + 1] && j != p1) p2 = j;       }       int L1 = i, L2, L3 = 1, L4, L5 = 1, L6;       if (p1 < p2) {         L2 = p1 - i;         L4 = p2 - p1 - 1;         L6 = n - p2 - 1;         Rev(L6);         Rev(L4 + L5);         Rev(L3);         Rev(L1 + L2 + L4 + L5 + L6);         Rev(L3 + L4 + L5 + L6);         Rev(L1);       } else {         L2 = p2 - i;         L4 = p1 - p2 - 1;         L6 = n - p1 - 1;         Rev(L6);         Rev(L5);         Rev(L3 + L4);         Rev(L1 + L2 + L6);         Rev(L3 + L4 + L5 + L6);         Rev(L1);       }     }     printf( %d n , Res.size());     for (i = 0; i < Res.size(); i++) printf( %d  , Res[i]);   }
#include <bits/stdc++.h>   using namespace std;   void strange(int t) {     while (t--) {       int n;       cin >> n;       vector<int> p(n);       for (int i = 0; i < n; i++) {         cin >> p[i];         p[i]--;       }       bool g = 1;       int MIN = n;       for (int i = 0; i < n; i++) {         if (p[i] < MIN)           MIN = p[i];         else if (p[i] == p[i - 1] + 1) {         } else {           g = false;           break;         }       }       cout << (g ?  Yes  :  No ) << endl;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int t;     cin >> t;     strange(t);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, a, t;     while (cin >> n >> m >> a) {       if (m < n) {         t = m;         m = n;         n = t;       }       if (a >= m) cout << 1 << endl;       if ((a < m) && (a >= n)) {         if (m % a == 0)           cout << m / a << endl;         else           cout << m / a + 1 << endl;       }       if ((a < n) && (m % a != 0) && (n % a != 0))         cout << (m / a + 1) * (n / a + 1) << endl;       if ((a < n) && (m % a != 0) && (n % a == 0))         cout << (m / a + 1) * (n / a) << endl;       if ((a < n) && (m % a == 0) && (n % a != 0))         cout << (m / a) * (n / a + 1) << endl;       if ((a < n) && (m % a == 0) && (n % a == 0))         cout << (m / a) * (n / a) << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int arr[1003];   int main() {     int n, x, ans, sum, t, ck;     cin >> t;     while (t--) {       cin >> n >> x;       for (int i = 1; i <= n; i++) cin >> arr[i];       sort(arr + 1, arr + n + 1);       ck = 0;       for (int i = 1; i <= n; i++) {         if (arr[i] == x) ck = 1;       }       sum = 0;       for (int i = 1; i <= n; i++) sum += (arr[i] - x);       if (arr[1] == arr[n] && arr[n] == x)         ans = 0;       else if (sum == 0 || ck)         ans = 1;       else         ans = 2;       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template <class T>   int ge(const vector<T>& v, T val) {     return distance(v.begin(), lower_bound(v.begin(), v.end(), val));   }   template <class T>   int gt(const vector<T>& v, T val) {     return distance(v.begin(), upper_bound(v.begin(), v.end(), val));   }   template <class T>   int le(const vector<T>& v, T val) {     auto itr = upper_bound(v.begin(), v.end(), val);     if (itr == v.begin()) return v.size();     return distance(v.begin(), --itr);   }   template <class T>   int lt(const vector<T>& v, T val) {     auto itr = lower_bound(v.begin(), v.end(), val);     if (itr == v.begin()) return v.size();     return distance(v.begin(), --itr);   }   template <class T>   struct BIT {     int size;     vector<T> data;     function<T(T, T)> f = [](T a, T b) { return a + b; };     BIT() {}     BIT(int n) { init(n); }     void init(int n) {       size = n;       data.assign(size + 1, 0);     }     T sum(int x) {       T res = 0;       while (x > 0) {         res = f(res, data[x]);         x -= (x & -x);       }       return res;     }     T sum(int lft, int rit) {       if (lft == 0) return sum(rit);       return sum(rit) - sum(lft - 1);     }     void add(int x, T y) {       while (x <= size) {         data[x] = f(data[x], y);         x += (x & -x);       }     }   };   int main(void) {     int n;     ll t;     cin >> n >> t;     vector<ll> a(n + 1, 0), sum(n + 1, 0), v;     for (int i = 1; i <= n; i++) {       cin >> a[i];       sum[i] = sum[i - 1] + a[i];     }     v.assign(sum.begin(), sum.end());     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     int vs = v.size();     BIT<int> bit(vs + 1);     ll ans = 0;     for (int l = n; l >= 0; l--) {       int i = lt(v, sum[l] + t);       if (i < vs) {         ans += bit.sum(i + 1);       }       i = ge(v, sum[l]);       bit.add(i + 1, 1);     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int NMAX = 2 * 1e5 + 2;   int t, n, valeurs[NMAX];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> t;     for (int test = 1; test <= t; test++) {       cin >> n;       if (n % 4 != 0) {         cout <<  NO n ;       } else {         int moitie = n / 2, somme = 0, cur = 0;         for (int i = 0; i < moitie; i++) {           valeurs[i] = 2 * (i + 1);           somme += valeurs[i];         }         for (int i = 0; i < moitie - 1; i++) {           valeurs[i + moitie] = 2 * i + 1;           cur += valeurs[i + moitie];         }         valeurs[n - 1] = somme - cur;         cout <<  YES n ;         for (int i = 0; i < n; i++) cout << valeurs[i] <<    ;         cout <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long n, k, m;   vector<pair<int, int> > v, u;   int a, b, c;   int main() {     cin >> n >> k >> m;     a = n;     for (int i = 0; i < n; i++) {       int w;       cin >> w;       if (v.size() > 0 && v.back().first == w) {         v.back().second++;         if (v.back().second == k) {           v.pop_back();           a -= k;         }       } else         v.push_back({w, 1});     }     u = v;     int b = a;     int x = 0, y = u.size() - 1;     while (x < y) {       if (u[x].first == u[y].first) {         if (u[x].second + u[y].second == k) {           x++;           y--;           b -= k;         } else {           if (u[x].second + u[y].second > k) b -= k;           break;         }       } else {         break;       }     }     long long r = m * b;     a -= b;     if (x == y) {       r = r % k;       if (r == 0) a = 0;     }     cout << a + r << endl;     return 0;   }
#include<iostream>   using namespace std;      int query(int a, int b) {    cout <<  ?   << a <<     << b << endl;    cout.flush();    int ret;    cin >> ret;    return ret;   }      int main() {    int n;    cin >> n;    int lt = 1, rt = n;    int sec = query(1, n);    while (rt - lt > 0) {     int mid;     if (rt + lt <= 2 * sec) {      mid = (rt + lt + 1) / 2;      if (mid == n) {       rt = n - 1;       break;      }      if (query(mid, n) == sec) lt = mid;      else rt = mid - 1;     }     if (rt+lt > 2*sec) {      mid = (rt + lt) / 2;      if (mid == 1) {       lt = 2;       break;      }      if (query(1, mid) == sec) rt = mid;      else lt = mid + 1;     }    }    cout <<  !   << lt << endl;    cout.flush();   }