malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 228;   vector<int> g[MAXN];   vector<bool> vis(MAXN);   int n, m;   int p[MAXN];   int col[MAXN], help[MAXN];   int t[4 * MAXN], lazy[4 * MAXN];   int tin[MAXN], tout[MAXN];   int timer;   void dfs(int v) {     vis[v] = 1;     tin[v] = timer++;     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       if (vis[to] == 0) {         dfs(to);       }     }     tout[v] = timer - 1;   }   void build(int v, int tl, int tr) {     if (tl == tr) {       t[v] = help[tl];       lazy[v] = -1;     } else {       int tm = (tl + tr) >> 1;       build(2 * v, tl, tm);       build(2 * v + 1, tm + 1, tr);       t[v] = t[2 * v] + t[2 * v + 1];       lazy[v] = -1;     }   }   void push(int v, int tl, int tr) {     if (lazy[v] != -1) {       if (lazy[v] % 2 == 1) {         t[v] = (tr - tl + 1) - t[v];       }       if (tl != tr) {         if (lazy[2 * v] == -1) {           lazy[2 * v] = lazy[v];         } else {           lazy[2 * v] += lazy[v];         }         if (lazy[2 * v + 1] == -1) {           lazy[2 * v + 1] = lazy[v];         } else {           lazy[2 * v + 1] += lazy[v];         }       }       lazy[v] = -1;     }   }   void update(int v, int tl, int tr, int l, int r, long long color) {     push(v, tl, tr);     if (l > r) {       return;     }     if (tl == l && tr == r) {       if (lazy[v] == -1) {         lazy[v] = color;       } else {         lazy[v] += color;       }       push(v, tl, tr);       return;     }     int tm = (tl + tr) >> 1;     update(2 * v, tl, tm, l, min(tm, r), color);     update(2 * v + 1, tm + 1, tr, max(tm + 1, l), r, color);     t[v] = t[2 * v] + t[2 * v + 1];   }   long long get(int v, int tl, int tr, int l, int r) {     push(v, tl, tr);     if (l > r) {       return 0;     }     if (tl == l && tr == r) {       return t[v];     }     int tm = (tl + tr) >> 1;     return get(2 * v, tl, tm, l, min(tm, r)) +            get(2 * v + 1, tm + 1, tr, max(tm + 1, l), r);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 1; i < n; i++) {       cin >> p[i];       p[i]--;       g[i].push_back(p[i]);       g[p[i]].push_back(i);     }     for (int i = 0; i < n; i++) {       cin >> col[i];     }     dfs(0);     for (int i = 0; i < n; i++) {       int start = tin[i];       help[start] = col[i];     }     build(1, 0, n - 1);     cin >> m;     for (int i = 0; i < m; i++) {       string a;       int x;       cin >> a >> x;       x--;       if (a ==  get ) {         cout << get(1, 0, n - 1, tin[x], tout[x]) << endl;       } else {         update(1, 0, n - 1, tin[x], tout[x], 1);       }     }     return 0;   }
#include <bits/stdc++.h>   int main() {     int num;     int count = 0, i;     int digitos[10];     scanf( %d , &num);     int aux = num;     for (i = 0; i < 10; i++) digitos[i] = 0;     while (aux != 0) {       digitos[aux % 10] = 1;       aux = aux / 10;     }     if (num == 1) {       printf( 1 n );       return 0;     }     for (i = 1; i * i <= num; i++) {       if (num % i == 0) {         aux = i;         while (aux != 0) {           if (digitos[aux % 10] == 1) {             count++;             break;           }           aux = aux / 10;         }         aux = num / i;         if (aux != i)           while (aux != 0) {             if (digitos[aux % 10] == 1) {               count++;               break;             }             aux = aux / 10;           }       }     }     printf( %d n , count);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, cnt = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       cnt += x;     }     if (cnt * 2 <= n) {       printf( %d n , n / 2);       for (int i = 1; i <= n / 2; i++) printf( 0  );       printf(  n );     } else {       int x = n / 2;       if (x & 1) x++;       printf( %d n , x);       for (int i = 1; i <= x; i++) printf( 1  );       printf(  n );     }     return;   }   int main() {     int t;     scanf( %d , &t);     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct candy {     candy(int t, int h, int m) {       this->type = t;       this->height = h;       this->mass = m;     }     int type;     int height;     int mass;     bool operator<(const candy &c) const { return this->mass < c.mass; }     bool operator>(const candy &c) const { return this->height < c.height; }   };   int amount(int first, vector<candy> &all_candies, int x) {     priority_queue<candy> candies0, candies1;     int all_candies_index = 0;     int max_height = x;     int last_taken = 1 - first;     int amount_taken = 0;     while (true) {       while (all_candies_index < all_candies.size()) {         if (all_candies[all_candies_index].height > max_height) {           break;         }         if (all_candies[all_candies_index].type == 0) {           candies0.push(all_candies[all_candies_index]);         } else {           candies1.push(all_candies[all_candies_index]);         }         all_candies_index++;       }       if (last_taken) {         if (candies0.empty()) {           break;         }         amount_taken++;         max_height += candies0.top().mass;         candies0.pop();         last_taken = 0;       } else {         if (candies1.empty()) {           break;         }         amount_taken++;         max_height += candies1.top().mass;         candies1.pop();         last_taken = 1;       }     }     return amount_taken;   }   int main() {     int n, x;     scanf( %d%d , &n, &x);     vector<candy> all_candies;     for (int i = 0; i < n; ++i) {       int t, h, m;       scanf( %d%d%d , &t, &h, &m);       all_candies.push_back(candy(t, h, m));     }     sort(all_candies.begin(), all_candies.end(), std::greater<candy>());     printf( %d n , max(amount(0, all_candies, x), amount(1, all_candies, x)));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i;     vector<char> v;     string s;     cin >> s;     n = s.size();     char c[n], ch;     i = n;     ch = s[n - 1];     while (i--) {       if (s[i] == ch)         v.push_back(s[i]);       else if (s[i] > ch) {         v.push_back(s[i]);         ch = s[i];       }     }     for (i = v.size(); i > 0; i--) cout << v[i - 1];   }
#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   bool iseven(long long int n) {     if ((n & 1) == 0) return true;     return false;   }   void print(long long int t) { cout << t; }   void print(int t) { cout << t; }   void print(string t) { cout << t; }   void print(char t) { cout << t; }   void print(double t) { cout << t; }   void print(long double t) { cout << t; }   template <class T, class V>   void print(pair<T, V> p);   template <class T>   void print(vector<T> v);   template <class T>   void print(set<T> v);   template <class T, class V>   void print(map<T, V> v);   template <class T>   void print(multiset<T> v);   template <class T, class V>   void print(pair<T, V> p) {     cout <<  { ;     print(p.first);     cout <<  , ;     print(p.second);     cout <<  } ;   }   template <class T>   void print(vector<T> v) {     cout <<  [  ;     for (T i : v) {       print(i);       cout <<    ;     }     cout <<  ] ;   }   template <class T>   void print(set<T> v) {     cout <<  [  ;     for (T i : v) {       print(i);       cout <<    ;     }     cout <<  ] ;   }   template <class T>   void print(multiset<T> v) {     cout <<  [  ;     for (T i : v) {       print(i);       cout <<    ;     }     cout <<  ] ;   }   template <class T, class V>   void print(map<T, V> v) {     cout <<  [  ;     for (auto i : v) {       print(i);       cout <<    ;     }     cout <<  ] ;   }   void solve() {     long long int n;     cin >> n;     string s;     cin >> s;     string original = s;     string ans =   ;     for (long long int i = 0; i < n; ++i) {       ans +=  9 ;     }     for (long long int i = 0; i < n; ++i) {       s = original;       long long int toincrease = 10 - (s[i] -  0 );       for (auto &ch : s) {         long long int temp = (ch -  0  + toincrease) % 10;         ch =  0  + temp;       }       string tempans =   ;       tempans += s[i];       long long int ptr = (i + 1) % n;       while (ptr != i) {         tempans += s[ptr];         ptr = (ptr + 1) % n;       }       ans = min(ans, tempans);     }     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }
#include <bits/stdc++.h>   int SequenceValue(char seq[], int *min_val) {     int value = 0;     *min_val = 0;     char *it = seq;     while (*it !=   0  && *it !=   n ) {       value += (*it ==  ( ) ? 1 : -1;       if (value < *min_val) {         *min_val = value;       }       it += 1;     }     return value;   }   char seq[300000 + 5];   int starts[300000];   int ends[300000];   int main() {     int n;     scanf( %d%*c , &n);     long long int pairs = 0;     int i;     for (i = 0; i < n; ++i) {       fgets(seq, 300000 + 1, stdin);       int min_val;       int value = SequenceValue(seq, &min_val);       if (min_val < value && min_val < 0) {         continue;       }       if (value == 0) {         pairs += 1LL * 2 * starts[0] + 1;         starts[0] += 1;         continue;       }       if (value > 0) {         pairs += 1LL * ends[value];         starts[value] += 1;       } else {         pairs += 1LL * starts[-value];         ends[-value] += 1;       }     }     printf( %I64d n , pairs);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long i;     long long n, m, k, l;     cin >> n >> m >> k >> l;     long long no = 0;     long long yes = 0;     long long j = (l + k);     long long ans = ((j - 1) / m) + 1;     if (((ans * m)) > n) no = 1;     if (no)       cout <<  -1 ;     else       cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long MOD(long long, long long);   void doublesetprecision(double, int);   bool isEven(long long);   int gcd(int, int);   int n, m, o, ans, k;   int a[1000000];   void solve() {     string s, t;     cin >> s >> t;     if (s == t) {       cout <<  yes ;       return;     }     if (s.size() != t.size()) {       cout <<  no ;       return;     }     int hs[26] = {0}, ht[26] = {0};     for (long long i = 0; i < s.size(); i++) hs[s[i] -  a ]++;     for (long long i = 0; i < t.size(); i++) ht[t[i] -  a ]++;     for (long long i = 0; i < 26; i++) {       if (ht[i] > 0 && hs[i] > 0) {         cout <<  yes ;         return;       }     }     cout <<  no ;   }   int main() {     int t = 1;     scanf( %d , &t);     while (t--) {       solve();       cout <<   n ;     }     return 0;   }   bool isEven(long long n) {     if (n % 2 == 0) return 1;     return 0;   }   void doublesetprecision(double x, int p) {     cout << fixed << setprecision(p) << x;   }   long long MOD(long long x, long long M) {     x = x % M;     if (x < 0) x += M;     return x;   }   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
#include <bits/stdc++.h>   using namespace std;   vector<int> E[1 << 18];   pair<int, int> R[1 << 18];   int dfs(int a, int p, int d) {     int cnt = 1;     for (auto it = (E[a]).begin(); it != (E[a]).end(); ++it) {       int b = *it;       if (b == p) continue;       cnt += dfs(b, a, d + 1);     }     R[a] = pair<int, int>(cnt - 1 - d, a);     return cnt;   }   int main() {     int n, k;     cin >> n >> k;     int i;     for (i = (1); i < (n); ++i) {       int a, b;       cin >> a >> b;       --a;       --b;       E[a].push_back(b);       E[b].push_back(a);     }     dfs(0, -1, 0);     sort(R, R + n);     long long res = 0;     for (i = (k); i < (n); ++i) res += R[i].first;     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int NMAX = 1e5 + 2;   int n, last[NMAX], x, y;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int query = 1; query <= n; query++) {       cin >> x >> y;       int ans = 0, autre;       for (int d = 1; d * d <= x; d++) {         if (x % d == 0) {           autre = x / d;           if (last[d] < query - y || y == 0) {             ans++;           }           if ((last[autre] < query - y || y == 0) && autre != d) {             ans++;           }           last[d] = last[autre] = query;         }       }       cout << ans <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7, LIM = 1e5;   unsigned long long int fact[2 * LIM + 1];   unsigned long long int binpow(long long int x, int y) {     unsigned long long int res = 1;     while (y > 0) {       if (y & 1) res = (res * x) % MOD;       y >>= 1;       x = (x * x) % MOD;     }     return res;   }   unsigned long long int inv_mod(unsigned long long int n) {     return binpow(n, MOD - 2);   }   unsigned long long int com(int n, int r) {     return (((fact[n] * inv_mod(fact[r])) % MOD) * inv_mod(fact[n - r])) % MOD;   }   bool comp(pair<int, int> p, pair<int, int> q) {     if (p.first == q.first) return p.second < q.second;     return p.first < q.first;   }   int main() {     fact[0] = 1;     for (int i = 1; i <= 2 * LIM; i++) {       fact[i] = (i * fact[i - 1]) % MOD;     }     int h, w, n;     cin >> h >> w >> n;     vector<pair<int, int>> pts(n + 1);     for (int i = 0; i < n; i++) {       int r, c;       cin >> r >> c;       pts[i] = {r, c};     }     pts[n] = {h, w};     sort(pts.begin(), pts.end(), comp);     unsigned long long int dp[n + 1];     for (int i = 0; i <= n; i++) {       dp[i] = com(pts[i].first + pts[i].second - 2, pts[i].first - 1);       for (int j = 0; j < i; j++) {         if (pts[j].first <= pts[i].first && pts[j].second <= pts[i].second) {           dp[i] = (dp[i] + MOD -                    (dp[j] * com(pts[i].first - pts[j].first + pts[i].second -                                     pts[j].second,                                 pts[i].first - pts[j].first)) %                        MOD) %                   MOD;         }       }     }     cout << dp[n];   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int n;   int a[N];   int fa(int x) {     for (int i = 0;; i++)       if ((1 << i) >= x) return (1 << i) - x;   }   int dis(int x, int y) {     int sum = 0;     while (x != y) x > y ? x = fa(x) : y = fa(y), sum++;     return sum;   }   int get(int x) {     int mx = 0, id = 0;     for (int i = 1; i <= n; i++) {       int t = dis(a[x], a[i]);       if (t > mx) mx = t, id = i;     }     return id;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     int t1 = get(1), t2 = get(t1);     printf( %d %d %d , t1, t2, dis(a[t1], a[t2]));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x;     cin >> n >> x;     int cnt = 0;     for (int j = 1; j * j <= x; j++) {       if (x % j == 0) {         if (j <= n && x / j <= n) {           if (j * j == x)             cnt++;           else             cnt += 2;         }       }     }     cout << cnt << endl;   }
#include <bits/stdc++.h>   void printResult() {}   int main(int argc, const char* argv[]) {     std::ios::sync_with_stdio(false);     int rows;     bool possible = false;     scanf( %d n , &rows);     std::shared_ptr<std::vector<char*>> bus =         std::make_shared<std::vector<char*>>();     if (rows > 0) {       for (auto i = 0; i < rows; i++) {         char* row = new char[4];         scanf( %c%c|%c%c n , &row[0], &row[1], &row[2], &row[3]);         bus->push_back(row);       }       for (auto row : *bus) {         if (row[0] == row[1] && row[0] ==  O ) {           possible = true;           row[0] =  + ;           row[1] =  + ;           break;         } else if (row[2] == row[3] && row[2] ==  O ) {           possible = true;           row[2] =  + ;           row[3] =  + ;           break;         }       }       if (possible == true) {         printf( YES n );         for (auto row : *bus) {           printf( %c%c|%c%c n , row[0], row[1], row[2], row[3]);         }       } else {         printf( NO n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     double d, h, v, e;     cin >> d >> h >> v >> e;     double pi = 2 * acos(0.0);     double he = (4 * v) / (pi * d * d);     if (he > e) {       cout <<  YES n ;       cout << setprecision(12) << (h) / (he - e);     } else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char s[1000010];   int c[20];   int main() {     int k;     scanf( %d , &k);     scanf( %s , s + 1);     int n = strlen(s + 1);     int sum = 0;     int i;     memset(c, 0, sizeof c);     int ans = 0;     for (i = 1; i <= n; i++) {       c[s[i] -  0 ]++;       sum += s[i] -  0 ;     }     sum = k - sum;     for (i = 0; i <= 9; i++)       while (sum > 0 && c[i]) {         sum -= 9 - i;         ans++;         c[i]--;       }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char s[2505][5005];   int a[5000];   int c1[30], c2[30];   int dif[2505];   int main() {     int k, n;     scanf( %d%d , &k, &n);     scanf( %s , s[0]);     bool flag = false;     for (int i = 0; i < n; ++i) {       c1[s[0][i] -  a ]++;       if (c1[s[0][i] -  a ] > 1) flag = true;     }     for (int i = 1; i < k; ++i) {       scanf( %s , s[i]);       memset(c2, 0, sizeof(c2));       for (int j = 0; j < n; ++j) {         c2[s[i][j] -  a ]++;         if (s[i][j] != s[0][j]) dif[i]++;       }       if (dif[i] > 4) {         cout <<  -1 n ;         return 0;       }       for (int j = 0; j < 26; ++j) {         if (c1[j] != c2[j]) {           cout <<  -1 n ;           return 0;         }       }     }     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {         bool ok = true;         for (int l = 1; l < k; ++l) {           a[l] = dif[l];           a[l] -= (s[0][i] != s[l][i]);           a[l] -= (s[0][j] != s[l][j]);           a[l] += (s[0][i] != s[l][j]);           a[l] += (s[0][j] != s[l][i]);           if ((a[l] == 0 && flag) || a[l] == 2) continue;           ok = false;           break;         }         if (ok) {           swap(s[0][i], s[0][j]);           cout << s[0] << endl;           return 0;         }       }     }     cout <<  -1 n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int maxn = 200011;   const int INF = 1 << 25;   const int logN = 18;   int n;   int a[maxn];   bool p[maxn];   int b[maxn];   bool used[maxn];   int main(int argc, char const *argv[]) {     scanf( %d , &(n));     for (int(i) = 0; i < (int)(n); ++(i)) scanf( %d , &(a[i]));     priority_queue<pair<int, int> > pq1, pq2;     for (int(i) = 0; i < (int)(n); ++(i))       pq1.push(make_pair((a[i] - (n - i)), (i)));     for (int i = n - 1; i >= 0; --i) {       while (!pq1.empty() && pq1.top().second > i) pq1.pop();       while (!pq1.empty() && used[pq1.top().second]) pq1.pop();       while (!pq2.empty() && used[pq2.top().second]) pq2.pop();       int id1 = -1, id2 = -1;       if (!pq1.empty()) id1 = pq1.top().second;       if (!pq2.empty()) id2 = pq2.top().second;       int pick;       if (id1 != -1 && id2 != -1) {         if (a[id1] - i + id1 > a[id2] - i + id2) {           pick = id1;           pq1.pop();         } else {           pick = id2;           pq2.pop();         }       } else if (id1 != -1) {         pick = id1;         pq1.pop();       } else {         pick = id2;         pq2.pop();       }       if (pick > i)         b[i] = a[pick] - i + pick;       else         b[i] = a[pick] - i + pick;       used[pick] = 1;       if (!used[i]) pq2.push(make_pair((a[i] + i), (i)));     }     bool f = 1;     for (int(i) = 0; i < (int)(n - 1); ++(i))       if (b[i] > b[i + 1]) f = 0;     if (f)       for (int(i) = 0; i < (int)(n); ++(i))         printf( %d%c , b[i], i == n - 1 ?   n  :    );     else       puts( :( );   }
#include <bits/stdc++.h>   int main() {     int n, s, i, sum = 0, max = 0;     scanf( %d %d , &n, &s);     int arr[n];     for (i = 0; i < n; i++) {       scanf( %d , &arr[i]);       sum = sum + arr[i];       if (arr[i] > max) max = arr[i];     }     if (sum - max > s)       printf( NO );     else       printf( YES );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 7;   int n, m, len, cnt1 = 0, cnt2 = 0, cnt3 = 0, fail[maxn];   long long ans = 0;   struct Node {     long long c;     char ch;   } s[maxn], str[maxn], _str[maxn];   bool ok(int i, int j) {     if (_str[i].c != _str[j].c) return false;     if (_str[i].ch != _str[j].ch) return false;     return true;   }   bool _ok(int i, int j) {     if (s[i].c != _str[j].c) return false;     if (s[i].ch != _str[j].ch) return false;     return true;   }   void getfail() {     fail[0] = fail[1] = 0;     for (int i = 1; i < cnt3; i++) {       int j = fail[i];       while (j && !ok(i, j)) j = fail[j];       fail[i + 1] = (ok(i, j)) ? j + 1 : 0;     }   }   void KMP_search() {     for (int i = 0, j = 0; i < cnt1; i++) {       while (j && !_ok(i, j)) j = fail[j];       if (_ok(i, j)) j++;       if (j == cnt3) {         if ((i - j + 1) && i != (cnt1 - 1) && s[i - j].ch == str[0].ch &&             s[i - j].c >= str[0].c && s[i + 1].ch == str[cnt2 - 1].ch &&             s[i + 1].c >= str[cnt2 - 1].c)           ans++;       }     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %I64d-%c , &s[cnt1].c, &s[cnt1].ch);       if (i && s[cnt1].ch == s[cnt1 - 1].ch)         s[cnt1 - 1].c += s[cnt1].c;       else         cnt1++;     }     for (int i = 0; i < m; i++) {       scanf( %I64d-%c , &str[cnt2].c, &str[cnt2].ch);       if (i && str[cnt2].ch == str[cnt2 - 1].ch)         str[cnt2 - 1].c += str[cnt2].c;       else         cnt2++;     }     if (cnt2 == 1) {       for (int i = 0; i < cnt1; i++)         if (s[i].ch == str[0].ch && s[i].c >= str[0].c)           ans += s[i].c - str[0].c + 1;     } else if (cnt2 == 2) {       for (int i = 0; i < cnt1; i++)         if (s[i].ch == str[0].ch && s[i].c >= str[0].c && (i != (cnt1 - 1)) &&             s[i + 1].ch == str[1].ch && s[i + 1].c >= str[i + 1].c)           ans++;     } else {       for (int i = 1; i < cnt2 - 1; i++) _str[cnt3++] = str[i];       getfail();       KMP_search();     }     printf( %I64d n , ans);     return 0;   }