malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long n, a[300005], b1[500005], b2[500005],       cnt = 0, pl[300005], c[300005], ans = 0, plc[300005], m, d, can[300005], r,       ne[300005];   pair<int, int> p;   pair<int, int> p1;   pair<int, int> p2;   map<pair<int, int>, int> m1;   string s;   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> a[i];       pl[a[i]] = i;       can[i] = 0;       ne[i] = i + 1;     }     for (int i = 0; i < m; i++) {       cin >> b1[i] >> b2[i];       p.first = b1[i];       p.second = b2[i];       m1[p] = 1;       if (b2[i] == a[n]) {         c[cnt] = b1[i];         plc[cnt] = pl[b1[i]];         cnt++;       }     }     sort(plc, plc + cnt);     for (int i = 0; i < cnt; i++) {       c[i] = a[plc[i]];     }     for (int i = cnt - 1; i >= 0; i--) {       d = 1;       r = ne[pl[c[i]]];       while (r < n) {         p1.first = c[i];         p1.second = a[r];         if (m1[p1] == 0) {           d = 0;           break;         }         r = ne[r];       }       if (d) {         ans++;         if (c[i] > 0) {           ne[pl[c[i]] - 1] = ne[pl[c[i]]];         }       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   struct BIT {     int n;     vector<T> dat;     BIT(int n = 0) { initialize(n); }     void initialize(int nin) {       n = nin;       dat.resize(n, 0);     }     T sum(int i) {       T s = 0;       while (i >= 0) {         s += dat[i];         i = (i & (i + 1)) - 1;       }       return s;     }     T sum_between(int i, int j) { return sum(j) - sum(i - 1); }     void plus(int i, T x) {       while (i < n) {         dat[i] += x;         i |= i + 1;       }     }     int lower_bound(T x) {       if (x < 0) return -1;       if (sum(n - 1) < x) return n;       int ret = -1;       int k = 1;       while (2 * k <= n) k <<= 1;       for (; k > 0; k >>= 1) {         if (ret + k < n && dat[ret + k] < x) {           x -= dat[ret + k];           ret += k;         }       }       return ret + 1;     }   };   void solve() {     string S;     cin >> S;     vector<pair<int, int>> D;     for (int i = 0; i < int(S.size()) - 1; i++)       if (S[i] == S[i + 1]) {         D.emplace_back(S[i] -  a , i + 1);       }     int N = D.size();     vector<int> num(26);     BIT<int> bit[26], bit_sum(N);     for (int k = 0; k < 26; k++) bit[k].initialize(N);     for (int i = 0; i < N; i++) {       int al = D[i].first;       num[al]++;       bit[al].plus(i, 1);       bit_sum.plus(i, D[i].second - (i ? D[i - 1].second : 0));     }     int sum = N;     int total = S.size();     vector<pair<int, int>> ans;     while (true) {       int mx_al = max_element(num.begin(), num.end()) - num.begin();       if (num[mx_al] == sum) break;       int fst = bit[mx_al].lower_bound(1);       int r = N, l = -1;       for (int k = 0; k < 26; k++)         if (k != mx_al) {           int pos = bit[k].lower_bound(bit[k].sum(fst) + 1);           r = min(r, pos);         }       if (r == N) {         r = fst;         for (int k = 0; k < 26; k++)           if (k != mx_al) {             int pos = bit[k].lower_bound(bit[k].sum(fst));             l = max(l, pos);           }       } else {         l = bit[mx_al].lower_bound(bit[mx_al].sum(r));       }       int LL = bit_sum.sum(l);       int RR = bit_sum.sum(r);       ans.emplace_back(LL, RR);       bit_sum.plus(l + 1, LL - RR);       total -= RR - LL;       sum -= 2;       num[D[l].first]--;       num[D[r].first]--;       bit[D[l].first].plus(l, -1);       bit[D[r].first].plus(r, -1);     }     for (int i = 0; i < N; i++) {       int s = bit[D[i].first].sum_between(i, i);       if (s > 0) {         int rem = bit_sum.sum(i);         ans.emplace_back(0, rem);         bit_sum.plus(0, -rem);         total -= rem;       }     }     ans.emplace_back(0, total);     printf( %d n , int(ans.size()));     for (auto& p : ans) printf( %d %d n , p.first + 1, p.second);   }   int main() {     int T;     cin >> T;     while (T--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int arr[n];     int orig[n];     int product = 1;     int counter = 0;     for (int i = 0; i < n; i++) {       cin >> arr[i];       orig[i] = arr[i];       if (arr[i] >= 0) {         arr[i] = -arr[i] - 1;       }       if (arr[i] < 0) {         counter++;       }     }     if (counter % 2 == 0) {       for (int i = 0; i < n; i++) {         cout << arr[i] <<    ;       }     } else {       int maxAbsVal = 0;       int maxIndex = 0;       for (int i = 0; i < n; i++) {         if (abs(arr[i]) >= maxAbsVal) {           maxAbsVal = abs(arr[i]);           maxIndex = i;         }       }       if (arr[maxIndex] == orig[maxIndex]) {         arr[maxIndex] = -arr[maxIndex] - 1;       } else {         arr[maxIndex] = orig[maxIndex];       }       for (int i = 0; i < n; i++) {         cout << arr[i] <<    ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   struct node {     int index, val, team;   };   node ni[N];   int divi[N], rest[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       ni[i].index = i;       scanf( %d , &ni[i].val);     }     sort(ni, ni + n, [](const node &a, const node &b) { return a.val < b.val; });     fill(begin(divi), end(divi), -0x3f3f3f3f);     for (int i = 3; i < n; ++i) {       divi[i] = rest[i - 3] + ni[i].val - ni[i - 1].val;       rest[i - 2] = max(divi[i - 2], rest[i - 3]);     }     int k = 1;     for (int i = n - 1; i >= n - 3; --i) {       ni[i].team = k;     }     for (int i = n - 3; i > 0;) {       if (rest[i] == divi[i]) {         ++k;         for (int j = i; j > 0 && j > i - 3; --j) {           ni[j - 1].team = k;         }         i -= 3;       } else {         i -= 1;         ni[i].team = k;       }     }     printf( %d %d n , ni[n - 1].val - ni[0].val - rest[n - 3], k);     sort(ni, ni + n,          [](const node &a, const node &b) { return a.index < b.index; });     for (int i = 0; i < n; ++i) {       printf( %d  , ni[i].team);     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool a[5][5];   int main() {     for (int i = 1; i < 4; ++i) {       for (int j = 1, x; j < 4; ++j) {         a[i][j] ^= 1;         cin >> x;         a[i][j] ^= (x & 1);         a[i + 1][j] ^= (x & 1);         a[i - 1][j] ^= (x & 1);         a[i][j + 1] ^= (x & 1);         a[i][j - 1] ^= (x & 1);       }     }     for (int i = 1; i < 4; ++i) {       for (int j = 1; j < 4; ++j) {         cout << a[i][j];       }       cout << endl;     }   }
#include <bits/stdc++.h>   int main() {     int n, m, i;     float x, max = 0.0;     scanf( %d%d , &n, &m);     int s[n][2];     for (i = 0; i < n; i++) {       scanf( %d%d , &s[i][0], &s[i][1]);       x = (float)(s[i][0]) / s[i][1];       if (max == 0.0) {         max = x;       }       if (max > x) {         max = x;       }     }     x = max * m;     printf( %.8f n , x);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> l(n), r(n);     for (int i = 0; i < n; i++) {       cin >> l[i] >> r[i];     }     int a = -1, b = -1;     bool ok = true;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (!((l[i] <= l[j] && r[i] <= r[j]) || (l[j] <= l[i] && r[j] <= l[i]))) {           if (a == -1) {             a = i;             b = j;           } else {             if (a == i && j == b)               continue;             else if (a == j && b == i)               continue;             else if (i == a && j != b) {               b = -1;             } else if (i == b && j != a) {               a = i;               b = -1;             } else if (j == a && i != b) {               b = -1;             } else if (j == b && i != a) {               a = j;               b = -1;             } else {               ok = false;             }           }         }       }     }     if (!ok)       cout <<  0 n ;     else if (a == -1 && b == -1) {       cout << n <<   n ;       for (int i = 0; i < n; i++) {         cout << i + 1 <<    ;       }       cout <<   n ;     } else if (b == -1) {       cout <<  1 n  << a + 1 <<   n ;     } else {       if (a > b) swap(a, b);       cout <<  2 n  << a + 1 <<     << b + 1 <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int s = 0;     for (int i = 0; i < 5; i++) {       int x;       cin >> x;       s += x;     }     if (s % 5 || !s)       cout <<  -1 ;     else       cout << s / 5;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000 + 3;   int N, Q, K;   long long a[MAXN], sum[MAXN], save[MAXN];   int main() {     scanf( %d , &N);     memset((save), (-1), sizeof(save));     for (int i = 1; i <= N; ++i) scanf( %lld , &a[i]);     sort(a + 1, a + 1 + N);     for (int i = 1; i <= N; ++i) sum[i] = sum[i - 1] + a[i];     scanf( %d , &Q);     while (Q--) {       scanf( %d , &K);       if (save[K] != -1) {         printf( %lld n , save[K]);         continue;       }       long long ans = 0, deep = 1, deep_size = K, now = N - 1;       while (now > 0) {         long long next = max(0ll, now - deep_size);         ans += (sum[now] - sum[next]) * deep;         ++deep;         now -= deep_size;         deep_size *= K;       }       save[K] = ans;       printf( %lld , ans);       putchar(Q ?     :   n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int cnt;   string s;   stack<int> st;   int main() {     cin >> s;     for (int i = 0; i < s.size(); ++i) {       if (s[i] ==  (  || s[i] ==  [  || s[i] ==  {  || s[i] ==  < ) {         st.push(int(s[i]));       } else {         if (st.empty()) {           cout <<  Impossible ;           return 0;         }         if (st.top() == 40) {           if (int(s[i]) != 41) {             cnt++;           }           if (!st.empty()) st.pop();         } else {           if (st.top() + 2 != int(s[i])) {             cnt++;           }           if (!st.empty()) st.pop();         }       }     }     if (!st.empty()) {       cout <<  Impossible ;       return 0;     }     cout << cnt;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int la, lb, cnt[20], ans[25];   char a[25], b[25];   void find(int v, bool flag) {     if (v > lb) {       for (int i = 1; i <= lb; i++) printf( %d , ans[i]);       exit(0);     }     if (flag && cnt[b[v] -  0 ]) {       cnt[b[v] -  0 ]--;       ans[v] = b[v] -  0 ;       find(v + 1, 1);       ans[v] = 0;       cnt[b[v] -  0 ]++;     }     for (int i = (flag ? b[v] -  0  - 1 : 9); i >= 0; i--)       if (cnt[i]) {         ans[v] = i;         cnt[i]--;         find(v + 1, 0);         ans[v] = 0;         cnt[i]++;       }   }   int main() {     scanf( %s%s , a + 1, b + 1);     la = strlen(a + 1), lb = strlen(b + 1);     for (int i = 1; i <= la; i++) cnt[a[i] -  0 ]++;     if (lb > la) {       lb = la;       for (int i = 1; i <= lb; i++) b[i] =  9 ;     }     find(1, 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, ans, fact[200005];   long long pangkat(long long a, long long b) {     if (b == 0) return 1;     long long temp = pangkat(a, b / 2);     if (b % 2 == 0) return (temp * temp) % 1000000007;     return (((temp * temp) % 1000000007) * a) % 1000000007;   }   long long comb(long long a, long long b) {     long long temp = (fact[b] * fact[a - b]) % 1000000007;     return (fact[a] * pangkat(temp, 1000000007 - 2)) % 1000000007;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     fact[0] = 1;     for (int i = 1; i <= 2 * n; i++) fact[i] = (fact[i - 1] * i) % 1000000007;     ans = 2 * comb(2 * n - 1, n) - n;     cout << ans % 1000000007 <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 48;   long long s, x, xCopy, sCopy, pos[MAXN][2], result;   int n, bitS, bitX;   int main() {     cin >> s >> x;     pos[0][0] = 1;     pos[0][1] = 0;     n = 0;     xCopy = x;     sCopy = s;     while (x || s) {       n++;       bitS = s % 2;       bitX = x % 2;       if (bitS == 0 && bitX == 0) {         pos[n][0] = pos[n - 1][0];         pos[n][1] = pos[n - 1][0];       } else {         if (bitS == 0 && bitX == 1) {           pos[n][0] = 0;           pos[n][1] = 2 * pos[n - 1][1];         } else {           if (bitS == 1 && bitX == 0) {             pos[n][0] = pos[n - 1][1];             pos[n][1] = pos[n - 1][1];           } else {             pos[n][0] = 2 * pos[n - 1][0];             pos[n][1] = 0;           }         }       }       s >>= 1;       x >>= 1;     }     result = pos[n][0];     if ((sCopy ^ 0) == xCopy) result -= 2;     cout << result;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 500100;   const int root = 800;   long long lazy[root];   unordered_set<long long> s[root];   long long v[MAXN];   signed main() {     memset(lazy, 0, sizeof lazy);     long long n, m;     scanf( %lld %lld , &n, &m);     for (long long i = 0; i < n; i++)       scanf( %lld , &v[i]), s[i / root].insert(v[i]);     for (long long i = 1; i <= m; i++) {       long long t;       scanf( %lld , &t);       if (t == 1) {         long long l, r, x;         scanf( %lld %lld %lld , &l, &r, &x);         l -= 1, r -= 1;         s[l / root].clear();         for (long long j = (l / root) * root; j < min((l / root + 1) * root, n);              j++) {           if (j >= l && j <= r) v[j] += x;           s[l / root].insert(v[j]);         }         if (l / root == r / root) continue;         for (long long j = l / root + 1; j < r / root; j++) {           lazy[j] += x;         }         s[r / root].clear();         for (long long j = (r / root) * root; j < min((r / root + 1) * root, n);              j++) {           if (j <= r && j >= l) v[j] += x;           s[r / root].insert(v[j]);         }       }       if (t == 2) {         long long y, ini, fim;         scanf( %lld , &y);         bool flag = false;         for (long long j = 0; j < root; j++) {           if (s[j].find(y - lazy[j]) != s[j].end()) {             flag = true;             for (long long k = j * root; k < (j + 1) * root; k++)               if (v[k] == y - lazy[j]) {                 ini = k;                 break;               }             break;           }         }         if (flag == false) {           printf( -1 n );           continue;         }         for (long long j = root - 1; j >= 0; j--) {           if (s[j].find(y - lazy[j]) != s[j].end()) {             flag = true;             for (long long k = min((j + 1) * root - 1, n - 1); k >= j * root;                  k--) {               if (v[k] == y - lazy[j]) {                 fim = k;                 break;               }             }             break;           }         }         printf( %lld n , fim - ini);       }     }   }
#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};   int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};   const int mod = 1e9 + 7;   int dcmp(long double x, long double y) {     return fabs(x - y) <= 1e-12 ? 0 : x < y ? -1 : 1;   }   void fast() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);   }   int n;   map<string, int> score;   map<int, vector<pair<string, int> > > ft;   int main() {     fast();     cin >> n;     string s;     int x, mx = 0;     for (int i = 0; i < n; i++) {       cin >> s >> x;       score[s] += x;       ft[score[s]].push_back({s, i});     }     for (auto e : score) {       mx = max(mx, e.second);     }     int mn = 2e9;     string wn =   ;     for (auto &e : ft) {       if (e.first < mx) continue;       for (int i = 0; i < e.second.size(); i++) {         if (score[e.second[i].first] == mx) {           if (mn > e.second[i].second) {             mn = e.second[i].second;             wn = e.second[i].first;           }         }       }     }     cout << wn <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct fenwick {     int N;     vector<int> sum;     void init(int n) {       N = n + 1;       sum.assign(N, 0);     }     void add(int i, int x) {       for (i++; i < N; i += i & -i) {         sum[i] += x;       }     }     int get(int i) {       int tot = 0;       for (i++; i > 0; i -= i & -i) {         tot += sum[i];       }       return tot;     }     int get(int i, int j) { return get(j) - (i > 0 ? get(i - 1) : 0); }   };   fenwick xs;   int lo, hi;   bool flip;   void init(int n, int x) {     xs.init(n);     for (int i = 0; i < n; i++) {       xs.add(i, x);     }     lo = 0;     hi = n - 1;   }   int len() { return hi - lo + 1; }   int get(int i) {     i = !flip ? lo + i : hi - i;     return xs.get(i, i);   }   int get(int i, int j) {     if (!flip) {       i += lo;       j += lo;     } else {       i = hi - i;       j = hi - j;       swap(i, j);     }     return xs.get(i, j);   }   void add(int i, int x) {     i = !flip ? lo + i : hi - i;     xs.add(i, x);   }   void drop(int k) {     if (!flip) {       lo += k;     } else {       hi -= k;     }   }   void dump() {     int n = len();     for (int i = 0; i < n; i++) {       cerr << get(i) <<    ;     }     cerr << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n, q;     cin >> n >> q;     init(n, 1);     while (q--) {       int t, i, j;       cin >> t >> i;       if (t == 1) {         if (i > len() / 2) {           i = len() - i;           flip = !flip;         }         for (int k = 0; i - 1 - k >= 0; k++) {           add(i + k, get(i - 1 - k));         }         drop(i);       } else {         cin >> j;         cout << get(i, j - 1) <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 510000;   int f[maxn];   int n, d, up;   int c[66];   int b[66];   int main() {     cin >> n >> d;     f[0] = 1;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       for (int j = (up += x); j >= x; j--)         if (f[j - x]) f[j] = 1;     }     int num = 0, ans = 0;     while (true) {       int to = ans + d;       while (!f[to] && to > ans) to--;       if (ans == to) break;       ans = to;       num++;     }     cout << ans <<     << num << endl;   }
#include <bits/stdc++.h>   using namespace std;   using namespace std;   const int MaxN = 100005, NA = -1, MaxC = 0x3F3F3F3F;   int a[MaxN];   int n;   multiset<int, greater<int> > s;   deque<int> d;   int main(void) {     int i, v;     multiset<int>::iterator it;     while (scanf(  %d , &n) != EOF) {       for (i = 0; i < n; i++) scanf(  %d , &a[i]);       s.clear();       for (i = 0; i < n; i++) s.insert(a[i]);       d.clear();       it = s.begin();       d.push_back(*it);       s.erase(it);       for (i = 1; i < n; i++)         if (d.front() > d.back()) {           v = d.front();           it = s.find(v + 1);           if (it != s.end()) {             d.push_front(*it);             s.erase(it);           } else {             it = s.find(v - 1);             if (it != s.end()) {               d.push_front(*it);               s.erase(it);             } else               break;           }         } else {           v = d.back();           it = s.find(v + 1);           if (it != s.end()) {             d.push_back(*it);             s.erase(it);           } else {             it = s.find(v - 1);             if (it != s.end()) {               d.push_back(*it);               s.erase(it);             } else               break;           }         }       if ((int)d.size() == n && abs(d.front() - d.back()) == 1)         printf( YES n );       else         printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) {     return {l.first + r.first, l.second + r.second};   }   typedef void (*callback_function)(void);   const long long ZERO = 0LL;   const long long INF64 = 1e18;   const int INF32 = 1e9;   const int MOD = 1e9 + 7;   const long double PI = acos(-1.0L);   const long double EPS = static_cast<long double>(1e-9);   long long inv(long long a, long long b) {     if (a != 1) {       return b - (inv(b % a, a) * b) / a;     }     return 1;   }   inline int Pow(int a, int k) {     int s = 1;     for (; k; k >>= 1) {       k& 1 ? s = 1LL * s * a % MOD : 0;       a = 1LL * a * a % MOD;     }     return s;   }   const int N = 1e6 + 7;   void input() {}   char a[N][20];   void solve() {     int n;     scanf( %d , &n);     for (int i = (0); i < (n); i++) {       scanf( %s , a[i]);     }     set<string> st;     for (int i = (n - 1); i >= (0); i--) {       if (!st.count(a[i])) {         printf( %s n , a[i]);         st.insert(a[i]);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie();     {       input();       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class A, class B>   inline bool mina(A &first, B second) {     return (first > second) ? (first = second, 1) : 0;   }   template <class A, class B>   inline bool maxa(A &first, B second) {     return (first < second) ? (first = second, 1) : 0;   }   const int MAXN = 3e5 + 5;   int in[MAXN];   int N, M;   int pos[MAXN];   pair<int, int> foes[MAXN];   int main() {     scanf( %d %d , &N, &M);     for (int i = 1; i <= N; i++) {       scanf( %d , &in[i]);       pos[in[i]] = i;     }     int a, b;     for (int(i) = 0; (i) < (M); ++(i)) {       scanf( %d %d , &a, &b);       a = pos[a];       b = pos[b];       if (a < b) swap(a, b);       foes[i] = pair<int, int>(a, b);     }     sort(foes, foes + M);     long long ans = 0;     int maxx = 0;     int cur = 0;     for (int i = 1; i <= N; i++) {       while (cur < M && foes[cur].first <= i) {         maxa(maxx, foes[cur].second);         cur++;       }       ans += i - maxx;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   char s[1000000];   char ans[1000000];   int main() {     int i, len, cnt;     char max = 0;     while (scanf( %s , s) != EOF) {       max = 0;       cnt = 1000000 - 1;       len = strlen(s);       for (i = len - 1; i >= 0; i--) {         if (s[i] >= max) {           max = s[i];           ans[cnt--] = s[i];         }       }       for (i = cnt + 1; i < 1000000; i++) printf( %c , ans[i]);       printf(  n );     }     return 0;   }