malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   const int maxn = 8005;   const int inf = 0x3f3f3f3f;   const long long mod = 998244353;   using namespace std;   long long vis[111];   int main() {     long long x = 1ll, y;     int n, q;     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; ++i) {       scanf( %lld , &y);       for (int j = 0; j <= 33; ++j) {         if ((x << j) == y) {           ++vis[j];           break;         }       }     }     for (int i = 1; i <= q; ++i) {       scanf( %lld , &y);       long long cnt = 0;       for (int j = 33; j >= 0; --j) {         cnt += min(y / (x << j), vis[j]);         y -= (x << j) * min(y / (x << j), vis[j]);       }       if (y) cnt = -1;       printf( %lld n , cnt);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     char sym[] = { A ,  O ,  M ,  H ,  I ,  o ,  T ,  V ,                    W ,  U ,  X ,  Y ,  x ,  w ,  v };     map<char, char> mp;     mp[ b ] =  d ;     mp[ d ] =  b ;     mp[ p ] =  q ;     mp[ q ] =  p ;     string s;     cin >> s;     int n = s.length();     int a = n / 2 - 1, b;     if (n & 1)       b = n / 2 + 1;     else       b = n / 2;     if (n & 1 && count(sym, sym + 16, s[n / 2]) == 0) {       cout <<  NIE ;       return 0;     }     for (int i = 0, j = n - 1; i <= a && j >= b; i++, j--) {       if (s[i] == s[j] && count(sym, sym + 16, s[i]) == 1) {         continue;       }       if (mp[s[i]] == s[j] && mp[s[j]] == s[i]) {         continue;       }       cout <<  NIE ;       return 0;     }     cout <<  TAK ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a[100] = {0,   1,  1, 1, 2,  1, 2,  1, 5,  2, 2,  1, 5,  1, 2, 1,                   14,  1,  5, 1, 5,  2, 2,  1, 15, 2, 2,  5, 4,  1, 4, 1,                   51,  1,  2, 1, 14, 1, 2,  2, 14, 1, 6,  1, 4,  2, 2, 1,                   52,  2,  5, 1, 5,  1, 15, 2, 13, 2, 2,  1, 13, 1, 2, 4,                   267, 1,  4, 1, 5,  1, 4,  1, 50, 1, 2,  3, 4,  1, 6, 1,                   52,  15, 2, 1, 15, 1, 2,  1, 12, 1, 10, 1, 4,  2};     long long b;     cin >> b;     cout << a[b] << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9 + 7;   const pair<int, int> pinf = pair<int, int>(inf, inf);   int up(int n) {     int ret = 1;     while (ret < n) ret <<= 1;     return ret;   }   template <class T>   class segtree {     vector<T> arr;     T min(int l, int r, int cl, int cr, int pos) {       if (cr <= l || r <= cl) return pinf;       if (l <= cl && cr <= r) return arr[pos];       int ct = (cl + cr) / 2;       return std::min(min(l, r, cl, ct, 2 * pos), min(l, r, ct, cr, 2 * pos + 1));     }     void pset(int pos, T x) {       arr[pos] = x;       while (pos /= 2) arr[pos] = std::min(arr[2 * pos], arr[2 * pos + 1]);     }       public:     segtree(int n) : arr(2 * up(n)) {}     T min(int l, int r) { return min(l, r, 0, arr.size() / 2, 1); }     void set(int pos, T x) { pset(pos + arr.size() / 2, x); }   };   class sumtree {     vector<int> arr;     int sum(int l, int r, int cl, int cr, int pos) {       if (cr <= l || r <= cl) return 0;       if (l <= cl && cr <= r) return arr[pos];       int ct = (cl + cr) / 2;       return sum(l, r, cl, ct, 2 * pos) + sum(l, r, ct, cr, 2 * pos + 1);     }     void pset(int pos, int x) {       arr[pos] = x;       while (pos /= 2) arr[pos] = arr[2 * pos] + arr[2 * pos + 1];     }       public:     sumtree(int n) : arr(2 * up(n)) {}     int sum(int l, int r) { return sum(l, r, 0, arr.size() / 2, 1); }     void set(int p, int x) { pset(p + arr.size() / 2, x); }   };   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     vector<int> arr(n);     for (auto &it : arr) cin >> it;     segtree<pair<int, int>> st(n);     sumtree cards(n);     for (int i = 0; i < arr.size(); i++) st.set(i, {arr[i], i});     long long cnt = 0;     int pos = 0;     sort(arr.begin(), arr.end());     for (int i = 0; i < n; i++) {       auto q = st.min(pos, n);       if (q.first == arr[i]) {         cnt += q.second - pos - cards.sum(pos, q.second);         pos = q.second;         st.set(pos, pinf);         cards.set(pos, 1);       } else {         cnt += n - pos - cards.sum(pos, n);         i--;         pos = 0;       }     }     cout << cnt + n << endl;     return 0;   }
#include <bits/stdc++.h>   bool V[11][11];   int T[11][11];   int R = 0, C = 0, N = 0, K = 0, t1 = 0, t2 = 0;   int W = 0;   int main() {     scanf( %d%d%d%d , &R, &C, &N, &K);     for (int i = 1; i <= N; i++) {       scanf( %d%d , &t1, &t2);       V[t1][t2] = true;     }     for (int i = 1; i <= R; i++)       for (int j = 1; j <= C; j++) {         T[i][j] = T[i - 1][j] + T[i][j - 1] - T[i - 1][j - 1];         if (V[i][j]) T[i][j]++;       }     for (int i = 1; i <= R; i++)       for (int j = 1; j <= C; j++)         for (int k = i; k <= R; k++)           for (int l = j; l <= C; l++) {             if (T[k][l] - T[i - 1][l] - T[k][j - 1] + T[i - 1][j - 1] >= K) W++;           }     printf( %d n , W);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int da[550];   int dp[550][1050];   int dp2[550][550];   struct kutija {     int in, out, w, s, v;   } K[505];   bool cmp(const kutija &a, const kutija &b) {     if (a.in != b.in) return a.in < b.in;     if (a.out != b.out) return a.out > b.out;     return a.s > b.s;   }   int rek(int x, int g) {     if (g < 0) return -1e9;     if (x == n + 1) return 0;     if (dp[x][g] != -1) return dp[x][g];     memset(dp2[x], 0, sizeof dp2[x]);     for (int i = x + 1; i <= n; i++) {       dp2[x][i + 1] = max(dp2[x][i + 1], dp2[x][i]);       if (K[i].in >= K[x].out || K[i].out > K[x].out) continue;       dp2[x][da[i]] = max(dp2[x][da[i]],                           dp2[x][i] + K[i].v + rek(i, min(K[i].s, g - K[i].w)));     }     dp[x][g] = dp2[x][n + 1];     return dp2[x][n + 1];   }   int main(void) {     scanf( %d%d , &n, &K[0].s);     K[0].in = 0;     K[0].out = 2 * n;     K[0].w = 0;     K[0].v = 0;     for (int i = 1; i <= n; i++)       scanf( %d%d%d%d%d , &K[i].in, &K[i].out, &K[i].w, &K[i].s, &K[i].v);     sort(K, K + n + 1, cmp);     memset(dp, -1, sizeof dp);     for (int i = 0; i <= n; i++) {       da[i] = n + 1;       for (int j = 0; j < n + 1; j++) {         if (K[i].out <= K[j].in) {           da[i] = j;           break;         }       }     }     printf( %d , rek(0, K[0].s));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[5][8];   int vis[123124];   struct Mp {     int a[30];     Mp() { memset(a, 0, sizeof(a)); }     bool operator<(const Mp &x) const {       for (int i = 0; i < 25; ++i)         if (x.a[i] != a[i]) return a[i] < x.a[i];       return 0;     }     void swap(int &a, int &b, int &c, int &d) {       int t = a;       a = b;       b = c;       c = d;       d = t;     }     Mp Rotate1() {       Mp res = *this;       swap(res.a[1], res.a[3], res.a[4], res.a[2]);       swap(res.a[13], res.a[5], res.a[17], res.a[21]);       swap(res.a[14], res.a[6], res.a[18], res.a[22]);       swap(res.a[15], res.a[7], res.a[19], res.a[23]);       swap(res.a[16], res.a[8], res.a[20], res.a[24]);       swap(res.a[9], res.a[10], res.a[12], res.a[11]);       return res;     }     Mp Rotate2() {       Mp res = *this;       swap(res.a[1], res.a[3], res.a[4], res.a[2]);       swap(res.a[13], res.a[5], res.a[17], res.a[21]);       swap(res.a[14], res.a[6], res.a[18], res.a[22]);       return res;     }     Mp Rotate3() {       Mp res = *this;       swap(res.a[2], res.a[23], res.a[10], res.a[6]);       swap(res.a[4], res.a[21], res.a[12], res.a[8]);       swap(res.a[17], res.a[18], res.a[20], res.a[19]);       return res;     }     bool check() {       for (int i = 1; i <= 24; i += 4) {         for (int j = i + 1; j <= i + 3; ++j)           if (a[j] != a[i]) return 0;       }       return 1;     }     void OUT() {       for (int i = 1; i <= 24; ++i) cout << a[i] <<    ;       cout << endl;     }   };   map<Mp, bool> hs;   int main() {     Mp init;     for (int i = 1; i <= 24; ++i) cin >> init.a[i];     queue<Mp> Q;     Q.push(init);     while (!Q.empty()) {       Mp now = Q.front();       Q.pop();       Mp news;       news = now.Rotate1();       if (!hs[news]) {         hs[news] = 1;         Q.push(news);       }       news = now.Rotate2();       if (news.check()) {         puts( yes );         return 0;       }       news = now.Rotate2().Rotate2().Rotate2();       if (news.check()) {         puts( yes );         return 0;       }       news = now.Rotate3();       if (news.check()) {         puts( yes );         return 0;       }       news = now.Rotate3().Rotate3().Rotate3();       if (news.check()) {         puts( yes );         return 0;       }     }     puts( no );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1011;   int n, m;   char a[N][N];   int b[N][N], ts;   const int dx[] = {-1, -1, 0, 0, 1, 1};   const int dy[] = {1, -1, 2, -2, 1, -1};   void output() {     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         int x = i, y = j;         if (a[i][j] ==  . ) {           printf( . );           continue;         }         if (a[i][j] ==  < )           ++y;         else if (a[i][j] ==  > )           --y;         else if (a[i][j] ==  A )           ++x;         else if (a[i][j] ==  V )           --x;         else if (a[i][j] !=  -  && a[i][j] !=  | ) {           printf( %c , a[i][j]);           continue;         }         char k =  a ;         if (x % 2 == 1) k =  c ;         if (x / 2 % 2 != y / 2 % 2) ++k;         putchar(k);       }       puts(  );     }   }   bool dfs(int x, int y) {     b[x][y] = ts;     if (x > 0 && x < n - 1 && a[x - 1][y] ==  w  && a[x + 1][y] ==  w ) {       a[x - 1][y] =  A ;       a[x][y] =  | ;       a[x + 1][y] =  V ;       int nc = 0;       for (int d = 0; d < 6; ++d) {         int nx = x + dy[d];         int ny = y + dx[d];         if (nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] ==  b ) {           ++nc;           if (dfs(nx, ny))             return true;           else             break;         }       }       if (!nc) return true;       a[x - 1][y] =  w ;       a[x][y] =  b ;       a[x + 1][y] =  w ;     }     if (y > 0 && y < m - 1 && a[x][y - 1] ==  w  && a[x][y + 1] ==  w ) {       a[x][y - 1] =  < ;       a[x][y] =  - ;       a[x][y + 1] =  > ;       int nc = 0;       for (int d = 0; d < 6; ++d) {         int nx = x + dx[d];         int ny = y + dy[d];         if (nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] ==  b ) {           ++nc;           if (dfs(nx, ny))             return true;           else             break;         }       }       if (!nc) return true;       a[x][y - 1] =  w ;       a[x][y] =  b ;       a[x][y + 1] =  w ;     }     return false;   }   int main() {     scanf( %d%d n , &n, &m);     for (int i = 0; i < n; ++i) scanf( %s , a[i]);     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) {         if (a[i][j] ==  b ) {           ts = i * m + j + 1;           if (!dfs(i, j)) {             puts( NO );             return 0;           }         }       }     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j)         if (a[i][j] ==  b  || a[i][j] ==  w ) {           puts( NO );           return 0;         }     puts( YES );     output();   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     long long a[n], b[n], c[n];     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     for (long long i = 0; i < n; i++) cin >> b[i];     for (long long i = 0; i < n; i++) {       c[i] = (a[i] - b[i]);     }     sort(c, c + n);     long long answer = 0, answer1 = 0;     for (long long i = 0; i < n; i++) {       long long ans = a[i] - b[i];       long long sum = 1 - ans;       if (ans > 0) answer1++;       long long ss = lower_bound(c, c + n, sum) - c;       answer += (n - ss);     }     answer -= answer1;     answer /= 2;     cout << answer << endl;     return 0;   }
#include <bits/stdc++.h>   const int oo = 2139063143;   const int N = 101000;   const int P = 1000000007;   using namespace std;   inline void sc(int &x) {     x = 0;     static int p;     p = 1;     static char c;     c = getchar();     while (!isdigit(c)) {       if (c ==  - ) p = -1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + (c - 48);       c = getchar();     }     x *= p;   }   inline void print(int x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) print(x / 10);     putchar(x % 10 +  0 );   }   inline void pr(int x) { print(x), putchar(  n ); }   int n, k;   int vis[N], cl = 1, cr = 0, ans = 0;   int a[N];   void add(int w, int d) {     if (!vis[w] && d == 1)       ++ans;     else if (vis[w] == 1 && d == -1)       --ans;     vis[w] += d;   }   int calc(int l, int r) {     while (cr < r) ++cr, add(a[cr], 1);     while (cl > l) --cl, add(a[cl], 1);     while (cr > r) add(a[cr], -1), --cr;     while (cl < l) add(a[cl], -1), ++cl;     return ans;   }   int f[N], g[N];   void solve(int wl, int wr, int l, int r) {     int mid = (l + r) >> 1, pos = 0;     f[mid] = 0;     for (int i = wl; i <= min(wr, mid - 1); i++) {       int sb = g[i] + calc(i + 1, mid);       if (sb > f[mid]) f[mid] = sb, pos = i;     }     if (l == r) return;     if (l < mid) solve(wl, pos, l, mid - 1);     if (r > mid) solve(pos, wr, mid + 1, r);   }   int las[N];   int main() {     sc(n), sc(k);     for (int i = 1; i <= n; i++) sc(a[i]);     for (int i = 1; i <= k; i++) {       solve(0, n, 1, n);       memcpy(g, f, sizeof(int) * (n + 1));     }     pr(f[n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, V;   pair<int, int> a[111111];   int x[111111], t[111111], b[111111], f[111111];   int ans1, ans2;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d%d , &x[i], &t[i]);     cin >> V;     for (int i = 1; i <= n; ++i)       t[i] *= V, a[i] = make_pair(t[i] + x[i], t[i] - x[i]);     sort(a + 1, a + 1 + n);     b[0] = -2000000000;     for (int i = 1; i <= n; ++i) b[i] = 2000000000;     for (int i = 1; i <= n; ++i) {       f[i] = lower_bound(b, b + 1 + n, a[i].second + 1) - b;       b[f[i]] = min(b[f[i]], a[i].second);       ans2 = max(ans2, f[i]);     }     int fd = 0;     for (int i = 1; i <= n; ++i)       if (a[i].first == 0 && a[i].second == 0) fd = i;     if (!fd) a[++n] = make_pair(0, 0);     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n; ++i)       if (a[i].first == 0 && a[i].second == 0) fd = i;     for (int i = 0; i <= n; ++i) b[i] = 2000000000;     b[(ans1 = f[fd] = 0)] = a[fd].second;     for (int i = fd + 1; i <= n; ++i) {       if (a[i].second >= 0) {         f[i] = lower_bound(b, b + 1 + n, a[i].second + 1) - b;         b[f[i]] = min(b[f[i]], a[i].second);         ans1 = max(ans1, f[i]);       }     }     printf( %d %d n , ans1, ans2);   }
#include <bits/stdc++.h>   using namespace std;   int n, m, ans;   int x[100010], y[100010];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &x[i]);     }     for (int i = 1; i <= m; i++) {       scanf( %d , &y[i]);     }     long long sum1 = 0, sum2 = 0;     for (int r1 = 0, r2 = 0; r1 <= n, r2 <= m;) {       while (sum1 < sum2 || r1 == 0) {         r1++;         sum1 += x[r1];       }       while (sum1 > sum2 || r2 == 0) {         r2++;         sum2 += y[r2];       }       if (sum1 == sum2) {         ans++;         r1++;         r2++;         sum1 = x[r1];         sum2 = y[r2];       }     }     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     char ch;     int arr[5001], brr[5001], mdays[5001], fdays[5001];     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> ch >> arr[i] >> brr[i];       if (ch ==  M ) {         for (int j = arr[i]; j <= brr[i]; j++) mdays[j]++;       } else {         for (int j = arr[i]; j <= brr[i]; j++) fdays[j]++;       }     }     int mx = 0;     for (int i = 1; i <= 366; i++) {       mx = max(mx, min(mdays[i], fdays[i]));     }     mx *= 2;     cout << mx << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int sq = (int)sqrt((int)1e6);   const int maxnb = (int)1e9 + 7;   const double pi = acos(-1.0);   const int maxnl = 100500;   const double eps = 1e-9;   set<pair<long long, int> > se;   int n, pos[50500];   char c[50500];   long long price;   string s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> s;     int n = s.size(), bal = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  ( ) bal++;       if (s[i] ==  ) ) bal--;       if (s[i] ==  ? ) {         long long a, b;         cin >> a >> b;         se.insert(make_pair(a - b, i));         price += b;         s[i] =  ) ;         bal--;       }       while (bal < 0) {         if (!se.size()) {           cout << -1;           return 0;         }         pair<long long, int> p = *se.begin();         price += p.first;         s[p.second] =  ( ;         bal += 2;         se.erase(se.begin());       }     }     if (bal != 0) {       cout << -1;       return 0;     }     cout << price <<   n ;     for (int i = 0; i < n; i++) {       cout << s[i];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 10;   string s;   int n, len;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> s;     len = s.size();     for (int i = 0; i < len; i++)       if (s[i] ==  a ) n++;     if (len / 2 < n) {       cout << len;       return 0;     }     cout << 2 * n - 1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k, ans(0);     bool flg = true;     cin >> n >> m >> k;     int ar[n * m];     for (int a = 0; a < n * m; a++) cin >> ar[a];     sort(ar, ar + n * m);     int median = (n * m % 2) ? ar[n * m / 2] : ar[(n * m + 1) / 2];     for (int a = 0; a < n * m; a++) {       if (abs(median - ar[a]) % k != 0) flg = false;       ans += abs(median - ar[a]) / k;     }     if (flg)       cout << ans;     else       cout << -1;   }
#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     int a[5005], ans = 1, max = 0;     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 1; i < n; i++)       for (int j = 0; j < n - i; j++)         if (a[j] > a[j + 1]) {           int t = a[j];           a[j] = a[j + 1];           a[j + 1] = t;         }     for (int i = n - 1; i >= 0; i--) {       if (a[i] == a[i - 1])         ans++;       else {         if (ans > max) {           max = ans;         }         ans = 1;       }     }     if (max == 0)       printf( 1 n );     else       printf( %d n , max);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<string> a(n + 1), b(n + 1);     vector<int> c(n + 1);     vector<long long> f1(n + 1), f2(n + 1);     for (int i = 1; i <= n; ++i) {       cin >> c[i];     }     for (int i = 1; i <= n; ++i) {       cin >> a[i];       b[i] = a[i];       reverse(b[i].begin(), b[i].end());     }     for (int i = 1; i <= n; ++i) {       f1[i] = f2[i] = LLONG_MAX;       if (f1[i - 1] != LLONG_MAX) {         if (a[i] >= a[i - 1]) {           f1[i] = min(f1[i], f1[i - 1]);         }         if (b[i] >= a[i - 1]) {           f2[i] = min(f2[i], f1[i - 1] + c[i]);         }       }       if (f2[i - 1] != LLONG_MAX) {         if (a[i] >= b[i - 1]) {           f1[i] = min(f1[i], f2[i - 1]);         }         if (b[i] >= b[i - 1]) {           f2[i] = min(f2[i], f2[i - 1] + c[i]);         }       }     }     if (f1[n] == LLONG_MAX && f2[n] == LLONG_MAX) {       cout << -1;     } else {       cout << min(f1[n], f2[n]);     }   }
#include <bits/stdc++.h>   using namespace std;   const int NN = 1010;   const int MM = 101010;   struct Node {     int id;     int x, y;   } Q[MM];   int a[NN][NN];   vector<int> adj[MM];   int ans[MM], f[MM];   int n, m, k;   void dfs(int u, int fa = 0, int crr = 0) {     if (Q[u].id == 1) {       int x = Q[u].x, y = Q[u].y;       if (!a[x][y]) {         a[x][y] = 1;         crr++;         f[u] = 1;       }     } else if (Q[u].id == 2) {       int x = Q[u].x, y = Q[u].y;       if (a[x][y]) {         a[x][y] = 0;         crr--;         f[u] = 1;       }     } else if (Q[u].id == 3) {       int x = Q[u].x;       for (int i = 1; i <= m; i++) {         if (a[x][i])           crr--;         else           crr++;         a[x][i] ^= 1;       }     }     ans[u] = crr;     for (int v : adj[u]) {       if (v == fa) continue;       dfs(v, u, ans[u]);     }     if (Q[u].id == 1) {       int x = Q[u].x, y = Q[u].y;       if (f[u]) a[x][y] = 0;     } else if (Q[u].id == 2) {       int x = Q[u].x, y = Q[u].y;       if (f[u]) a[x][y] = 1;     } else if (Q[u].id == 3) {       int x = Q[u].x;       for (int i = 1; i <= m; i++) a[x][i] ^= 1;     }   }   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= k; i++) {       int id, x, y;       scanf( %d , &id);       if (id < 3) {         scanf( %d %d , &x, &y);         Q[i] = {id, x, y};         adj[i - 1].push_back(i);         adj[i].push_back(i - 1);         continue;       }       scanf( %d , &x);       if (id == 4) {         adj[x].push_back(i);         adj[i].push_back(x);       } else {         adj[i - 1].push_back(i);         adj[i].push_back(i - 1);       }       Q[i] = {id, x, 0};     }     dfs(0);     for (int i = 1; i <= k; i++) {       printf( %d n , ans[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, q;   vector<int> gr[300001];   int par[300001], comp[300001];   int mx[300001][2];   int dfs(int s, int k) {     comp[s] = k;     mx[s][0] = mx[s][1] = 0;     for (int i = 0; i < gr[s].size(); i++) {       if (comp[gr[s][i]] == -1) {         int p = dfs(gr[s][i], k) + 1;         par[gr[s][i]] = s;         if (p > mx[s][0]) {           mx[s][1] = mx[s][0];           mx[s][0] = p;         } else if (p > mx[s][1])           mx[s][1] = p;       }     }     return mx[s][0];   }   int mark[300001];   void dfs2(int s, int d) {     mark[s] = 1;     if (d > mx[s][0]) {       mx[s][1] = mx[s][0];       mx[s][0] = d;     } else if (d > mx[s][1])       mx[s][1] = d;     for (int i = 0; i < gr[s].size(); i++) {       if (!mark[gr[s][i]]) {         if (mx[s][0] == (mx[gr[s][i]][0] + 1)) {           ;           dfs2(gr[s][i], mx[s][1] + 1);         } else           dfs2(gr[s][i], mx[s][0] + 1);       }     }   }   int mlen[300001];   int mpar[300001];   int get(int id) {     if (par[id] == id) return id;     return par[id] = get(par[id]);   }   void merge(int x, int y) { par[x] = get(par[y]); }   int main() {     cin >> n >> m >> q;     int a, b;     for (int i = 0; i < m; i++) {       scanf( %d%d , &a, &b);       gr[a - 1].push_back(b - 1);       gr[b - 1].push_back(a - 1);     }     memset(comp, -1, sizeof comp);     int c = 0;     for (int i = 0; i < n; i++) {       if (comp[i] == -1) {         dfs(i, i);       }     }     memset(mark, 0, sizeof mark);     for (int i = 0; i < n; i++)       if (!mark[i]) dfs2(i, 0);     memset(mlen, -1, sizeof mlen);     for (int i = 0; i < n; i++) {       mlen[i] = mx[i][0] + mx[i][1];       mlen[comp[i]] = max(mlen[i], mlen[comp[i]]);       mpar[i] = mpar[comp[i]];       if (i == comp[i])         mpar[i] = i;       else if (mx[i][0] < mx[mpar[i]][0])         mpar[comp[i]] = i;     }     for (int i = 0; i < n; i++) {       mlen[i] = mlen[comp[i]];       mpar[i] = mpar[comp[i]];       par[i] = mpar[i];     }     for (int i = 0; i < q; i++) {       scanf( %d , &c);       if (c == 1) {         scanf( %d , &a);         printf( %d n , mlen[get(a - 1)]);       } else {         scanf( %d%d , &a, &b);         a = get(a - 1);         b = get(b - 1);         if (a != b) {           if (mx[a][0] >= mx[b][0]) {             merge(b, a);             mlen[a] = max(mlen[a], mx[a][0] + mx[b][0] + 1);             mx[a][0] = max(mx[a][0], mx[b][0] + 1);           } else {             merge(a, b);             mlen[b] = max(mlen[b], mx[b][0] + mx[a][0] + 1);           }         }       }     }     return 0;   }