malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 3000000 + 7;   const int M = 1500 + 7;   const int inf = 1e9 + 7;   const long long linf = 1ll * inf * (inf - 1);   const double pi = acos(-1);   const double eps = 1e-6;   const bool multipleTest = false;   const int mod[2] = {inf, 984723593 + 7};   const int gen[2] = {(rand() % inf) + 1, (rand() % 92473247) + 1};   long long f[2][N];   void init() {     f[0][0] = f[1][0] = 1;     for (int i = 1; i < N; ++i) {       for (int t = 0; t < 2; ++t)         f[t][i] = (long long)f[t][i - 1] * gen[t] % mod[t];     }   }   pair<int, int> val[N];   pair<int, int> getNext(pair<int, int> cur, pair<int, int> nxt, int len) {     int fir = (cur.first * f[0][len] + nxt.first) % mod[0];     int sec = (cur.second * f[1][len] + nxt.second) % mod[1];     return make_pair(fir, sec);   }   pair<int, int> getVal(int u, int v) {     ++u;     ++v;     long long fir = (val[v].first - val[u - 1].first * f[0][v - u + 1]) % mod[0];     if (fir < 0) fir += mod[0];     long long sec =         (val[v].second - val[u - 1].second * f[1][v - u + 1]) % mod[1];     if (sec < 0) sec += mod[1];     return make_pair(fir, sec);   }   int n, k;   string s;   map<pair<int, int>, int> all;   int ans[N];   set<int> has;   void solve() {     init();     cin >> n >> k;     cin >> s;     int m;     cin >> m;     for (int i = 0; i < m; ++i) {       string x;       cin >> x;       pair<int, int> cur = make_pair(0, 0);       for (int j = 0, len = (int)(x).size(); j < len; ++j) {         cur = getNext(cur, make_pair(x[j], x[j] + 2), 1);       }       all[cur] = i + 1;     }     for (int t = 0; t < k; ++t) s += s[t];     val[0] = make_pair(0, 0);     for (int i = 1, len = (int)(s).size(); i <= len; ++i) {       val[i] = getNext(val[i - 1], make_pair(s[i - 1], s[i - 1] + 2), 1);     }     for (int i = 0; i < k; ++i) {       bool ok = true;       int t = 0;       has.clear();       for (int j = i; t < n; j += k) {         pair<int, int> cur = getVal(j, j + k - 1);         if (!all.count(cur) || has.count(all[cur])) {           ok = false;           break;         } else {           ans[t++] = all[cur];           has.insert(ans[t - 1]);         }       }       if (ok) {         puts( YES );         for (int j = 0; j < n; ++j) {           printf( %d  , ans[j]);         }         return;       }     }     puts( NO );   }   int main() {     int Test = 1;     if (multipleTest) {       cin >> Test;     }     for (int i = 0; i < Test; ++i) {       solve();     }   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:512000000 )   using namespace std;   void solve();   void precalc();   clock_t start;   int testNumber = 1;   bool todo = true;   int main() {     start = clock();     int t = 1;     cout.sync_with_stdio(0);     cin.tie(0);     precalc();     cout.precision(10);     cout << fixed;     int testNum = 1;     while (t--) {       solve();       ++testNumber;     }     return 0;   }   template <typename T>   T binpow(T q, T w, T mod) {     if (!w) return 1 % mod;     if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod;     return binpow(q * 1LL * q % mod, w / 2, mod);   }   void precalc() {}   template <typename T>   T gcd(T q, T w) {     while (w) {       q %= w;       swap(q, w);     }     return q;   }   template <typename T>   T lcm(T q, T w) {     return q / gcd(q, w) * w;   }   struct SegTree {     const int shift = 1 << 18;     vector<int> tree;     SegTree() { tree.assign(2 * shift, 0); }     int rmq(int l, int r) {       if (l >= r) {         return 0;       }       if (l & 1) {         return tree[l] + rmq(l + 1, r);       }       if (r & 1) {         return tree[r - 1] + rmq(l, r - 1);       }       return rmq(l / 2, r / 2);     }     int get_sum(int l, int r) { return rmq(l + shift, r + shift); }     void update(int v, int val) {       v += shift;       tree[v] = val;       v /= 2;       while (v) {         tree[v] = tree[2 * v] + tree[2 * v + 1];         v /= 2;       }     }   };   void solve() {     int n, k, Q;     vector<int> a(2);     cin >> n >> k >> a[0] >> a[1] >> Q;     vector<int> sums(n, 0);     vector<SegTree> tree(2);     for (int w = 0; w < Q; ++w) {       int type;       cin >> type;       if (type == 1) {         int day, num;         cin >> day >> num;         --day;         sums[day] += num;         for (int i = 0; i < 2; ++i) {           tree[i].update(day, min(sums[day], a[i]));         }       } else {         int l;         cin >> l;         --l;         long long res = (long long)tree[1].get_sum(0, l) +                         (long long)tree[0].get_sum(l + k, n);         cout << res <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-10;   const int maxn = 1000 * 1000 * 1000 + 2;   int n;   int a[3][5 * 100 * 1000 + 2];   long double s, e = maxn;   bool check(long double x) {     long double maxr = -1000000001;     for (int i = 0; i < n; i++) {       if (a[2][i] > 0 &&           (long double)(a[1][i] + (long double)(((long double)x) *                                                 ((long double)a[2][i]))) > maxr)         maxr = (long double)(a[1][i] + (long double)(((long double)x) *                                                      ((long double)a[2][i])));       if (a[2][i] < 0 && maxr != -1000000001 &&           maxr >= (long double)(a[1][i] + (long double)(((long double)x) *                                                         ((long double)a[2][i]))))         return 1;     }     return 0;   }   long double bs(long double x, long double y) {     if (y - x < eps) return x;     long double min = (long double)((x + y) / (long double)2.0);     if (check(min))       return bs(x, min);     else       return bs(min, y);   }   int main() {     cout << fixed << setprecision(12);     cin >> n;     for (int i = 0; i < n; i++) cin >> a[1][i] >> a[2][i];     if (!check(1000 * 1000 * 1000 + 1.0)) {       cout << -1 <<  .00000000000000000000  << endl;       return 0;     }     cout << bs(s, e) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int M = 1000001;   int n, m, k, a[M], ver[M], nex[M], head[M], cnt, fa[M], dp[M], tmp[M], sz[M];   vector<int> v[M];   long long d[M];   void add(int x, int y) {     ver[++cnt] = y, nex[cnt] = head[x], head[x] = cnt;     ver[++cnt] = x, nex[cnt] = head[y], head[y] = cnt;   }   void dfs(int x, int f) {     fa[x] = f;     dp[x] = dp[f] + 1;     for (int i = head[x]; i; i = nex[i]) {       if (ver[i] == f) continue;       dfs(ver[i], x);     }   }   bool cmp(int a, int b) { return dp[a] > dp[b]; }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++)       scanf( %d , &a[i]), v[a[i]].push_back(i), m = max(m, a[i]);     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       add(x, y);     }     dfs(1, 0);     for (int i = 1; i <= m; i++) {       int cnt = 0;       for (int j = i; j <= m; j += i) {         for (int l = 0; l < v[j].size(); l++) tmp[++cnt] = v[j][l];       }       sort(tmp + 1, tmp + 1 + cnt, cmp);       d[i] = cnt;       for (int j = 1; j <= cnt; j++) sz[tmp[j]] = 1;       for (int j = 1; j <= cnt; j++) {         int x = tmp[j], f = fa[x];         if (!sz[f]) continue;         d[i] = (d[i] + (long long)sz[x] * sz[f]);         sz[f] += sz[x];       }       for (int j = 1; j <= cnt; j++) sz[tmp[j]] = 0;     }     for (int i = m; i >= 1; i--)       for (int j = i + i; j <= m; j += i) d[i] -= d[j];     for (int i = 1; i <= m; i++)       if (d[i]) printf( %d %lld n , i, d[i]);   }
#include <bits/stdc++.h>   using namespace std;   void fre() {     freopen( c://test//input.in ,  r , stdin);     freopen( c://test//output.out ,  w , stdout);   }   template <class T>   inline void gmax(T &a, T b) {     if (b > a) a = b;   }   template <class T>   inline void gmin(T &a, T b) {     if (b < a) a = b;   }   const int N = 1010, M = 0, Z = 1e9 + 7, ms63 = 1061109567;   const int dy[4] = {-1, 0, 0, 1}, dx[4] = {0, -1, 1, 0};   int n, m;   char s[N][N];   struct BFS {     queue<pair<int, int> > q;     int f[N][N];     int ans[3];     void init() {       memset(f, 63, sizeof(f));       memset(ans, 63, sizeof(ans));     }     void inq(int y, int x, int step) {       if (y < 0 || y == n || x < 0 || x == m) return;       if (s[y][x] ==  # ) return;       if (step >= f[y][x]) return;       f[y][x] = step;       if (isdigit(s[y][x])) {         gmin(ans[s[y][x] -  1 ], f[y][x]);       }       q.push(make_pair(y, x));     }     void bfs() {       while (!q.empty()) {         int y = q.front().first;         int x = q.front().second;         q.pop();         for (int i = 0; i < 4; i++) {           inq(y + dy[i], x + dx[i], f[y][x] + 1);         }       }     }   } a[3];   int main() {     while (~scanf( %d%d , &n, &m)) {       for (int i = 0; i < 3; i++) a[i].init();       for (int i = 0; i < n; i++) {         scanf( %s , s[i]);         for (int j = 0; s[i][j]; j++)           if (isdigit(s[i][j])) {             a[s[i][j] -  1 ].inq(i, j, 0);           }       }       for (int i = 0; i < 3; i++) a[i].bfs();       int ans = ms63;       for (int i = 0; i < 3; i++) {         gmin(ans, a[i].ans[0] + a[i].ans[1] + a[i].ans[2] - 2);       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           long long tmp = -2;           for (int k = 0; k < 3; k++) tmp += a[k].f[i][j];           if (tmp < ans) ans = tmp;         }       }       if (ans > 1e8) ans = -1;       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   namespace my_useful_tools {   const int INF = 0x3f3f3f3f;   inline void pc(char c) { putchar(c); }   template <class T>   inline T gcd(T a, T b) {     return b == 0 ? a : gcd(b, a % b);   }   template <class T>   inline void W(T p) {     if (p < 0) pc( - ), p = -p;     if (p / 10 != 0) W(p / 10);     pc( 0  + p % 10);   }   template <class T>   inline void Wn(T p) {     W(p), puts(  );   }   template <class T>   inline void W(T a, T b) {     W(a), pc(   ), W(b);   }   template <class T>   inline void Wn(T a, T b) {     W(a), pc(   ), Wn(b);   }   template <class T>   inline void W(T a, T b, T c) {     W(a), pc(   ), W(b), pc(   ), W(c);   }   inline char gchar() {     char ret = getchar();     for (; ret ==   n  || ret ==   r  || ret ==    ; ret = getchar())       ;     return ret;   }   template <class T>   inline void fr(T& ret) {     char c =    ;     int flag = 1;     for (c = getchar(); c !=  -  && !( 0  <= c && c <=  9 ); c = getchar())       ;     if (c ==  - )       flag = -1, ret = 0;     else       ret = c -  0 ;     for (c = getchar();  0  <= c && c <=  9 ; c = getchar())       ret = ret * 10 + c -  0 ;     ret = ret * flag;   }   inline int fr() {     int x;     fr(x);     return x;   }   template <class T>   inline void fr(T& a, T& b) {     fr(a), fr(b);   }   template <class T>   inline void fr(T& a, T& b, T& c) {     fr(a), fr(b), fr(c);   }   template <class T>   inline T fast_pow(T base, T index, T mod = 2147483647, T ret = 1) {     for (; index; index >>= 1, base = base * base % mod)       if (index & 1) ret = ret * base % mod;     return ret;   }   const int maxv = 100, maxe = 100;   struct Edge {     int edge, head[maxv], to[maxe], next[maxe];     Edge() {       edge = 0;       memset(head, -1, sizeof head);     }     void addedge(int u, int v) {       to[edge] = v, next[edge] = head[u];       head[u] = edge++;     }   };   };  // namespace my_useful_tools   using namespace my_useful_tools;   long long t, a, b;   int main() {     fr(t, a, b);     if (t == 2 && a == 3 && b > 10000)       puts( 0 );     else if (t == a) {       if (a == b) {         if (a == 1)           puts( inf );         else           puts( 2 );       } else         puts( 0 );     } else if ((a - b) % (t - a) == 0) {       if (t != b)         puts( 1 );       else         puts( 0 );     } else       puts( 0 );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int i, j, n, c, d;   string a[105], b[105];   int main() {     for (cin >> n; i++ < n; d += !c)       for (cin >> a[i] >> b[i], j = c = 0; ++j < i;)         c += a[i] == a[j] & b[i] == b[j];     cout << d;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 998244853;   int n, m;   long long dp[2005][2005], k[2005][2005], C[4005][4005];   void init() {     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= m; j++) {         if (i == 0)           k[i][j] = 1;         else if (j == 0)           k[i][j] = 0;         else if (i > j)           k[i][j] = 0;         else           k[i][j] = (k[i - 1][j] + k[i][j - 1]) % mod;       }     }     C[0][0] = C[1][0] = C[1][1] = 1;     for (int i = 2; i <= n + m; i++) {       C[i][0] = 1;       for (int j = 1; j <= i; j++)         C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;     }   }   int main() {     cin >> n >> m;     init();     for (int i = 0; i <= n; i++) dp[i][0] = i, dp[0][i] = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         dp[i][j] =             ((dp[i - 1][j] + C[i + j - 1][i - 1]) % mod +              (dp[i][j - 1] - C[i + j - 1][j - 1] + k[i][j - 1] + mod) % mod) %             mod;     cout << dp[n][m] << endl;     return 0;   }
/*    * @Descripttion:     * @version:     * @Author: 纸月二五    * @Date: 2021-05-11 17:50:44    */   #include<bits/stdc++.h>   using namespace std;      int num[200010];      long long cal(long long m)   {       return m * (m - 1) / 2;   }      int main()   {       int T;       scanf( %d ,&T);          while(T --)       {           int n;           scanf( %d ,&n);           for(int i = 1;i <= n;i ++)           {               scanf( %d ,&num[i]);               num[i] = num[i] - i;           }              sort(num + 1,num + 1 + n);              long long sum = 0;           int last = num[1];           long long ans = 0;           for(int i = 1;i <= n;i ++)           {               if(num[i] == last)               {                   sum ++;                   if(i == n)                   ans += cal(sum);               }               else               {                   ans += cal(sum);                   sum = 1;                   last = num[i];               }           }           printf( %lld n ,ans);       }       //system( pause );       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int k, s, luu;   int F[100000];   int a[100000];   int res;   int main() {     cin >> s >> k;     F[0] = 1;     F[1] = 1;     for (int i = 2; i <= 10000; i++) {       F[i] = 2 * F[i - 1];       if (i >= k + 1) F[i] -= F[i - k - 1];       if (F[i] > 1000000000) {         luu = i;         break;       }     }     for (int i = luu; i >= 0; i--)       if (s >= F[i]) {         res++;         a[res] = F[i];         s -= F[i];       }     res++;     cout << res << endl;     cout << 0 <<    ;     for (int i = 1; i < res; ++i) cout << a[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int cmax = 223466;   int p[cmax];   set<int> s;   int n, q;   int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }   void merge(int x, int y) {     int xx = find(x);     int yy = find(y);     if (xx == yy) return;     if (rand() % 2) swap(xx, yy);     p[xx] = yy;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> q;     for (int i = 1; i <= n; i++) p[i] = i;     for (int i = 0; i < n; i++) s.insert(i);     s.insert(1 << 30);     for (int i = 0; i < q; i++) {       int t = 0, x = 0, y = 0;       cin >> t >> x >> y;       if (x > y) swap(x, y);       if (t == 1) {         merge(x, y);       } else if (t == 2) {         vector<int> v;         auto it = s.lower_bound(x);         while (it != s.end()) {           if (*it >= y) break;           v.push_back(*it);           it++;         }         for (int i = 0; i < v.size(); i++) s.erase(v[i]);         for (int i = 0; i < v.size(); i++) merge(v[i], v[i] + 1);       } else {         int xx = find(x);         int yy = find(y);         if (xx == yy)           cout <<  YES n ;         else           cout <<  NO n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, ta, b, i, j, tb, h, m, t;     int d, ar;     char ch[10];     cin >> a >> ta >> b >> tb;     cin >> ch;     h = (ch[0] -  0 ) * 10 + ch[1] -  0 ;     m = (ch[3] -  0 ) * 10 + ch[4] -  0 ;     t = (h - 5) * 60 + m;     j = 0;     long long count = 0;     for (i = 0; i < 1140; i += b) {       d = i;       ar = i + tb;       if (i > t + ta) break;       if (ar > t && d < t + ta) {         count++;       }     }     cout << count << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 16505;   const int inf = 1e9 + 7;   char s[N];   int fa[N * 2], rk[N * 2];   bool c[N], p[N];   inline int C(char q) {     if (q <=  9 )       return q -  0 ;     else       return q -  A  + 10;   }   int tot;   inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }   inline void Unite(int x, int y, int type) {     x = find(x);     y = find(y);     if (x != y) tot -= type;     if (rk[x] < rk[y])       fa[x] = y;     else       fa[y] = x;     if (rk[x] == rk[y]) rk[x]++;   }   int vis[N * 2], t[N], pos[N];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = (1); i <= (m * 2); i++) fa[i] = i;     for (int i = (1); i <= (m); i++) t[i] = i;     for (int T = (1); T <= (n); T++) {       for (int i = (1); i <= (m); i++) p[i] = c[i];       scanf( %s , s + 1);       int cnt = 0;       for (int i = (1); i <= (m / 4); i++) {         int pq = C(s[i]);         for (int k = (0); k <= (3); k++) {           ++cnt;           if (pq & (1 << (3 - k)))             c[cnt] = 1;           else             c[cnt] = 0;         }       }       for (int i = (1); i <= (m); i++)         if (c[i]) {           if (p[i]) Unite(i, i + m, 1);           if (i < m && c[i + 1]) Unite(i + m, i + m + 1, 1);           tot++;         }       memset(vis, 0, sizeof(vis));       int xjt = 0;       for (int i = (m + 1); i <= (m * 2); i++) {         if (c[i - m]) {           int x = find(i);           if (!vis[x]) {             vis[x] = ++xjt;             pos[xjt] = i - m;           }           t[i - m] = vis[x];         } else           t[i - m] = 0;       }       for (int i = (1); i <= (m * 2); i++) fa[i] = i;       memset(rk, 0, sizeof(rk));       for (int i = (1); i <= (m); i++)         if (t[i]) Unite(i, pos[t[i]], 0);       for (int i = (1); i <= (m - 1); i++)         if (p[i] && p[i + 1]) Unite(i, i + 1, 0);     }     printf( %d n , tot);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e3 + 55;   int x[MAX];   int s[2];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> x[i];     int i = 0;     int l = 0, r = n - 1;     while (l <= r) {       if (x[l] > x[r]) {         s[i % 2] += x[l];         l++;       } else {         s[i % 2] += x[r];         r--;       }       i++;     }     cout << s[0] <<     << s[1];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[1111111];   int main() {     ios_base::sync_with_stdio(false);     int n;     long long ts, tf, t;     cin >> ts >> tf >> t;     cin >> n;     long long ansV = ts, ans = 0;     long long total = ts;     for (int i = 0; i <= n; i++) {       long long x;       if (i == n) {         x = tf - t + 1;       } else {         cin >> x;       }       if (x - 1 >= total) {         if (total + t <= tf) {           ansV = 0;           ans = total;         }       } else {         if (total + t <= tf) {           if (total - (x - 1) < ansV) {             ansV = total - (x - 1);             ans = x - 1;           }         }       }       total = max(total, x) + t;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void c_p_c() {}   void solution() {     int a, b, c;     cin >> a >> b >> c;     cout << min(a, min(b / 2, c / 4)) * 7 << endl;   }   int32_t main() {     c_p_c();     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     solution();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int tree[262192];   int a[262192], pre[262192], ok[262192];   int n;   void check() {     int i, all_1 = 1;     for (i = 1; i <= n; ++i) {       if (a[i] != 1) all_1 = 0;     }     if (all_1) {       printf( 1 n );       exit(0);     }   }   void update(int pt, int l, int r, int index, int val) {     if (l > r || pt < l || pt > r) return;     if (l == r) {       tree[index] = max(tree[index], val);       return;     }     if (pt <= ((l + r) >> 1)) {       update(pt, l, ((l + r) >> 1), (index << 1), val);     } else {       update(pt, ((l + r) >> 1) + 1, r, (index << 1 | 1), val);     }     tree[index] = min(tree[(index << 1)], tree[(index << 1 | 1)]);   }   int query(int start, int end, int l, int r, int index) {     if (start > end || l > r || start > r || l > end) return 0;     if (start <= l && r <= end) return tree[index];     if (end <= ((l + r) >> 1))       return query(start, end, l, ((l + r) >> 1), (index << 1));     if (start >= ((l + r) >> 1) + 1)       return query(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1));     return min(query(start, end, l, ((l + r) >> 1), (index << 1)),                query(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1)));   }   int main() {     int i;     scanf( %d , &n);     for (i = 1; i <= n; ++i) scanf( %d , &a[i]);     check();     for (i = 1; i <= n; ++i) {       if (query(1, a[i] - 1, 1, n, 1) > pre[a[i]]) {         ok[a[i]] = 1;       }       pre[a[i]] = i;       update(a[i], 1, n, 1, i);     }     for (i = 1; i <= n; ++i) {       if (query(1, i, 1, n, 1) > pre[i + 1]) {         ok[i + 1] = 1;       }     }     for (i = 2; i <= n + 2; ++i) {       if (!ok[i]) {         printf( %d n , i);         break;       }     }     return 0;   }
#include <bits/stdc++.h>   inline long long ceil_div(long long a, long long b) { return (a + b - 1) / b; }   using namespace std;   inline int qr() {     int f = 0, fu = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) fu = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       f = (f << 3) + (f << 1) + (c & 15);       c = getchar();     }     return f * fu;   }   const int N = 305, M = 2e6 + 10;   const int dx[4] = {1, 0, -1, 0};   const int dy[4] = {0, 1, 0, -1};   int a[N][N], id[N][N], ans[M], n, m, q, tot, num;   struct node {     int x, y, c, p;   } op[M];   struct UF {     int fa[M << 1];     int get(int x) { return x == fa[x] ? x : (fa[x] = get(fa[x])); }     inline void merge(int x, int y) {       if (same(x, y)) return;       num--, fa[get(x)] = get(y);     }     inline bool same(int x, int y) { return get(x) == get(y); }   } U;   inline void merge(int x, int y) {     for (int i = 0, nx, ny; i < 4; i++) {       nx = x + dx[i], ny = y + dy[i];       if (nx < 1 || nx > n || ny < 1 || ny > m) continue;       if (a[x][y] == a[nx][ny]) {         U.merge(id[x][y], id[nx][ny]);       }     }   }   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 1, x, y, c; i <= q; i++) {       scanf( %d%d%d , &x, &y, &c);       op[i] = {x, y, c, a[x][y]}, a[x][y] = c;     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) a[i][j] = 0;     for (int i = 1, x, y; i <= q; i++) {       if (op[i].c == op[i].p) continue;       x = op[i].x, y = op[i].y, num = 0;       a[x][y] = op[i].c, id[x][y] = ++tot, U.fa[tot] = tot;       merge(x, y), ans[i] += num;     }     tot = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) id[i][j] = ++tot, U.fa[tot] = tot;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) merge(i, j);     for (int i = q, x, y; i >= 1; i--) {       if (op[i].c == op[i].p) continue;       x = op[i].x, y = op[i].y, num = 0;       a[x][y] = op[i].p, id[x][y] = ++tot, U.fa[tot] = tot;       merge(x, y), ans[i] -= num;     }     ans[0] = 1;     for (int i = 1; i <= q; i++) printf( %d n , ans[i] += ans[i - 1]);   }