malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long int n, m;   char arr[2004][2004];   pair<int, int> v[2004][2004];   long long int dist[8000003];   const long long int oo = 1000000007;   const int dx[] = {1, -1, 0, 0};   const int dy[] = {0, 0, 1, -1};   int get_node(int x, int y) { return x * m + y; }   void bfs(int r, int c) {     for (int i = 0; i < n * m; i++) dist[i] = oo;     dist[get_node(r, c)] = 0;     deque<pair<int, int>> dq;     dq.push_back({r, c});     v[r][c] = {0, 0};     while (!dq.empty()) {       int x = dq[0].first;       int y = dq[0].second;       dq.pop_front();       for (int i = 0; i < 4; i++) {         int xx = x + dx[i];         int yy = y + dy[i];         if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue;         if (arr[xx][yy] ==  * ) continue;         int flag = 0;         if (yy < y) flag = 1;         int node = get_node(xx, yy);         int u = get_node(x, y);         if (dist[node] > dist[u] + flag) {           dist[node] = dist[u] + flag;           v[xx][yy] = v[x][y];           if (i == 2) v[xx][yy].second++;           if (i == 3) v[xx][yy].first++;           if (flag)             dq.push_back({xx, yy});           else             dq.push_front({xx, yy});         }       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     long long int r, c;     cin >> r >> c;     r--;     c--;     long long int le, ri;     cin >> le >> ri;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> arr[i][j], v[i][j] = {oo, oo};     bfs(r, c);     long long int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (arr[i][j] ==  * ) continue;         if (v[i][j].first <= le && v[i][j].second <= ri) ans++;       }     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int maxmn = 505;   long long a[maxmn][maxmn];   long long luu1[maxmn][maxmn];   bool k[1000005];   long long maxx = 1e9;   void readquick() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int main() {     readquick();     vector<long long> luu;     memset(k, true, sizeof k);     for (int i = 2; i <= sqrt(1000000); i++) {       if (k[i] == true) {         for (int j = i * i; j <= 1000000; j += i) {           k[j] = false;         }       }     }     for (int i = 2; i <= 1000000; i++) {       if (k[i] == true) luu.push_back(i);     }     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> a[i][j];         int w = lower_bound(luu.begin(), luu.end(), a[i][j]) - luu.begin();         luu1[i][j] = luu[w] - a[i][j];       }     }     for (int i = 1; i <= n; i++) {       long long tong = 0;       for (int j = 1; j <= m; j++) {         tong += luu1[i][j];       }       if (tong >= 0) {         maxx = min(maxx, tong);       }     }     for (int i = 1; i <= m; i++) {       long long tong = 0;       for (int j = 1; j <= n; j++) {         tong += luu1[j][i];       }       if (tong >= 0) {         maxx = min(maxx, tong);       }     }     cout << maxx;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   vector<vector<int> > v(N);   int col[N];   int par[N];   int n;   int in[N], out[N];   int timer;   bool all_same[N];   bool can[N];   int euler[2 * N];   int tree[8 * N];   int answer = -1;   void dfs(int node) {     all_same[node] = true;     can[node] = true;     timer++;     in[node] = timer;     euler[timer] = col[node];     for (int to : v[node]) {       if (to == par[node]) continue;       par[to] = node;       dfs(to);       can[node] &= (all_same[to]);       all_same[node] &= (all_same[to] and (col[node] == col[to]));     }     out[node] = timer;   }   void build(int id, int lo, int hi) {     if (lo == hi) {       tree[id] = euler[lo];       return;     }     int mid = (lo + hi) >> 1;     build(2 * id, lo, mid);     build(2 * id + 1, mid + 1, hi);     if (tree[2 * id + 1] == tree[2 * id]) tree[id] = tree[2 * id];   }   int query(int id, int lo, int hi, int l, int r) {     if (r < l) return -2;     if (l > hi or r < lo) return -2;     if (l <= lo and r >= hi) {       if (tree[id] == 0) return -1;       return (tree[id]);     }     int mid = (lo + hi) >> 1;     int tmp1 = query(2 * id, lo, mid, l, r);     int tmp2 = query(2 * id + 1, mid + 1, hi, l, r);     if (tmp1 == tmp2) {       return tmp1;     }     if (tmp1 == -2) {       return tmp2;     }     if (tmp2 == -2) {       return tmp1;     }     return -1;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       int x, y;       scanf( %d %d , &x, &y);       v[x].emplace_back(y);       v[y].emplace_back(x);     }     for (int i = 1; i <= n; i++) {       scanf( %d , &col[i]);     }     dfs(1);     build(1, 1, timer);     for (int i = 1; i <= n; i++) {       if (can[i]) {         int node = i;         int tmp1 = query(1, 1, timer, 1, in[node] - 1);         int tmp2 = query(1, 1, timer, out[node] + 1, timer);         if ((tmp1 == tmp2 and tmp1 != -1) or (tmp1 == -2 and tmp2 != -1) or             (tmp1 != -1 and tmp2 == -2)) {           puts( YES );           printf( %d n , i);           return 0;         }       }     }     puts( NO );   }
#include <bits/stdc++.h>   using namespace std;   clock_t time_p = clock();   void ktj() {     time_p = clock() - time_p;     cerr <<  Time elapsed :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, i, j, k;     cin >> n;     vector<long long> a(n);     for (i = 0; i < n; i++) {       cin >> a[i];     }     if (n % 2) {       for (i = 0; i < n; i++) {         if (a[i] >= 0) {           a[i] = -a[i] - 1;         }       }       auto mn = min_element(a.begin(), a.end());       a[mn - a.begin()] = -a[mn - a.begin()] - 1;       for (i = 0; i < n; i++) {         cout << a[i] <<    ;       }       cout <<   n ;     } else {       for (i = 0; i < n; i++) {         if (a[i] >= 0)           cout << -a[i] - 1 <<    ;         else           cout << a[i] <<    ;       }       cout <<   n ;     }     ktj();   }
#include <bits/stdc++.h>   using namespace std;   int n, a, b, x, y, f[1000001];   void make(int l, int r) {     for (int i = l; i < r; i++) f[i] = i + 1;     f[r] = l;   }   int main() {     scanf( %d%d%d , &n, &a, &b);     for (x = 0; x * a <= n; x++)       if ((n - x * a) % b == 0) {         y = (n - x * a) / b;         break;       }     if (a * x + b * y != n) return printf( -1 n ), 0;     for (int i = 1; i <= x; i++) make(i * a - a + 1, i * a);     for (int i = 1; i <= y; i++) make(a * x + i * b - b + 1, a * x + i * b);     for (int i = 1; i <= n; i++) printf( %d%c , f[i], i < n ?     :   n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mx[] = {0, 2, 3, 1};   long long get_base(long long n) {     long long b = 4;     for (;;) {       if (b > n) return b / 4;       b <<= 2;     }     return 0;   }   long long func1(long long n) {     long long b = get_base(n);     return (n - b) / 3 + b;   }   long long func2(long long n) {     long long b = get_base(n);     long long m = (n - b - 1) / 3;     long long bb = b;     vector<int> v;     while (bb >= 4) {       v.push_back(m % 4);       m /= 4;       bb /= 4;     }     long long r = b + b;     bb = b / 4;     for (int i = v.size() - 1; i >= 0; i--) {       r += bb * mx[v[i]];       bb /= 4;     }     if (n == 2) return 2;     return r;   }   int main() {     int t;     long long n, ans;     ios::sync_with_stdio(0);     cin.tie(0);     cin >> t;     while (t--) {       cin >> n;       if (n % 3 == 0)         ans = func1(n - 2) ^ func2(n - 1);       else if (n % 3 == 1)         ans = func1(n);       else         ans = func2(n);       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3 * 1e5 + 10, D = 313, mod = 1e9 + 7;   long long hsh[maxn], n, ht, ans, tz, p[maxn];   string t, s[maxn];   vector<pair<int, int> > nei[maxn];   void find_p() {     p[0] = 1;     for (int i = 1; i < maxn; i++) p[i] = (p[i - 1] * D) % mod;   }   long long get_hash(int st, int en) {     return ((hsh[en] - hsh[st] * p[en - st]) % mod + mod) % mod;   }   void st_hash() {     for (int i = 0; i < tz; i++) hsh[i + 1] = ((hsh[i] * D) + t[i]) % mod;     ht = hsh[tz];     memset(hsh, 0, sizeof hsh);   }   void dfs(int v, int par, int ptr) {     for (auto u : nei[v])       if (u.first != par) {         for (auto i : s[u.second]) {           hsh[ptr + 1] = ((hsh[ptr] * D) + i) % mod;           ptr++;           if (ptr - tz >= 0 && get_hash(ptr - tz, ptr) == ht) ans++;         }         dfs(u.first, v, ptr);         ptr -= (int)s[u.second].size();       }   }   int main() {     ios::sync_with_stdio(0), cout.tie(0), cin.tie(0);     int a;     string b;     cin >> n;     for (int i = 1; i < n; i++) {       cin >> a >> b;       a--;       s[i] = b;       nei[i].push_back({a, i});       nei[a].push_back({i, i});     }     cin >> t;     tz = (int)t.size();     find_p();     st_hash();     dfs(0, -1, 0);     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t, n;     cin >> t;     while (t--) {       cin >> n;       long long int c = 0;       string s =   ;       cin >> s;       string h = s;       sort(h.begin(), h.end());       for (int j = 0; j < n; j++)         if (h.at(j) != s.at(j)) c++;       cout << c << endl;     }     return 0;   }
#include <bits/stdc++.h>   int n, k;   int count;   int x;   std::vector<int> negTemps;   std::priority_queue<int, std::vector<int>, std::greater<int> > intervals;   int main(void) {     scanf(  %d %d , &n, &k);     for (int i = 0; i <= n - 1; ++i) {       scanf(  %d , &x);       if (x < 0) {         negTemps.push_back(i);       }     }     if (!negTemps.size()) {       printf( 0 n );       return 0;     }     if (k < negTemps.size()) {       printf( -1 n );       return 0;     }     int count = negTemps.size() << 1;     int rem = k - negTemps.size();     auto it1 = negTemps.begin();     auto it2 = it1 + 1;     while (it2 != negTemps.end()) {       intervals.push(*it2 - 1 - *it1);       ++it1;       ++it2;     }     while (intervals.size() && intervals.top() <= rem) {       count -= 2;       rem -= intervals.top();       intervals.pop();     }     if (n - *it1 - 1 <= rem) --count;     printf( %d n , count);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e6 + 10;   long long a[maxn], sum[maxn], n, m, big = 0, tmp;   int main() {     scanf( %lld , &n);     for (int i = 0; i < n; i++) {       scanf( %lld , &m);       a[m]++;       big = max(big, m);     }     for (int i = 0; i <= big; i++) {       for (int j = 0; j <= big; j++) {         if ((long long)i * j > maxn - 10) break;         sum[i * j] += a[i] * a[j];         if (i == j) sum[i * j] -= a[i];       }     }     for (int i = 1; i < maxn; i++) sum[i] += sum[i - 1];     scanf( %lld , &m);     for (int i = 0; i < m; i++) {       scanf( %lld , &tmp);       printf( %lld n , n * (n - 1) - sum[tmp - 1]);     }   }
#include <bits/stdc++.h>   using namespace std;   const int M = 2e6 + 2e5;   const int N = M >> 1;   const int MX = 2e6;   const int D = 1e6;   double L[M], R[M];   int g[M];   struct pnt {     int x, y;     void input() { scanf( %d %d , &x, &y); }   } p[N];   int n;   double ans;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       p[i].input();       p[i].x += D, p[i].y += D;     }     function<void(int, double)> update = [&](int y, double x) {       L[y] = min(L[y], x);       R[y] = max(R[y], x);     };     for (int step = 0; step < 2; step++) {       for (int i = 0; i <= MX; i++) L[i] = M, R[i] = -1;       for (int i = 1; i <= n; i++) {         update(p[i].y, p[i].x);         int ni = i == n ? 1 : i + 1;         if (p[i].y == p[ni].y) continue;         double d = (p[ni].x - p[i].x) * 1.0 / (p[ni].y - p[i].y);         double cur;         cur = p[i].x;         for (int j = p[i].y + 1; j < p[ni].y; j++) {           cur += d;           update(j, cur);         }         cur = p[ni].x;         for (int j = p[ni].y + 1; j < p[i].y; j++) {           cur += d;           update(j, cur);         }       }       long long tot = 0;       for (int i = 0; i <= MX; i++) {         int lx = L[i] + 0.9999999;         int rx = R[i] + 0.0000001;         g[i] = rx >= lx ? rx - lx + 1 : 0;         tot += g[i];       }       double c = 0, d = 0, dd = 0;       for (int i = 0; i < MX; i++) {         double r = g[i] * 1.0 / (tot - 1);         d = d + dd + r;         dd = dd + r + r;         c = c + d;         ans += 1.0 * g[i + 1] / tot * c;       }       for (int i = 1; i <= n; i++) swap(p[i].x, p[i].y);     }     cout << fixed << setprecision(10) << ans << endl;     return 0;   }
#include <bits/stdc++.h>   static int v1[100001];   static int v2[100001];   int comp(const void *elem1, const void *elem2) {     return *((const int *)elem2) - *((const int *)elem1);   }   int main(void) {     int k, r, n;     scanf( %d %d %d , &n, &k, &r);     k--;     r--;     for (int i = 0; i < n; ++i) {       int a;       scanf( %d , &a);       v1[i] = a;     }     for (int i = 0; i < n; ++i) {       int a;       scanf( %d , &a);       v2[i] = a;     }     qsort(v1 + k, r - k + 1, sizeof(int), comp);     qsort(v2 + k, r - k + 1, sizeof(int), comp);     for (int i = 0; i < n; ++i) {       if (v1[i] != v2[i]) {         printf( LIE );         return 0;       }     }     printf( TRUTH );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int r, g, b, w;       cin >> r >> g >> b >> w;       int e = 0;       if (r % 2 == 0) e++;       if (g % 2 == 0) e++;       if (b % 2 == 0) e++;       if (e == 3) {         cout <<  YES n ;         continue;       } else if (e == 2) {         if (w % 2 == 0) {           cout <<  YES n ;           continue;         } else {           cout <<  NO n ;           continue;         }       } else if (e == 1) {         if (r > 0 && b > 0 && g > 0 && w % 2) {           cout <<  YES n ;           continue;         } else {           cout <<  NO n ;           continue;         }       } else {         cout <<  YES n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   long long powm(long long a, long long n) {     long long base = 1;     a %= MOD;     while (n) {       if (n & 1) base = base * a % MOD;       a = a * a % MOD;       n >>= 1;     }     return base;   }   int main() {     long long a, b, t;     cin >> a >> b >> t;     if ((a & 1) != (b & 1) && t == -1) {       cout << 0 << endl;       return 0;     }     long long ans = powm(2, a - 1);     ans = powm(ans, b - 1);     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long int modpow(long long int a, long long int n, long long int temp) {     long long int res = 1;     while (n > 0) {       res = (res * res) % temp;       if (n & 1) res = (res * a) % temp;       n /= 2;     }     return res;   }   long long int gcd(long long int a, long long int b) {     if (a == 0)       return (b);     else       return (gcd(b % a, a));   }   int main() {     int n;     vector<int> a;     cin >> n;     for (int i = 0; i < n; i++) {       int t;       cin >> t;       a.push_back(t);     }     sort(a.begin(), a.end());     for (int i = 0; i < n - 1; i++) {       while (a[i + 1] == a[i]) i++;       if (i >= n - 1) break;       if (2 * a[i] > a[i + 1]) {         cout <<  YES ;         return 0;       }     }     cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     while (n--) {       string s;       cin >> s;       int len = s.size();       int ok = 1;       for (int i = 0; i < len; i++) {         if (i == len) {           if (s[i - 1] !=  a )             s[i] =  a ;           else if (s[i - 1] !=  b )             s[i] =  b ;           else if (s[i - 1] !=  c )             s[i] =  c ;         }         if (s[i] ==  ?  && i != len) {           if (s[i - 1] !=  a  && s[i + 1] !=  a )             s[i] =  a ;           else if (s[i - 1] !=  b  && s[i + 1] !=  b )             s[i] =  b ;           else if (s[i - 1] !=  c  && s[i + 1] !=  c )             s[i] =  c ;         }       }       for (int i = 0; i < len - 1; i++) {         if (s[i] == s[i + 1]) ok = 0;       }       if (!ok)         cout <<  -1               <<   n ;       else         cout << s <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     while (scanf( %d%d , &n, &k) != EOF) {       int i;       for (i = 0; i <= n; i++) {         if (i * 2 + (n - i) * 5 >= k && i * 2 + (n - i) * 3 <= k) break;       }       printf( %d n , i);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   vector<bool> used;   vector<vector<int>> g;   int dfs(int v) {     if (used[v]) return 0;     used[v] = true;     int size = 1;     for (int i = 0; i < g[v].size(); i++) size += dfs(g[v][i]);     return size;   }   void Solve() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     g.assign(n, vector<int>());     vector<int> c(k);     for (int i = 0; i < k; i++) {       scanf( %d , &c[i]);       c[i]--;     }     for (int i = 0; i < m; i++) {       int u, v;       scanf( %d%d , &u, &v);       u--, v--;       g[u].push_back(v);       g[v].push_back(u);     }     used.assign(n, false);     int answer = 0;     int maxsize = 0;     for (int i = 0; i < k; i++) {       int size = dfs(c[i]);       maxsize = max(maxsize, size);       answer += size * (size - 1) / 2;     }     for (int i = 0; i < n; i++) {       if (!used[i]) {         int size = dfs(i);         answer += size * (size - 1) / 2 + size * maxsize;         maxsize += size;       }     }     printf( %d , answer - m);   }   int main() {     Solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, p;     cin >> t;     while (t--) {       cin >> n;       string s;       cin >> s;       int c = 0;       for (int i = 0; i < n; i++) {         if (s[i] == s[i + 1]) c++;       }       if (c == 1)         cout << c << endl;       else if (c % 2 != 0) {         c /= 2;         ++c;         cout << c << endl;       } else {         c /= 2;         cout << c << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   bool diverse(string x) {     sort(x.begin(), x.end());     for (int i = 1; i < x.size(); i++)       if (x[i] - x[i - 1] != 1) return 0;     return 1;   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     int n;     cin >> n;     cin.ignore(256,   n );     vector<string> in(n);     for (int i = 0; i < n; i++) getline(cin, in[i],   n );     for (int i = 0; i < n; i++) {       if (diverse(in[i]) || in[i].size() == 1)         cout <<  Yes  << endl;       else         cout <<  No  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<int> inchat;   set<int> inchat2;   bool leaders[100005];   int id[100005];   char in[100005];   int main() {     int N, M;     scanf( %d %d , &N, &M);     for (int i = 1; i <= N; i++) leaders[i] = true;     for (int i = 0; i < M; i++) {       scanf(  %c %d n , &in[i], &id[i]);     }     for (int i = M - 1; i > 0; i--) {       if (in[i] ==  - ) {         inchat2.insert(id[i]);       } else {         auto it = inchat2.find(id[i]);         if (it != inchat2.end()) {           inchat2.erase(it);         }       }     }     for (int i = 0; i < M; i++) {       if (in[i] ==  + ) {         if (inchat.size() == 0 &&             (inchat2.size() == 0 ||              (inchat2.size() == 1 && *inchat2.begin() == id[i]))) {           if (i == 0 || id[i - 1] == id[i]) {           } else {             leaders[id[i]] = false;           }         } else {           leaders[id[i]] = false;         }         inchat.insert(id[i]);       } else {         if (inchat.size() == 0 ||             (inchat.size() == 1 && (*inchat.begin() == id[i]))) {           if (inchat2.size() == 0 ||               (inchat2.size() == 1 && *inchat2.begin() == id[i])) {             if (i == M - 1 || id[i + 1] == id[i]) {             } else {               leaders[id[i]] = false;             }           } else {             leaders[id[i]] = false;           }         } else {           leaders[id[i]] = false;         }         auto it = inchat.find(id[i]);         if (it != inchat.end()) {           inchat.erase(it);         }       }       if (i != M - 1) {         if (in[i + 1] ==  - ) {           auto it = inchat2.find(id[i + 1]);           if (it != inchat2.end()) {             inchat2.erase(it);           }         } else {           inchat2.insert(id[i + 1]);         }       }     }     int cnt = 0;     for (int i = 1; i <= N; i++) {       if (leaders[i]) {         cnt++;       }     }     printf( %d n , cnt);     for (int i = 1; i <= N; i++) {       if (leaders[i]) {         printf( %d  , i);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int n;   pair<int, int> a[N];   pair<int, int> ra[N];   int ans[N];   pair<int, int> cross(pair<int, int> a, pair<int, int> b) {     if (a > b) swap(a, b);     return pair<int, int>(b.first, min(a.second, b.second));   }   void solve() {     ra[n] = pair<int, int>(a[n].first, a[n].first + a[n].second);     for (int i = n - 1; i >= 1; i--) {       ra[i] = cross(pair<int, int>(ra[i + 1].first - 1, ra[i + 1].second + 1),                     pair<int, int>(a[i].first, a[i].first + a[i].second));       if (ra[i].first > ra[i].second) {         cout <<  -1 ;         return;       }     }     ans[1] = ra[1].second;     for (int i = 2; i <= n; i++) {       pair<int, int> cur =           cross(pair<int, int>(ans[i - 1] - 1, ans[i - 1] + 1), ra[i]);       ans[i] = cur.second;     }     long long sum = 0;     for (int i = 1; i <= n; i++) sum += ans[i] - a[i].first;     cout << sum <<   n ;     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].first, &a[i].second);     solve();   }