malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int dl = 10069;   void bib() {     for (int i = 0; i < (int)10; i++) {       cout <<   a ;     }   }   const int M = 4 * 3 * 100 * 1000 + 10;   int seg[M];   int ans[M / 2];   int n, m;   void battle(int s, int e, int winner, int l = 0, int r = n, int id = 1) {     if (s >= r || e <= l) return;     if (seg[id] != -1) return;     if (s <= l && r <= e && (winner < l || winner >= r)) {       seg[id] = winner;     }     if (r - l < 2) return;     int mid = (r + l) / 2;     battle(s, e, winner, l, mid, id * 2);     battle(s, e, winner, mid, r, id * 2 + 1);   }   void print(int l = 0, int r = n, int id = 1) {     if (r - l == 1) {       ans[l] = seg[id] + 1;       return;     }     int mid = (l + r) / 2;     print(l, mid, id * 2);     print(mid, r, id * 2 + 1);   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m;     for (int i = 0; i < (int)M; i++) seg[i] = -1;     for (int i = 0; i < (int)m; i++) {       int l, r, x;       cin >> l >> r >> x;       l--;       x--;       battle(l, r, x);     }     print();     for (int i = 0; i < (int)n; i++) {       cout << ans[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   int a[510];   int dp[510][510];   int dfs(int l, int r) {     if (dp[l][r] != -1) {       return dp[l][r];     }     if (l > r) {       return 0;     }     if (l == r) {       return 1;     }     int mi = 1 + dfs(l + 1, r);     if (a[l] == a[l + 1]) {       mi = min(mi, 1 + dfs(l + 2, r));     }     for (int i = l + 2; i <= r; ++i) {       if (a[i] == a[l]) {         mi = min(mi, dfs(l + 1, i - 1) + dfs(i + 1, r));       }     }     return dp[l][r] = mi;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     memset(dp, -1, sizeof(dp));     printf( %d n , dfs(0, n - 1));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     while (t--) {       long long n, a, b, c, d;       cin >> n >> a >> b >> c >> d;       int x = 0;       if (((c + d) < n * (a - b)) || ((c - d) > n * (a + b)))         cout <<  No  << endl;       else         cout <<  Yes  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[1005];   bool t[1005];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     memset(t, 0, sizeof(t));     int count = 0;     int taken = 0;     bool right = true;     while (1) {       if (right) {         for (int i = 0; i < n; i++) {           if (taken >= a[i] && !t[i]) {             t[i] = true;             taken++;           }         }       } else {         for (int i = n - 1; i >= 0; i--) {           if (taken >= a[i] && !t[i]) {             t[i] = true;             taken++;           }         }       }       if (taken == n) break;       count++;       right = !right;     }     printf( %d n , count);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, speed, limit, t6, ans;   stack<int> st;   int main() {     cin >> N;     for (int t, i = 1; i <= N; i++) {       scanf( %d , &t);       if (t == 6) {         t6++;       } else if (t == 4) {         t6 = 0;       } else if (t == 2) {         ans += t6;         t6 = 0;       } else if (t == 1) {         scanf( %d , &speed);         while (!st.empty() && speed > st.top()) {           ans++;           st.pop();         }       } else if (t == 5) {         while (!st.empty()) st.pop();       } else if (t == 3) {         scanf( %d , &limit);         st.push(limit);         while (!st.empty() && speed > st.top()) {           ans++;           st.pop();         }       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, t, dp[20][4][4][11][11];   int main() {     scanf( %d%d , &n, &t);     memset(dp, 0, sizeof(dp));     for (int i = 0; i != 4; ++i)       for (int j = 0; j != 4; ++j) dp[1][i][j][0][0] = i != j;     for (int i = 2; i != n; ++i)       for (int j = 0; j != 4; ++j)         for (int k = 0; k != 4; ++k)           for (int l = 0; l <= t; ++l)             for (int m = 0; m <= t; ++m)               for (int o = 0; o != 4; ++o)                 dp[i][k][o][l + (j < k && k > o)][m + (j > k && k < o)] +=                     k == o ? 0 : dp[i - 1][j][k][l][m];     int ans = 0;     for (int i = 0; i != 4; ++i)       for (int j = 0; j != 4; ++j) ans += dp[n - 1][i][j][t][t - 1];     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 0x3f3f3f3f;   const int N = 2e5 + 10;   const int M = 1e6 + 10;   const long long mod = 1e9 + 7;   const double PI = acos(-1);   int sq = 3000;   int cntq, cntc;   map<int, int> mp;   int vis[N];   int cnt[N];   int num[N];   int ans[N];   struct query {     int l, r, z, id;   } qq[N];   struct change {     int x, y;   } ch[N];   bool cmp1(query a, query b) {     if (a.l / sq == b.l / sq) {       if (a.r / sq == b.r / sq)         return a.z < b.z;       else         return a.r < b.r;     } else       return a.l < b.l;   }   void update(int x, int y) {     vis[cnt[num[x]]]--;     cnt[num[x]] += y;     vis[cnt[num[x]]]++;   }   void solve() {     int l = 1, r = 1, c = 0, res;     update(1, 1);     for (int i = 1; i <= cntq; ++i) {       while (r < qq[i].r) update(++r, 1);       while (r > qq[i].r) update(r--, -1);       while (l > qq[i].l) update(--l, 1);       while (l < qq[i].l) update(l++, -1);       while (c > qq[i].z) {         if (ch[c].x >= l && ch[c].x <= r) update(ch[c].x, -1);         swap(ch[c].y, num[ch[c].x]);         if (ch[c].x >= l && ch[c].x <= r) update(ch[c].x, 1);         c--;       }       while (c < qq[i].z) {         c++;         if (ch[c].x >= l && ch[c].x <= r) update(ch[c].x, -1);         swap(ch[c].y, num[ch[c].x]);         if (ch[c].x >= l && ch[c].x <= r) update(ch[c].x, 1);       }       res = 1;       while (vis[res]) res++;       ans[qq[i].id] = res;     }   }   int main() {     int n, q, tot = 0, x, op, l, r;     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; ++i) {       scanf( %d , &x);       if (mp[x])         num[i] = mp[x];       else         num[i] = mp[x] = ++tot;     }     for (int i = 1; i <= q; ++i) {       scanf( %d%d%d , &op, &l, &r);       if (op == 1) {         ++cntq;         qq[cntq] = {l, r, cntc, cntq};       } else {         ++cntc;         if (mp[r])           r = mp[r];         else           r = mp[r] = ++tot;         ch[cntc] = {l, r};       }     }     sort(qq + 1, qq + cntq + 1, cmp1);     solve();     for (int i = 1; i <= cntq; ++i) printf( %d n , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > solve(int N) {     vector<vector<int> > res(2, vector<int>(1, 1));     for (int n = 2, r = 0; n <= N; ++n) {       res[r].push_back(n);       ++r;       if (r >= int((res).size())) {         vector<int> new_row;         for (const vector<int>& r : res) new_row.push_back(r.back());         res.push_back(new_row);         r = 0;       }     }     return res;   }   int main(int argc, char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int N;     cin >> N;     vector<vector<int> > res = solve(N);     cout << int((res).size()) <<   n ;     for (const vector<int>& row : res) {       for (int x : row) cout << x <<    ;       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 505;   int n, m;   int v[MAXN];   int head[MAXN];   double e[MAXN * MAXN];   int a, b, c;   int main() {     while (scanf( %d%d , &n, &m) != EOF) {       for (int i = 1; i <= n; i++) scanf( %d , &v[i]);       for (int i = 1; i <= m; i++) {         scanf( %d%d%d , &a, &b, &c);         e[i] = 1.0 * (v[a] + v[b]) / c;       }       double ans = 0.0;       for (int i = 1; i <= m; i++) ans = max(ans, e[i]);       printf( %.12lf n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct debugger {     template <typename T>     debugger& operator,(const T& v) {       cerr << v <<    ;       return *this;     }   } debug1;   template <typename T1, typename T2>   inline ostream& operator<<(ostream& os, const pair<T1, T2>& p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T>   inline ostream& operator<<(ostream& os, const vector<T>& v) {     bool first = true;     os <<  [ ;     for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {       if (!first) os <<  ,  ;       os << *ii;       first = false;     }     return os <<  ] ;   }   const long long inf = numeric_limits<long long>::min();   const int NN = 112345;   long long dp[NN][3], cnt[NN];   bool vis[NN][3];   long long f(int x, int last) {     last = ((cnt[x - 1] == 0 and last == 0) ? 2 : last);     if (cnt[x] == 0 and last == 0) return inf;     if (x > 100000) return 0LL;     if (vis[x][last]) return dp[x][last];     vis[x][last] = true;     long long ans = 0;     if (last == 0) {       ans = cnt[x] * (long long)x;       ans += f(x + 1, 1);     } else if (last == 1)       ans = f(x + 1, 2);     else {       ans = max(ans, f(x + 1, 0));       if (cnt[x] > 0) {         long long add = cnt[x] * (long long)x;         ans = max(ans, add + f(x + 1, 1));       }     }     dp[x][last] = ans;     return ans;   }   void solve() {     int n, num;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> num;       cnt[num]++;     }     long long ans = f(1, 2);     cout << ans <<   n ;   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m, a, b;     cin >> n >> m >> a >> b;     int ans = 3;     if (a % m == 1 && b % m == 0) {       ans = min(ans, 1);     }     if (a % m == 1 || b % m == 0) {       ans = min(ans, 2);     }     int bf = (a - 1) / m;     int bs = (b - 1) / m;     if (bf == bs) {       ans = min(ans, 1);     }     if (bf + 1 == bs) {       ans = min(ans, 2);     }     if (a % m == 1 && b == n) {       ans = min(ans, 1);     }     if (b == n) {       ans = min(ans, 2);     }     int p1 = (a - 1) % m;     int p2 = (b - 1) % m;     if (p1 == p2 + 1) {       ans = min(ans, 2);     }     if (m == 1) {       ans = min(ans, 1);     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5;   vector<int> g[N];   int deg[N];   int cnt[N];   void dfs(int v, int &l) {     for (int i = 0; i < g[v].size(); i++) {       cnt[g[v][i]] = l + 1;       l++;       dfs(g[v][i], l);       l--;     }   }   int main() {     ios_base::sync_with_stdio(), cout.tie(NULL), cin.tie(NULL);     ;     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> deg[i];       if (deg[i] != -1) {         g[deg[i]].push_back(i);       }     }     int ans = 1;     for (int i = 1; i <= n; i++) {       if (deg[i] == -1) {         int level = 1;         dfs(i, level);       }     }     for (int i = 1; i <= n; i++) {       ans = max(ans, cnt[i]);     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool getmin(T *a, const T &b) {     if (b < *a) {       *a = b;       return true;     }     return false;   }   template <class T>   inline bool getmax(T *a, const T &b) {     if (b > *a) {       *a = b;       return true;     }     return false;   }   template <class T>   inline void read(T *a) {     char c;     while (isspace(c = getchar())) {     }     bool flag = 0;     if (c ==  - )       flag = 1, *a = 0;     else       *a = c - 48;     while (isdigit(c = getchar())) *a = *a * 10 + c - 48;     if (flag) *a = -*a;   }   const int mo = 1000000007;   template <class T>   T pow(T a, T b, int c = mo) {     T res = 1;     for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c)       if (b & i) res = 1LL * res * a % c;     return res;   }   const int N = 100100;   int n;   int a[N];   int s1[N], s2[N];   pair<int, int> ans[N];   int check(int x) {     int j = 0, i = 1, num1 = 0, num2 = 0, last = 0;     while (i <= n) {       int l = i, r = n, res = 0;       while (l <= r) {         int mid = (l + r) >> 1;         if (max(s1[mid] - s1[j], s2[mid] - s2[j]) >= x)           res = mid, r = mid - 1;         else           l = mid + 1;       }       if (!res) return 0;       if (s1[res] - s1[j] == x)         ++num1, last = 1;       else         ++num2, last = 2;       j = res, i = res + 1;     }     if (num1 > num2 && last == 1)       return num1;     else if (num2 > num1 && last == 2)       return num2;     return 0;   }   int main() {     cin >> n;     for (int i = (1); i <= (n); ++i) {       read(a + i);       s1[i] = s1[i - 1] + (a[i] == 1);       s2[i] = s2[i - 1] + (a[i] == 2);     }     int m = 0;     for (int i = (1); i <= (n); ++i) {       int z = check(i);       if (z) ans[++m] = std::make_pair(z, i);     }     sort(ans + 1, ans + m + 1);     cout << m << endl;     for (int i = (1); i <= (m); ++i)       printf( %d %d n , ans[i].first, ans[i].second);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   int n, m, dis[55][55], fast[55], slow[55], g1, g2, s1, s2;   long long f[55][55][55];   bool cang[55], cans[55], canb[55];   long long work(int g, int b, int su, int sd) {     memset(f, 0, sizeof(f));     memset(cang, false, sizeof(cang));     memset(cans, false, sizeof(cans));     memset(canb, false, sizeof(canb));     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i == j) {           continue;         }         if (dis[i][j] <= g) {           cang[i] = true;         }         if (dis[i][j] > su && dis[i][j] < sd) {           cans[i] = true;         }         if (dis[i][j] >= b) {           canb[i] = true;         }       }     }     f[0][0][0] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= i; j++) {         for (int k = 0; j + k <= i; k++) {           if (cang[i]) {             f[i][j + 1][k] += f[i - 1][j][k];           }           if (cans[i]) {             f[i][j][k + 1] += f[i - 1][j][k];           }           if (canb[i]) {             f[i][j][k] += f[i - 1][j][k];           }         }       }     }     long long res = 0;     for (int i = g1; i <= g2; i++) {       for (int j = s1; j <= s2; j++) {         res += f[n][i][j];       }     }     return res;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i < n; i++) {       for (int j = i + 1; j <= n; j++) {         dis[i][j] = dis[j][i] = inf;       }     }     for (int i = 0; i < m; i++) {       int x, y, w;       scanf( %d%d%d , &x, &y, &w);       dis[x][y] = dis[y][x] = w;     }     scanf( %d%d%d%d , &g1, &g2, &s1, &s2);     for (int k = 1; k <= n; k++) {       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (dis[i][j] < inf) {           dis[i][j] = dis[i][j] * n + i;         }       }     }     for (int i = 1; i <= n; i++) {       fast[i] = inf;       slow[i] = -inf;       for (int j = 1; j <= n; j++) {         if (i == j) {           continue;         }         fast[i] = min(fast[i], dis[i][j]);         slow[i] = max(slow[i], dis[i][j]);       }     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         ans += work(fast[i], slow[j], fast[i], slow[j]) -                work(fast[i] - 1, slow[j], fast[i], slow[j]) -                work(fast[i], slow[j] + 1, fast[i], slow[j]) +                work(fast[i] - 1, slow[j] + 1, fast[i], slow[j]);       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   stack<int> S;   int n, m, q, x, y;   struct rs_tree {     int hed[200005], ver[600005], nxt[600005], tot, cnt;     int dep[200005], fa[200005], top[200005], siz[200005], son[200005];     void push(int x, int y) {       ver[++tot] = y;       nxt[tot] = hed[x];       hed[x] = tot;     }     void dfs1(int now, int en) {       dep[now] = dep[en] + 1;       fa[now] = en;       for (int i = hed[now]; i; i = nxt[i]) {         if (ver[i] == en) continue;         dfs1(ver[i], now);         if (son[now] == 0 || siz[son[now]] < siz[ver[i]]) son[now] = ver[i];         siz[now] += siz[ver[i]];       }       return;     }     void dfs2(int now, int TOP) {       top[now] = TOP;       if (son[now]) dfs2(son[now], TOP);       for (int i = hed[now]; i; i = nxt[i]) {         if (ver[i] == fa[now] || ver[i] == son[now]) continue;         dfs2(ver[i], ver[i]);       }       return;     }     int find_lca(int x, int y) {       int sum = dep[x] + dep[y];       while (top[x] != top[y]) {         if (dep[top[x]] < dep[top[y]]) swap(x, y);         x = fa[top[x]];       }       if (dep[x] > dep[y]) swap(x, y);       sum -= (2 * dep[x] - 1);       return sum >> 1;     }   } RST;   struct graph {     int hed[100005], ver[1000005], nxt[1000005], tot;     int dfn[100005], low[100005], num, cnt, vis[100005];     void push(int x, int y) {       ver[++tot] = y;       nxt[tot] = hed[x];       hed[x] = tot;     }     void dfs(int now, int en) {       S.push(now);       dfn[now] = low[now] = ++cnt;       vis[now] = 1;       for (int i = hed[now]; i; i = nxt[i]) {         if (!dfn[ver[i]]) {           dfs(ver[i], now);           low[now] = min(low[now], low[ver[i]]);           if (low[ver[i]] >= dfn[now]) {             num++;             int z;             do {               z = S.top();               S.pop();               RST.push(z, num);               RST.push(num, z);               vis[z] = 0;             } while (z != ver[i]);             RST.push(now, num);             RST.push(num, now);           }         } else if (vis[ver[i]])           low[now] = min(low[now], dfn[ver[i]]);       }     }     void tarjan() {       num = n;       cnt = 0;       dfs(1, 0);     }   } G;   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &x, &y);       G.push(x, y);       G.push(y, x);     }     G.tarjan();     RST.dfs1(1, 0);     RST.dfs2(1, 1);     for (int i = 1; i <= q; i++) {       scanf( %d%d , &x, &y);       printf( %d n , RST.find_lca(x, y));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long i, j, m, n, p, q, r, ans, l, ch = 1;   string s;   int f;   vector<long long> v;   int main() {     cin >> s;     l = s.size();     f = s.find( bear , 0);     while (f >= 0) {       p = f + 1;       ch = abs(p - ch) + 1;       q = p + 3;       r = (l - q) + 1;       ans += (r * ch);       ch = p + 1;       f = s.find( bear , ch);     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long num = 0, neg = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) neg = -1;       c = getchar();     }     while (isdigit(c)) {       num = (num << 3) + (num << 1) + c -  0 ;       c = getchar();     }     return num * neg;   }   const long long mod = 998244353;   long long n, a[100010], m, g[300010], f[300010], ans;   inline long long qpow(long long x, long long k) {     long long res = 1;     while (k) {       if (k & 1) res = res * x % mod;       x = x * x % mod;       k >>= 1;     }     return res;   }   int main() {     n = read();     g[0] = n - 1;     for (int i = 1; i <= n; i++) a[i] = read(), m += a[i];     for (int i = 1; i <= m; i++)       g[i] = (m * (n - 1) % mod + (n - 1) * i % mod * g[i - 1] % mod) % mod *              qpow(m - i, mod - 2) % mod;     f[m] = g[m];     for (int i = m - 1; i >= 0; i--) f[i] = (f[i + 1] + g[i]) % mod;     for (int i = 1; i <= n; i++) ans = (ans + f[a[i]]) % mod;     ans = (ans - ((n - 1) * f[0] % mod) + mod) % mod;     printf( %lld n , ans * qpow(n, mod - 2) % mod);     return 0;   }
#include <bits/stdc++.h>   const int pi = acos(-1);   const long long MOD = 1000000007;   const int M = 100005;   const int N = 1000006;   using namespace std;   int n, m, x, y, ans;   string s;   char a[55][55];   bool vis[55][55];   vector<string> v;   void calc(int i, string s) {     if (i == 4) {       v.push_back(s);       return;     }     bool u = 0, d = 0, l = 0, r = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  u ) u = 1;       if (s[i] ==  d ) d = 1;       if (s[i] ==  l ) l = 1;       if (s[i] ==  r ) r = 1;     }     if (!u) calc(i + 1, s +  u );     if (!d) calc(i + 1, s +  d );     if (!l) calc(i + 1, s +  l );     if (!r) calc(i + 1, s +  r );   }   int bfs(string k) {     map<char, char> lol;     lol[ 0 ] = k[0];     lol[ 1 ] = k[1];     lol[ 2 ] = k[2];     lol[ 3 ] = k[3];     queue<pair<int, pair<int, int> > > q;     q.push(make_pair(0, make_pair(x, y)));     while (!q.empty()) {       int x = q.front().second.first;       int y = q.front().second.second;       int i = q.front().first;       q.pop();       if (x < 0 || x >= n || y < 0 || y >= m) return 0;       if (a[x][y] ==  # ) return 0;       if (a[x][y] ==  E ) return 1;       if (i >= s.size()) return 0;       char lel = lol[s[i]];       if (lel ==  d ) q.push(make_pair(i + 1, make_pair(x + 1, y)));       if (lel ==  u ) q.push(make_pair(i + 1, make_pair(x - 1, y)));       if (lel ==  r ) q.push(make_pair(i + 1, make_pair(x, y + 1)));       if (lel ==  l ) q.push(make_pair(i + 1, make_pair(x, y - 1)));     }     return 0;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) scanf( %s , a[i]);     cin >> s;     calc(0,   );     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (a[i][j] ==  S ) {           x = i;           y = j;         }     for (int i = 0; i < v.size(); i++) ans += bfs(v[i]);     cout << ans;     return 0;   }