malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int M = 2e5 + 10;   struct Edge {     int u, v, cap, flow = 0;     Edge(int u, int v, int cap) : u(u), v(v), cap(cap) {}   };   class Dinic {    public:     vector<Edge> edges;     vector<vector<int>> G;     int n, m = 0, s, t;     vector<int> H, ptr;     queue<int> q;     Dinic(int n, int s, int t) : n(n), s(s), t(t) {       G.resize(n + 1);       H = ptr = vector<int>(n + 1);     }     void add(int u, int v, int cap) {       edges.emplace_back(u, v, cap);       edges.emplace_back(v, u, 0);       G[u].push_back(m++);       G[v].push_back(m++);     }     bool bfs() {       while (!q.empty()) {         int u = q.front();         q.pop();         for (int v : G[u]) {           int to = edges[v].v;           int gap = edges[v].cap - edges[v].flow;           if (H[to] != -1 or gap < 1) continue;           H[to] = H[u] + 1, q.push(to);         }       }       return H[t] != -1;     }     int dfs(int u, int X) {       if (!X) return 0;       if (u == t) return X;       for (int &i = ptr[u]; i < (int)(G[u]).size(); ++i) {         int v = G[u][i], to = edges[v].v;         int gap = edges[v].cap - edges[v].flow;         if (H[to] != (H[u] + 1) or gap < 1) continue;         int Y = dfs(to, min(X, gap));         if (!Y) continue;         edges[v].flow += Y;         edges[v ^ 1].flow -= Y;         return Y;       }       return 0;     }     int flow() {       int ans = 0;       while (true) {         fill(H.begin(), H.end(), -1);         H[s] = 0, q.push(s);         if (!bfs()) break;         fill(ptr.begin(), ptr.end(), 0);         while (int X = dfs(s, (int)(1e18))) ans += X;       }       return ans;     }   };   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<pair<int, pair<int, int>>> edges;     vector<int> cand;     for (int i = 0; i < m; ++i) {       int u, v, w;       cin >> u >> v >> w;       edges.push_back({w, {u, v}});       cand.push_back(w);     }     sort((cand).begin(), (cand).end());     cand.erase(unique((cand).begin(), (cand).end()), cand.end());     int lo = 0, hi = (int)(cand).size() - 1, mid, ans = -1;     int s = 0, t = 2 * n + 1;     while (lo <= hi) {       mid = (lo + hi) >> 1;       Dinic dinic(2 * n + 2, s, t);       for (int i = 1; i <= n; ++i) {         dinic.add(s, i, 1);         dinic.add(i + n, t, 1);       }       for (auto [w, p] : edges)         if (w <= cand[mid]) dinic.add(p.first, p.second + n, 1);       int x = dinic.flow();       if (x == n)         hi = mid - 1, ans = cand[mid];       else         lo = mid + 1;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       int arr[n + 1];       for (int i = 1; i <= n; i++) {         cin >> arr[i];       }       int peaks = 0, ind = 1;       for (int i = 2; i < k; i++) {         if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) peaks++;       }       int ans = peaks;       for (int j = 2; j + k - 1 <= n; j++) {         if (arr[j] > arr[j - 1] && arr[j] > arr[j + 1] && ans > 0) ans--;         if (arr[j + k - 2] > arr[j + k - 1] && arr[j + k - 2] > arr[j + k - 3])           ans++;         if (ans > peaks) {           peaks = ans;           ind = j;         }       }       cout << peaks + 1 <<     << ind << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<int> a[n];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int x;         cin >> x;         x--;         a[i].push_back(x);       }     }     int ans = 0;     for (int j = 0; j < m; j++) {       vector<int> cnt(n);       int c = 0;       for (int i = 0; i < n; i++) {         if (a[i][j] % m != j || a[i][j] / m > n)           c++;         else {           if (i >= a[i][j] / m)             cnt[i - a[i][j] / m]++;           else             cnt[i + n - a[i][j] / m]++;         }       }       int mn = 1e9;       for (int i = 0; i < n; i++) {         mn = min(mn, n - cnt[i] + i);       }       ans += mn;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int sol = (int)1e9 + 7;   int op, lim;   int ask(int r, int c) {     op++;     if (op == lim) {       cout <<  !   << sol << endl;       exit(0);     }     cout <<  ?   << r <<     << c << endl;     int x;     cin >> x;     sol = min(sol, x);     return x;   }   int go = 1;   void solve(int r1, int c1, int r2, int c2) {     if (r1 > r2 || c1 > c2) {       return;     }     if (r1 == r2 && c1 == c2) {       ask(r1, c1);       return;     }     int rm = (r1 + r2) / 2;     vector<int> e;     for (int j = max(go, c1); j <= c2; j++) {       e.push_back(ask(rm, j));     }     int mn = e[0];     for (auto &x : e) mn = min(mn, x);     for (int j = c1; j <= c2; j++)       if (e[j - c1] == mn) {         solve(r1, c1, rm - 1, j);         go = max(go, j);         solve(rm + 1, j, r2, c2);         return;       }   }   int main() {     int n, m;     cin >> n >> m;     lim = 4 * (n + m);     solve(1, 1, n, m);     cout <<  !   << sol << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main(int argc, char *argv[]) {     map<int, int> set;     int n, a, b, check1, check2, temp;     cin >> n >> a >> b;     int arr[n];     vector<int> check;     for (int i = int(0); i <= int(n - 1); i++) {       cin >> arr[i];       set[arr[i]] = -1;     }     if (a == b) {       for (int i = int(0); i <= int(n - 1); i++) {         if (set[a - arr[i]] == 0) {           cout <<  NO  << endl;           return 0;         }       }       cout <<  YES  << endl;       for (int i = int(0); i <= int(n - 1); i++) {         cout <<  0  ;       }       return 0;     }     for (int i = int(0); i <= int(n - 1); i++) {       if (set[arr[i]] != -1) continue;       check.clear();       check.push_back(arr[i]);       check1 = a - arr[i];       check2 = b - arr[i];       bool f = false;       while (!f) {         if (set[check1] == -1 && set[check2] == -1) {           check.push_back(check1);           check.push_back(check2);           temp = check1;           check1 = a - check2;           check2 = b - temp;         } else if (set[check1] == -1) {           check.push_back(check1);           f = 1;           for (int i = int(0); i <= int(check.size() - 1); i++) {             set[check.at(i)] = 1;           }         } else if (set[check2] == -1) {           check.push_back(check2);           f = 1;           for (int i = int(0); i <= int(check.size() - 1); i++) {             set[check.at(i)] = 2;           }         } else {           cout <<  NO  << endl;           return 0;         }       }     }     cout <<  YES  << endl;     for (int i = int(0); i <= int(n - 1); i++) {       cout << set[arr[i]] - 1 <<    ;     }     return EXIT_SUCCESS;   }
#include <bits/stdc++.h>   using namespace std;   int N, M;   int _next[10000000];   struct event {     int l, r, x;     event() {}     event(int l, int r, int x) : l(l), r(r), x(x) {}   };   vector<event> e[7005];   inline bool cross(int l1, int r1, int l2, int r2) {     if (l1 > r2) return false;     if (r1 < l2) return false;     return true;   }   int main() {     for (int i = 1; i < (1 << 21) - 1; i++) {       _next[i] = 1;     }     for (int i = 0; i < 21; i++) {       _next[1 << i]++;     }     for (int i = 2; i < (1 << 21) - 1; i++) {       _next[i] += _next[i - 1];     }     cin >> N;     cin >> M;     int t, v;     int n, l, r, x;     for (int i = 0; i < M; i++) {       cin >> t;       if (t == 1) {         cin >> n >> l >> r >> x;         e[n].push_back(event(l, r, x));       } else {         set<int> S;         cin >> n >> v;         l = v - 1, r = v;         for (int i = n; i <= N; i++) {           for (int j = 0; j < e[i].size(); j++) {             if (cross(l + 1, r, e[i][j].l, e[i][j].r)) S.insert(e[i][j].x);           }           l = _next[l], r = _next[r];         }         cout << S.size() << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k;   long long count(long long num) {     long long ans = 0, sum = num, cnt = 0, hest = 0;     while (sum) {       hest = sum % 10;       sum /= 10;       cnt++;     }     cnt--;     long long k = 1;     for (long long i = 1; i <= cnt; i++) {       ans += k;       k *= 10;     }     if (hest > 1) {       ans += k;     } else {       ans += num - k + 1;     }     return ans;   }   double pro(long long l, long long r) {     double ans = 0;     ans = ((double)(count(r) - count(l - 1))) / ((double)(r - l + 1));     return ans;   }   long long l, r;   double p[1005];   double dp[1005][1005];   int main() {     scanf( %lld , &n);     for (long long i = 1; i <= n; i++) {       scanf( %lld%lld , &l, &r);       p[i] = pro(l, r);     }     scanf( %lld , &k);     dp[0][0] = 1;     for (long long j = 1; j <= n; j++) {       dp[0][j] = dp[0][j - 1] * (1.0 - p[j]);     }     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= n; j++) {         dp[i][j] = dp[i - 1][j - 1] * p[j] + dp[i][j - 1] * (1 - p[j]);       }     }     double ans = 0;     for (long long i = (long long)(n * k / 100.0 + 0.999); i <= n; i++) {       ans += dp[i][n];     }     printf( %.10lf , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     while (t--) {       int n, m, k;       cin >> n >> m >> k;       vector<long long> a(n + 1), pref_a(n + 1);       for (int i = 1; i <= n; i++) {         cin >> a[i];         pref_a[i] = pref_a[i - 1] + a[i];       }       vector<long long> b(m + 1), pref_b(m + 1);       for (int i = 1; i <= m; i++) {         cin >> b[i];         pref_b[i] = pref_b[i - 1] + b[i];       }       vector<int> divs;       for (int i = 1; i * 1ll * i <= k; i++) {         if (k % i == 0) {           divs.push_back(i);           if (k / i != i) {             divs.push_back(k / i);           }         }       }       long long ans = 0;       for (int x : divs) {         int y = k / x;         int lin = 0, col = 0;         for (int i = x; i <= n; i++) {           if (pref_a[i] - pref_a[i - x] == x) {             lin++;           }         }         for (int i = y; i <= m; i++) {           if (pref_b[i] - pref_b[i - y] == y) {             col++;           }         }         ans += lin * col;       }       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       vector<string> G(n);       for (int i = 0; i < n; i++) {         cin >> G[i];       }       vector<vector<pair<int, int>>> arr(27, vector<pair<int, int>>());       bool ret_flag = false;       bool empty_flag = true;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           if (G[i][j] ==  . ) continue;           empty_flag = false;           auto temp = G[i][j] -  a ;           if (arr[temp].size() == 1 &&               !(arr[temp][0].first == i || arr[temp][0].second == j)) {             ret_flag = true;             break;           } else if (arr[temp].size() > 1 &&                      !((arr[temp][0].first == i &&                         arr[temp][arr[temp].size() - 1].first == i) ||                        (arr[temp][0].second == j &&                         arr[temp][arr[temp].size() - 1].second == j))) {             ret_flag = true;             break;           }           arr[temp].push_back(make_pair(i, j));         }         if (ret_flag) break;       }       if (empty_flag) {         cout <<  YES  << endl;         cout << 0 << endl;         continue;       }       if (ret_flag) {         cout <<  NO  << endl;         continue;       }       int max_snake = 0;       for (int i = 25; i >= 0; i--) {         if (arr[i].size() == 0) continue;         max_snake = max(max_snake, i);         if (arr[i][0].first == arr[i][arr[i].size() - 1].first) {           int r = arr[i][0].first;           for (int j = arr[i][0].second; j <= arr[i][arr[i].size() - 1].second;                j++) {             if (G[r][j] -  a  >= i) continue;             ret_flag = true;             break;           }           if (ret_flag) break;         } else if (arr[i][0].second == arr[i][arr[i].size() - 1].second) {           int c = arr[i][0].second;           for (int j = arr[i][0].first; j <= arr[i][arr[i].size() - 1].first;                j++) {             if (G[j][c] >=  a  + i) continue;             ret_flag = true;             break;           }           if (ret_flag) break;         }         if (ret_flag) break;       }       if (ret_flag) {         cout <<  NO  << endl;         continue;       }       cout <<  YES  << endl;       cout << max_snake + 1 << endl;       for (int i = 0; i <= 25; i++) {         int j = i;         if (arr[i].empty()) {           while (i < 26 && arr[i].empty()) i++;         }         if (i == 26) break;         cout << arr[i][0].first + 1 <<     << arr[i][0].second + 1 <<                  << arr[i].back().first + 1 <<     << arr[i].back().second + 1              << endl;         i = j;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string a, b;   int t;   string ssort(string x) {     if (x.size() & 1) return x;     int len = x.size() / 2;     string xx, yy;     xx = ssort(x.substr(0, len));     yy = ssort(x.substr(len, len));     if (xx < yy)       return xx + yy;     else       return yy + xx;   }   signed main() {     cin >> a >> b;     if (a == b) {       printf( YES n );       return 0;     }     if (ssort(a) == ssort(b))       printf( YES n );     else       printf( NO n );   }
#include <bits/stdc++.h>   using namespace std;   int T, n;   char s[50];   int a[50], b[50];   int main() {     scanf( %d , &T);     while (T--) {       int cnt = 0;       scanf( %d , &n);       scanf( %s , s + 1);       for (int i = 1; i <= n; ++i) a[i] = s[i] -  a , b[i] = a[i];       sort(a + 1, a + n + 1);       for (int i = 1; i <= n; ++i)         if (a[i] != b[i]) cnt++;       cout << cnt << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N_MAX = 1e5, V = (1 << 20);   int n, x[N_MAX], t[N_MAX];   struct edge {     int l, v;   };   vector<edge> G[N_MAX];   long long cnt_tree[V + 1], total_tree[V + 1];   void add(int i, long long a, long long tree[]) {     while (i <= V) {       tree[i] += a;       i += (i & -i);     }   }   long long sum(int i, long long tree[]) {     long long s = 0;     while (i) {       s += tree[i];       i -= (i & -i);     }     return s;   }   int ind(long long a, long long tree[]) {     int idx = 0, mask = V;     a--;     while (mask && idx < V) {       int t = idx + mask;       if (a >= tree[t]) {         idx = t;         a -= tree[t];       }       mask >>= 1;     }     return idx + 1;   }   long long T, sol[N_MAX];   void DFS(int v) {     add(t[v], x[v], cnt_tree);     add(t[v], (long long)x[v] * t[v], total_tree);     int i = ind(T, total_tree);     if (i > V)       sol[v] = sum(V, cnt_tree);     else       sol[v] = sum(i - 1, cnt_tree) + (T - sum(i - 1, total_tree)) / i;     long long m1 = -1, m2 = -1;     for (edge e : G[v]) {       if (T <= e.l) continue;       T -= e.l;       DFS(e.v);       T += e.l;       if (m1 < sol[e.v]) {         m2 = m1;         m1 = sol[e.v];       } else if (m2 < sol[e.v])         m2 = sol[e.v];     }     if (v == 0)       sol[v] = max(sol[v], m1);     else       sol[v] = max(sol[v], m2);     add(t[v], -x[v], cnt_tree);     add(t[v], -(long long)x[v] * t[v], total_tree);   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> T;     for (int i = 0; i < n; i++) cin >> x[i];     for (int i = 0; i < n; i++) cin >> t[i];     for (int i = 1; i <= n - 1; i++) {       int p;       edge e;       cin >> p >> e.l;       e.v = i;       e.l *= 2;       G[p - 1].push_back(e);     }     DFS(0);     cout << sol[0] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1100;   const int M = 256;   const int K = 26;   const int INF = 2000000000;   const double eps = 1e-12;   int k, m, n, d[K], a[N];   char s[N];   bool ok;   int F();   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     k = 1;     for (int i = 0; i < 26; i++) d[i] = k, k <<= 1;     for (int i = 0; i < n; i++) {       cin >> s;       m = 0;       for (int j = 0; s[j]; j++) m = m | d[s[j] -  a ];       a[i] = m;     }     sort(a, a + n);     m = 1;     for (int i = 1; i < n; i++)       if (a[i] != a[i - 1]) m++;     cout << m;     return 0;   }   int F() { return 0; }
#include <bits/stdc++.h>   using namespace std;   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   int main() {     long long test, n, a[55], b[55];     scanf( %lld , &test);     while (test--) {       scanf( %lld , &n);       long long mina = 1000000000;       long long minb = 1000000000;       for (long long i = 0; i < n; i++) {         scanf( %lld , &a[i]);         mina = min(a[i], mina);       }       for (long long i = 0; i < n; i++) {         scanf( %lld , &b[i]);         minb = min(b[i], minb);       }       long long ans = 0;       for (long long i = 0; i < n; i++) {         long long p = a[i] - mina;         long long q = b[i] - minb;         ans += min(p, q);         ans += (p - min(p, q)) + (q - min(p, q));       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[100001], p[100001];   int main() {     int min = 1000, sum = 0, n, i, j;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d %d , &a[i], &p[i]);       if (p[i] < min) {         min = p[i];       }       sum += a[i] * min;     }     printf( %d n , sum);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   const long long MOD = 1e9 + 7;   long long n, m, connected = 0;   long long col[N], vis[N];   vector<pair<long long, long long> > g[N];   long long pow(long long a, long long b, long long m) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans;   }   void dfs(long long k, long long c) {     if (vis[k]) {       if (col[k] != c) {         cout <<  0 ;         exit(0);       }       return;     }     vis[k] = 1;     col[k] = c;     for (auto &it : g[k]) {       dfs(it.first, c ^ (1 - it.second));     }   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> m;     for (long long i = 1; i <= m; i++) {       long long u, v, t;       cin >> u >> v >> t;       g[u].push_back({v, t});       g[v].push_back({u, t});     }     for (long long i = 1; i <= n; i++) {       if (!vis[i]) {         dfs(i, 0);         connected++;       }     }     long long ans = pow(2LL, connected - 1, MOD);     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 510000;   int n;   int sz[MAXN];   int was[MAXN];   int up[MAXN];   vector<int> eds[MAXN];   int ans[MAXN];   void dfs1(int v) {     sz[v] = 1;     was[v] = 1;     for (int u : eds[v]) {       if (!was[u]) {         dfs1(u);         sz[v] += sz[u];       }     }   }   void dfs2(int v) {     was[v] = 1;     up[v] = 0;     if (sz[v] * 2 <= n) up[v] = sz[v];     for (int u : eds[v]) {       if (!was[u]) {         dfs2(u);         up[v] = max(up[v], up[u]);       }     }   }   void dfs3(int v, int now) {     ans[v] = 1;     was[v] = 1;     if ((n - sz[v]) * 2 <= n) now = max(now, n - sz[v]);     if ((n - sz[v] - now) * 2 > n) ans[v] = 0;     int mx1 = 0;     int mx2 = 0;     for (int u : eds[v]) {       if (!was[u]) {         mx2 = max(mx2, up[u]);         if (mx2 > mx1) swap(mx1, mx2);       }     }     for (int u : eds[v]) {       if (!was[u]) {         if ((sz[u] - up[u]) * 2 > n) ans[v] = 0;         int go = now;         if (up[u] == mx1)           go = max(now, mx2);         else           go = max(now, mx1);         dfs3(u, go);       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       int a, b;       scanf( %d%d , &a, &b);       --a;       --b;       eds[a].push_back(b);       eds[b].push_back(a);     }     dfs1(0);     memset(was, 0, sizeof(was));     dfs2(0);     memset(was, 0, sizeof(was));     dfs3(0, 0);     for (int i = 0; i < n; ++i) printf( %d  , ans[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   void print_row(int s, int *l, int size) {     int i = s + 1;     printf( %d , l[s]);     while (i < size) printf(  %d , l[i++]);     i = 0;     while (i < s) printf(  %d , l[i++]);     printf(  n );   }   int main() {     int n, k;     scanf( %d %d , &n, &k);     int l[n];     l[0] = k;     for (int i = 1; i < n; i++) l[i] = 0;     for (int i = 0; i < n; i++) print_row(i, l, n);     return 0;   }