malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long a[100005][2];   int dp[100005][3];   enum direction {     n,     l,     r,   };   int main() {     int N;     cin >> N;     for (size_t i = 1; i <= N; i++) {       cin >> a[i][0] >> a[i][1];       dp[i][n] = dp[i][l] = dp[i][r] = 0;     }     a[0][0] = -1e18;     a[0][1] = 0;     dp[0][l] = dp[0][r] = dp[0][n] = 0;     a[N + 1][0] = 1e18;     a[N + 1][1] = 0;     for (size_t i = 0; i <= N; i++) {       if (a[i + 1][0] - a[i][0] > a[i + 1][1] + a[i][1]) {         dp[i + 1][l] = 1 + max(max(dp[i][l], dp[i][r]), dp[i][n]);         dp[i + 1][n] = max(max(dp[i][l], dp[i][r]), dp[i][n]);       } else if (a[i + 1][0] - a[i][0] > a[i + 1][1]) {         dp[i + 1][l] = 1 + max(dp[i][l], dp[i][n]);         dp[i + 1][n] = max(dp[i][l], max(dp[i][n], dp[i][r]));       }       if (a[i + 2][0] - a[i + 1][0] > a[i + 2][1] + a[i + 1][1]) {         dp[i + 1][r] = 1 + max(max(dp[i][l], dp[i][r]), dp[i][n]);         dp[i + 1][n] = max(max(dp[i][l], dp[i][r]), dp[i][n]);       } else if (a[i + 2][0] - a[i + 1][0] > a[i + 1][1]) {         dp[i + 1][r] = 1 + max(dp[i][r], max(dp[i][n], dp[i][l]));         dp[i + 1][n] = max(max(dp[i][n], dp[i][r]), dp[i][l]);       }       if (!(a[i + 2][0] - a[i + 1][0] > a[i + 1][1]) &&           !(a[i + 1][0] - a[i][0] > a[i + 1][1])) {         dp[i + 1][n] = max(max(dp[i][l], dp[i][r]), dp[i][n]);       }     }     cout << max(max(dp[N][1], dp[N][0]), dp[N][2]);   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long inf = 0x3f3f3f3f;   const long long llinf = 1e18;   const long long nax = 0;   int n, m;   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     cout.tie();     cin >> n >> m;     vector<int> a(n + 5), b(m + 5);     int maks1 = 0, mins1 = inf, mins2 = inf;     for (int i = 0; i < n; ++i) {       cin >> a[i];       maks1 = max(maks1, a[i]);       mins1 = min(mins1, a[i]);     }     for (int i = 0; i < m; ++i) {       cin >> b[i];       mins2 = min(mins2, b[i]);     }     if (max(mins1 * 2, maks1) < mins2) {       cout << max(mins1 * 2, maks1) <<   n ;     } else       cout <<  -1  <<   n ;   }
#include<bits/stdc++.h>   #include<algorithm>   using namespace std;       #define ll long long   #define ld long double       #define rep(i,a,b) for(ll i=a;i<b;i++)   #define repb(i,a,b) for(ll i=a;i>=b;i--)       #define err() cout<< ================================== <<endl;   #define errA(A) for(auto i:A)   cout<<i<<   ;cout<<endl;   #define err1(a) cout<<#a<<   <<a<<endl   #define err2(a,b) cout<<#a<<   <<a<<   <<#b<<   <<b<<endl   #define err3(a,b,c) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<endl   #define err4(a,b,c,d) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<   <<#d<<   <<d<<endl       #define pb push_back   #define all(A)  A.begin(),A.end()   #define allr(A)    A.rbegin(),A.rend()   #define ft first   #define sd second       #define pll pair<ll,ll>   #define V vector<ll>   #define S set<ll>   #define VV vector<V>   #define Vpll vector<pll>   #define VVpll vector<Vpll>       #define endl   n        const ll logN = 20;   const ll N=100005;   const ll M = 1000000007;   const ll M2=998244353 ;   const ll INF = 1e12;   #define PI 3.14159265   #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)   ll pow1(ll a,ll b){       ll res=1;       while(b>0){           if(b&1){            res=(res*a)%M;     }           a=(a*a)%M;           b>>=1;       }       return res%M;   }      map<ll,ll>mp;   V a;   ll n;   void recur(ll depth,ll ind1=0, ll ind2=n-1 )   {       if(ind1>ind2)return;       ll mx_ind;       ll mx=LLONG_MIN;       rep(i,ind1,ind2+1)       {           if(a[i]>mx)           {               mx=a[i];               mx_ind=i;           }       }       mp[mx_ind]=depth;       recur(depth+1,ind1,mx_ind-1);       recur(depth+1,mx_ind+1,ind2);       return;   }         int main()   {       fast;          #ifndef ONLINE_JUDGE       freopen( ../input.txt ,  r , stdin);       freopen( ../output.txt ,  w , stdout);       #endif              ll T=1;       cin>>T;       while(T--)       {           cin>>n;           a=V(n);           rep(i,0,n)cin>>a[i];           recur(0);           for(auto it:mp)           cout<<it.sd<<   ;           cout<<endl;           mp.clear();       }      }
#include <bits/stdc++.h>   using namespace std;   const int MX = 300300;   int t, n, m, i, k[MX], c[MX];   long long r;   int main() {     cin >> t;     while (t--) {       int n, x, sum = 0, ctr = 0;       cin >> n >> x;       int arr[n];       for (int i = 0; i < n; i++) {         cin >> arr[i];         sum += arr[i];         ctr += (arr[i] == x);       }       if (ctr == n)         cout << 0 <<   n ;       else if (ctr >= 1 || sum == n * x)         cout << 1 <<   n ;       else         cout << 2 <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, fu = 0;     char ch = getchar();     for (; ch < 48 || ch > 57; ch = getchar()) fu |= (ch ==  - );     for (; ch > 47 && ch < 58; ch = getchar()) x = x * 10 + ch -  0 ;     return fu ? -x : x;   }   int n, mx = -1000000000, mn = 1000000000, ans = 1, tmp, no;   set<int> s;   char S[20];   int main() {     for (n = read(), s.insert(mx), s.insert(mn); n--;) {       scanf( %s , S), tmp = read();       if (S[1] ==  D )         s.insert(tmp), no += (tmp > mx && tmp < mn);       else {         if (tmp < mx || tmp > mn) return puts( 0 ), 0;         ans = (tmp > mx && tmp < mn) ? (ans << 1) % 1000000007 : ans;         s.erase(tmp), no = 0;         set<int>::iterator it = s.lower_bound(tmp);         mn = *it, --it, mx = *it;       }     }     printf( %d n , (int)(1LL * ans * (no + 1) % 1000000007));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     char a[111][111];     for (long long i = 1; i <= 3; i++)       for (long long j = 1; j <= 3; j++) cin >> a[i][j];     if (a[1][1] == a[3][3] && a[3][2] == a[1][2] && a[3][1] == a[1][3] &&         a[2][3] == a[2][1])       cout <<  YES ;     else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int tree[500000];   int a[100000], pos[100001];   void update(int node, int l, int r, int pos, int u) {     if (l == r) {       tree[node] += u;       return;     }     int mid = (l + r) / 2;     if (pos <= mid)       update(2 * node, l, mid, pos, u);     else       update(2 * node + 1, mid + 1, r, pos, u);     tree[node] = tree[2 * node] + tree[2 * node + 1];   }   int query(int node, int l, int r, int ql, int qr) {     if (ql > r || qr < l) return 0;     if (ql <= l && qr >= r) return tree[node];     int mid = (l + r) / 2;     return query(2 * node, l, mid, ql, qr) +            query(2 * node + 1, mid + 1, r, ql, qr);   }   int main() {     int i, j, k;     int n, res = 0;     scanf( %d , &n);     for (i = 0; i < n; i++) scanf( %d , &a[i]);     for (i = 0; i < n; i++) {       scanf( %d , &j);       pos[j] = i;     }     for (i = 0; i < n; i++) {       if (query(1, 0, n, pos[a[i]] + 1, n)) res++;       update(1, 0, n, pos[a[i]], 1);     }     printf( %d , res);   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int ans;   int a[400050];   int back[400050];   vector<int> edge[400050];   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1, u, v; i <= m; i++) {       cin >> u >> v;       edge[v].push_back(u);     }     for (auto iter : edge[a[n]]) back[iter]++;     for (int i = n - 1; i >= 1; i--) {       if (back[a[i]] == n - i - ans)         ans++;       else {         for (auto iter : edge[a[i]]) back[iter]++;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> p;   void sort() {     int temp = 0;     if (p.first < p.second) {       temp = p.first;       p.first = p.second;       p.second = temp;     }   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int op = 0;       cin >> p.first >> p.second;       if (p.first == p.second)         op = 1;       else {         if (p.first % p.second == 0)           op = p.first / p.second;         else {           while (p.first % p.second != 0) {             sort();             op += p.first / p.second;             p.first = p.first % p.second;           }         }       }       cout << op << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, a, b, finish1 = INFINITY, finish2 = INFINITY, start1 = 0,                        start2 = 0, mx = 0;     scanf( %lld , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld %lld , &a, &b);       start2 = max(start2, a);       finish1 = min(finish1, b);     }     scanf( %lld , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld %lld , &a, &b);       start1 = max(start1, a);       finish2 = min(finish2, b);     }     mx = max(mx, start1 - finish1);     mx = max(mx, start2 - finish2);     if (mx <= 0)       printf( 0 n );     else       printf( %lld n , mx);     return 0;   }
#include <bits/stdc++.h>   int x[100001] = {0};   int main() {     int i, n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &x[i]);     }     int sum = 0;     int cn = 0;     for (i = 0; i < n; i++) {       sum = sum + x[i] - x[i + 1];       if (sum < 0) {         do {           cn++;           sum++;         } while (sum < 0);       }     }     printf( %d , cn);     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, m;     scanf( %d%d , &n, &m);     int a[n], b[m], i, j;     for (i = 0; i < n; i++) scanf( %d , &a[i]);     for (i = 0; i < m; i++) scanf( %d , &b[i]);     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if (b[j] == a[i]) printf( %d  , a[i]);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll mod = 1e9 + 7;   const int maxn = 1e5 + 5, maxa = 71, lim = 1 << 19;   ll ev[maxn], od[maxn], dp[2][lim];   int n, a, f[lim], comp[maxa];   vector<int> p;   int calc_msk(int x) {     int msk = 0, b = 1;     for (int y : p) {       while (x % (y * y) == 0) x /= y * y;       msk = (x % y == 0) * b + msk;       b *= 2;     }     return msk;   }   int main() {     ios_base::sync_with_stdio(0);     for (int i = (2); i < (maxa); i++)       for (int j = i + i; j < maxa; j += i) comp[j] = 1;     for (int i = (2); i < (maxa); i++)       if (!comp[i]) p.push_back(i);     ev[0] = ev[1] = 1;     for (int i = (2); i < (maxn); i++) ev[i] = ev[i - 1] * 2 % mod;     od[0] = 0;     od[1] = 1;     for (int i = (2); i < (maxn); i++) od[i] = od[i - 1] * 2 % mod;     cin >> n;     for (int i = (0); i < (n); i++) {       cin >> a;       f[calc_msk(a)]++;     }     dp[0][0] = 1;     for (int i = (1); i < (maxa); i++) {       int x = calc_msk(i);       for (int msk = (0); msk < (lim); msk++)         dp[i & 1][msk] =             (dp[i & 1 ^ 1][msk] * ev[f[x]] + dp[i & 1 ^ 1][msk ^ x] * od[f[x]]) %             mod;       f[x] = 0;     }     cout << (dp[maxa & 1 ^ 1][0] + mod - 1) % mod <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int M = 100000 + 10;   int leaf[M];   vector<int> adj[M];   int childnum[M];   void dfs(int cur, int par) {     for (int i = 0; i < adj[cur].size(); i++) {       int nxt = adj[cur][i];       if (nxt != par) {         childnum[cur]++;         dfs(nxt, cur);       }     }   }   bool ok = true;   long long int coef[M];   long long int thres = 1e16 + 10;   void dfs1(int cur, int par) {     coef[cur] = childnum[par] * coef[par];     if (coef[cur] > thres) {       ok = false;       return;     }     for (int i = 0; i < adj[cur].size(); i++) {       int nxt = adj[cur][i];       if (nxt != par) {         dfs1(nxt, cur);       }     }   }   vector<int> leafnode;   long long int gcd(long long int x, long long int y) {     return y == 0 ? x : gcd(y, x % y);   }   int main() {     int n;     cin >> n;     long long int s = 0;     for (int i = 1; i <= n; i++) {       cin >> leaf[i];       s = s + leaf[i];     }     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     for (int i = 2; i <= n; i++) {       if (adj[i].size() == 1) {         leafnode.push_back(i);       }     }     dfs(1, 0);     coef[0] = 1;     childnum[0] = 1;     dfs1(1, 0);     long long int minv = -1;     for (int i = 0; i < leafnode.size(); i++) {       int curleaf = leafnode[i];       long long int x = leaf[curleaf];       long long int k = coef[curleaf];       long long int v = k * x;       if (minv == -1) {         minv = v;       } else {         minv = min(minv, v);       }     }     if (!ok) {       cout << s << endl;     } else {       long long int lcm = 1;       for (int i = 0; i < leafnode.size(); i++) {         int curleaf = leafnode[i];         long long int k = coef[curleaf];         long long int d = gcd(lcm, k);         lcm = lcm * k / d;         if (lcm > minv) {           ok = false;           break;         }       }       if (!ok) {         cout << s << endl;       } else {         lcm = minv / lcm * lcm;         long long int ans = 0;         for (int i = 0; i < leafnode.size(); i++) {           int curleaf = leafnode[i];           long long int k = coef[curleaf];           long long int x = lcm / k;           long long int val = leaf[curleaf] - x;           ans = ans + val;         }         cout << ans << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   long long a[300000];   int main() {     while (cin >> n) {       for (int i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       long long ans = 0;       for (int i = 0; i < n; i++) {         int x = (i == n - 1 ? -1 : 0);         ans += (x + i + 2) * a[i];       }       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool check(const string &str) {     vector<string> vec{ ABSINTH ,  BEER ,     BRANDY ,  CHAMPAGNE ,  GIN ,  RUM ,                         SAKE ,     TEQUILA ,  VODKA ,   WHISKEY ,    WINE };     for (int i = 0; i < (int)vec.size(); ++i) {       if (str == vec[i]) return true;     }     return false;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     int cnt = 0;     for (int i = 0; i < n; ++i) {       string str;       cin >> str;       if (isdigit(str[0])) {         int num = stoi(str);         if (num < 18) ++cnt;       } else {         if (check(str)) ++cnt;       }     }     cout << cnt <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       vector<vector<int> > a(n), b(m);       for (int i = 0; i < n; i++) {         a[i].resize(m);         for (int j = 0; j < m; j++) cin >> a[i][j];       }       for (int j = 0; j < m; j++) {         b[j].resize(n);         for (int i = 0; i < n; i++) cin >> b[j][i];       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           if (a[i][0] == b[j][0]) {             swap(a[0], a[i]);             swap(b[0], b[j]);             break;           }         }       }       for (int j = 0; j < n; j++) {         for (int i = 0; i < n; i++) {           if (a[i][0] == b[0][j]) {             for (auto x : a[i]) cout << x <<    ;             cout <<   n ;             break;           }         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   int yy[100005], id;   int n, m;   int st[100005 * 4], lz[100005 * 4];   int a[100005];   int l[305], r[305];   inline void pu(int rt) { st[rt] = max(st[2 * rt + 1], st[2 * rt + 2]); }   void pd(int rt, int l, int r, int m) {     if (lz[rt]) {       int t = lz[rt];       lz[2 * rt + 1] += t;       lz[2 * rt + 2] += t;       st[2 * rt + 1] += t;       st[2 * rt + 2] += t;       lz[rt] = 0;     }   }   void build(int rt, int l, int r) {     lz[rt] = 0;     if (l == r) {       st[rt] = a[l];       return;     }     int mid = (l + r) / 2;     build(2 * rt + 1, l, mid);     build(2 * rt + 2, mid + 1, r);     pu(rt);   }   void ud(int rt, int l, int r, int ul, int ur, int dt) {     if (l > ur || r < ul) return;     if (ul <= l && r <= ur) {       lz[rt] += dt;       st[rt] += dt;       return;     }     int mid = (l + r) / 2;     pd(rt, l, r, mid);     ud(2 * rt + 1, l, mid, ul, ur, dt), ud(2 * rt + 2, mid + 1, r, ul, ur, dt);     pu(rt);   }   int query(int rt, int l, int r, int ql, int qr) {     if (l > qr || r < ql) return -(0x3f3f3f3f);     if (ql <= l && r <= qr) return st[rt];     int mid = (l + r) / 2;     pd(rt, l, r, mid);     return max(query(2 * rt + 1, l, mid, ql, qr),                query(2 * rt + 2, mid + 1, r, ql, qr));   }   void init() {}   int main() {     cin >> n >> m;     int ans = -(0x3f3f3f3f), p = -1;     int mmx = -(0x3f3f3f3f), mmn = (0x3f3f3f3f);     for (int i = (1); i <= (n); i++) {       scanf( %d , a + i);       mmx = max(a[i], mmx), mmn = min(a[i], mmn);     }     if (m == 0) {       printf( %d n0 , mmx - mmn);       return 0;     }     for (int i = (0); i < (m); i++) scanf( %d%d , l + i, r + i);     build(0, 1, n);     for (int i = (1); i <= (n); i++) {       for (int j = (0); j < (m); j++) {         if (l[j] == i) {           ud(0, 1, n, l[j], r[j], -1);         } else if (i - 1 == r[j]) {           ud(0, 1, n, l[j], r[j], 1);         }       }       int mx = query(0, 1, n, 1, n);       int nowmin = query(0, 1, n, i, i);       if (mx - nowmin > ans) {         ans = mx - nowmin;         p = i;       }     }     for (int i = (0); i < (m); i++) {       if (p >= l[i] && p <= r[i]) {         yy[id++] = i + 1;       }     }     printf( %d n%d n , ans, id);     for (int i = (0); i < (id); i++) printf( %d  , yy[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 10;   const int INF = 2147000000;   int n, k;   int a[maxn][maxn];   int b[maxn], vis[maxn];   int ans = INF;   int make(int num) {     int res = 0;     for (int i = 1; i <= k; i++) {       int c = b[i];       res = res * 10 + a[num][c];     }     return res;   }   void solve() {     int Max = -INF, Min = INF;     for (int i = 1; i <= n; i++) {       int res = make(i);       Max = max(Max, res);       Min = min(Min, res);     }     ans = min(ans, Max - Min);   }   void dfs(int cur) {     if (cur == k + 1) {       solve();       return;     }     for (int i = 1; i <= k; i++) {       if (!vis[i]) {         vis[i] = 1;         b[cur] = i;         dfs(cur + 1);         vis[i] = 0;       }     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= k; j++) {         char c;         scanf(  %c , &c);         a[i][j] = c -  0 ;       }     }     memset(vis, 0, sizeof(vis));     dfs(1);     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int m, n, arr[2000][2000], temp[2000][2000];   char ctrl(int i) {     for (int j = 1; j <= n; j++)       if (arr[i][j] and arr[i][j] != temp[i][j]) return 0;     return 1;   }   char solve(int a, int b, int c, int d, int ttt = 0) {     if (ttt) ttt = 3;     for (int i = 1; i <= m; i++) {       if (i & 1) {         for (int j = 1; j <= n; j++)           if (j & 1)             temp[i][j] = a;           else             temp[i][j] = b;         if (ctrl(i)) continue;         for (int j = 1; j <= n; j++)           if (j & 1)             temp[i][j] = b;           else             temp[i][j] = a;         if (ctrl(i)) continue;         return 0;       } else {         for (int j = 1; j <= n; j++)           if (j & 1)             temp[i][j] = c;           else             temp[i][j] = d;         if (ctrl(i)) continue;         for (int j = 1; j <= n; j++)           if (j & 1)             temp[i][j] = d;           else             temp[i][j] = c;         if (ctrl(i)) continue;         return 0;       }     }     while (ttt--) {       for (int i = 1; i <= m; i++)         for (int j = 1; j <= n; j++) arr[j][m - i + 1] = temp[i][j];       swap(m, n);       for (int i = 1; i <= m; i++)         for (int j = 1; j <= n; j++) temp[i][j] = arr[i][j];     }     for (int i = 1; i <= m; i++) {       for (int j = 1; j <= n; j++) cout << temp[i][j];       cout << endl;     }     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> m >> n;     char c;     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) {         cin >> c;         arr[i][j] = c -  0 ;       }     if (solve(1, 2, 3, 4)) return 0;     if (solve(1, 3, 2, 4)) return 0;     if (solve(1, 4, 2, 3)) return 0;     if (solve(2, 3, 1, 4)) return 0;     if (solve(2, 4, 1, 3)) return 0;     if (solve(3, 4, 1, 2)) return 0;     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) temp[j][m - i + 1] = arr[i][j];     swap(m, n);     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) arr[i][j] = temp[i][j];     if (solve(1, 2, 3, 4, 1)) return 0;     if (solve(1, 3, 2, 4, 1)) return 0;     if (solve(1, 4, 2, 3, 1)) return 0;     if (solve(2, 3, 1, 4, 1)) return 0;     if (solve(2, 4, 1, 3, 1)) return 0;     if (solve(3, 4, 1, 2, 1)) return 0;     cout << 0 << endl;   }
#include <bits/stdc++.h>   using namespace std;   struct Data {     int r, c, v;     bool operator<(const Data &r) const { return v < r.v; }   };   vector<vector<int> > mat, ans;   Data sorteds[1001000];   int mR[1001000], mC[1001000];   map<pair<int, int>, vector<int> > mr, mc;   void recursiveFix(int rr, int cc) {     vector<int> &vv = mr[pair<int, int>(rr, mat[rr][cc])];     vector<int> t = vv;     for (int j = int(0); j < int(vv.size()); j++) {       ans[rr][vv[j]] = ans[rr][cc];     }     vv.clear();     vector<int> &vv2 = mc[pair<int, int>(cc, mat[rr][cc])];     vector<int> t2 = vv2;     for (int j = int(0); j < int(vv2.size()); j++) {       ans[vv2[j]][cc] = ans[rr][cc];     }     vv2.clear();     for (int j = int(0); j < int(t2.size()); j++) recursiveFix(t2[j], cc);     for (int j = int(0); j < int(t.size()); j++) recursiveFix(rr, t[j]);   }   int main() {     int N, M;     while (scanf( %d %d , &N, &M) != EOF) {       int c = 0;       vector<int> t(M + 1);       mr.clear(), mc.clear();       mat.clear();       ans.clear();       mat.push_back(t);       ans.push_back(t);       for (int i = int(1); i < int(N + 1); i++) {         mat.push_back(t);         ans.push_back(t);         for (int j = int(1); j < int(M + 1); j++) {           scanf( %d , &mat[i][j]);           sorteds[c++] = Data{i, j, mat[i][j]};           ans[i][j] = 1;           mr[pair<int, int>(i, mat[i][j])].push_back(j);           mc[pair<int, int>(j, mat[i][j])].push_back(i);         }       }       sort(sorteds, sorteds + c);       memset(mR, 0, sizeof(mR));       memset(mC, 0, sizeof(mC));       int prev = -1;       priority_queue<Data> q;       for (int i = int(0); i < int(c); i++) {         if (prev != sorteds[i].v) {           while (!q.empty()) {             int rr = q.top().r, cc = q.top().c;             recursiveFix(rr, cc);             q.pop();           }         }         int r = sorteds[i].r, c = sorteds[i].c;         ans[r][c] = 0;         if (mat[r][mR[r]] == mat[r][c])           ans[r][c] = max(ans[r][c], ans[r][mR[r]]);         else           ans[r][c] = max(ans[r][c], ans[r][mR[r]] + 1);         if (mat[mC[c]][c] == mat[r][c])           ans[r][c] = max(ans[r][c], ans[mC[c]][c]);         else           ans[r][c] = max(ans[r][c], ans[mC[c]][c] + 1);         q.push(Data{r, c, ans[r][c]});         mR[r] = c;         mC[c] = r;         prev = sorteds[i].v;       }       while (!q.empty()) {         int rr = q.top().r, cc = q.top().c;         recursiveFix(rr, cc);         q.pop();       }       for (int i = int(0); i < int(N); i++) {         for (int j = int(0); j < int(M); j++)           printf( %d%c , ans[i + 1][j + 1],    n [j == M - 1]);       }     }     return 0;   }