malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1000000007;   const long double PI = 2 * acos(0.0);   const vector<long long int> dx = {1, -1, 0, 0};   const vector<long long int> dy = {0, 0, 1, -1};   vector<long long int> ga(long long int n, bool oneIndexed = false) {     vector<long long int> a;     if (oneIndexed) a.push_back(0ll);     for (long long int i = 0; i < n; i++) {       long long int p;       cin >> p;       a.push_back(p);     }     return move(a);   }   vector<unsigned long long int> gau(unsigned long long int n,                                      bool oneIndexed = false) {     vector<unsigned long long int> a;     if (oneIndexed) a.push_back(0llu);     for (unsigned long long int i = 0; i < n; i++) {       unsigned long long int p;       cin >> p;       a.push_back(p);     }     return move(a);   }   vector<string> gas(unsigned long long int n, bool oneIndexed = false) {     vector<string> a;     if (oneIndexed) a.push_back(  );     for (unsigned long long int i = 0; i < n; i++) {       string p;       cin >> p;       a.push_back(p);     }     return move(a);   }   template <typename T, typename A>   void pa(vector<T, A> const &a, long long int begin = 0,           long long int end = -1) {     if (end == -1) end = (long long int)a.size() - 1;     for (long long int i = begin; i <= end; i++) {       cout << a[i] <<    ;     }     cout <<   n ;   }   template <typename T, typename A>   void papair(vector<T, A> const &a, long long int begin = 0,               long long int end = -1) {     if (end == -1) end = (long long int)a.size() - 1;     for (long long int i = begin; i <= end; i++) {       cout << a[i].first <<     << a[i].second <<   n ;     }     cout <<   n ;   }   void yes() { cout <<  YES n ; }   void no() { cout <<  NO n ; }   void yesno(int f) {     if (f)       yes();     else       no();   }   void solve() {     vector<long long int> infreq, outfreq;     long long int n, q;     cin >> n >> q;     vector<long long int> a = ga(n, 1);     infreq = vector<long long int>(n + 1, 0);     outfreq = vector<long long int>(n + 1, 0);     while (q--) {       long long int l, r;       cin >> l >> r;       infreq[l] += 1;       outfreq[r] += 1;     }     vector<long long int> freq(n + 1, 0);     for (long long int i = 1; i <= n; i++) {       freq[i] += freq[i - 1];       freq[i] += infreq[i];       freq[i] -= outfreq[i - 1];     }     vector<pair<long long int, long long int> > data;     for (long long int i = 1; i <= n; i++) data.push_back({freq[i], i});     sort(data.begin(), data.end());     sort(a.begin(), a.end());     long long int s = 0;     while (!data.empty()) {       s += data.back().first * a.back();       a.pop_back();       data.pop_back();     }     cout << s;   }   int main(void) {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 1;   int plot[MAX], n, k;   void input() {     cin >> n >> k;     for (int i = 1; i <= n; i++) scanf( %d , &plot[i]);   }   int main() {     input();     int res = 0, k1 = 0, k2 = 0;     for (int i = 1; i <= n; i++) {       if (plot[i] != k1) k2++;       if (plot[i] == k1) res = max(res, k2), k2 = 1;       k1 = plot[i];     }     res = max(res, k2);     cout << res;   }
#include <bits/stdc++.h>   using namespace std;   const int Maxn = 100000;   struct Node {     int t;     int id;   };   bool cmp(Node lhs, Node rhs) {     return lhs.t == rhs.t ? lhs.id < rhs.id : lhs.t < rhs.t;   }   int N, P;   Node A[Maxn + 5];   queue<int> q1;   priority_queue<int, vector<int>, greater<int> > q2;   long long ans[Maxn + 5];   int main() {     scanf( %d %d , &N, &P);     for (int i = 1; i <= N; i++) {       scanf( %d , &A[i].t);       A[i].id = i;     }     sort(A + 1, A + N + 1, cmp);     long long now = 0;     int last = N + 1;     for (int i = 1; i <= N;) {       if (!q1.empty()) {         int id = q1.front();         q1.pop();         ans[id] = now + P;         now += P, last = id;       } else if (A[i].t <= now) {         if (q1.empty() && A[i].id < last)           q1.push(A[i].id);         else if (!q1.empty() && q1.back() > A[i].id && A[i].id < last)           q1.push(A[i].id);         else           q2.push(A[i].id);         i++;       } else {         if (!q2.empty()) {           int id = q2.top();           q2.pop();           ans[id] = now + P;           now += P, last = id;         } else           now = A[i].t, last = N + 1;       }     }     while (!q1.empty()) {       int id = q1.front();       q1.pop(), ans[id] = now + P;       now += P;     }     while (!q2.empty()) {       int id = q2.top();       q2.pop(), ans[id] = now + P;       now += P;     }     for (int i = 1; i <= N; i++) printf( %lld  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long ans[100100];   int c[100100];   int v[100100];   int main() {     int n, q;     scanf( %d%d , &n, &q);     for (int i = (0); i < (n); ++i) scanf( %d , &v[i]);     for (int i = (0); i < (n); ++i) {       scanf( %d , &c[i]);       c[i]--;     }     for (int i = (0); i < (q); ++i) {       int a, b;       scanf( %d%d , &a, &b);       if (n == 1) {         long long res = ((0 < v[0] * 1ll * b) ? (v[0] * 1ll * b) : (0));         cout << res << endl;       } else {         for (int i = (0); i < (n); ++i) ans[i] = -1000000000000000000ll;         long long max1 = -1000000000000000000ll;         long long max2 = max1;         int v1 = 0, v2 = 1;         for (int i = (0); i < (n); ++i) {           long long k1 = ans[c[i]];           k1 += v[i] * 1ll * a;           long long V = max2;           if (v1 != c[i]) V = max1;           long long k2 = V;           k2 += v[i] * 1ll * b;           long long k3 = v[i] * 1ll * b;           k1 = ((k1 < k2) ? (k2) : (k1));           k1 = ((k1 < k3) ? (k3) : (k1));           if (k1 > max1) {             if (v1 != c[i]) {               max2 = max1;               v2 = v1;               max1 = k1;               v1 = c[i];             } else {               max1 = k1;             }           } else if (k1 > max2) {             if (c[i] != v1) {               max2 = k1;               v2 = c[i];             }           }           if (ans[c[i]] < k1) ans[c[i]] = k1;         }         long long res = 0;         for (int i = (0); i < (n); ++i) res = ((res < ans[i]) ? (ans[i]) : (res));         cout << res << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int main() {     int n;     cin >> n;     map<int, long long> cnt;     map<int, long long> last;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       map<int, long long> next;       last[x]++;       for (map<int, long long>::iterator j = last.begin(); j != last.end(); j++) {         int g = gcd(j->first, x);         next[g] += j->second;         cnt[g] += j->second;       }       last = next;     }     int q;     cin >> q;     while (q--) {       int x;       cin >> x;       cout << cnt[x] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   const int maxn = 301, maxk = 10, maxs = 1 << 9, INF = 0x3f3f3f3f;   int n, a[maxn], c[maxn], tot, fact[maxk], f[maxs], ans = INF;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , a + i);     for (int i = 1; i <= n; ++i) scanf( %d , c + i);     for (int i = 1, j, k; i <= n; ++i) {       tot = 0;       for (j = 2, k = a[i]; j * j <= k; ++j)         if (k % j == 0) {           fact[tot++] = j;           while (k % j == 0) k /= j;         }       if (k > 1) fact[tot++] = k;       memset(f, 0x3f, sizeof f);       f[(1 << tot) - 1] = c[i];       for (j = 1; j <= n; ++j) {         int mask = 0;         for (k = 0; k < tot; ++k)           if (a[j] % fact[k] == 0) mask |= 1 << k;         for (int s = (1 << tot) - 1; s >= 0; --s)           if (f[s & mask] > f[s] + c[j]) f[s & mask] = f[s] + c[j];       }       if (ans > f[0]) ans = f[0];     }     if (ans == INF)       puts( -1 );     else       printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct point {     int x, y;   } q[1010 * 1010 * 4];   struct node {     int next, now;   } d[1010 * 1010 * 2];   int c_c[1010], c_r[1010];   int dis[1010 * 1010];   bool vis[1010 * 1010], use_r[1010], use_c[1010];   int n, m;   void add_edge_col(int u, int v, int i) {     d[i].next = c_c[u];     c_c[u] = i;     d[i].now = v;   }   void add_edge_row(int u, int v, int i) {     d[i].next = c_r[u];     c_r[u] = i;     d[i].now = v;   }   void init() {     cin >> n >> m;     int l = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         char ch;         cin >> ch;         if (ch ==  # ) {           add_edge_col(j, i, ++l);           add_edge_row(i, j, ++l);         }       }     add_edge_row(n, m + 1, ++l);   }   void solve() {     int head = 1, tail = 1;     memset(dis, 64, sizeof(dis));     q[1].x = 1;     q[1].y = 0;     dis[0] = 0;     vis[0] = true;     bool flag = 0;     while (head <= tail) {       int u = (q[head].x - 1) * m + q[head].y;       if (dis[u] >= dis[n * m + 1]) {         vis[u] = false;         head++;         continue;       }       if (!use_r[q[head].x])         for (int i = c_r[q[head].x]; i; i = d[i].next) {           int v = (q[head].x - 1) * m + d[i].now;           if (dis[v] > dis[u] + 1) {             if (v == n * m + 1) flag = 1;             if (!vis[v] && v != n * m + 1) {               tail++;               q[tail].x = q[head].x;               q[tail].y = d[i].now;               vis[v] = true;             }             dis[v] = dis[u] + 1;           }         }       use_r[q[head].x] = true;       if (!use_c[q[head].y])         for (int i = c_c[q[head].y]; i; i = d[i].next) {           int v = (d[i].now - 1) * m + q[head].y;           if (dis[v] > dis[u] + 1) {             if (v == n * m + 1) flag = 1;             if (!vis[v] && v != n * m + 1) {               tail++;               q[tail].x = d[i].now;               q[tail].y = q[head].y;               vis[v] = true;             }             dis[v] = dis[u] + 1;           }         }       use_c[q[head].y] = true;       vis[u] = false;       head++;     }     if (flag)       cout << dis[n * m + 1] - 1 << endl;     else       cout <<  -1  << endl;   }   int main() {     ios::sync_with_stdio(false);     init();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, K, A, B, Q, x, y, t, ans;   int data[200010];   class segleaf {    public:     int s[2];   } seg[2000010];   void change(int x, int l, int r, int t, int delta) {     if (l == r) {       data[t] += delta;       seg[x].s[0] = min(data[t], B);       seg[x].s[1] = min(data[t], A);       return;     }     int mid = (l + r) >> 1;     if (t <= mid)       change(x << 1, l, mid, t, delta);     else       change((x << 1) + 1, mid + 1, r, t, delta);     seg[x].s[0] = seg[x << 1].s[0] + seg[(x << 1) + 1].s[0];     seg[x].s[1] = seg[x << 1].s[1] + seg[(x << 1) + 1].s[1];   }   int ask(int x, int l, int r, int ll, int rr, int tt) {     if (l == ll && r == rr) return seg[x].s[tt];     int mid = (l + r) >> 1;     if (rr <= mid)       return ask(x << 1, l, mid, ll, rr, tt);     else if (ll > mid)       return ask((x << 1) + 1, mid + 1, r, ll, rr, tt);     else       return ask(x << 1, l, mid, ll, mid, tt) +              ask((x << 1) + 1, mid + 1, r, mid + 1, rr, tt);   }   int main() {     scanf( %d%d%d%d%d , &N, &K, &A, &B, &Q);     for (int i = 1; i <= Q; i++) {       scanf( %d , &t);       if (t == 1) {         scanf( %d%d , &x, &y);         change(1, 1, N, x, y);       } else {         scanf( %d , &x);         ans = 0;         if (x > 1) ans += ask(1, 1, N, 1, x - 1, 0);         if (x + K <= N) ans += ask(1, 1, N, x + K, N, 1);         printf( %d n , ans);       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, w, b;     int a[100];     cin >> n >> w >> b;     for (int i = 0; i < n; i++) cin >> a[i];     int mini, maxi;     if (w <= b) {       mini = w;       maxi = b;     } else {       mini = b;       maxi = w;     }     int st = 0, endi = n - 1, cost = 0;     while (st <= endi) {       if (a[st] == a[endi] && a[st] == 2) {         if (st != endi)           cost += 2 * mini;         else           cost += mini;       }       if (a[st] != a[endi]) {         if (a[st] != 2 && a[endi] != 2) {           cout <<  -1  << endl;           return 0;         }         if (a[st] == 0 || a[endi] == 0)           cost += w;         else if (a[st] == 1 || a[endi] == 1)           cost += b;       }       st++;       endi--;     }     cout << cost << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 10005;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }   long long fact(long long n) {     long long ret = 1;     for (int i = 1; i <= n; i++) ret *= i;     return ret;   }   bool is_vowel(char c) {     if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u  || c ==  y )       return 1;     return 0;   }   long double getDistance(long double x1, long double y1, long double x2,                           long double y2) {     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));   }   int n, b, q, upto[N], qu[N];   vector<pair<int, int> > v;   int main() {     scanf( %d%d%d , &n, &b, &q);     for (int i = 0; i < q; i++) {       scanf( %d%d , &upto[i], &qu[i]);       v.push_back(pair<int, int>(upto[i], qu[i]));     }     sort(v.begin(), v.end());     v.push_back(pair<int, int>(b, n));     for (int i = 0; i < q; i++) {       if (v[i].second > v[i + 1].second) {         cout <<  unfair ;         return 0;       }     }     for (int i = 1; i < (1 << 5); i++) {       int sum = 0;       for (int j = 0; j < 5; j++) {         if (i & (1 << j)) sum++;       }       sum *= (n / 5);       int cur = 1, prev = 0;       for (int j = 0; j < v.size(); j++) {         if (v[j].first < cur) continue;         int matching = 0;         while (cur <= v[j].first) {           int rem = cur % 5;           if (i & (1 << rem)) {             matching++;           }           cur++;         }         sum -= min(matching, v[j].second - prev);         prev = v[j].second;       }       if (sum > 0) {         printf( unfair );         return 0;       }     }     printf( fair );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 1;   const int mod = 1e9 + 7;   int dp[N][21][2];   int n, f[21][2], two[21], three[2];   long long a, b, c;   signed main() {     scanf( %d , &n);     for (int i = 0; i <= 20; ++i) {       if (n < (int)(pow(2, i))) break;       for (int j = 0; j <= 1; ++j) {         if (n < (int)(pow(2, i) * pow(3, j))) break;         f[i][j] = n / (int)(pow(2, i) * pow(3, j));       }     }     three[0] = two[0] = 1;     three[1] = 3;     for (int i = 1; i <= 20; ++i) {       two[i] = two[i - 1] * 2;     }     memset(dp, 0, sizeof(dp));     a = log2(n);     dp[1][a][0] = 1;     if (n >= pow(2, a - 1) * 3) dp[1][a - 1][1] = 1;     for (int i = 2; i <= n; ++i) {       for (int j = 0; j <= 20; ++j) {         if (n < two[j]) break;         for (int k = 0; k <= 1; ++k) {           if (n < two[j] * three[k]) break;           b = dp[i - 1][j][k];           c = f[j][k] - (i - 1);           a = (b * c) % mod;           a %= mod;           if (n >= two[j + 1] * three[k]) {             b = dp[i - 1][j + 1][k];             c = f[j][k] - f[j + 1][k];             a += (b * c) % mod;             a %= mod;           }           if (!k && n >= two[j] * three[k + 1]) {             b = dp[i - 1][j][k + 1];             c = f[j][k] - f[j][k + 1];             a += (b * c) % mod;             a %= mod;           }           dp[i][j][k] = a % mod;         }       }     }     printf( %d , dp[n][0][0]);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100;   const int mod = 95542721, magic = 48;   int inf = 1e9;   int tr[4 * N][magic], lz[4 * N];   int a[N];   void build(int cn, int b, int e) {     if (b == e) {       int x = a[b];       for (int i = 0; i < magic; i++) {         tr[cn][i] = x;         int y = 1ll * x * x % mod;         y = 1ll * y * x % mod;         x = y;       }       return;     }     int lc = cn << 1, rc = lc | 1, mid = b + e >> 1;     build(lc, b, mid);     build(rc, mid + 1, e);     for (int i = 0; i < magic; i++) tr[cn][i] = (tr[lc][i] + tr[rc][i]) % mod;   }   void propagate(int u, int st, int en) {     if (!lz[u]) return;     if (st != en) {       lz[2 * u] += lz[u];       lz[2 * u] %= magic;       lz[2 * u + 1] += lz[u];       lz[2 * u + 1] %= magic;     }     int tmp[magic];     for (int i = 0; i < magic; i++) tmp[i] = tr[u][(i + lz[u]) % magic];     for (int i = 0; i < magic; i++) tr[u][i] = tmp[i];     lz[u] = 0;   }   void update(int u, int st, int en, int l, int r) {     propagate(u, st, en);     if (r < st || en < l)       return;     else if (l <= st && en <= r) {       lz[u]++;       lz[u] %= magic;       propagate(u, st, en);     } else {       int mid = (st + en) / 2;       update(2 * u, st, mid, l, r);       update(2 * u + 1, mid + 1, en, l, r);       for (int i = 0; i < magic; i++)         tr[u][i] = (tr[u << 1][i] + tr[(u << 1) | 1][i]) % mod;     }   }   int query(int u, int st, int en, int l, int r) {     propagate(u, st, en);     if (r < st || en < l)       return 0;     else if (l <= st && en <= r)       return tr[u][0];     else {       int mid = (st + en) / 2;       return (query(2 * u, st, mid, l, r) + query(2 * u + 1, mid + 1, en, l, r)) %              mod;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, q;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     build(1, 1, n);     cin >> q;     while (q--) {       int t, l, r;       cin >> t >> l >> r;       if (t == 2)         update(1, 1, n, l, r);       else         cout << query(1, 1, n, l, r) <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll N = 1e6 + 10;   ll n, m, q, v;   ll kl, ke;   ll l[N];   ll e[N];   void umin(ll &a, ll b) { a = min(a, b); }   signed main() {     ios::sync_with_stdio(0), cin.tie(0);     cin >> n >> m;     cin >> kl >> ke;     cin >> v;     for (ll i = 0; i < kl; ++i) {       cin >> l[i];     }     for (ll i = 0; i < ke; ++i) {       cin >> e[i];     }     sort(l, l + kl);     sort(e, e + ke);     cin >> q;     while (q--) {       ll x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       if (y1 > y2) {         swap(x1, x2);         swap(y1, y2);       }       ll ans = (ll)1e18;       auto it = lower_bound(l, l + kl, y1);       if (it != l + kl) {         umin(ans, abs(y1 - *it) + abs(y2 - *it) + abs(x1 - x2));       }       it = upper_bound(l, l + kl, y2);       if (it != l) {         --it;         umin(ans, abs(y1 - *it) + abs(y2 - *it) + abs(x1 - x2));       }       it = lower_bound(e, e + ke, y1);       if (it != e + ke) {         umin(ans, abs(y1 - *it) + abs(y2 - *it) + (abs(x1 - x2) + v - 1) / v);       }       it = upper_bound(e, e + ke, y2);       if (it != e) {         --it;         umin(ans, abs(y1 - *it) + abs(y2 - *it) + (abs(x1 - x2) + v - 1) / v);       }       if (x1 == x2) {         ans = abs(y1 - y2);       }       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int cg(long long int n) { return n ^ (n >> 1); }   long long int SUM(long long int a) { return (a * (a + 1) / 2); }   bool CAN(int x, int y, int n, int m) {     if (x >= 0 && y >= 0 && x < n && y < m) {       return true;     }     return false;   }   double her(double x1, double arayikhalatyan, double x2, double y2) {     return sqrt((x1 - x2) * (x1 - x2) +                 (arayikhalatyan - y2) * (arayikhalatyan - y2));   }   string strsum(string a, string b) {     int p = 0;     string c;     reverse(a.begin(), a.end());     reverse(b.begin(), b.end());     if (b.length() < a.length()) {       for (int i = b.length(); i < a.length(); i++) {         b +=  0 ;       }     } else {       for (int i = a.length(); i < b.length(); i++) {         a +=  0 ;       }     }     a +=  0 , b +=  0 ;     for (int i = 0; i < a.length(); i++) {       c += (a[i] -  0  + b[i] -  0  + p) % 10 +  0 ;       p = (a[i] + b[i] -  0  -  0  + p) / 10;     }     if (c[c.length() - 1] ==  0 ) c.erase(c.length() - 1, 1);     reverse(c.begin(), c.end());     return c;   }   string strmin(string a, string b) {     if (a.length() > b.length()) return b;     if (b.length() > a.length()) return a;     for (int i = 0; i < a.length(); i++) {       if (a[i] > b[i]) return b;       if (b[i] > a[i]) return a;     }     return a;   }   char vow[] = { a ,  e ,  i ,  o ,  u };   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, 1, -1};   const int N = 1e6 + 30;   const long long int mod = 1e9 + 7;   int q;   int n, k;   vector<pair<long long int, int> > g[N];   long long int dp[N][2];   void dfs(int v, int par) {     vector<int> sm;     for (auto p : g[v]) {       if (p.second == par) continue;       dfs(p.second, v);       dp[v][0] += dp[p.second][1];       dp[v][1] += dp[p.second][1];       sm.push_back(p.first + dp[p.second][0] - dp[p.second][1]);     }     sort(sm.begin(), sm.end(), greater<int>());     for (int i = 0; i < sm.size(); i++) {       if (i == k) break;       if (i == k - 1) {         dp[v][1] += max(0, sm[i]);       } else {         dp[v][0] += max(0, sm[i]);         dp[v][1] += max(0, sm[i]);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     cin >> q;     while (q--) {       cin >> n >> k;       for (int i = 0; i < n - 1; i++) {         int a, b, c;         cin >> a >> b >> c;         g[a].push_back(make_pair(c, b));         g[b].push_back(make_pair(c, a));       }       dfs(1, -1);       cout << dp[1][1] << endl;       for (int i = 1; i <= n; i++) {         g[i].clear();         dp[i][0] = 0;         dp[i][1] = 0;       }     }     return 0;   }
#include <bits/stdc++.h>   struct itv {     int s1;     int e1;     int s2;     int e2;     int gr;     void up() {       gr = (e2 - s2) + 1;       if (e1) {         gr += (e1 - s1) + 1;       };     };   };   std::vector<std::pair<int, int> > aus;   void merge(int s1, int s2, int sz) {     for (int i = 0; i < sz; i++) {       aus.push_back({s1 + i, s2 + i});     };   };   itv tt[1000];   int main(void) {     int n;     scanf( %d , &n);     int akt = 1;     for (int i = 1; i <= 20; i++) {       int sz = (1 << i);       if (sz > n) break;       for (int j = 1; j <= n; j += sz) {         int s1 = j;         int s2 = j + (sz / 2);         if (s1 + sz > n + 1) break;         merge(s1, s2, sz / 2);       };     };     akt = n;     std::vector<int> pw;     for (int i = 0; i <= 20; i++) {       if (n & (1 << i)) {         pw.push_back(i);         tt[i].s2 = (akt - (1 << i)) + 1;         tt[i].e2 = akt;         tt[i].s1 = 0;         tt[i].e1 = 0;         tt[i].up();         akt -= (1 << i);       };     };     if (pw.size() > 2) {       int ak = 1;       for (int i = 0; i < pw.size() - 2; i++) {         int ap = pw[i];         int np = pw[i + 1];         int ss = tt[ap].e2 - tt[ap].s2 + 1;         if (!i) {           merge(tt[ap].s2, ak, ss);           tt[ap].s1 = ak;           ak += (ss);           tt[ap].e1 = ak - 1;         };         tt[ap].up();         while (tt[ap].gr < tt[np].gr) {           merge(tt[ap].s1, ak, tt[ap].e1 - tt[ap].s1 + 1);           ak += (tt[ap].e1 - tt[ap].s1 + 1);           merge(tt[ap].s2, ak, ss);           ak += (ss);           tt[ap].e1 = ak - 1;           tt[ap].up();         };         merge(tt[ap].s2, tt[np].s2, ss);         merge(tt[ap].s1, tt[np].s2 + ss, tt[ap].e1 - tt[ap].s1 + 1);         tt[np].e2 = tt[ap].e2;         tt[np].s1 = tt[ap].s1;         tt[np].e1 = tt[ap].e1;         tt[np].up();       };     };     printf( %d n , aus.size());     for (auto i : aus) {       printf( %d %d n , i.first, i.second);     };     return 0;   };