malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int i, j, n, s = 0, c = 0, x, k, y, m, z, ans = 0;     cin >> n >> m;     cin >> x >> z >> y;     vector<long long int> a(n), b(m);     for (i = 0; i < n; i++) {       cin >> a[i];     }     for (i = 0; i < m; i++) {       cin >> b[i];     }     if (n == m) {       for (i = 0; i < n; i++) {         if (a[i] != b[i]) {           cout << -1;           return;         }       }       cout << 0;       return;     }     if (m > n) {       cout << -1;       return;     }     i = 0;     j = 0;     k = 0;     long long int ma = INT_MIN, l;     while (i < n && j < m) {       if (a[i] != b[j]) {         ma = max(ma, a[i]);         i++;       } else {         l = i - k;         if (l == 0) {           i++;           j++;           k = i;           continue;         }         long long int maxel;         if (k > 0) {           maxel = max(a[k - 1], a[i]);         } else {           maxel = a[i];         }         if (l < z) {           if (maxel < ma) {             cout << -1;             return;           }           ans += l * y;         } else {           if (y * z < x && maxel > ma) {             ans += l * y;           } else if (y * z < x) {             l = l - z;             ans += x + l * y;           } else {             long long int rem = l % z;             long long int gro = l / z;             ans += gro * x + rem * y;           }         }         j++;         i++;         k = i;         ma = INT_MIN;       }     }     l = n - k;     for (long long int q = i; q < n; q++) {       ma = max(ma, a[q]);     }     if (l > 0) {       long long int maxel = a[k - 1];       if (l < z) {         if (maxel < ma) {           cout << -1;           return;         }         ans += l * y;       } else {         if (y * z < x && maxel > ma) {           ans += l * y;         } else if (y * z < x) {           l = l - z;           ans += x + l * y;         } else {           long long int rem = l % z;           long long int gro = l / z;           ans += gro * x + rem * y;         }       }       j++;       i++;       k = i;       ma = INT_MIN;     }     cout << ans;   }   int main() {     long long int t;     t = 1;     while (t--) {       solve();       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k, l, m, n, p, x, y;     string s1, s2, s3, s4;     cin >> s1 >> s2;     cin >> n;     cout << s1 <<     << s2;     cout << endl;     for (i = 0; i < n; i++) {       cin >> s3 >> s4;       if (s3 == s1) {         s1 = s4;         cout << s1 <<     << s2;       }       if (s3 == s2) {         s2 = s4;         cout << s1 <<     << s2;       }       cout << endl;     }   }
#include <bits/stdc++.h>   using namespace std;      typedef long double             ld;   typedef long long int           int64;   typedef unsigned long long int  uint64;   typedef pair<int, int>          PII;   typedef vector<int>             VI;   typedef vector<long long>       VLL;   typedef vector<PII>             VII;   typedef vector<VI>              VVI;      #define MEM(a, b)               memset(a, (b), sizeof(a))   #define clr(a)                  fill(a, 0)      #define FOR(i, j, k, in)        for (int i = (j); i < (k); i += (in))   #define FORW(i, j, k, in)       for (int i = (j); i <= (k); i += (in))   #define RFOR(i, j, k, in)       for (int i = (j); i >= (k); i -= (in))      #define all(cont)               cont.begin(), cont.end()   #define rall(cont)              cont.rbegin(), cont.rend()   #define sz(cont)                int((cont).size())   #define pb                      push_back   #define mp                      make_pair   #define fi                      first   #define se                      second      #define IINF                    0x3f3f3f3f   #define LLINF                   1000111000111000111LL   #define PI                      3.1415926535897932384626433832795      #define FastIO                  ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);   #define hardio(name)            freopen(name .inp , r ,stdin), freopen(name .out , w ,stdout);   #define endl                      n          int main(int argc, char* argv[]) {       FastIO;   #ifndef ONLINE_JUDGE       hardio( input );   #endif          vector<int> a(26);       a[0] = 10010; a[1] = 11020; a[2] = 20011; a[3] = 21012;       a[4] = 11011; a[5] = 21021; a[6] = 22022; a[7] = 12021;       a[8] = 11011; a[9] = 12012; a[10] = 10120; a[11] = 11130;       a[12] = 20121; a[13] = 21122; a[14] = 11121; a[15] = 21131;       a[16] = 22132; a[17] = 12131; a[18] = 11121; a[19] = 12122;       a[20] = 10221; a[21] = 11231; a[22] = 12113; a[23] = 20222;       a[24] = 21223; a[25] = 11222;          int n; cin >> n;       string s =   ;       FOR(i, 0, n, 1) {           int cur = 0;           FOR(j, 0, 5, 1) {               int x; cin >> x;               cur = cur * 10 + x;           }           FOR(j, 0, 26, 1) if (cur == a[j]) { s += char(j +  a ); break; }       }          cout << s << endl;          return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long P = 1e9 + 7;   const int N = 2e5 + 5;   const int inf = 0x3f3f3f3f;   const double pi = acos(-1);   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int n, x, y, a[2];   vector<int> g[N];   void dfs(int x, int fa, int grp) {     a[grp]++;     for (auto u : g[x])       if (u != fa) dfs(u, x, 1 - grp);   }   int main() {     scanf( %d , &n);     for (int i = (1); i <= (n - 1); ++i) {       scanf( %d%d , &x, &y);       g[x].push_back(y), g[y].push_back(x);     }     dfs(1, 0, 0);     cout << min(a[0], a[1]) - 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s;   int main() {     cin >> s;     for (int i = 1; i < s.size(); i++) {       if (s[i] == s[i - 1])         for (char j =  a ; j <=  z ; j++)           if (s[i - 1] != j && s[i + 1] != j) {             s[i] = j;             break;           }     }     cout << s << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int t[n];     for (int i = 0; i < n; i++) {       cin >> t[i];     }     if (n == 1) {       cout << m - t[0];       return 0;     }     int ans = 0;     for (int k = 0; k < n * n * n; k++) {       set<int> st;       for (int i = 0; i < n; i++) {         if (st.count(t[i]) == 0 && t[i] < m) {           st.insert(t[i]);           t[i]++;         }       }       if (st.size()) {         ans++;       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int cnt;     bool leaf;     int nx[26];     node() {       leaf = false;       cnt = 0;       memset(nx, -1, sizeof nx);     }   };   string s, d;   node t[300100];   int ans, sz;   vector<string> v;   void add(string s) {     int cur = 0;     for (int i = 0; i < s.size(); i++) {       if (t[cur].nx[s[i] -  a ] != -1)         cur = t[cur].nx[s[i] -  a ];       else {         t[cur].nx[s[i] -  a ] = ++sz;         cur = sz;       }       t[cur].cnt++;     }     t[cur].leaf = true;   }   bool check(string s) {     int cur = 0;     for (int i = 0; i < s.size(); i++) {       if (t[cur].nx[s[i] -  a ] != -1)         cur = t[cur].nx[s[i] -  a ];       else         return false;     }     if (t[cur].leaf) return true;     return false;   }   int calc(string s) {     int d = -1, cur = 0, mn = -1;     for (int i = 0; i < s.size(); i++) {       if (t[cur].nx[s[i] -  a ] != -1)         cur = t[cur].nx[s[i] -  a ];       else         break;       d = i + 1;     }     if (d == -1) return s.size();     for (int i = 0; i < 26; i++)       if (t[cur].nx[i] != -1) return s.size();     cur = 0;     for (int i = 0; i < s.size(); i++) {       if (t[cur].nx[s[i] -  a ] != -1)         cur = t[cur].nx[s[i] -  a ];       else         break;       if (t[cur].cnt == 1) {         mn = i + 1;         break;       }     }     if (mn == -1) return s.size();     return min(s.size(), mn + 1 + s.size() - d);   }   int main() {     while (getline(cin, d)) {       s += d;       s +=  # ;       ans++;       for (int i = 0; i < d.size(); i++)         if (!isalpha(d[i])) ans++;     }     for (int i = 0; i < s.size();) {       if (!isalpha(s[i]))         i++;       else {         int j = i + 1;         while (true) {           if (!isalpha(s[j])) break;           j++;         }         j--;         v.push_back(s.substr(i, j - i + 1));         i = j + 1;       }     }     for (auto a : v) {       ans += calc(a);       if (!check(a)) add(a);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   using vi = vector<int>;   int n, m, p, nm;   vector<char> g;   vector<bool> vis;   vector<int> s, cnt;   vector<queue<int>> ex;   queue<int> q;   bool isEmpty(int v) {     if (v < 0) return false;     int i = v / m, j = v % m;     if (i < 0 || i >= n) return false;     if (j < 0 || j >= m) return false;     return (g[v] ==  . );   }   int dist(int u, int v) {     int x1 = u / m, y1 = u % m;     int x2 = v / m, y2 = v % m;     return abs(x1 - x2) + abs(y1 - y2);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> m >> p;     nm = n * m;     g.resize(nm);     s.resize(p);     cnt.resize(p);     ex.resize(p);     for (int& e : s) cin >> e;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int v = i * m + j;         char c;         cin >> c;         g[v] = c;         if (c !=  .  && c !=  # ) {           int pl = c -  1 ;           cnt[pl]++;           ex[pl].push(v);         }       }     }     bool found = true;     while (found) {       found = false;       for (int cur = 0; cur < p; cur++) {         int dis = 0;         while (dis != s[cur] && !ex[cur].empty()) {           queue<int> tmp;           while (!ex[cur].empty()) {             int u = ex[cur].front();             ex[cur].pop();             for (int mul : {-m, -1, m, 1}) {               int to = u + mul;               if (to / m != u / m && to % m != u % m) continue;               if (!isEmpty(to)) continue;               g[to] = cur +  1 ;               cnt[cur]++;               found = true;               tmp.push(to);             }           }           ex[cur] = tmp;           dis++;         }       }     }     for (int i : cnt) cout << i <<    ;     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int maxm = 1e5 + 5;   const int maxn = 1e5 + 5;   const int inf = 0x3f3f3f3f;   int n, m;   int a[maxn];   struct edge {     int u, v, w, next;   } e[maxm * 2];   int head[maxn];   bool vis[maxn];   int cnt;   priority_queue<int, vector<int>, greater<int> > q;   void addedge(int u, int v, int w) {     e[cnt].u = u;     e[cnt].v = v;     e[cnt].w = w;     e[cnt].next = head[u];     head[u] = cnt++;   }   void add_edge(int u, int v, int w) {     addedge(u, v, w);     addedge(v, u, w);   }   void bfs() {     vis[1] = 1;     q.push(1);     int temp = 1;     while (!q.empty()) {       int u = q.top();       a[temp++] = u;       q.pop();       for (int i = head[u]; ~i; i = e[i].next) {         int v = e[i].v;         if (!vis[v]) {           q.push(v);           vis[v] = 1;         }       }     }   }   int main() {     cnt = 0;     memset(vis, 0, sizeof(vis));     memset(head, -1, sizeof(head));     cin >> n >> m;     int u, v;     for (int i = 1; i <= m; i++) {       cin >> u >> v;       add_edge(u, v, 1);     }     bfs();     for (int i = 1; i <= n; i++) {       printf( %d%c , a[i], i == n ?   n  :    );     }   }
#include <bits/stdc++.h>   #ifndef ONLINE_JUDGE   #define debug(x) cout << #x <<  :   << (x) << endl   #else   #define debug(x)   #endif   using namespace std;   typedef long long ll;   typedef vector<int> vi;   const int maxn=1e6+7,inf=0x3f3f3f3f,mod=1e9+7;      string s[4];   int n;   int cnt1[4],cnt0[4];      int match(int i,int j)   {       if(cnt0[i]>=n && cnt0[j]>=n && cnt0[i]>=cnt0[j])       {           vi b;           b.push_back(-1);           int cc=0;           for(int k=0;k<2*n;++k) if(s[j][k]== 0  && cc<n) b.push_back(k),cc++;           b.push_back(2*n);           cc=0;           for(int k=0;k<2*n;++k)           {               if(s[i][k]== 0  && cc<n)               {                   for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x];                   cc++;               }               cout<<s[i][k];           }           for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x];           cout<<  n ;           return true;       }       else if(cnt1[i]>=n && cnt1[j]>=n && cnt1[i]>=cnt1[j])       {           vi b;           b.push_back(-1);           int cc=0;           for(int k=0;k<2*n;++k) if(s[j][k]== 1  && cc<n) b.push_back(k),cc++;           b.push_back(2*n);           cc=0;           for(int k=0;k<2*n;++k)           {               if(s[i][k]== 1  && cc<n)               {                   for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x];                   cc++;               }               cout<<s[i][k];           }           for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x];           cout<<  n ;           return true;       }       return false;   }      int main()   {       ios::sync_with_stdio(false);       cin.tie(0);       int kase;       cin>>kase;       while(kase--)       {           cin>>n;           s[0].clear(),s[1].clear(),s[2].clear();           cin>>s[0]>>s[1]>>s[2];           for(int j=0;j<3;++j) cnt1[j]=0,cnt0[j]=0;           for(int j=0;j<3;++j) for(int i=0;i<2*n;++i) cnt1[j]+=(s[j][i]== 1 ),cnt0[j]+=(s[j][i]== 0 );           bool ok=0;           for(int i=0;i<3;++i)           {               for(int j=0;j<3;++j)               {                   if(i==j) continue;                   if(match(i,j))                   {                       ok=1;                       break;                   }               }               if(ok) break;           }           assert(ok);       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string n;     int count = 0;     cin >> n;     stack<char> s;     for (int i = 0; i < n.length(); i++) {       if (n[i] ==  ( ) {         s.push(n[i]);       } else if (n[i] ==  ) ) {         if (s.size() != 0) {           char tops = s.top();           s.pop();           if (tops ==  ( ) {             count += 2;           }         }       }     }     cout << count << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct nodo {     int v, c, i, d;     nodo() {}     nodo(int V, int C, int I, int D) { v = V, c = C, i = I, d = D; }   };   int ans[200055], I[200055];   int n, m;   void solve(vector<vector<nodo> > v) {     int d[m + 5];     memset(d, -1, sizeof(d));     queue<int> Q;     Q.push(0);     while (!Q.empty()) {       int u = Q.front();       Q.pop();       for (int i = 0; i < v[u].size(); i++) {         int id = v[u][i].i;         if (d[id] == -1) {           d[id] = v[u][i].d;           int go = v[u][i].v;           I[go] -= v[u][i].c;           if (go != n - 1 && I[go] == 0) {             Q.push(go);           }         }       }     }     for (int i = 0; i < m; i++) ans[i] = d[i];   }   int main() {     scanf( %d%d , &n, &m);     vector<vector<nodo> > v(n + 5);     int a, b, c;     for (int i = 0; i < m; i++) {       scanf( %d%d%d , &a, &b, &c);       a--;       b--;       v[a].push_back(nodo(b, c, i, 0));       v[b].push_back(nodo(a, c, i, 1));       I[a] += c;       I[b] += c;     }     for (int i = 0; i < n; i++) I[i] /= 2;     solve(v);     for (int i = 0; i < m; i++) printf( %d n , ans[i]);   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e5 + 9;   vector<long long> g[maxn];   long long n, m, a, b;   bool used[maxn];   pair<long long, long long> dfs(long long v, long long u) {     used[v] = 1;     pair<long long, long long> ans(1, v == u);     for (long long to : g[v]) {       if (!used[to]) {         pair<long long, long long> cur = dfs(to, u);         ans.first += cur.first;         ans.second |= cur.second;       }     }     return ans;   }   void solve() {     cin >> n >> m >> a >> b;     a--;     b--;     for (long long i = 0; i < m; i++) {       long long x, y;       cin >> x >> y;       x--;       y--;       g[x].emplace_back(y);       g[y].emplace_back(x);     }     long long sz1 = 0, sz2 = 0;     for (long long i = 0; i < n; i++) used[i] = 0;     used[a] = 1;     for (long long to : g[a]) {       if (!used[to]) {         pair<long long, long long> cur = dfs(to, b);         if (!cur.second) sz1 += cur.first;       }     }     for (long long i = 0; i < n; i++) used[i] = 0;     used[b] = 1;     for (long long to : g[b]) {       if (!used[to]) {         pair<long long, long long> cur = dfs(to, a);         if (!cur.second) sz2 += cur.first;       }     }     for (long long i = 0; i < n; i++) {       g[i].resize(0);       g[i].shrink_to_fit();     }     cout << sz1 * sz2 <<   n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     for (long long j = 0; j < t; j++) solve();   }
#include <bits/stdc++.h>   using namespace std;   struct point {     int x, id;   };   struct node {     int l, r, t;     inline void in() { scanf( %d%d , &l, &r), --r; }     inline bool operator<(const node &a) const { return l < a.l; }   } a[500005];   int n, K;   long long sta[2][500005];   int f[500005], shu[500005], pos[500005];   set<node> cc;   vector<point> buc[500005];   inline void T_add(int x, int y) {     long long num = 1ll * x * y;     for (; x <= n; x += x & -x) sta[0][x] += y, sta[1][x] += num;   }   inline long long query(int opt, int x) {     long long sum = 0;     for (; x; x -= x & -x) sum += sta[opt][x];     return sum;   }   inline int calc_(int x) {     memset(shu, 0, sizeof(shu));     int ans = 0, cnt = 0, head = 1;     for (int i = 1; i <= n; ++i) {       ans += f[i];       int size = buc[i].size();       for (int j = 1; j <= size; ++j) {         point t = buc[i][j - 1];         if (t.id < head)           ans += t.x;         else           shu[t.id] += t.x;       }       while (head <= i && ans >= x) {         ans += shu[head];         ans -= a[head].r - a[head].l + 1;         ++head;       }       cnt += head - 1;       pos[i] = head - 1;       if (cnt > K) return K + 1;     }     return cnt;   }   int main() {     scanf( %d%d , &n, &K);     for (int i = 1; i <= n; ++i) a[i].in();     for (int i = 1; i <= n; ++i) {       f[i] = a[i].r - a[i].l + 1;       while (cc.size()) {         set<node>::iterator it;         it = cc.upper_bound((node){a[i].l - 1, 0, 0});         if (it == cc.end()) break;         if (it->l > a[i].r) break;         f[i] -= min(it->r, a[i].r) - it->l + 1;         buc[i].push_back((point){min(it->r, a[i].r) - it->l + 1, it->t});         int l = a[i].r + 1, r = it->r, t = it->t;         cc.erase(it);         if (l <= r) cc.insert((node){l, r, t});       }       if (cc.size() && cc.begin()->l < a[i].l) {         set<node>::iterator it;         it = cc.upper_bound((node){a[i].l - 1, 0, 0});         it--;         if (it->r >= a[i].l) {           f[i] -= min(it->r, a[i].r) - a[i].l + 1;           buc[i].push_back((point){min(it->r, a[i].r) - a[i].l + 1, it->t});           int l = it->l, r = it->r, t = it->t;           cc.erase(it);           cc.insert((node){l, a[i].l - 1, t});           if (r > a[i].r) cc.insert((node){a[i].r + 1, r, t});         }       }       cc.insert((node){a[i].l, a[i].r, i});     }     int Min, num;     for (int l = 1, r = 1e9, mid, cnt; l <= r;) {       mid = l + r >> 1;       cnt = calc_(mid);       if (cnt > K)         l = mid + 1;       else         Min = mid, r = mid - 1;     }     num = calc_(Min);     long long ans = 0;     for (int i = 1; i <= n; ++i) {       int size = buc[i].size();       for (int j = 1; j <= size; ++j) {         point t = buc[i][j - 1];         T_add(t.id, -t.x);       }       T_add(i, a[i].r - a[i].l + 1);       if (pos[i]) {         ans += query(1, pos[i]);         ans += (query(0, i) - query(0, pos[i])) * pos[i];       }     }     printf( %lld n , ans + 1ll * (K - num) * (Min - 1));   }
#include <bits/stdc++.h>   using namespace std;   int main() {     map<long long, pair<long long, long long> > mpl;     vector<long long> ml;     long long n, q, xi, amyor = 0;     scanf( %lld %lld , &n, &q);     deque<long long> dq;     for (int i = (0); i < int(n); i++) {       scanf( %lld , &xi);       amyor = max(amyor, xi);       dq.push_back(xi);     }     long long cnt = 0;     while (1) {       if (dq.front() == amyor) break;       long long A = dq.front();       dq.pop_front();       long long B = dq.front();       dq.pop_front();       if (B > A) {         dq.push_front(B);         dq.push_back(A);       } else {         dq.push_front(A);         dq.push_back(B);       }       mpl[cnt] = {A, B};       cnt++;     }     dq.pop_front();     while (!dq.empty()) {       ml.push_back(dq.front());       dq.pop_front();     }     while (q--) {       long long m;       scanf( %lld , &m);       if (m <= cnt) {         m--;         long long x, y;         printf( %lld %lld n , mpl[m].first, mpl[m].second);       } else {         long long N = cnt;         long long rango = (m - N);         N = ml.size();         long long D = rango % N;         if (D == 0) D = N;         D--;         m = D;         printf( %lld %lld n , amyor, ml[m]);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<vector<int> > g;   vector<bool> used;   vector<bool> cyc;   vector<int> cl;   vector<int> p;   int cycle_st, cycle_end;   bool dfs(int v, int parent) {     cl[v] = 1;     for (size_t i = 0; i < g[v].size(); ++i) {       int to = g[v][i];       if (to == parent) continue;       if (cl[to] == 0) {         p[to] = v;         if (dfs(to, v)) return true;       } else if (cl[to] == 1) {         cycle_end = v;         cycle_st = to;         return true;       }     }     cl[v] = 2;     return false;   }   bool dfs2(int k, int step) {     if (cyc[k]) {       cout << step <<    ;       return true;     }     used[k] = true;     for (vector<int>::iterator i = g[k].begin(); i != g[k].end(); ++i) {       if (!used[*i]) {         if (dfs2(*i, step + 1)) return true;       }     }     return false;   }   int main(int argc, const char* argv[]) {     cin >> n;     vector<int> temp;     g.assign(n, temp);     cyc.assign(n, false);     for (int i = 0; i < n; i++) {       int k1, k2;       cin >> k1 >> k2;       g[k1 - 1].push_back(k2 - 1);       g[k2 - 1].push_back(k1 - 1);     }     p.assign(n, -1);     cl.assign(n, 0);     cycle_st = -1;     for (int i = 0; i < n; ++i)       if (dfs(i, i)) break;     cyc[cycle_st] = true;     for (int v = cycle_end; v != cycle_st; v = p[v]) {       cyc[v] = true;     }     for (int i = 0; i < n; i++) {       used.assign(n, false);       dfs2(i, 0);     }     return 0;   }