malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   char s[105][105];   int sz[105];   vector<int> g[105];   int usd[30];   int ord[30], szo = 0, din[30];   bool dfs(int v) {     usd[v] = 1;     for (int i = 0; i < g[v].size(); ++i) {       int u = g[v][i];       if (usd[u] == 1)         return false;       else if (!usd[u])         if (!dfs(u)) return false;     }     usd[v] = 2;     ord[szo++] = v;     return true;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       scanf( %s , s[i]);       sz[i] = strlen(s[i]);     }     for (int i = 1; i < n; ++i) {       int j, mn = min(sz[i], sz[i - 1]);       for (j = 0; j < mn; ++j)         if (s[i][j] != s[i - 1][j]) {           g[s[i - 1][j] -  a ].push_back(s[i][j] -  a );           din[s[i][j] -  a ]++;           break;         }       if (j == mn && sz[i] == mn) {         cout <<  Impossible n ;         return 0;       }     }     for (int i = 0; i < 26; ++i)       if (din[i] == 0)         if (!dfs(i)) {           cout <<  Impossible n ;           return 0;         }     if (szo < 26) {       cout <<  Impossible n ;       return 0;     }     for (int i = szo - 1; i >= 0; --i) printf( %c , ord[i] +  a );     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[200], b[200];   int check(int n, int d, int a[]) {     for (int i = 1; i <= n; ++i) {       int flag = 0;       for (int v = 1; v <= 100; ++v)         if (a[v] >= d) {           a[v] -= d;           flag = 1;           break;         }       if (flag == 0) return 0;     }     return 1;   }   int main() {     cin >> n >> m;     for (int i = 0; i < m; ++i) {       cin >> b[i];     }     int ans = 0;     int l = 1, r = m;     while (l <= r) {       memset(a, 0, sizeof(a));       for (int i = 0; i < m; ++i) a[b[i]]++;       int k = (l + r) / 2;       if (check(n, k, a)) {         ans = max(ans, k);         l = k + 1;       } else         r = k - 1;     }     memset(a, 0, sizeof(a));     for (int i = 0; i < m; ++i) a[b[i]]++;     if (check(n, l, a)) ans = max(ans, l);     memset(a, 0, sizeof(a));     for (int i = 0; i < m; ++i) a[b[i]]++;     if (check(n, r, a)) ans = max(ans, r);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void getint(int &v) {     char ch, fu = 0;     for (ch =  * ; (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar())       ;     if (ch ==  - ) fu = 1, ch = getchar();     for (v = 0; ch >=  0  && ch <=  9 ; ch = getchar()) v = v * 10 + ch -  0 ;     if (fu) v = -v;   }   int main() {     int maxmax = 0, max, x, sum = 0, n, m;     long long int sumsum = 0;     getint(n);     if (n == 1) printf( %d n , 0), exit(0);     for (int i = 0; i < n; ++i) {       getint(m);       max = 0;       for (int j = 0; j < m; ++j) {         getint(x);         if (x > max) max = x;       }       sumsum += (long long)max * m;       sum += m;       if (max > maxmax) maxmax = max;     }     printf( %lld n , (long long)sum * (long long)maxmax - sumsum);   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   long long gcd(long long x, long long y) {     if (x > y) swap(x, y);     if (x == 0) return y;     if (y % x == 0) return x;     return gcd(y % x, x);   }   int main() {     long long n, k, t;     long long m, d, w, g;     long long x, y, z;     long long s, ans;     bool v = true;     ans = 0;     scanf( %lld , &t);     for (long long tt = 0; tt < (long long)(t); tt++) {       scanf( %lld %lld %lld , &m, &d, &w);       if ((m == 1) || (d == 1)) {         printf( 0 n );         continue;       }       g = gcd(d - 1, w);       k = w / g;       x = min(m, d);       y = x % k;       x = x / k;       s = (x + 1) * x;       s /= 2;       ans = s * y;       s = x * (x - 1);       s /= 2;       ans += (s * (k - y));       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007ll;   long long s[1000010];   int h[1000010];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &h[i]);       h[i]--;     }     s[2] = min(h[1], h[2]);     for (int i = 2; i < n; i++)       s[i + 1] = (s[i] * min(h[i - 1], min(h[i], h[i + 1])) % mod +                   min(h[i], h[i + 1])) %                  mod;     long long ans = 0;     for (int i = 1; i <= n; i++) ans = (ans + h[i]) % mod;     for (int i = 2; i <= n; i++)       ans = (ans + min(h[i - 1], h[i]) * s[i] % mod) % mod;     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a;   char str[100002];   int sum[100002];   int cnt[100002];   int main() {     scanf( %d n , &a);     gets(str);     int len = strlen(str);     sum[0] = (str[0] -  0 );     for (int i = 1; i < len; i++) {       sum[i] = sum[i - 1] + (str[i] -  0 );     }     int id, x, y, mx = 0;     for (int i = 0; i < len; i++) {       for (int j = i; j < len; j++) {         if (i > 0)           id = sum[j] - sum[i - 1];         else           id = sum[j];         cnt[id]++;         mx = max(mx, id);       }     }     long long ans = 0;     for (int i = 0; i < len; i++) {       for (int j = i; j < len; j++) {         if (i > 0)           x = sum[j] - sum[i - 1];         else           x = sum[j];         if (a == 0) {           ans += cnt[0];         }         if (x == 0) continue;         if (a % x != 0) continue;         y = a / x;         if (y > mx) continue;         ans = ans + (long long)(cnt[y]);       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = (-1u) / 2;   const long long int INF2 = (-1ull) / 2;   int a, b, i, d[1011000], j, k, n, m, timer = 0, l, r, x, y;   int c[1011000], cnt = 0, fl = 0, a2, a3 = -1000000, ans = 0;   pair<int, int> p[100100];   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> a >> b;     for (i = 0; i < n; i++) {       cin >> l;       if (b < a)         p[i] = make_pair(l, i);       else         p[i] = make_pair(l, i * -1);     }     sort(p, p + n);     for (i = n - 1; i >= n - min(a, b); i--) {       if (a > b)         c[abs(p[i].second)] = 2;       else         c[abs(p[i].second)] = 1;     }     for (i = 0; i < n; i++) {       if (a == b) {         if (i < a)           cout <<  1  ;         else           cout <<  2  ;       } else {         if (a > b && c[i] == 0)           c[i] = 1;         else if (a < b && c[i] == 0)           c[i] = 2;         cout << c[i] <<    ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using pii = pair<int, int>;   using ll = long long;   using llu = long long unsigned;   using ld = long double;   const ld EPS = 1e-9;   inline int cmp(ld first, ld second = 0, ld tol = EPS) {     return (first <= second + tol) ? (first + tol < second) ? -1 : 0 : 1;   }   const int MOD = 1;   inline int mod(ll first, int m = MOD) { return (int)(((first % m) + m) % m); }   const int P = 1e5 + 100;   ll n;   bool not_prime[P];   vector<int> primes;   int main() {     not_prime[0] = not_prime[1] = true;     for (int i = 2; i * i < P; i++) {       if (!not_prime[i])         for (int j = i * i; j < P; j += i) not_prime[j] = true;     }     for (int i = 2; i < P; i++)       if (!not_prime[i]) primes.push_back(i);     scanf( %lld , &n);     bool isPrime = true;     int pos;     for (int i = 0; i < ((int)(primes).size()); i++)       if (n % primes[i] == 0) {         isPrime = false;         pos = (primes[i] - 3) / 2;         break;       }     if (isPrime)       printf( 1 n );     else if (n % 2 == 0 or n % 3 == 0)       printf( %lld n , n / 2);     else       printf( %lld n , (n / 2) - pos);     return 0;   }
#include <bits/stdc++.h>   long long strange[100001];   int main() {     for (int i = 1; i <= 100000; i++) {       long long a = i, b = i;       while (b) {         a *= 10;         a += b % 10;         b /= 10;       }       strange[i] = a;     }     long long k, p;     while (scanf( %lld%lld , &k, &p) != EOF) {       long long sum = 0;       for (int i = 1; i <= k; i++) sum += strange[i];       printf( %lld n , sum % p);     }   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long int LINF = 0x3f3f3f3f3f3f3f3fll;   int g;   bool endswith(string a, string b) {     if ((int)((a).size()) < (int)((b).size())) return 0;     for (int i = 0; i < (int)((b).size()); i++)       if (a[(int)((a).size()) - 1 - i] != b[(int)((b).size()) - 1 - i]) return 0;     return 1;   }   bool is_adj(string second) {     if (endswith(second,  lios ))       g = 1;     else if (endswith(second,  liala ))       g = 2;     else       return 0;     return 1;   }   bool is_noun(string second) {     if (endswith(second,  etr ))       g = 1;     else if (endswith(second,  etra ))       g = 2;     else       return 0;     return 1;   }   bool is_verb(string second) {     if (endswith(second,  initis ))       g = 1;     else if (endswith(second,  inites ))       g = 2;     else       return 0;     return 1;   }   int main() {     ios::sync_with_stdio(false);     vector<string> v;     string second;     while (cin >> second) v.push_back(second);     if ((int)((v).size()) == 1) {       if (is_adj(v[0]) or is_noun(v[0]) or is_verb(v[0]))         cout <<  YES  << endl;       else         cout <<  NO  << endl;       return 0;     }     int last = -1;     int n = 0;     bool masc, deu = 1;     for (int i = 0; i < (int)((v).size()); i++) {       if (is_adj(v[i])) {         if (last > 0) deu = 0;         last = 0;       } else if (is_noun(v[i])) {         if (last > 1) deu = 0;         n++;         last = 1;       } else if (is_verb(v[i])) {         last = 2;       } else         deu = 0;       if (!i)         masc = (g == 1);       else if (masc and g == 2)         deu = 0;       else if (!masc and g == 1)         deu = 0;     }     if (n != 1) deu = 0;     int cu;     cout << (deu ?  YES  :  NO ) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int in() {     int x;     scanf( %d , &x);     return x;   }   const int INF = 1e9 + 10;   const long long LINF = 1000ll * 1000 * 1000 * 1000 * 1000 * 1000 + 100;   const int MN = 3e3 + 10;   int n;   vector<pair<int, int> > edges[MN];   int ans;   int dp1[MN], dp2[MN];   int sz[MN];   void input() {     n = in();     for (int i = 0; i < n - 1; ++i) {       int a, b;       a = in(), b = in();       edges[a].push_back(pair<int, int>(b, 1));       edges[b].push_back(pair<int, int>(a, 0));     }   }   void dfs1(int s, int pr) {     for (int i = 0; i < ((int)(edges[s]).size()); ++i) {       if (edges[s][i].first == pr) continue;       if (edges[s][i].second == 0) ++sz[s];       dfs1(edges[s][i].first, s);       sz[s] += sz[edges[s][i].first];     }   }   void dfs2(int s, int pr) {     for (int i = 0; i < ((int)(edges[s]).size()); ++i) {       if (edges[s][i].first == pr) continue;       dfs2(edges[s][i].first, s);     }     int Mn = sz[s];     for (int i = 0; i < ((int)(edges[s]).size()); ++i) {       if (edges[s][i].first == pr) continue;       if (edges[s][i].second) {         Mn = min(Mn, dp1[edges[s][i].first] + 1 + sz[s] - sz[edges[s][i].first]);       } else {         Mn = min(Mn, dp1[edges[s][i].first] + sz[s] - 1 - sz[edges[s][i].first]);       }     }     dp1[s] = Mn;     Mn = INF;     for (int i = 0; i < ((int)(edges[s]).size()); ++i) {       if (edges[s][i].first == pr) continue;       if (edges[s][i].second) {         Mn = min(Mn, dp2[edges[s][i].first] + sz[s] - sz[edges[s][i].first]);       } else {         Mn = min(Mn, dp1[edges[s][i].first] + sz[s] - sz[edges[s][i].first] - 1);         Mn = min(Mn, dp2[edges[s][i].first] + sz[s] - sz[edges[s][i].first]);       }     }     dp2[s] = Mn;   }   int main() {     input();     int mn = INF;     for (int i = 0; i < n; ++i) {       memset(dp1, 0, sizeof dp1);       memset(dp2, 0, sizeof dp2);       memset(sz, 0, sizeof sz);       dfs1(i, -1);       dfs2(i, -1);       mn = min(mn, dp2[i]);     }     if (n <= 2) mn = 0;     cout << mn <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     while (t--) {       long long int n, i, j;       cin >> n;       long long int a[n];       for (i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       std::vector<long long int> ans;       long long int last = 64 - __builtin_clzll(a[n - 1]);       for (i = 0; i < last + 1; i++) {         std::vector<long long int> temp;         for (j = 0; j < n; j++) {           bitset<70> b(a[j]);           if (b[i]) temp.push_back(a[j]);         }         bool f = 0;         for (j = i - 1; j > -1; j--) {           bool f1 = 0;           for (auto x : temp) {             bitset<70> b(x);             if (b[j] == 0) f1 = 1;           }           if (!f1) {             f = 1;             break;           }         }         if (f) continue;         if (temp.size()) {           ans = temp;         }       }       cout << ans.size() <<   n ;       for (auto x : ans) cout << x <<    ;     }     cerr <<  Time Taken:  << (float)clock() / CLOCKS_PER_SEC * 1000 <<  ms           <<   n ;     return 0;   }
#include <bits/stdc++.h>   const long long N = 1000006;   using namespace std;   const long long MOD = 1000000007LL;   template <typename T>   T gcd(T a, T b) {     if (a == 0) return b;     return gcd(b % a, a);   }   template <typename T>   T power(T x, T y, long long m = MOD) {     T ans = 1;     while (y > 0) {       if (y & 1LL) ans = (ans * x) % m;       y >>= 1LL;       x = (x * x) % m;     }     return ans % m;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     if (n == 1) {       cout <<  0 ;       return 0;     }     multiset<long long> l, r;     vector<pair<long long, long long> > v;     for (long long(i) = (0); i < (n); i++) {       long long x, y;       cin >> x >> y;       l.insert(x);       r.insert(y);       v.emplace_back(make_pair(x, y));     }     long long ans = 0;     for (long long(i) = (0); i < (n); i++) {       l.erase(l.find(v[i].first));       r.erase(r.find(v[i].second));       long long dist = max(0LL, *r.begin() - *l.rbegin());       ans = max(ans, dist);       l.insert(v[i].first);       r.insert(v[i].second);     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int a[100001], n;   inline long long sum(int t) {     long long s = 0;     for (int i = 1; i <= n; ++i) s += min(a[i], t);     return s;   }   int main() {     int i, j, l, r, m, t = 1;     int nr = 0;     long long k, s = 0, smin;     scanf( %d %I64d , &n, &k);     for (i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       s += a[i];     }     if (s < k)       printf( -1 );     else if (s == k)       printf(  );     else {       l = 0;       r = 1000000000;       while (l + 1 < r) {         m = (l + r) >> 1;         if (sum(m) <= k)           l = m;         else           r = m;       }       k -= sum(l);       if (s == k)         printf(  );       else {         for (i = 1; i <= n; ++i) {           if (k == 0) break;           if (a[i] > l) {             --k;             --a[i];           }         }         for (j = i; j <= n; ++j)           if (a[j] > l) printf( %d  , j);         for (j = 1; j < i; ++j)           if (a[j] > l) printf( %d  , j);       }     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000;   int n, m, k, start = MAXN + 1;   int type[MAXN + 3], a[MAXN + 3], counter[MAXN + 3], stc[MAXN + 3];   bool check[MAXN + 3];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &type[i]);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) counter[a[i]]++;     for (int i = 1; i <= n; i++)       if (counter[i] == 1) check[i] = true;     int cnt = 0;     for (int i = n; i >= 1; i--) {       if (type[i] == 1) {         cnt = 1;         int v = a[i];         while (v != 0 && check[v]) {           cnt++;           v = a[v];         }         if (cnt > k) k = cnt, start = i;       }     }     printf( %d n , k);     for (int i = 0, v = start; i < k; i++, v = a[v]) stc[i] = v;     for (int i = k - 1; i >= 0; i--) printf( %d  , stc[i]);     puts(  );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long l, r, ans;   vector<long long> lucky;   void lu(int f, int s) {     string x =   ;     for (int i = 0; i < f; i++) x +=  4 ;     for (int i = 0; i < s; i++) x +=  7 ;     do lucky.push_back(atoll(x.c_str()));     while (next_permutation(x.begin(), x.end()));   }   int main() {     cin >> l >> r;     for (int i = 0; i <= 10; i++)       for (int j = 0; j <= 10; j++)         if (i + j <= 10) lu(i, j);     sort(lucky.begin(), lucky.end());     int j, k;     j = k = 0;     while (lucky[j++] < l)       ;     j--;     while (lucky[k++] < r)       ;     k--;     ans += lucky[j] * (min(r - l, lucky[j] - l) + 1);     if (lucky[j] == lucky[k]) return cout << ans << endl, 0;     ans += lucky[k] * (r - lucky[k - 1]);     for (int i = j + 1; i < k; i++) ans += (lucky[i] - lucky[i - 1]) * lucky[i];     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};   int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};   int a[33];   string s;   int main() {     for (int i = 0; i < 26; ++i) scanf( %d , &a[i]);     cin >> s;     int len = s.length();     vector<long long> v(len + 1);     for (int i = 0; i < len; ++i) {       v[i + 1] = a[s[i] -  a ] + v[i];     }     map<pair<int, long long>, int> m;     long long res = 0;     for (int i = 0; i < len; i++) {       if (m[make_pair(s[i] -  a , v[i + 1])]) {         res += m[make_pair(s[i] -  a , v[i + 1])];       }       m[make_pair(s[i] -  a , v[i + 1] + a[s[i] -  a ])]++;     }     cout << res <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     int arr[101][101];     int row[101], col[101];     for (int i = 0; i <= n - 1; i++) {       row[i] = col[i] = 0;     }     int temp;     for (int i = 0; i <= n - 1; i++) {       for (int j = 0; j <= n - 1; j++) {         scanf( %d , &temp);         arr[i][j] = temp;         row[i] += temp;         col[j] += temp;       }     }     int ans = 0;     for (int i = 0; i <= n - 1; i++) {       for (int j = 0; j <= n - 1; j++) {         if (col[j] > row[i]) ans++;       }     }     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   int m, n, k, t;   int a[100001];   int main() {     scanf( %d %d , &n, &k);     for (int i = 0; i < n; ++i) {       scanf( %d , &t);       a[i % k] += t;     }     m = 0;     for (int i = 1; i < k; ++i)       if (a[i] < a[m]) m = i;     printf( %d n , m + 1);   }
#include <bits/stdc++.h>   using namespace std;   vector<int> vec[1000006], V;   struct Node {     int x, y, w;     bool operator<(const Node& a) const { return y < a.y; }   } node[500006];   int n;   struct Info {     long long len, mx, w;     int p;     Info() {}     Info(long long _len, long long _mx, long long _w, int _p) {       len = _len;       mx = _mx;       w = _w;       p = _p;     }     Info operator+(const Info& a) {       Info ret;       ret.len = len + a.len;       ret.w = w + a.w;       if (a.mx >= a.w + mx - a.len) {         ret.mx = a.mx;         ret.p = a.p;       } else {         ret.mx = a.w + mx - a.len;         ret.p = p;       }       return ret;     }     void pr() { printf( %lld %lld %lld %d n , len, mx, w, p); }   } info[1000006 << 2];   void build(int p, int l, int r) {     if (l == r) {       int len = 0;       if (l > 1) len = V[l - 1] - V[l - 2];       info[p] = Info(len, 0, 0, l);       return;     }     int mid = (l + r) >> 1;     build(p << 1, l, mid);     build(p << 1 | 1, mid + 1, r);     info[p] = info[p << 1] + info[p << 1 | 1];   }   void update(int p, int l, int r, int aim, long long w) {     if (l == r) {       info[p].w += w;       info[p].mx += w;       return;     }     int mid = (l + r) >> 1;     if (aim <= mid)       update(p << 1, l, mid, aim, w);     else       update(p << 1 | 1, mid + 1, r, aim, w);     info[p] = info[p << 1] + info[p << 1 | 1];   }   Info query(int p, int l, int r, int L, int R) {     if (L <= l && r <= R) {       return info[p];     }     int mid = (l + r) >> 1;     if (L <= mid && R > mid)       return query(p << 1, l, mid, L, R) + query(p << 1 | 1, mid + 1, r, L, R);     else if (L <= mid)       return query(p << 1, l, mid, L, R);     else if (R > mid)       return query(p << 1 | 1, mid + 1, r, L, R);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d%d , &node[i].x, &node[i].y, &node[i].w);       if (node[i].x > node[i].y) swap(node[i].x, node[i].y);       V.push_back(node[i].x);       V.push_back(node[i].y);     }     sort(node + 1, node + n + 1);     sort(V.begin(), V.end());     V.resize(unique(V.begin(), V.end()) - V.begin());     for (int i = 1; i <= n; i++) {       node[i].x = lower_bound(V.begin(), V.end(), node[i].x) - V.begin();       node[i].y = lower_bound(V.begin(), V.end(), node[i].y) - V.begin();       vec[node[i].y].push_back(i);     }     int End = V.size();     build(1, 1, End);     long long ans = -1e15;     int ansl, ansr;     for (int i = 0; i < End; i++) {       int siz = (int)vec[i].size();       for (int j = 0; j < siz; j++)         update(1, 1, End, node[vec[i][j]].x + 1, node[vec[i][j]].w);       Info tmp = query(1, 1, End, 1, i + 1);       if (tmp.mx > ans) {         ans = tmp.mx;         ansl = V[tmp.p - 1];         ansr = V[i];       }     }     if (ans < 0) ans = 0, ansl = ansr = 2e9;     printf( %lld n%d %d %d %d , ans, ansl, ansl, ansr, ansr);     return 0;   }
#include <bits/stdc++.h>   int main() {     int t, arr[110], i, l, s, m, n, j;     scanf( %d , &t);     l = -1;     s = 1001;     for (i = 0; i < t; i++) scanf( %d , &arr[i]);     for (i = 0; i < t; i++) {       for (j = i + 1; j < t; j++) {         if (arr[j] < arr[i]) {           s = arr[j];           arr[j] = arr[i];           arr[i] = s;         }       }     }     printf( %d  , arr[t - 1]);     for (i = 1; i < t - 1; i++) {       printf( %d  , arr[i]);     }     printf( %d n , arr[0]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, a[3010][6010], fa[18010000];   int dx[9] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[9] = {-1, 0, 1, -1, 1, -1, 0, 1};   inline int read() {     int x = 0, f = 0;     char ch = getchar();     while (!isdigit(ch)) f = ch ==  - , ch = getchar();     while (isdigit(ch)) x = (x << 3) + (x << 1) + ch - 48, ch = getchar();     return f ? -x : x;   }   inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   inline int calc(int x, int y) { return (x - 1) * m * 2 + y; }   int main() {     int ans = 0;     n = read(), m = read();     int Q = read();     for (int i = 1; i <= n; i++)       for (int j = 1; j <= 2 * m; j++) fa[calc(i, j)] = calc(i, j);     if (m == 1) {       puts( 0 );       return 0;     }     while (Q--) {       int x = read(), y = read();       int flag = 0;       for (int i = 0; i < 8; i++) {         int xx = x + dx[i], yy = y + dy[i];         if (yy == 0) yy = 2 * m;         if (xx < 1 || xx > n || !a[xx][yy]) continue;         for (int j = 0; j < 8; j++) {           int xxx = x + dx[j], yyy = y + m + dy[j];           if (yyy > 2 * m) yyy = 1;           if (xxx < 1 || xxx > n || !a[xxx][yyy]) continue;           if (find(calc(xx, yy)) == find(calc(xxx, yyy))) {             flag = 1;             break;           }         }       }       if (flag) continue;       a[x][y] = a[x][y + m] = 1;       ans++;       int fx1 = find(calc(x, y)), fx2 = find(calc(x, y + m));       for (int i = 0; i < 8; i++) {         int xx = x + dx[i], yy = y + dy[i];         if (yy > m) {           if (a[xx][yy]) fa[find(calc(xx, yy))] = fx1;           if (a[xx][1]) fa[find(calc(xx, 1))] = fx2;         } else if (yy < 1) {           if (a[xx][yy + m]) fa[find(calc(xx, yy + m))] = fx2;           if (a[xx][2 * m]) fa[find(calc(xx, 2 * m))] = fx1;         } else {           if (a[xx][yy]) fa[find(calc(xx, yy))] = fx1;           if (a[xx][yy + m]) fa[find(calc(xx, yy + m))] = fx2;         }       }     }     printf( %d n , ans);     return 0;   }