malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 5;   struct Node {     int p;     vector<int> v;     Node() { p = 0, v = {1}; }     inline int size() { return v.size(); }   };   vector<int> G[maxn];   int ans[maxn];   void merge(Node& a, Node b) {     b.v.push_back(0);     if (a.size() < b.size()) swap(a, b);     int shift = a.size() - b.size();     for (int i = 0; i < b.size(); i++) {       a.v[i + shift] += b.v[i];       if (a.v[i + shift] >= a.v[a.p]) a.p = i + shift;     }   }   Node dfs(int u, int fa) {     Node cur;     for (auto& v : G[u]) {       if (v != fa) {         merge(cur, dfs(v, u));       }     }     ans[u] = cur.size() - cur.p - 1;     return cur;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       G[u].push_back(v), G[v].push_back(u);     }     dfs(1, 0);     for (int i = 1; i <= n; i++) {       cout << ans[i] <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int sad = 0;   int* visited;   vector<int>* adj;   void dfs(int curr, int prev) {     visited[curr] = 1;     for (auto& e : adj[curr]) {       if (e == prev) continue;       if (visited[e])         sad++;       else         dfs(e, curr);     }   }   int main() {     int n, k;     cin >> n >> k;     visited = new int[n];     memset(visited, 0, n * sizeof(int));     adj = new vector<int>[n];     unordered_set<string> chk;     int dups = 0;     for (int i = 0; i < k; i++) {       int a, b;       scanf( %d%d , &a, &b);       int tmp = min(a, b);       int tmp2 = max(a, b);       string key = to_string(tmp) + to_string(tmp2);       if (chk.count(key)) {         dups++;         continue;       }       chk.insert(key);       adj[a - 1].push_back(b - 1);       adj[b - 1].push_back(a - 1);     }     for (int i = 0; i < n; i++) {       if (visited[i]) continue;       dfs(i, -1337);     }     cout << sad / 2 + dups;   }
#include <bits/stdc++.h>   using namespace std;   int vis[10100];   int dist[10100];   int pre[10100], cntt[30];   char str[10100];   struct Edge {     int u, v, c, cost, next;   } edge[1000100];   int anss, head[10100], cnt;   void init() {     cnt = 0;     memset(head, -1, sizeof(head));   }   void addedge(int u, int v, int c, int cost) {     edge[cnt].u = u;     edge[cnt].v = v;     edge[cnt].cost = cost;     edge[cnt].c = c;     edge[cnt].next = head[u];     head[u] = cnt++;     edge[cnt].u = v;     edge[cnt].v = u;     edge[cnt].cost = -cost;     edge[cnt].c = 0;     edge[cnt].next = head[v];     head[v] = cnt++;   }   bool spfa(int begin, int end) {     int u, v;     queue<int> q;     for (int i = 0; i <= end + 2; i++) {       pre[i] = -1;       vis[i] = 0;       dist[i] = 99999999;     }     vis[begin] = 1;     dist[begin] = 0;     q.push(begin);     while (!q.empty()) {       u = q.front();       q.pop();       vis[u] = 0;       for (int i = head[u]; i != -1; i = edge[i].next) {         if (edge[i].c > 0) {           v = edge[i].v;           if (dist[v] > dist[u] + edge[i].cost) {             dist[v] = dist[u] + edge[i].cost;             pre[v] = i;             if (!vis[v]) {               vis[v] = true;               q.push(v);             }           }         }       }     }     return dist[end] != 99999999;   }   int MCMF(int begin, int end) {     int ans = 0, flow;     int flow_sum = 0;     while (spfa(begin, end)) {       flow = 99999999;       for (int i = pre[end]; i != -1; i = pre[edge[i].u])         if (edge[i].c < flow) flow = edge[i].c;       for (int i = pre[end]; i != -1; i = pre[edge[i].u]) {         edge[i].c -= flow;         edge[i ^ 1].c += flow;       }       ans += dist[end] * flow;       flow_sum += flow;     }     anss = flow_sum;     return ans;   }   int main() {     int n, m, a, b, c, cost, i, j, cc, len;     scanf( %s , str);     len = strlen(str);     init();     for (i = 0; i < strlen(str); i++) cntt[str[i] - 97]++;     scanf( %d , &n);     for (i = n + 1; i <= n + 26; i++) addedge(i, n + 27, cntt[i - n - 1], 0);     for (i = 1; i <= n; i++) {       scanf( %s%d , str, &cc);       addedge(0, i, cc, i);       memset(cntt, 0, sizeof(cntt));       for (j = 0; j < strlen(str); j++) cntt[str[j] - 97]++;       for (j = 0; j < 26; j++) addedge(i, n + 1 + j, cntt[j], 0);     }     int z = MCMF(0, n + 27);     if (anss == len)       printf( %d n , z);     else       printf( -1 );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 100;   char s[N];   int n, m, t[N];   int a[N * 4], b[N * 4], ab[N * 4], ba[N * 4], aba[N * 4], tag[N * 4];   void pushup(int now) {     a[now] = max(a[(now << 1)], a[(now << 1 | 1)]);     b[now] = min(b[(now << 1)], b[(now << 1 | 1)]);     ab[now] = max(ab[(now << 1)],                   max(ab[(now << 1 | 1)], a[(now << 1)] - 2 * b[(now << 1 | 1)]));     ba[now] = max(ba[(now << 1)],                   max(ba[(now << 1 | 1)], a[(now << 1 | 1)] - 2 * b[(now << 1)]));     aba[now] = max(max(aba[(now << 1)], aba[(now << 1 | 1)]),                    max(ab[(now << 1)] + a[(now << 1 | 1)],                        ba[(now << 1 | 1)] + a[(now << 1)]));   }   void build(int now, int l, int r) {     if (l + 1 == r) {       a[now] = b[now] = t[l];       ab[now] = ba[now] = -t[l];       return;     }     int mid = (l + r) >> 1;     build((now << 1), l, mid);     build((now << 1 | 1), mid, r);     pushup(now);   }   void down(int now, int val) {     a[now] += val;     b[now] += val;     ab[now] -= val;     ba[now] -= val;     tag[now] += val;   }   void pushdown(int now) {     down((now << 1), tag[now]);     down((now << 1 | 1), tag[now]);     tag[now] = 0;   }   void Insert(int now, int l, int r, int a, int b, int val) {     if (a <= l && b >= r - 1) {       down(now, val);       return;     }     int mid = (l + r) >> 1;     pushdown(now);     if (a < mid) Insert((now << 1), l, mid, a, b, val);     if (b >= mid) Insert((now << 1 | 1), mid, r, a, b, val);     pushup(now);   }   int main() {     int x, y, z;     scanf( %d%d , &n, &m);     n = n * 2 - 2;     scanf( %s , s + 1);     for (int i = 1; i <= n; ++i) {       if (s[i] ==  ( )         t[i] = t[i - 1] + 1;       else         t[i] = t[i - 1] - 1;       Insert(1, 1, n + 1, i, i, t[i]);     }     printf( %d n , aba[1]);     for (int i = 0; i < m; ++i) {       scanf( %d%d , &x, &y);       if (x > y) swap(x, y);       if (s[x] ==  ( )         z = -2;       else         z = 2;       Insert(1, 1, n + 1, x, y - 1, z);       swap(s[x], s[y]);       printf( %d n , aba[1]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps(1e-8), Inf(1e10);   const double pi(3.14159265358979);   const int N = 6060;   struct point {     double x, y;   } p[N + N] = {};   int n, q, k = 0, a, b, x[N] = {};   double ans[N] = {};   void init() {     cin >> n >> q >> a >> b;     for (int i = 1; i <= n; ++i) cin >> x[i];   }   void work() {     p[++k] = {1.0, 2.0 * (-x[1])};     p[++k] = {Inf, 2.0 * (Inf - x[1])};     ans[1] = x[1];     double zero = x[1];     for (int i = 2, d = 1; i <= n; ++i) {       for (int j = k; j > d; --j) p[j + 2] = p[j];       k += 2;       p[d + 1] = {zero, 0};       p[d + 2] = {zero, 0};       for (int j = 1; j <= d + 1; ++j) p[j].x += a;       for (int j = d + 2; j <= k; ++j) p[j].x += b;       for (int j = 1; j <= k; ++j) p[j].y += 2 * (p[j].x - x[i]);       if (p[1].y > 0)         zero = p[1].x, d = 0;       else {         for (d = 1; p[d].y * p[d + 1].y > 0; ++d)           ;         zero = (-p[d].y) / (p[d + 1].y - p[d].y) * (p[d + 1].x - p[d].x) + p[d].x;       }       ans[i] = zero;     }     ans[n] = min<double>(ans[n], q);     for (int i = n - 1; i >= 1; --i) {       ans[i] = max(ans[i + 1] - b, ans[i]);       ans[i] = min(ans[i + 1] - a, ans[i]);     }     double sum = 0;     for (int i = 1; i <= n; ++i) sum += (x[i] - ans[i]) * (x[i] - ans[i]);     for (int i = 1; i <= n; ++i) printf( %.6f  , ans[i]);     printf(  n%.6f n , sum);   }   int main() {     init();     work();     return 0;   }
#include <bits/stdc++.h>   int n, m, num;   char tab[100][100];   int xx[10000][4];   int yy[10000][4];   int ch[100][100];   int id[100][100];   int getdir(int cd, int dd) {     if (cd == 1)       return (dd + 1) % 4;     else       return dd;   }   int main() {     scanf( %d %d , &n, &num);     for (int i = 0; i < n; i++) {       scanf( %s , tab[i]);       m = strlen(tab[i]);       for (int j = 0; j < m; j++) {         tab[i][j] -=  0 ;       }     }     int count = 1;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (tab[i][j] == 0) {           id[i][j] = 0;           continue;         }         if (i - 1 >= 0 && tab[i - 1][j] == tab[i][j]) {           id[i][j] = id[i - 1][j];         } else if (j - 1 >= 0 && tab[i][j - 1] == tab[i][j]) {           id[i][j] = id[i][j - 1];         } else {           id[i][j] = count++;           xx[id[i][j]][0] = i;           yy[id[i][j]][0] = j;           xx[id[i][j]][1] = i;           yy[id[i][j]][3] = j;         }         if ((i + 1 >= n || tab[i][j] != tab[i + 1][j]) &&             (j + 1 >= m || tab[i][j] != tab[i][j + 1])) {           yy[id[i][j]][1] = j;           xx[id[i][j]][3] = i;           xx[id[i][j]][2] = i;           yy[id[i][j]][2] = j;         }       }     }     int tx, ty, cd = 0, dd = 1;     int pos = id[tx][ty];     for (int p = 0; p < num; p++) {       int dir = getdir(cd, dd);       tx = xx[pos][dir];       ty = yy[pos][dir];       int dx, dy;       if (dd == 0) {         dx = -1;         dy = 0;       }       if (dd == 1) {         dx = 0;         dy = 1;       }       if (dd == 2) {         dx = 1;         dy = 0;       }       if (dd == 3) {         dx = 0;         dy = -1;       }       if (tx + dx < 0 || tx + dx >= n || ty + dy < 0 || ty + dy >= m ||           id[tx + dx][ty + dy] == 0) {         if (cd == 0) {           cd = 1;         } else {           cd = 0;           dd += 1;           dd %= 4;         }       } else {         pos = id[tx + dx][ty + dy];         tx += dx;         ty += dy;       }     }     printf( %d n , tab[tx][ty]);   }
#include  bits/stdc++.h    using namespace std;   using ll = long long;   using pii = pair<int,int>;   using pll = pair<ll,ll>;   template<typename T>   int sz(const T &a){return int(a.size());}   const int MN=601;   int dist[MN];   bool used[MN];   int arr[MN][MN];   int main(){       cin.tie(NULL);       ios_base::sync_with_stdio(false);       int n,m,a,b,c;       cin>>n>>m;       for(int i=0;i<n;i++)for(int j=0;j<n;j++)arr[i][j]=INT_MAX;       for(int i=1;i<=m;i++){           cin>>a>>b>>c;           arr[a][b]=c;       }       for(int i=0;i<n;i++){           for(int j=0;j<n;j++)dist[j]=INT_MAX,used[j]=false;           dist[i]=0;           for(int level=1;level<n;level++){               int cur=-1;               for(int j=0;j<n;j++){                   if(!used[j]&&(cur==-1||dist[j]<dist[cur]))cur=j;               }               used[cur]=true;               int mi=INT_MAX;               for(int j=0;j<n;j++)if(arr[cur][(j-dist[cur]%n+n)%n]!=INT_MAX)mi=min(mi,arr[cur][(j-dist[cur]%n+n)%n]+n-1-j);               for(int j=0;j<n;j++){                   mi=min(mi+1,arr[cur][(j-dist[cur]%n+n)%n]);                   dist[j]=min(dist[j],dist[cur]+mi);               }           }           for(int j=0;j<n;j++)printf( %d%c ,dist[j],   n [j==n-1]);       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double Pi = acos(-1.0);   const int MAXN = 1e6 + 5;   int n, m, t;   int v[MAXN];   char s[MAXN];   int main() {     cin >> t;     while (t--) {       cin >> n >> s + 1;       int sum1 = 0, sum2 = 0, now = 0;       for (int i = 1; i <= n * 2; i++) {         if (s[i] ==  1 )           now++;         else {           sum1 += max(now - 1, 0);           now = 0;         }       }       if (now) sum1 += now - 1;       now = 0;       for (int i = 1; i <= n * 2; i++) {         if (s[i] ==  0 )           now++;         else {           sum2 += max(now - 1, 0);           now = 0;         }       }       if (now) sum2 += now - 1;       cout << max(sum1, sum2) << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 300005;   pair<int, int> seg[2 * MAX];   void update(int l, int r, int fight, int value) {     for (l += MAX, r += MAX; l < r; l >>= 1, r >>= 1) {       if (l & 1) seg[l++] = make_pair(fight, value);       if (r & 1) seg[--r] = make_pair(fight, value);     }   }   int query(int p) {     pair<int, int> res = make_pair(INT_MAX, 0);     for (p += MAX; p > 1; p >>= 1) res = min(res, seg[p]);     return res.second;   }   int main() {     int n, m;     cin >> n >> m;     vector<pair<pair<int, int>, int> > v;     for (int i = 0; i < 2 * MAX; i++) seg[i] = make_pair(INT_MAX, 0);     for (int i = 0; i < m; i++) {       int a, b, x;       cin >> a >> b >> x;       v.push_back(make_pair(make_pair(a, b), x));     }     for (int i = m - 1; i >= 0; i--) {       int l = v[i].first.first;       int r = v[i].first.second;       int win = v[i].second;       update(l - 1, win - 1, i, win);       update(win, r, i, win);     }     for (int i = 0; i < n; i++) cout << query(i) <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int *c;   int main() {     int a, b, t, f, n, m, d, g, sum = 0;     int mas[4];     cin >> mas[0] >> mas[1] >> mas[2] >> mas[3] >> a >> b;     c = min_element(mas, mas + 4);     *c = min(*c, b + 1);     if (*c - a >= 0)       cout << *c - a;     else       cout <<  0 ;   }
#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int t, n, m, x = 0, y = 0, flag = 0, cnt = 0, sum = 0, sum1 = 0, sum0 = 0,                  flag1 = 0;     int a1, a2, a3, b1, b2, b3;     vector<int> d;     cin >> t;     int a[t];     for (int i = 0; i < t; i++) {       cin >> a[i];     }     x = t - 1;     while (x > 0 && a[x - 1] < a[x]) {       x--;     }     cout << x <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, tab[70], p;   int main() {     scanf( %d %d , &n, &k);     for (int i = 0; i < n; i++) scanf( %d , &tab[i]);     sort(tab, tab + n);     if (k > n) {       printf( -1 n );       return 0;     }     printf( %d %d n , tab[n - k], tab[n - k]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = int(1e6) + 10;   const int MAX_M = 21;   int n, r, c;   int a[MAX_N], b[MAX_N];   int f[MAX_N][MAX_M];   char buffer[MAX_N * 6];   int main() {     scanf( %d%d%d n , &n, &r, &c);     c++;     gets(buffer);     int m = strlen(buffer);     buffer[m++] =    ;     for (int i = 0, j = 0; i < n; i++, j++) {       b[i] = j;       for (a[i] = 0; j < m && buffer[j] !=    ; j++, a[i]++)         ;     }     b[n] = m;     for (int i = 0, j = 0, sum = 0; i < n; i++) {       for (; j < n && sum + a[j] + 1 <= c; sum += a[j++] + 1)         ;       f[i][0] = j - i;       if (i < j) {         sum -= a[i] + 1;       } else {         j++;       }     }     for (int j = 1; 1 << j <= r; j++) {       for (int i = 0; i < n; i++) {         f[i][j] = f[i][j - 1] + f[i + f[i][j - 1]][j - 1];       }     }     int result = 0, start = 0, l = 0;     for (; 1 << l <= r; l++)       ;     for (int i = 0; i < n; i++) {       int sum = 0, tmp = r;       for (int k = l; k >= 0; k--) {         if (tmp - (1 << k) >= 0) {           sum += f[i + sum][k];           tmp -= 1 << k;         }       }       if (sum > result) {         result = sum;         start = i;       }     }     for (int i = 0, p = start; i < r; i++) {       int sum = 0;       for (; p < n && (sum + a[p] + 1) <= c; sum += a[p++] + 1) {         if (sum > 0) {           putchar(   );         }         for (int j = b[p]; j < b[p + 1] - 1; j++) {           putchar(buffer[j]);         }       }       if (sum > 0) {         puts(  );       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int s[n + 1];     s[0] = 0;     for (int i = 1; i <= n; i++) cin >> s[i];     int dp[n + 1][n + 1];     memset(dp, 0, sizeof(dp));     int ans = 0;     for (int i = 1; i <= n; i++) {       int prev = 0;       for (int j = 0; j < i; j++) {         dp[i][j] = dp[j][prev] + 1;         if (s[i] == s[j]) prev = j;         ans = max(ans, dp[i][j]);       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int32_t MAX = 1000000 + 1;   int64_t ans[MAX];   int main(int argc, char *argv[]) {     ios_base::sync_with_stdio(false);     ifstream in( input.txt );     ofstream out( output.txt );     int64_t n, q, k = 0, type, evenSh = 0, oddSh = 0, parity = 0, lastSh = 0;     scanf( %I64d , &n);     scanf( %I64d , &q);     for (int32_t t = 0; t < q; ++t) {       scanf( %I64d , &type);       if (type == 1) {         scanf( %I64d , &k);         evenSh += k;         oddSh += k;         lastSh += k;       } else {         if ((parity + lastSh) % 2 == 0) {           lastSh = 0;           parity = 1;           evenSh -= 1;           oddSh += 1;         } else {           lastSh = 0;           parity = 0;           evenSh += 1;           oddSh -= 1;         }       }     }     if (oddSh < 0) oddSh = n - ((-oddSh) % n);     if (evenSh < 0) evenSh = n - ((-evenSh) % n);     for (int64_t i = 0; i < n; ++i)       if ((i + 1) % 2 == 1)         ans[(i + oddSh) % n] = i + 1;       else         ans[(i + evenSh) % n] = i + 1;     for (int64_t i = 0; i < n; ++i) printf( %I64d  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long X[200200];   int Xi[200200], xi;   int X2[200200], x2;   int Xm[200200], xm;   int a[200200], w[200200], x, n, q;   inline void update(int u) {     for (; u <= n; u += u & -u) {       X[u] += x;       Xi[u] = (Xi[u] + xi) % 1000000007;       X2[u] = (X2[u] + x2) % 1000000007;       Xm[u] = (Xm[u] + xm) % 1000000007;     }   }   inline long long calc_x(int u, long long ans = 0) {     for (; u; u -= u & -u) ans += X[u];     return ans;   }   inline void calc(int u) {     x = 0, xi = 0, x2 = 0, xm = 0;     for (; u; u -= u & -u) {       x = (x + X[u]) % 1000000007;       xi = (xi + Xi[u]) % 1000000007;       x2 = (x2 + X2[u]) % 1000000007;       xm = (xm + Xm[u]) % 1000000007;     }   }   int collect(int u, int L, int R) {     calc(L - 1);     int tx = x, txi = xi, tx2 = x2, txm = xm;     calc(u - 1);     tx = (x - tx + 1000000007) % 1000000007,     txi = (xi - txi + 1000000007) % 1000000007,     tx2 = (x2 - tx2 + 1000000007) % 1000000007,     txm = (xm - txm + 1000000007) % 1000000007;     long long ans = 0;     if (L < u) {       ans += (long long)a[u] * tx;       ans -= txm;       ans += txi - (long long)u * tx;     }     tx = (x + w[u]) % 1000000007, txi = (xi + (long long)u * w[u]) % 1000000007,     tx2 = (x2 + (long long)w[u] * w[u]) % 1000000007,     txm = (xm + (long long)a[u] * w[u]) % 1000000007;     calc(R);     tx = (x - tx + 1000000007) % 1000000007,     txi = (xi - txi + 1000000007) % 1000000007,     tx2 = (x2 - tx2 + 1000000007) % 1000000007,     txm = (xm - txm + 1000000007) % 1000000007;     if (u < R) {       ans += txm;       ans -= (long long)a[u] * tx;       ans += (long long)u * tx - txi;     }     ans %= 1000000007;     if (ans < 0) ans += 1000000007;     return ans;   }   int main() {     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i <= n; i++) {       scanf( %d , w + i);       x = w[i];       xi = (long long)i * x % 1000000007;       x2 = (long long)x * x % 1000000007;       xm = (long long)a[i] * x % 1000000007;       update(i);     }     while (q--) {       int L, R;       scanf( %d %d , &L, &R);       if (L < 0) {         L = -L;         x = -w[L];         xi = 1000000007 - (long long)L * w[L] % 1000000007;         x2 = 1000000007 - (long long)w[L] * w[L] % 1000000007;         xm = 1000000007 - (long long)a[L] * w[L] % 1000000007;         update(L);         w[L] = R;         x = w[L];         xi = (long long)L * x % 1000000007;         x2 = (long long)x * x % 1000000007;         xm = (long long)a[L] * x % 1000000007;         update(L);       } else if (L == R)         puts( 0 );       else {         long long L_ = calc_x(L - 1), all = calc_x(R) - L_;         int st = L - 1, ed = R;         while (st + 1 < ed) {           int md = st + ed >> 1;           long long sa = calc_x(md) - L_, sb = all - sa;           if (sa > sb)             ed = md;           else             st = md;         }         printf( %d n , collect(ed, L, R));       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 101010;   int n, ans;   int pa[N << 1], ns[N], nt[N], ma[N], pos[N];   vector<pair<int, int> > res;   char s[N], sr[N];   void Manacher(char *s, int n, int *pa) {     pa[0] = 1;     for (int i = 1, j = 0; i < (n << 1) - 1; ++i) {       int p = i >> 1, q = i - p, r = ((j + 1) >> 1) + pa[j] - 1;       pa[i] = r < q ? 0 : min(r - q + 1, pa[(j << 1) - i]);       while (0 <= p - pa[i] && q + pa[i] < n && s[p - pa[i]] == s[q + pa[i]])         pa[i]++;       if (q + pa[i] - 1 > r) j = i;     }     for (int i = (0); i < (n); ++i) pa[i] = pa[i << 1];   }   void kmp(char *s, int *ns, char *t, int *nt) {     int lens = strlen(s);     int lent = strlen(t);     nt[0] = -1;     for (int i = 0, j = -1; i < lens; ++i) {       while (j >= 0 && s[i] != t[j + 1]) j = nt[j];       if (s[i] == t[j + 1]) ++j;       ns[i] = j;       if (j + 1 == lent) j = nt[j];     }   }   int main() {     std::ios::sync_with_stdio(0);     std::cin.tie(0);     cin >> s;     n = strlen(s);     Manacher(s, n, pa);     for (int i = (0); i < (n); ++i) sr[i] = s[n - 1 - i];     sr[n] =   0 ;     kmp(sr + 1, nt + 1, sr, nt);     kmp(s, ns, sr, nt);     for (int i = (0); i < (n); ++i)       if (~ns[i]) ++ns[i];     ma[0] = ns[0];     pos[0] = 0;     for (int i = (1); i < (n); ++i) {       ma[i] = ma[i - 1];       pos[i] = pos[i - 1];       if (ma[i] < ns[i]) {         ma[i] = ns[i];         pos[i] = i;       }     }     for (int i = (0); i < (n); ++i) {       int k = i - pa[i];       int t = max(0, min(k < 0 ? 0 : ma[k], n - i - pa[i]));       int c = pa[i] * 2 - 1 + t * 2;       if (ans < c) {         ans = c;         res.clear();         res.push_back(make_pair(i - pa[i] + 1, pa[i] * 2 - 1));         if (t) {           res.push_back(make_pair(n - t, t));           int o = pos[k] - 2 * ns[pos[k]] + t + 1;           res.push_back(make_pair(o, t));         }       }     }     sort(res.begin(), res.end());     cout << (int)res.size() <<   n ;     for (auto u : res) cout << u.first + 1 <<     << u.second <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int a[maxn], b[maxn];   int main() {     int n, i, j;     cin >> n;     for (i = 0; i < n; i++) cin >> j, a[j] = i;     multiset<int> s;     for (i = 0; i < n; i++) cin >> b[i], s.insert(i - a[b[i]]);     for (int i = 0; i < n; i++) {       multiset<int>::iterator it = s.lower_bound(i);       int ans = maxn;       if (it != s.end()) ans = min(ans, *it - i);       if (it != s.begin()) ans = min(ans, i - *(--it));       printf( %d n , ans);       it = s.find(i - a[b[i]]);       s.erase(it);       s.insert(i - a[b[i]] + n);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   const int sq = 320;   int n, q, p[N], r[N], dp[N];   int f(int x) {     int y = x;     for (int i = 0; i < sq; i++) x = p[x];     return dp[y] = x;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> q;     for (int i = 1; i <= n; i++) {       cin >> p[i];       r[p[i]] = i;     }     for (int i = 1; i <= n; i++) f(i);     while (q--) {       int t, x, y;       cin >> t >> x >> y;       if (t == 1) {         swap(r[p[x]], r[p[y]]);         swap(p[x], p[y]);         vector<int> v = {x, y};         for (int au : v) {           int nxt = f(au);           for (int i = 0; i < sq; i++) {             dp[au] = nxt;             au = r[au];             nxt = r[nxt];           }         }       } else {         while (y >= sq) {           y -= sq;           x = dp[x];         }         while (y) {           y--;           x = p[x];         }         cout << x <<   n ;       }     }     return 0;   }