malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   inline long long read() {     long long k = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       k = k * 10 + ch -  0 ;       ch = getchar();     }     return k * f;   }   long long n, h, w, minx = 0, maxx = 0, miny = 0, maxy = 0, ans;   long long a[500010], b[500010];   char s[500010];   inline void shuchu() {     printf( %lld , ans);     exit(0);   }   signed main() {     n = read();     h = read();     w = read();     scanf( %s , s + 1);     long long cnt = 0, x = 0, y = 0, cc = 0;     for (long long i = 1; i <= n; i++) {       cnt++;       if (s[i] ==  U ) x--;       if (s[i] ==  D ) x++;       if (s[i] ==  L ) y--;       if (s[i] ==  R ) y++;       if (x < minx) minx = x, ans = (ans + cnt * w) % MOD, h--;       if (x > maxx) maxx = x, ans = (ans + cnt * w) % MOD, h--;       if (y < miny) miny = y, ans = (ans + cnt * h) % MOD, w--;       if (y > maxy) maxy = y, ans = (ans + cnt * h) % MOD, w--;       if (h == 0 || w == 0) shuchu();     }     if (x == 0 && y == 0) return puts( -1 ) & 0;     for (long long i = 1; i <= n; i++) {       cnt++;       if (s[i] ==  U ) x--;       if (s[i] ==  D ) x++;       if (s[i] ==  L ) y--;       if (s[i] ==  R ) y++;       if (x < minx)         minx = x, ans = (ans + cnt * w) % MOD, h--, a[++cc] = 1, b[cc] = cnt % n;       if (x > maxx)         maxx = x, ans = (ans + cnt * w) % MOD, h--, a[++cc] = 2, b[cc] = cnt % n;       if (y < miny)         miny = y, ans = (ans + cnt * h) % MOD, w--, a[++cc] = 3, b[cc] = cnt % n;       if (y > maxy)         maxy = y, ans = (ans + cnt * h) % MOD, w--, a[++cc] = 4, b[cc] = cnt % n;       if (h == 0 || w == 0) shuchu();       if (cc && b[cc] == 0) b[cc] = n;     }     long long p = 2, i = 1;     while (h != 0 && w != 0) {       if (a[i] == 1 || a[i] == 2)         ans = (ans + (p * n % MOD + b[i]) * w) % MOD, h--;       if (a[i] == 3 || a[i] == 4)         ans = (ans + (p * n % MOD + b[i]) * h) % MOD, w--;       if (i == cc)         i = 1, p++;       else         i++;     }     shuchu();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MN = 1e4 + 20;   vector<int> adj[MN], topo;   vector<pair<int, int>> edge;   int scc, st, s[MN], h[MN], t[MN];   bool os[MN];   stack<int> ms;   int n, m;   void add(int u, int v) {     adj[u + m].push_back(v);     adj[v + m].push_back(u);     adj[u].push_back(v + m);     adj[v].push_back(u + m);   }   void dfs(int v) {     os[v] = 1;     s[v] = h[v] = ++st;     ms.push(v);     for (int i : adj[v]) {       if (!s[i]) dfs(i);       if (os[i]) h[v] = min(h[v], h[i]);     }     if (h[v] == s[v]) {       for (int i = ms.top();; i = ms.top()) {         ms.pop();         os[i] = 0;         h[i] = h[v];         t[i] = topo.size();         if (i == v) break;       }       scc++;       topo.push_back(h[v]);     }   }   int main() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       u--;       v--;       if (v > u) swap(u, v);       for (int j = 0; j < i; j++) {         if (u == edge[j].first || u == edge[j].second || v == edge[j].first ||             v == edge[j].second)           continue;         if ((u < edge[j].first && u > edge[j].second) ^             (v < edge[j].first && v > edge[j].second))           add(i, j);       }       edge.push_back({u, v});     }     for (int i = 0; i < 2 * m; i++)       if (!s[i]) dfs(i);     reverse(topo.begin(), topo.end());     for (int i = 0; i < m; i++) {       if (h[i] == h[i + m]) return cout <<  Impossible , 0;     }     for (int i = 0; i < m; i++) {       t[i] < t[i + m] ? cout <<  i  : cout <<  o ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Edge {     int u, v, cap, cost, next;   } edge[555 * 1111];   int head[555];   int NV, NE, vs, vt;   void addEdge(int u, int v, int cap, int cost) {     edge[NE].u = u;     edge[NE].v = v;     edge[NE].cap = cap;     edge[NE].cost = cost;     edge[NE].next = head[u];     head[u] = NE++;     edge[NE].u = v;     edge[NE].v = u;     edge[NE].cap = 0;     edge[NE].cost = -cost;     edge[NE].next = head[v];     head[v] = NE++;   }   bool vis[555];   int d[555], pre[555];   bool SPFA() {     for (int i = 0; i < NV; ++i) {       vis[i] = 0;       d[i] = (1 << 30);     }     vis[vs] = 1;     d[vs] = 0;     queue<int> que;     que.push(vs);     while (!que.empty()) {       int u = que.front();       que.pop();       for (int i = head[u]; i != -1; i = edge[i].next) {         int v = edge[i].v;         if (edge[i].cap && d[v] > d[u] + edge[i].cost) {           d[v] = d[u] + edge[i].cost;           pre[v] = i;           if (!vis[v]) {             vis[v] = 1;             que.push(v);           }         }       }       vis[u] = 0;     }     return d[vt] != (1 << 30);   }   int MCMF() {     int res = 0;     while (SPFA()) {       int flow = (1 << 30), cost = 0;       for (int u = vt; u != vs; u = edge[pre[u]].u) {         flow = min(flow, edge[pre[u]].cap);       }       for (int u = vt; u != vs; u = edge[pre[u]].u) {         edge[pre[u]].cap -= flow;         edge[pre[u] ^ 1].cap += flow;         cost += flow * edge[pre[u]].cost;       }       res += cost;     }     return res;   }   int tn, ch[55500][26], fail[55500];   vector<int> vec[55500];   void insert(char *s, int k) {     int x = 0;     for (int i = 0; s[i]; ++i) {       int y = s[i] -  a ;       if (ch[x][y] == 0) ch[x][y] = ++tn;       x = ch[x][y];     }     vec[x].push_back(k);   }   void getfail() {     queue<int> que;     for (int i = 0; i < 26; ++i) {       if (ch[0][i]) que.push(ch[0][i]);     }     while (!que.empty()) {       int x = que.front();       que.pop();       for (int i = 0; i < 26; ++i) {         if (ch[x][i]) {           fail[ch[x][i]] = ch[fail[x]][i];           que.push(ch[x][i]);         } else           ch[x][i] = ch[fail[x]][i];       }     }   }   int val[111], len[111];   void ac(char *s) {     int x = 0;     for (int i = 0; s[i]; ++i) {       int y = s[i] -  a ;       x = ch[x][y];       for (int tmp = x; tmp; tmp = fail[tmp]) {         for (int j = 0; j < vec[tmp].size(); ++j) {           int k = vec[tmp][j];           addEdge(i - len[k] + 1, i + 1, 1, -val[k]);         }       }     }   }   char S[555], T[555];   int main() {     int n, m, x;     scanf( %d%s%d , &n, S, &m);     for (int i = 1; i <= m; ++i) {       scanf( %s%d , T, val + i);       len[i] = strlen(T);       insert(T, i);     }     scanf( %d , &x);     vs = n + 1;     vt = vs + 1;     NV = vt + 1;     NE = 0;     memset(head, -1, sizeof(head));     addEdge(vs, 0, x, 0);     addEdge(n, vt, x, 0);     for (int i = 1; i <= n; ++i) {       addEdge(i - 1, i, (1 << 30), 0);     }     getfail();     ac(S);     printf( %d , -MCMF());     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int x;   int f(int a, int b) { return (a * b + 1) / 2; }   bool ok(int n) {     if (x == 3 && n == 3) return false;     if (n % 2 == 0 && x % 4) return false;     if (n % 2 == 0) {       if (x / 4 <= f(n / 2 - 1, n / 2 - 1)) return true;       return false;     }     int z = f(n, n);     if (x > z) return false;     return true;   }   int main() {     cin >> x;     for (int i = (int)(1), _n = (int)(100000); i < _n; i++)       if (ok(i)) {         cout << i << endl;         break;       }     {       int _;       cin >> _;     }   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   inline int get_power(int a, int n) {     int res = 1;     while (n > 0) {       res = n & 1 ? 1ll * res * a % mod : res;       a = 1ll * a * a % mod;       n >>= 1;     }     return res;   }   inline int get_inv(int x) { return get_power(x, mod - 2); }   inline int C2(int x) { return (x * (x - 1ll) >> 1) % mod; }   const int max_n = 2e3 + 5;   int power_p[max_n], power_q[max_n];   int f[max_n], g[max_n], h[max_n][max_n];   int main() {     int n, a, b;     scanf( %d%d%d , &n, &a, &b);     int p = 1ll * a * get_inv(b) % mod;     power_p[0] = power_q[0] = 1;     for (int i = 1; i <= n; ++i) {       power_p[i] = 1ll * power_p[i - 1] * p % mod;       power_q[i] = power_q[i - 1] * (mod + 1ll - p) % mod;     }     h[1][0] = h[1][1] = 1;     for (int i = 2; i <= n; ++i)       for (int j = 0; j <= i; ++j)         h[i][j] = (1ll * h[i - 1][j] * power_p[j] +                    (j > 0 ? 1ll * h[i - 1][j - 1] * power_q[i - j] : 0)) %                   mod;     for (int i = 1; i <= n; ++i) {       g[i] = 1;       for (int j = 1; j <= i - 1; ++j) {         g[i] -= 1ll * g[j] * h[i][j] % mod;         g[i] += g[i] < 0 ? mod : 0;       }     }     for (int i = 1; i <= n; ++i) {       f[i] = 1ll * g[i] * h[i][i] % mod * C2(i) % mod;       for (int j = 1; j <= i - 1; ++j)         f[i] = (f[i] + 1ll * g[j] * h[i][j] % mod *                            (1ll * f[j] + f[i - j] + C2(i) - C2(i - j) + mod)) %                mod;       f[i] = 1ll * f[i] * get_inv(mod + 1 - 1ll * g[i] * h[i][i] % mod) % mod;     }     printf( %d n , f[n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   int main() {     int n, m;     cin >> n >> m;     vector<vector<char> > table(n + 1, vector<char>(m + 1));     int row = 0, col = 0, count = 0;     for (int i = 1; i <= n; i++) {       string tmp;       cin >> tmp;       for (int j = 0; j < tmp.length(); j++) {         table[i][j + 1] = tmp[j];       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (table[i][j] ==  B ) {           count++;           row += i;           col += j;         }       }     }     row /= count;     col /= count;     cout << row <<     << col << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   T gcd(T a, T b) {     if (a < b) {       swap(a, b);     }     T t;     while (b > 0) {       t = a % b;       a = b;       b = t;     }     return a;   }   template <class T>   T gcdL(vector<T>& v) {     int l = v.size();     if (l == 1) return v[0];     T g = gcd(v[0], v[1]);     for (int i = 2; i < l; i++) {       g = gcd(g, v[i]);     }     return g;   }   long long foo(long long x) {     while (x % 2 == 0) x /= 2;     while (x % 3 == 0) x /= 3;     return x;   }   bool ok(vector<long long>& v) {     for (int i = 0; i < (int)v.size(); i++) {       v[i] = foo(v[i]);     }     for (int i = 0; i < (int)v.size(); i++) {       if (v[i] == 1)         continue;       else         return 0;     }     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout << setprecision(10);     long long n, x;     vector<long long> v;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       v.push_back(x);     }     long long g = gcdL(v);     for (int i = 0; i < n; i++) {       v[i] /= g;     }     if (ok(v))       cout <<  Yes ;     else       cout <<  No ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 0, input = 0, temp = 0, sum = 0, c = 0;     cin >> n;     for (int i = 0; i < 2 * n; i++) {       cin >> temp;     }     if (sum % 2 == 0) {       cout <<  Karen ;     } else       cout <<  Koyomi ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int K;     vector<int> a(5, 0);     for (int i = 0; i < 5; i++) cin >> a[i];     cin >> K;     vector<int> b(K);     for (int i = 0; i < K; i++) {       string temp;       cin >> temp;       if (temp ==  XXL )         b[i] = 4;       else if (temp ==  XL )         b[i] = 3;       else if (temp ==  L )         b[i] = 2;       else if (temp ==  M )         b[i] = 1;       else         b[i] = 0;     }     for (int i = 0; i < K; i++) {       if (a[b[i]] > 0) {         a[b[i]]--;       } else {         if (b[i] + 1 < 5 && a[b[i] + 1] > 0) {           b[i]++;           a[b[i]] -= 1;         } else if (b[i] - 1 >= 0 && a[b[i] - 1] > 0) {           b[i]--;           a[b[i]] -= 1;         } else if (b[i] + 2 < 5 && a[b[i] + 2] > 0) {           b[i] += 2;           a[b[i]] -= 1;         } else if (b[i] - 2 >= 0 && a[b[i] - 2] > 0) {           b[i] -= 2;           a[b[i]] -= 1;         } else if (b[i] + 3 < 5 && a[b[i] + 3] > 0) {           b[i] += 3;           a[b[i]] -= 1;         } else if (b[i] - 3 >= 0 && a[b[i] - 3] > 0) {           b[i] -= 3;           a[b[i]] -= 1;         } else if (b[i] + 4 < 5 && a[b[i] + 4] > 0) {           b[i] += 4;           a[b[i]] -= 1;         } else if (b[i] - 4 >= 0 && a[b[i] - 4] > 0) {           b[i] -= 4;           a[b[i]] -= 1;         }       }     }     for (int i = 0; i < K; i++) {       if (b[i] == 4)         cout <<  XXL  << endl;       else if (b[i] == 3)         cout <<  XL  << endl;       else if (b[i] == 2)         cout <<  L  << endl;       else if (b[i])         cout <<  M  << endl;       else         cout <<  S  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int output(int n) {     int sum = 0;     while (n != 1) {       int i;       for (i = 2; i * i <= n; i++)         if (n % i == 0) {           sum += n;           n /= i;           break;         }       if (i * i > n) {         sum += n;         break;       }     }     return ++sum;   }   int main() {     int t;     t = 1;     while (t--) {       int n;       cin >> n;       cout << output(n) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     long long l, r;     int now;     vector<pair<int, int> > v;   } st[250005 * 4];   int pi, ri, n, N;   int p[250005], m[250005];   long long r, sp[250005];   pair<long long, int> d[250005];   queue<int> q;   vector<long long> rr;   bool vis[250005];   void init(int id, int l, int r) {     if (l == r) {       st[id].l = rr[l - 1] + 1;       st[id].r = rr[l];       int L = lower_bound(d + 2, d + n + 2, make_pair(rr[l - 1] + 1, 0)) - d;       int R = upper_bound(d + 2, d + n + 2, make_pair(rr[l], 1000000000)) - d;       for (int i = L; i < R; i++) {         st[id].v.push_back(make_pair(m[d[i].second], d[i].second));       }       sort(st[id].v.begin(), st[id].v.end());       return;     }     int mid = l + r >> 1;     init(id * 2, l, mid);     init(id * 2 + 1, mid + 1, r);     st[id].l = st[id * 2].l;     st[id].r = st[id * 2 + 1].r;     st[id].v.insert(st[id].v.begin(), st[id * 2].v.begin(), st[id * 2].v.end());     st[id].v.insert(st[id].v.end(), st[id * 2 + 1].v.begin(),                     st[id * 2 + 1].v.end());     sort(st[id].v.begin(), st[id].v.end());   }   void query(int id, int l, int r, long long L, long long R, int p) {     if (R < st[id].l || st[id].r < L) return;     if (L <= st[id].l && st[id].r <= R) {       int len = st[id].v.size();       while (st[id].now < len && st[id].v[st[id].now].first <= p) {         if (!vis[st[id].v[st[id].now].second])           q.push(st[id].v[st[id].now].second);         vis[st[id].v[st[id].now].second] = 1;         st[id].now++;       }       return;     }     int mid = l + r >> 1;     query(id * 2, l, mid, L, R, p);     query(id * 2 + 1, mid + 1, r, L, R, p);   }   int main() {     long long x, y, xi, yi;     scanf( %I64d%I64d%d%I64d%d , &xi, &yi, &p[1], &r, &n);     r *= r;     sp[1] = r;     rr.push_back(r);     for (int i = 2; i <= n + 1; i++) {       scanf( %I64d%I64d%d%d%I64d , &x, &y, &m[i], &p[i], &r);       x -= xi;       y -= yi;       d[i].first = x * x + y * y;       d[i].second = i;       r *= r;       sp[i] = r;       rr.push_back(r);     }     rr.push_back(0);     sort(rr.begin(), rr.end());     rr.resize(unique(rr.begin(), rr.end()) - rr.begin());     N = rr.size() - 1;     sort(d + 2, d + n + 2);     init(1, 1, N);     q.push(1);     while (!q.empty()) {       int now = q.front();       q.pop();       query(1, 1, N, 1, sp[now], p[now]);     }     int cnt = 0;     for (int i = 2; i <= n + 1; i++) {       if (vis[i]) {         cnt++;       }     }     printf( %d n , cnt);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string str;     cin >> str;     for (int i = 1; i < str.size() - 1; i++) {       if (str[i + 1] !=  .  && str[i - 1] !=  .  && str[i] !=  . ) {         if (str[i + 1] != str[i - 1] && str[i - 1] != str[i] &&             str[i + 1] != str[i]) {           cout <<  Yes  << endl;           return 0;         }       }     }     cout <<  No  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100;   int mem[MAXN + 10];   int main() {     int N;     cin >> N;     mem[0] = 0, mem[1] = 1, mem[2] = 2, mem[3] = 4;     for (int i = 4; i <= N; ++i) {       mem[i] = i + mem[i - 2];     }     cout << mem[N] <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   long long modexp(long long base, long long exp) {     if (exp == 1)       return base;     else if (exp == 0)       return 1;     else {       if (exp % 2 == 0) {         long long base1 = pow(modexp(base, exp / 2), 2);         if (base1 >= 1000000007)           return base1 % 1000000007;         else           return base1;       } else {         long long ans = (base * pow(modexp(base, (exp - 1) / 2), 2));         if (ans >= 1000000007)           return ans % 1000000007;         else           return ans;       }     }   }   long long gcd(long long a, long long b) {     if (!a) return b;     return gcd(b % a, a);   }   map<int, int> d;   set<int> s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int a[n];     for (long long i = 0; i < n; ++i) cin >> a[i];     for (long long i = 0; i < n; ++i) d[a[i]]++;     long long ans = 0;     for (long long i = 0; i < n; ++i) {       d[a[i]]--;       if (!d[a[i]]) {         d.erase(a[i]);       }       if (s.find(a[i]) == s.end()) {         ans += d.size();         s.insert(a[i]);       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 10;   bool valid;   int n, p[maxn], a[maxn], head, tail;   int main() {     int T;     scanf( %d , &T);     while (T--) {       scanf( %d , &n);       for (int i = 1; i <= n; ++i) {         scanf( %d , &p[i]);         a[p[i]] = i;       }       head = a[1], tail = n;       valid = true;       for (int i = 1; i <= n; ++i) {         if (a[i] != a[i - 1] + 1 && a[i] != head) {           valid = false;           break;         }         if (a[i] == tail) {           tail = head - 1;           head = a[i + 1];         }       }       if (valid)         puts( Yes );       else         puts( No );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N=2e5+10;   int a[N],tt;   int cnt[N];   int pre[N];   int main(){       int t;       scanf( %d ,&t);       while(t--){           int n;           tt=0;           scanf( %d ,&n);           for(int i=1;i<=n;i++) scanf( %d ,&a[i]);           sort(a+1,a+n+1);           for(int i=1;i<=n;i++){               if(a[i]==a[i-1]) cnt[tt]++;               else{                   tt++;                   cnt[tt]++;               }           }           priority_queue<int>s;           for(int i=1;i<=tt;i++) {               s.push(cnt[i]);               cnt[i]=0;           }           int sh=0;           int ans;           while(1){               if(s.size()==1) {                   ans = s.top();                   break;               }               if(s.empty()){                   ans=0;                   break;               }               int a=s.top();               s.pop();               int b=s.top();               s.pop();               a--;               b--;               if(a!=0)                   s.push(a);               if(b!=0)                   s.push(b);              }           printf( %d n ,ans);       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     while (cin >> t) {       string a;       cin >> a;       int s = 0;       for (int i = 0; i < t - 1;) {         if (a.at(i) == a.at(i + 1)) {           if (a.at(i) ==  a )             a.at(i) =  b ;           else             a.at(i) =  a ;           s++;         }         i += 2;         if (i >= a.size()) break;       }       cout << s << endl << a << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     vector<pair<long long, short> > mass;     long n, k;     scanf( %li %li , &n, &k);     long long a, b;     for (long i = 0; i < n; i++) {       scanf( %I64d %I64d , &a, &b);       mass.push_back(make_pair(a, -1));       mass.push_back(make_pair(b, 1));     }     sort(mass.begin(), mass.end());     vector<pair<long long, long long> > ans(n);     long long x = 0, balance = 0;     for (long i = 0; i < n * 2; i++) {       if (mass[i].second == -1 && balance == k - 1) ans[x].first = mass[i].first;       if (balance == k && mass[i].second == 1) ans[x++].second = mass[i].first;       balance -= mass[i].second;     }     printf( %I64d  n , x);     for (long i = 0; i < x; i++)       printf( %I64d %I64d  n , ans[i].first, ans[i].second);   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3fffffff;   const long long _INF = 0x7ffffffffffffffll;   const double EPS = 1e-9;   const double PI = acos(-1.0);   template <class T, class INT>   inline T pow(T x, INT y) {     T res(1);     for (T tt(x); y; y /= 2) {       if (y & 1) res *= tt;       tt *= tt;     }     return res;   }   template <class T, class INT>   inline T pow(T x, INT y, T p) {     T res(1);     for (T tt(x); y; y /= 2) {       if (y & 1) res = res * tt % p;       tt = tt * tt % p;     }     return res;   }   int toInt(string s) {     int x = 0;     istringstream sin(s);     sin >> x;     return x;   }   template <class T>   inline string toString(T x) {     ostringstream sout;     sout << x;     return sout.str();   }   template <class INT>   inline INT gcd(INT x, INT y) {     return y ? gcd(y, x % y) : x;   }   template <class INT>   inline int calc_bits(INT x) {     return x ? (x & 1) + calc_bits(x >> 1) : 0;   }   int n, x, xx, y, yy, ans;   int main(int argc, char* argv[]) {     cin >> n >> x >> y >> xx >> yy;     ans = abs(x - xx) + abs(y - yy);     if (x == 0 && xx == n || x == n && xx == 0)       ans += min(n - max(y, yy), min(y, yy)) * 2;     if (y == 0 && yy == n || y == n && yy == 0)       ans += min(n - max(x, xx), min(x, xx)) * 2;     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, a[1001][1001];   set<int> r[1001], c[1001];   map<int, int> r_m[1001], c_m[1001];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %d , &a[i][j]);         r[i].insert(a[i][j]);         c[j].insert(a[i][j]);       }     }     for (int i = 1; i <= n; i++) {       int w = 1;       for (int x : r[i]) {         r_m[i][x] = w;         w++;       }     }     for (int i = 1; i <= m; i++) {       int w = 1;       for (int x : c[i]) {         c_m[i][x] = w;         w++;       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         int x = r_m[i][a[i][j]];         int y = c_m[j][a[i][j]];         if (x > y) {           printf( %d  , max((int)r[i].size(), (int)c[j].size() + x - y));         } else {           printf( %d  , max((int)r[i].size() + y - x, (int)c[j].size()));         }       }       printf(  n );     }     return 0;   }