malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   template <class X>   void temfunc(const X a) {     for (auto it = a.begin(); it != a.end(); it++) cout << *it <<    ;     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int t, l, r;     cin >> t;     while (t--) {       cin >> l >> r;       if (l == r)         cout << 0 <<   n ;       else {         int temp;         if (r & 1) {           int temp = r / 2;           if (temp >= l)             cout << temp <<   n ;           else {             cout << r % l <<   n ;           }         } else {           temp = r / 2;           if (temp >= l)             cout << temp - 1 <<   n ;           else             cout << r % l <<   n ;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     string s;     cin >> n >> k;     cin >> s;     int j = 0;     for (int i = 0; i < n - 1; i++) {       if (s.substr(0, i + 1) == s.substr(n - i - 1)) {         j = i + 1;       }     }     string a = s.substr(j);     cout << s;     for (int i = 1; i < k; i++) {       cout << a;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int ns = 0;     bool ok = true;     for (int i = 0; i < n; i++) {       int val;       cin >> val;       string type;       cin >> type;       if (type ==  South ) {         if (ns + val > 20000)           ok &= false;         else           ns += val;       } else if (type ==  North ) {         if (ns - val < 0)           ok &= false;         else           ns -= val;       } else {         if (ns == 20000 || ns == 0) ok &= false;       }     }     ok &= ns == 0;     cout << (ok ?  YES n  :  NO n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   const int mod = 1e9 + 7;   int n, q;   long long a[N], w[N], c[N << 1][2];   inline void add(int x, long long v, int id) {     if (!id)       for (; x <= n; x += x & -x) c[x][id] += v;     else       for (; x <= n; x += x & -x) c[x][id] = (c[x][id] + v + mod) % mod;   }   inline long long ask(int x, int id) {     long long ret = 0;     if (!id)       for (; x; x -= x & -x) ret += c[x][id];     else       for (; x; x -= x & -x) ret = (ret + c[x][id] + mod) % mod;     return ret;   }   inline long long gets(int x, int y, int id) {     if (!id)       return y >= x ? ask(y, id) - ask(x - 1, id) : 0;     else       return (ask(y, id) - ask(x - 1, id) + mod) % mod;   }   inline int bsearch(int x, int y) {     int l = x, r = y, mid;     while (l < r) {       mid = (l + r) / 2;       if (gets(x, mid, 0) >= gets(mid + 1, y, 0))         r = mid;       else         l = mid + 1;     }     return r;   }   int main() {     scanf( %d%d , &n, &q);     for (register int i = 1; i <= n; i++) scanf( %lld , &a[i]);     for (register int i = 1; i <= n; i++)       scanf( %lld , &w[i]), add(i, w[i], 0), add(i, w[i] * (a[i] - i), 1);     while (q--) {       int x, y;       scanf( %d%d , &x, &y);       if (x < 0) {         x = -x;         add(x, -w[x], 0);         add(x, -w[x] * (a[x] - x), 1);         w[x] = y;         add(x, w[x], 0);         add(x, w[x] * (a[x] - x), 1);       } else {         if (x == y) {           puts( 0 );           continue;         }         long long pos = bsearch(x, y), ans = 0;         ans = (ans + (gets(x, pos, 0) % mod) * (int)abs(a[pos] - pos) % mod -                gets(x, pos, 1) + mod) %               mod;         ans = (ans - (gets(pos, y, 0) % mod) * (int)abs(a[pos] - pos) % mod +                gets(pos, y, 1) + mod) %               mod;         printf( %lld n , ans);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int n, k, a[N];   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + 1 + n);     int p = n / 2 + 1, t = n / 2 + 1;     for (int i = n / 2 + 2; i <= n; i++)       if (1ll * (a[i] - a[i - 1]) * (i - t) > k)         break;       else {         k -= 1ll * (a[i] - a[i - 1]) * (i - t);         p = i;       }     printf( %d n , a[p] + k / (p - t + 1));   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 5, maxm = 100010;   int n, m, h[maxm][maxn], v[maxm][maxn];   long long f[maxm][1 << maxn];   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m - 1; i++)       for (int j = 0; j < n; j++) scanf( %d , &h[i][j]);     for (int i = 0; i < m; i++)       for (int j = 0; j < n; j++) scanf( %d , &v[i][j]);     for (int i = 1; i < 1 << n; i++) f[0][i] = 1LL << 60LL;     f[0][0] = 0;     for (int i = 1; i < m; i++)       for (int opt = 0; opt < 1 << n; opt++) {         f[i][opt] = 1LL << 60LL;         long long s = 0;         for (int j = 0; j < n; j++)           s += ((opt & (1 << ((j + 1) % n))) > 0 != (opt & (1 << j)) > 0) *                v[i][j];         for (int opt0 = 0; opt0 < 1 << n; opt0++) {           long long ss = 0;           for (int j = 0; j < n; j++)             ss += ((opt & (1 << j)) != (opt0 & (1 << j))) * h[i - 1][j];           f[i][opt] = min(f[i][opt], f[i - 1][opt0] + s + ss);         }       }     printf( %I64d n , f[m - 1][(1 << n) - 1]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void dfs(int node, int par, int gp, int p, vector<int> adj[], vector<int> &src,            vector<int> &dest, vector<int> &ans) {     if (gp % 2 != 0) {       src[node - 1] = !src[node - 1];     }     if (src[node - 1] != dest[node - 1]) {       ans.push_back(node);       int temp = p;       p = gp + 1;       gp = temp;     } else {       int temp = p;       p = gp;       gp = temp;     }     for (int i = 0; i < adj[node].size(); i++) {       if (adj[node][i] != par) {         dfs(adj[node][i], node, gp, p, adj, src, dest, ans);       }     }   }   int main() {     int n;     cin >> n;     vector<int> adj[n + 1];     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     vector<int> src(n);     vector<int> dest(n);     for (int i = 0; i < n; i++) cin >> src[i];     for (int i = 0; i < n; i++) cin >> dest[i];     vector<int> ans;     dfs(1, -1, 0, 0, adj, src, dest, ans);     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200010;   int n, m;   int a[105][105];   int ans, row[105], col[105];   void caluRow() {     for (int i = 1; i <= n; i++) {       int mi = 501;       for (int j = 1; j <= m; j++) mi = min(mi, a[i][j]);       if (mi > 0) {         ans += mi;         row[i] += mi;         for (int j = 1; j <= m; j++) a[i][j] -= mi;       }     }   }   void caluCol() {     for (int i = 1; i <= m; i++) {       int mi = 501;       for (int j = 1; j <= n; j++) mi = min(mi, a[j][i]);       if (mi > 0) {         ans += mi;         col[i] += mi;         for (int j = 1; j <= n; j++) a[j][i] -= mi;       }     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]);     if (m > n) {       caluRow();       caluCol();     } else {       caluCol();       caluRow();     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j]) {           printf( -1 );           return 0;         }     printf( %d n , ans);     for (int i = 1; i <= n; i++)       while (row[i]--) printf( row %d n , i);     for (int i = 1; i <= m; i++)       while (col[i]--) printf( col %d n , i);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[4 * 100005];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) scanf( %lld , &a[i]);     sort(a, a + n);     int i = 0, j = n - 1;     long long ans = 0;     while (i < j) {       ans += (a[i] + a[j]) * (a[i] + a[j]);       i++;       j--;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> g[5005];   int h[5005][5005], siz[5005];   int f[5005][5005], fa;   int n, m, a[5005], b[5005], k;   inline int min(const int x, const int y) { return x > y ? y : x; }   void dfs(int x) {     int to;     siz[x] = 1;     h[x][1] = b[x];     f[x][0] = 0, f[x][1] = a[x];     for (int i = g[x].size() - 1; i >= 0; i--) {       to = g[x][i];       dfs(to);       for (int j = siz[x]; j >= 0; j--) {         for (int u = 1; u <= siz[to]; u++) {           if (f[to][u] > k && h[to][u] > k) break;           if (f[to][u] <= k) {             h[x][j + u] = min(h[x][j + u], f[to][u] + h[x][j]);             f[x][j + u] = min(f[x][j + u], f[to][u] + f[x][j]);           }           if (h[to][u] <= k) h[x][j + u] = min(h[x][j + u], h[to][u] + h[x][j]);         }       }       siz[x] += siz[to];     }   }   int main() {     memset(h, 0x3f, sizeof(h));     memset(f, 0x3f, sizeof(f));     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %d%d , a + i, b + i);       b[i] = a[i] - b[i];       if (i > 1) {         scanf( %d , &fa);         g[fa].push_back(i);       }     }     dfs(1);     for (int i = n; i >= 0; i--)       if (h[1][i] <= k || f[1][i] <= k) {         printf( %d n , i);         return 0;       }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Result {     int u, v, w, z;   };   int N, M;   vector<vector<int>> edges, distances;   vector<vector<pair<int, int>>> D, R;   vector<int> enqueued;   int best = -1;   Result result;   int scan() {     auto c = getchar();     while (c <  0  || c >  9 ) c = getchar();     int n;     for (n = 0;  0  <= c && c <=  9 ; c = getchar()) n = n * 10 + c -  0 ;     return n;   }   void bfs(int start) {     queue<pair<int, int>> q;     q.push({start, 0});     enqueued[start] = start;     while (!q.empty()) {       auto &p = q.front();       q.pop();       D[start].push_back(p);       R[p.first].push_back({start, p.second});       distances[start][p.first] = p.second;       for (auto &v : edges[p.first]) {         if (enqueued[v] != start) {           q.push({v, p.second + 1});           enqueued[v] = start;         }       }     }   }   inline bool cmp(const pair<int, int> &a, const pair<int, int> &b) {     return a.second > b.second;   }   pair<int, int> find(const vector<pair<int, int>> &X, int a, int b, int c) {     for (auto &x : X) {       if (x.first != a && x.first != b && x.first != c) return x;     }     return {-1, -1};   }   void process() {     for (auto i = 0; i < N; i++) {       for (auto j = 0; j < N; j++) {         if (i == j || distances[i][j] == 0) continue;         do {           auto k = find(R[i], i, j, -1);           if (k.first == -1) break;           auto l = find(D[j], i, j, k.first);           if (l.first == -1) break;           auto value = k.second + l.second + distances[i][j];           if (value > best) {             best = value;             result = {k.first, i, j, l.first};           }         } while (false);         do {           auto l = find(D[j], i, j, -1);           if (l.first == -1) break;           auto k = find(R[i], i, j, l.first);           if (k.first == -1) break;           auto value = k.second + l.second + distances[i][j];           if (value > best) {             best = value;             result = {k.first, i, j, l.first};           }         } while (false);       }     }   }   int main() {     N = scan();     M = scan();     edges.resize(N);     distances.resize(N);     D.resize(N);     R.resize(N);     enqueued.resize(N);     fill(enqueued.begin(), enqueued.end(), -1);     for (auto i = 0; i < M; i++) {       auto x = scan() - 1;       auto y = scan() - 1;       edges[x].push_back(y);     }     for (auto i = 0; i < N; i++) {       vector<int> &v = edges[i];       sort(v.begin(), v.end());       v.resize(distance(v.begin(), unique(v.begin(), v.end())));       distances[i].resize(N);     }     for (auto i = 0; i < N; i++) bfs(i);     for (auto i = 0; i < N; i++) {       sort(D[i].begin(), D[i].end(), cmp);       sort(R[i].begin(), R[i].end(), cmp);     }     process();     printf( %d %d %d %d n , result.u + 1, result.v + 1, result.w + 1,            result.z + 1);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n, T, a, b;   int diff[N];   pair<int, int> prob[N];   void solve() {     scanf( %d %d %d %d , &n, &T, &a, &b);     int allA = 0, allB = 0;     for (int i = 0; i < n; ++i) {       scanf( %d , &diff[i]);       if (diff[i])         ++allB;       else         ++allA;     }     for (int i = 0; i < n; ++i) {       scanf( %d , &prob[i].first);       prob[i].second = i;     }     if (1LL * allA * a + 1LL * allB * b <= T) {       printf( %d n , n);       return;     }     sort(prob, prob + n);     int ans = 0;     int mustA = 0, mustB = 0;     for (int i = 0; i < n;) {       int dead = prob[i].first;       long long taken = 1LL * mustA * a + 1LL * mustB * b;       if (taken < dead) {         int tmp = mustA + mustB;         int left = dead - taken - 1;         int moreA = min(left / a, allA - mustA);         tmp += moreA;         left -= moreA * a;         int moreB = min(left / b, allB - mustB);         tmp += moreB;         left -= moreB * b;         ans = max(ans, tmp);       }       int j = i;       while (j < n && prob[j].first == dead) {         if (diff[prob[j].second])           ++mustB;         else           ++mustA;         ++j;       }       i = j;     }     printf( %d n , ans);   }   int main() {     int t;     scanf( %d , &t);     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   vector<int> G[20000];   int fa[20000], c[20000], ans = 0;   void dfs(int x, int f, int col, int ori) {     if (col != ori) ans++;     for (int i = 0; i < G[x].size(); i++) {       int u = G[x][i];       if (u == f) continue;       dfs(u, x, c[u], col);     }   }   int main() {     std::ios::sync_with_stdio(0), cin.tie(0);     int n, x;     cin >> n;     for (int i = 1; i <= n; i++) G[i].clear();     for (int i = 1; i < n; i++) {       cin >> x;       G[i + 1].push_back(x);       G[x].push_back(i + 1);     }     for (int i = 1; i <= n; i++) cin >> c[i];     dfs(1, -1, c[1], 0);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s;   map<int, int> dp;   int k;   int n;   int dp1[1001][1001][2];   int go(int t) {     if (t == 1) return 0;     int cnt = 0;     while (t) {       cnt += (t % 2);       t /= 2;     }     return go(cnt) + 1;   }   int solve(int i, int pre, int q) {     if (i >= n) {       return dp[pre] == k;     }     if (dp1[i][pre][q] != -1) return dp1[i][pre][q];     if (q) {       return dp1[i][pre][1] =                  ((solve(i + 1, pre + 1, 1) % (long long)1000000007) +                   (solve(i + 1, pre, 1) % (long long)1000000007)) %                  (long long)1000000007;     }     if (s[i] ==  1 ) {       return dp1[i][pre][0] =                  ((solve(i + 1, pre + 1, 0) % (long long)1000000007) +                   (solve(i + 1, pre, 1) % (long long)1000000007)) %                  (long long)1000000007;     }     return dp1[i][pre][0] = solve(i + 1, pre, 0) % (long long)1000000007;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> s >> k;     n = s.size();     memset(dp1, -1, sizeof(dp1));     dp[1] = 1;     dp[0] = 0;     for (int i = 2; i <= 1000; i++) {       dp[i] = go(i) + 1;     }     cout << solve(0, 0, 0) - (k == 1 ? 1 : 0);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, a[1000005];   std::vector<pair<double, pair<long long, long long> > > v;   signed main() {     scanf( %lld , &n);     for (long long i = 1; i <= n; i++) {       scanf( %lld , &a[i]);     }     for (long long i = 1; i <= n; i++) {       v.push_back(make_pair(1.0 * a[i], make_pair(i, i)));       while (v.size() > 1 && v[v.size() - 1].first <= v[v.size() - 2].first) {         double val1 = v[v.size() - 1].first;         double val2 = v[v.size() - 2].first;         pair<long long, long long> interval1 = v[v.size() - 1].second;         pair<long long, long long> interval2 = v[v.size() - 2].second;         v.pop_back();         v.pop_back();         v.push_back(make_pair((val1 * (interval1.second - interval1.first + 1) +                                val2 * (interval2.second - interval2.first + 1)) /                                   (interval1.second - interval2.first + 1),                               make_pair(interval2.first, interval1.second)));       }     }     for (long long i = 0; i < v.size(); i++) {       pair<long long, long long> interval = v[i].second;       for (long long j = interval.first; j <= interval.second; j++) {         printf( %.10lf n , v[i].first);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<char, int> mp;   char t[27];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, mx = 0, ans = 0, f, f1;     string ch;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> ch;       t[ch[0] -  a ]++;     }     for (int i = 0; i < 27; i++) {       mx = t[i];       f = mx / 2;       if (mx % 2 == 0)         f1 = mx / 2;       else         f1 = mx / 2 + 1;       ans += (f1 * (f1 - 1) / 2) + (f * (f - 1) / 2);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3000 + 5;   struct node {     int val, id;   } a[N];   int n;   int dp[N * N], tree[N], ranks[N];   inline int lowbit(int x) { return x & -x; }   inline int query(int x) {     int sum = 0;     while (x != 0) {       sum += tree[x];       x -= lowbit(x);     }     return sum;   }   inline void update(int x, int v) {     while (x <= n) {       tree[x] += v;       x += lowbit(x);     }     return;   }   bool cmp(node a, node b) {     if (a.val == b.val) return a.id < b.id;     return a.val < b.val;   }   signed main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i].val;       a[i].id = i;     }     sort(a + 1, a + n + 1, cmp);     for (int i = 1; i <= n; i++) {       ranks[a[i].id] = i;     }     int ans = 0;     for (int i = 1; i <= n; i++) {       ans += (i - 1) - query(ranks[i]);       update(ranks[i], 1);     }     printf( %.6lf , (double)(2 * ans - (ans & 1)));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, M;   vector<vector<char>> v;   bool Mastervis[50][50];   bool vis[50][50];   int dx[] = {0, 0, 1, -1};   int dy[] = {1, -1, 0, 0};   bool isCyc(int r, int c, int pr, int pc) {     if (vis[r][c]) return true;     vis[r][c] = true;     Mastervis[r][c] = true;     int nr, nc;     for (int k = 0; k < 4; k++) {       nr = r + dy[k];       nc = c + dx[k];       if (nr >= 0 && nr < N && nc >= 0 && nc < M && !(nr == pr && nc == pc)) {         if (v[r][c] == v[nr][nc]) {           if (isCyc(nr, nc, r, c)) return true;         }       }     }     return false;   }   int main() {     string s;     cin >> N >> M;     v.resize(N);     memset(Mastervis, false, 50 * 50 * sizeof(bool));     for (int i = 0; i < N; i++) {       cin >> s;       for (int j = 0; j < M; j++) {         v[i].push_back(s[j]);       }     }     for (int i = 0; i < N; i++) {       for (int j = 0; j < M; j++) {         if (!Mastervis[i][j]) {           memset(vis, false, 50 * 50 * sizeof(bool));           if (isCyc(i, j, -2, -2)) {             cout <<  Yes ;             return 0;           }         }       }     }     cout <<  No ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long N;       cin >> N;       vector<long long> a(N);       for (auto &x : a) cin >> x;       ;       long long l = -1;       for (int i = 0; i < N - 1; i++) {         if (abs(a[i + 1] - a[i]) >= 2) {           l = i;           break;         }       }       if (l == -1)         cout <<  No  << endl;       else         cout <<  Yes n  << l + 1 <<     << l + 2 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1 << 21;   int n, m;   char s[maxn];   char cmd[maxn];   struct lucky {     int c4, c7, c47, c74;     lucky() { c4 = 0, c7 = 0, c47 = 0, c74 = 0; }     void merge(lucky a, lucky b) {       c4 = a.c4 + b.c4;       c7 = a.c7 + b.c7;       c47 = max(a.c47 + b.c7, a.c4 + b.c47);       c74 = max(a.c74 + b.c4, a.c7 + b.c74);     }     void reset(char c) {       if (c ==  7 )         c7 = 1;       else         c4 = 1;       c47 = 1;       c74 = 1;     }     void chng() {       swap(c4, c7);       swap(c47, c74);     }     void recieve(bool bb) {       if (bb) chng();     }   };   lucky seg[maxn];   bool lazy[maxn];   lucky build(int rt, int lp, int rp) {     if (lp + 1 == rp) {       seg[rt].reset(s[lp]);     } else {       int mid = (lp + rp) / 2;       seg[rt].merge(build(rt * 2, lp, mid), build(rt * 2 + 1, mid, rp));     }     return seg[rt];   }   int qa, qb;   lucky update(int rt, int lp, int rp) {     if (lp >= qb || rp <= qa) return seg[rt];     if (qa <= lp && rp <= qb) {       lazy[rt] ^= true;       seg[rt].chng();     } else {       int mid = (lp + rp) / 2;       seg[rt].merge(update(rt * 2, lp, mid), update(rt * 2 + 1, mid, rp));       seg[rt].recieve(lazy[rt]);     }     return seg[rt];   }   int main() {     scanf( %d%d%s , &n, &m, s);     build(1, 0, n);     for (int i = 0; i < m; i++) {       scanf( %s , cmd);       if (cmd[0] ==  c ) {         printf( %d n , seg[1].c47);       } else {         scanf( %d%d , &qa, &qb);         qa--;         update(1, 0, n);       }     }   }
#include <bits/stdc++.h>   using namespace std;   int n;   char a[5005][5005];   bool vst[5005];   bool dfs(int s, int k) {     if (vst[k]) return false;     vst[k] = true;     for (int i = 0; i < n; ++i) {       if (a[k][i] ==  1 ) {         if (a[i][s] ==  1 )           return printf( %d %d %d n , s + 1, k + 1, i + 1), true;         if (dfs(k, i)) return true;       }     }     return false;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %s , a[i]);     int i;     for (i = 0; i < n; ++i) {       if (dfs(i, i)) break;     }     if (i == n) puts( -1 );   }