malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, m, s, d;   vector<int> a;   map<int, int> p, q;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m >> s >> d;     a.resize(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     sort(a.begin(), a.end());     if (a[0] <= s) {       cout <<  IMPOSSIBLE  << endl;       return 0;     }     p[a[0] - 1] = 0;     for (int i = 0; i < n; ++i) {       int next = a[i] + 1;       if (binary_search(a.begin(), a.end(), next)) {         continue;       }       int prev = next - d;       auto pi = p.lower_bound(prev);       if (pi == p.end()) {         continue;       }       q[next] = pi->first;       if (i < n - 1 && next + s < a[i + 1]) {         p[a[i + 1] - 1] = next;       } else {         if (i == n - 1) {           p[m] = next;         }       }     }     if (p.find(m) != p.end()) {       int last = m;       vector<pair<int, bool>> res;       while (last != 0) {         int prev = p[last];         if (prev < last) {           res.push_back(make_pair(last - prev, true));           last = prev;         }         if (last > 0) {           int prev_j = q[last];           res.push_back(make_pair(last - prev_j, false));           last = prev_j;         }       }       reverse(res.begin(), res.end());       for (auto x : res) {         if (x.second) {           cout <<  RUN   << x.first <<   n ;         } else {           cout <<  JUMP   << x.first <<   n ;         }       }     } else {       cout <<  IMPOSSIBLE  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rng(1234);   const long long mod = 1e9 + 7;   long long n, m, k, r, c;   long long ax, ay, bx, by;   long long modpow(long long u, long long v) {     long long res = 1, t = u;     while (v) {       if (v & 1) res = res * t % mod;       t = t * t % mod, v >>= 1;     }     return res;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.precision(15);     cin >> n >> m >> k >> r >> c;     cin >> ax >> ay >> bx >> by;     if (ax == bx && ay == by) {       cout << modpow(k, n * m);       return 0;     }     long long comp = n * m - r * c;     cout << modpow(k, comp);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int n, m;   int pre[maxn];   void init(int n) {     for (int i = 0; i <= n + 1; i++) pre[i] = i;   }   int finds(int x) {     if (x == pre[x])       return x;     else       return pre[x] = finds(pre[x]);   }   int main() {     while (cin >> n >> m) {       int ans = 0;       init(n);       for (int i = 1, a, b; i <= m; i++) {         cin >> a >> b;         int fa = finds(a);         int fb = finds(b);         if (fa != fb) {           pre[fa] = fb;         } else           ans++;       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 30, maxRow = maxN * maxN;   int n, m, a, b, d[maxN], g[maxN][maxN];   double A[maxRow][maxRow], p[maxN][maxN];   int main() {     scanf( %d%d%d%d , &n, &m, &a, &b);     --a, --b;     for (int i = 0; i < m; ++i) {       int x, y;       scanf( %d%d , &x, &y);       --x, --y;       ++d[x], ++d[y], g[x][y] = g[y][x] = 1;     }     for (int i = 0; i < n; ++i) {       scanf( %lf , &p[i][i]);       for (int j = 0; j < n; ++j)         if (g[i][j]) p[i][j] = (1.0 - p[i][i]) / d[i];     }     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) {         int now = i * n + j;         A[now][now] = -1.0;         for (int x = 0; x < n; ++x)           for (int y = 0; y < n; ++y)             if (x != y) {               int that = x * n + y;               A[now][that] += p[x][i] * p[y][j];             }       }     int N = n * n;     A[a * n + b][N] = -1.0;     for (int i = 0; i < N; ++i) {       int r = i;       for (int j = i + 1; j < N; ++j)         if (fabs(A[j][i]) > fabs(A[r][i])) r = j;       if (r != i)         for (int j = 0; j < N + 1; ++j) swap(A[i][j], A[r][j]);       for (int k = i + 1; k < N; ++k) {         double f = A[k][i] / A[i][i];         for (int j = i; j <= N; ++j) A[k][j] -= A[i][j] * f;       }     }     for (int i = N - 1; i >= 0; --i) {       for (int j = i + 1; j < N; ++j) A[i][N] -= A[j][N] * A[i][j];       A[i][N] /= A[i][i];     }     for (int i = 0; i < n; ++i) printf( %.7lf n , A[i * n + i][N]);     return 0;   }
#include <bits/stdc++.h>   void build_comp(int v, int comp_num, std::vector<int> &cur_comp,                   std::vector<int> &rev_comps,                   const std::vector<std::vector<int>> &g) {     cur_comp.push_back(v);     rev_comps[v] = comp_num;     for (int to : g[v]) {       if (rev_comps[to] == -1) {         build_comp(to, comp_num, cur_comp, rev_comps, g);       }     }   }   void solve() {     int n;     std::cin >> n;     std::vector<std::vector<std::pair<int, int>>> positions(n);     const auto reader = [n, &positions](int num) {       for (int i = 0; i < n; ++i) {         int a;         std::cin >> a;         positions[a - 1].emplace_back(num, i);       }     };     const auto neg = [n](int val) { return (val + n) % (2 * n); };     reader(0);     reader(1);     for (int i = 0; i < n; ++i) {       if (positions[i].size() != 2) {         std::cout << -1 <<   n ;         return;       }     }     std::vector<std::vector<int>> g(2 * n);     for (int i = 0; i < n; ++i) {       auto pa = positions[i][0];       auto pb = positions[i][1];       if (pa.second == pb.second) {         continue;       }       int a = pa.second;       int b = pb.second;       if (pa.first == pb.first) {         a = neg(a);       }       g[a].push_back(b);       g[b].push_back(a);       g[neg(a)].push_back(neg(b));       g[neg(b)].push_back(neg(a));     }     int comp_num = 0;     std::vector<std::vector<int>> comps;     std::vector<int> rev_comps(2 * n, -1);     for (int i = 0; i < 2 * n; ++i) {       if (rev_comps[i] == -1) {         std::vector<int> cur_comp;         build_comp(i, comp_num++, cur_comp, rev_comps, g);         comps.emplace_back(std::move(cur_comp));       }     }     for (int i = 0; i < n; ++i) {       if (rev_comps[i] == rev_comps[neg(i)]) {         std::cout << -1 <<   n ;         return;       }     }     std::set<int> res;     std::set<size_t> skipping_comps;     for (size_t i = 0; i < comps.size(); ++i) {       if (skipping_comps.find(i) != skipping_comps.end()) {         continue;       }       const auto &line = comps[i];       int positive = 0;       for (int v : line) {         if (v < n) {           ++positive;         }       }       int negative = line.size() - positive;       if (positive <= negative) {         for (int v : line) {           if (v < n) {             res.insert(v);           }         }       }       if (positive == negative) {         skipping_comps.insert(rev_comps[neg(line[0])]);       }     }     std::cout << res.size() <<   n ;     for (int v : res) {       std::cout << v + 1 <<    ;     }     std::cout <<   n ;   }   int main() {     int t;     std::cin >> t;     for (; t; --t) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   long long int a[100005], c[100005], s[100005];   using namespace std;   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       c[a[i]]++;     }     s[0] = 0;     s[1] = c[1];     for (int i = 2; i <= 100000; i++) {       s[i] = max(s[i - 1], (s[i - 2] + c[i] * i));     }     cout << s[100000];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k1, k2, u[1001], q[1001], start, x, y;   vector<int> vec[1001];   int a, b;   int f(int v, int pr) {     if (u[v] == 1) {       return v;     }     int yy = 0;     for (int i = 0; i < vec[v].size(); i++) {       if (vec[v][i] != pr) {         yy = max(yy, f(vec[v][i], v));       }     }     return yy;   }   pair<int, int> get(int v, int pr) {     pair<int, int> ans = make_pair(0, 0);     if (v == y) {       ans.first = 1;       return ans;     }     if (u[v] == 1) {       ans.second = v;     }     pair<int, int> yy = make_pair(0, 0);     for (int i = 0; i < vec[v].size(); i++) {       if (vec[v][i] != pr) {         yy = max(yy, get(vec[v][i], v));       }     }     if (yy.first != 0) {       ans.first = 1;     }     if (yy.first != 0 && yy.second != 0) {       ans = yy;     }     return ans;   }   void solve() {     for (int i = 0; i < 1001; i++) {       vec[i].clear();       u[i] = 0;       q[i] = 0;     }     cin >> n;     for (int i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       vec[a].push_back(b);       vec[b].push_back(a);     }     cin >> k1;     for (int i = 0; i < k1; i++) {       scanf( %d , &x);       u[x] = 1;     }     cin >> k2;     for (int i = 0; i < k2; i++) {       scanf( %d , &x);       q[x] = 1;     }     start = f(1, -1);     cout <<  A   << start << endl;     fflush(stdout);     cin >> y;     if (q[y] == 1) {       cout <<  C   << start << endl;       fflush(stdout);       return;     } else {       cout <<  B   << x << endl;       fflush(stdout);       cin >> y;       if (u[y] == 1) {         cout <<  C   << y << endl;         fflush(stdout);         return;       } else {         pair<int, int> ppp = get(1, -1);         if (ppp.second == 0) {           cout <<  C -1  << endl;           fflush(stdout);           return;         }         start = ppp.second;         cout <<  A   << start << endl;         fflush(stdout);         cin >> y;         if (q[y] == 1) {           cout <<  C   << start << endl;           fflush(stdout);           return;         } else {           cout <<  C -1  << endl;           fflush(stdout);           return;         }       }     }   }   int main() {     int test;     cin >> test;     while (test--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool chmax(T& a, const T& b) {     return a < b ? (a = b, true) : false;   }   template <typename T>   bool chmin(T& a, const T& b) {     return b < a ? (a = b, true) : false;   }   int N;   vector<int> A;   list<int> edges[100001];   long long dp[100001][2];   long long dfs(int curr, int prev, int k) {     vector<long long> v[2];     long long acc[2] = {};     long long res = 0;     for (const auto& it : edges[curr])       if (it != prev) {         res += dfs(it, curr, k);         for (int i = (int)(0); i < (int)(2); i++) {           v[i].push_back(dp[it][i]);           acc[i] += dp[it][i];         }       }     long long x = 0;     for (int i = (int)(0); i < (int)(v[0].size()); i++) {       for (int j = (int)(0); j < (int)(2); j++) {         if (j ^ (A[curr] >> k & 1)) {           x += (v[j][i] * (acc[0] - v[0][i])) << k;         } else {           x += (v[j][i] * (acc[1] - v[1][i])) << k;         }       }     }     res += x / 2;     if (A[curr] >> k & 1) {       dp[curr][0] += acc[1];       dp[curr][1] += acc[0];       dp[curr][1]++;     } else {       dp[curr][0] += acc[0];       dp[curr][1] += acc[1];       dp[curr][0]++;     }     return res;   }   int main() {     scanf( %d , &N);     A.assign(N + 1, 0);     for (int i = (int)(1); i < (int)(N + 1); i++) {       scanf( %d , &A[i]);     }     for (int i = (int)(0); i < (int)(N - 1); i++) {       int u, v;       scanf( %d%d , &u, &v);       edges[u].push_back(v);       edges[v].push_back(u);     }     long long ans = 0;     for (int i = (int)(0); i < (int)(20); i++) {       memset(dp, 0, sizeof(dp));       ans += dfs(1, -1, i);       for (int j = (int)(1); j < (int)(N + 1); j++) {         ans += dp[j][1] << i;       }     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, i;     while (scanf( %d , &n) != EOF) {       if (n == 1)         printf( %d n , n);       else {         printf( %d  , n);         for (i = 1; i < n; i++) {           if (i != n - 1)             printf( %d  , i);           else             printf( %d n , i);         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Maxn = 100 * 1000 + 5;   int n;   vector<int> arr[Maxn];   int main() {     scanf( %d , &n);     int x, k;     for (int i = 0; i < n; i++) {       scanf( %d%d , &x, &k);       arr[k - 1].push_back(x);     }     for (int i = 0; i < Maxn; i++) {       int num = -1;       for (int j = 0; j < (int)arr[i].size(); j++) {         if (arr[i][j] > num + 1) {           printf( NO n );           return 0;         }         if (arr[i][j] == num + 1) num++;       }     }     printf( YES n );     return 0;   }
#include<bits/stdc++.h>   using namespace std;   using ll = long long;   int main() {    ios::sync_with_stdio(false), cin.tie(nullptr);    int _;    for (cin >> _; _--;) {     int n;     cin >> n;     int a[n + 1];     int cnt = 0;     for (int i = 1; i <= n; ++i) {      cin >> a[i];      if (a[i] != i)cnt++;     }     if (cnt == 0) cout <<  0 n ;     else if (a[1] == 1 || a[n] == n)cout <<  1 n ;     else {      if (a[1] == n and a[n] == 1)cout <<  3 n ;      else cout <<  2 n ;     }    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1123;   const int MAXK = 312345;   struct S2DSum {     long long x[MAXN][MAXN];     void integrate(int n, int m) {       for (int j = 0; j < m; j++) {         for (int i = 1; i < n; i++) {           x[i][j] += x[i - 1][j];         }       }       for (int i = 0; i < n; i++) {         for (int j = 1; j < m; j++) {           x[i][j] += x[i][j - 1];         }       }     }     void add(int a, int b, int c, int d, long long e) {       x[a][b] += e;       x[a][d + 1] -= e;       x[c + 1][b] -= e;       x[c + 1][d + 1] += e;     }     long long sum(int a, int b, int c, int d) {       long long ans = x[c][d];       if (a) ans -= x[a - 1][d];       if (b) ans -= x[c][b - 1];       if (a and b) ans += x[a - 1][b - 1];       return ans;     }   };   int n, m, k;   int pic[MAXN][MAXN];   S2DSum orig;   S2DSum cost[26];   S2DSum dc;   int a[MAXK], b[MAXK], c[MAXK], d[MAXK], e[MAXK];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       for (int j = 0; j < m; j++) pic[i][j] = s[j] -  a ;     }     for (int i = 0; i < k; i++) {       char ce;       cin >> a[i] >> b[i] >> c[i] >> d[i] >> ce;       e[i] = ce -  a ;       a[i]--;       b[i]--;       c[i]--;       d[i]--;       orig.add(0, 0, n - 1, m - 1, 1);       orig.add(a[i], b[i], c[i], d[i], -1);       for (int j = 0; j < 26; j++) {         cost[j].add(a[i], b[i], c[i], d[i], abs(e[i] - j));       }     }     orig.integrate(n, m);     orig.integrate(n, m);     for (int cl = 0; cl < 26; cl++) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           long long hm = orig.sum(i, j, i, j);           cost[cl].add(i, j, i, j, abs(cl - pic[i][j]) * hm);         }       }       cost[cl].integrate(n, m);       cost[cl].integrate(n, m);     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         dc.add(i, j, i, j, cost[pic[i][j]].sum(i, j, i, j));       }     }     dc.integrate(n, m);     dc.integrate(n, m);     long long ans = numeric_limits<long long>::max();     long long ini = dc.sum(0, 0, n - 1, m - 1);     for (int i = 0; i < k; i++) {       long long sub = ini;       sub -= dc.sum(a[i], b[i], c[i], d[i]);       sub += cost[e[i]].sum(a[i], b[i], c[i], d[i]);       ans = min(ans, sub);     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y, msk;     string s;     node(int x1, int x2, int x3, string x4) { x = x1, y = x2, msk = x3, s = x4; }     bool operator<(const node &b) const {       if (s.size() != b.s.size()) return s.size() > b.s.size();       return s > b.s;     }   };   int gx[5] = {0, 1, 0, -1, 0}, gy[5] = {0, 0, 1, 0, -1};   char s[51][51];   set<int> st[51][51];   int las[51][51];   int count(int x) {     int ans = 0;     while (x) ans += x & 1, x >>= 1;     return ans;   }   signed main() {     int x = 0, y = 0;     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; i++) cin >> s[i] + 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (s[i][j] ==  S ) x = i, y = j;     priority_queue<node> q;     q.push(node(x, y, 0,   ));     while (!q.empty()) {       node u = q.top();       q.pop();       for (int i = 1; i <= 4; i++) {         int tox = u.x + gx[i];         int toy = u.y + gy[i];         if (tox > n || toy > m || tox <= 0 || toy <= 0) continue;         if (s[tox][toy] ==  T ) {           cout << u.s;           return 0;         }         int mk = u.msk | (1 << (s[tox][toy] -  a ));         if (count(mk) > k) continue;         if (las[tox][toy] && ((las[tox][toy] & mk) == las[tox][toy])) continue;         las[tox][toy] = mk;         if (st[tox][toy].count(mk)) continue;         st[tox][toy].insert(mk);         string tmp = u.s;         q.push(node(tox, toy, mk, u.s + s[tox][toy]));       }     }     printf( -1 );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct rest {     int s, f;   };   rest lis[1000500];   bool cmp(rest &a, rest &b) { return a.f < b.f; }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       scanf( %d %d , &lis[i].s, &lis[i].f);     }     sort(lis + 1, lis + n + 1, cmp);     int now = 0, ans = 0;     for (int i = 1; i <= n; i++) {       if (lis[i].s > now) {         ans++;         now = lis[i].f;       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   char s[2000000], t[2000000];   int n, i, YY = 0, YN = 0, NY = 0, NN = 0, Yaro = 0, Andrew = 0;   int main() {     scanf( %d n , &n);     for (i = 0; i < 2 * n; i++) scanf( %c , &s[i]);     scanf(  n );     for (i = 0; i < 2 * n; i++) scanf( %c , &t[i]);     for (i = 0; i < 2 * n; i++) {       if (s[i] ==  1  && t[i] ==  1 ) {         YY++;       } else if (s[i] ==  1  && t[i] ==  0 ) {         YN++;       } else if (s[i] ==  0  && t[i] ==  1 ) {         NY++;       } else if (s[i] ==  1  && t[i] ==  1 ) {         NN++;       }     }     for (i = 0; i < n; i++) {       if (YY > 0) {         YY--;         Yaro++;       } else if (YN > 0) {         YN--;         Yaro++;       } else if (NY > 0) {         NY--;       } else {         NN--;       }       if (YY > 0) {         YY--;         Andrew++;       } else if (NY > 0) {         NY--;         Andrew++;       } else if (YN > 0) {         YN--;       } else {         NN--;       }     }     if (Yaro == Andrew) {       printf( Draw n );     } else if (Yaro > Andrew) {       printf( First n );     } else if (Yaro < Andrew) {       printf( Second n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   clock_t T;   constexpr auto EPS = 1e-7;   const int mod = (int)1e9 + 7;   const double pi = acos(-1);   int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};   int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};   long long gcd(long long a, long long b) { return (a) ? gcd(b % a, a) : b; }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   void COME_BACK() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);   }   int main() {     COME_BACK();     int n, x;     cin >> n >> x;     if (n == 2 && !x)       return cout <<  NO                    <<   n ,              0;     if (n == 1)       return cout <<  YES                    <<   n                    << x,              0;     if (n == 2)       return cout <<  YES                    <<   n                    << 0 <<     << x,              0;     cout <<  YES           <<   n ;     for (int i = 0; i < n - 3; i++) cout << i <<    , x ^= i;     cout << ((1 << 18) ^ x) <<     << (1 << 19) <<     << ((1 << 18) ^ (1 << 19))          <<   n ;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( avx,avx2,fma )   using namespace std;   using ll = long long;   using ld = long double;   const ld eps = (ld)1e-11;   const long long INF = 4e18;   long long n, q;   vector<long long> a, b;   vector<array<long long, 4>> qs;   long long sz = 400;   struct block {     vector<array<long long, 3>> ln, chln;     vector<ld> dot;     vector<long long> ans;     block() { ans.resize(q); }     inline ld intr(array<long long, 3> ln1, array<long long, 3> ln2) {       if (ln1[0] == ln2[0])         return (ln1[1] > ln2[1] ? -INF : INF);       else         return ld(ln2[1] - ln1[1]) / ld(ln1[0] - ln2[0]);     }     void build() {       sort(ln.begin(), ln.end());       dot.clear();       chln.clear();       for (long long i = 0; i < ln.size(); ++i) {         while (dot.size() && intr(ln[i], chln.back()) - eps < dot.back()) {           chln.pop_back();           dot.pop_back();         }         if (chln.size()) dot.push_back(intr(ln[i], chln.back()));         chln.push_back(ln[i]);       }       long long pos = 0;       for (long long i = 0; i < q; ++i) {         while (pos < dot.size() && dot[pos] < qs[i][0]) ++pos;         ans[i] = chln[pos][2];       }     }   };   vector<block> d;   void build() {     for (long long i = 0; i < n; ++i) {       long long cur = i / sz;       if (cur >= d.size()) d.push_back({});       d[cur].ln.push_back({b[i], a[i], i});     }     for (auto &x : d) x.build();   }   long long qry(long long l, long long r, long long num) {     long long ans = -1, l0 = l % sz;     while (l < r) {       if (!l0 && l + sz <= r) {         long long pos = l / sz, cur = d[pos].ans[num];         if (ans == -1 ||             a[cur] + qs[num][0] * b[cur] > a[ans] + qs[num][0] * b[ans])           ans = cur;         l += sz;       } else {         if (ans == -1 || a[l] + qs[num][0] * b[l] > a[ans] + qs[num][0] * b[ans])           ans = l;         ++l;         l0 = (l0 + 1) % sz;       }     }     return ans;   }   void solve() {     cin >> n >> q;     a.resize(n);     b = a;     for (long long i = 0; i < n; ++i) {       cin >> a[i] >> b[i];     }     qs.resize(q);     for (long long i = 0; i < q; ++i) {       cin >> qs[i][1] >> qs[i][2] >> qs[i][0];       --qs[i][1];       qs[i][3] = i;     }     sort(qs.begin(), qs.end());     build();     vector<long long> ans(q);     for (long long i = 0; i < q; ++i) {       ans[qs[i][3]] = qry(qs[i][1], qs[i][2], i);     }     for (auto x : ans) cout << x + 1 <<   n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long test_case = 1;     for (long long i = 0; i < test_case; ++i) {       solve();       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mod(int a, int b) {     if (a > b)       return a - b;     else       return b - a;   }   int main() {     int n, m, i, j, l = 0;     cin >> n >> m;     int a[n];     for (i = 0; i < n; i++) cin >> a[i];     for (i = 0; i < n; i++) {       for (j = 0; j < n; j++) {         if (i != j) {           if (mod(a[i], a[j]) <= m) l++;         }       }     }     cout << l;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 41;   int n, m, q, cs;   char a[N][N];   int s[N][N];   int dp[N][N][N][N];   int main() {     memset(dp, 0, sizeof(int) * N * N * N * N);     cin >> n >> m >> q;     for (int i = 0; i < n; ++i) {       cin.ignore();       for (int j = 0; j < m; ++j) {         cin >> a[i + 1][j + 1];       }     }     for (int i = 1; i <= n; ++i) {       s[i][0] = 0;       for (int j = 1; j <= m; ++j) {         s[i][j] = (a[i][j] ==  0  ? s[i][j - 1] + 1 : 0);       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         for (int k = i; k <= n; ++k) {           for (int l = j; l <= m; ++l) {             dp[i][j][k][l] =                 dp[i][j][k - 1][l] + dp[i][j][k][l - 1] - dp[i][j][k - 1][l - 1];             int mn = l - j + 1;             for (int i1 = k; i1 >= i; --i1) {               mn = min(mn, s[i1][l]);               dp[i][j][k][l] += mn;             }             if (dp[i][j][k][l] < 0)               return cout << i <<     << j <<     << k <<     << l, 0;           }         }       }     }     for (int i = 0; i < q; ++i) {       int a, b, c, d;       cin >> a >> b >> c >> d;       cout << dp[a][b][c][d] << endl;     }   }