malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   const long long N = 4e5 + 10;   long long fa[N][21] = {0}, sum[N][21], cnt = 0, last = 0, x, y, Q, op, w[N];   void add(long long u, long long val) {     cnt++;     w[cnt] = val;     if (w[u] >= val)       fa[cnt][0] = u;     else {       for (int i = 20; i >= 0; i--) {         if (w[fa[u][i]] < w[cnt]) u = fa[u][i];       }       fa[cnt][0] = fa[u][0];     }     if (fa[cnt][0] == 0)       sum[cnt][0] = 1e18;     else       sum[cnt][0] = w[fa[cnt][0]];     for (int i = 1; i <= 20; i++) {       fa[cnt][i] = fa[fa[cnt][i - 1]][i - 1];       if (fa[cnt][i] == 0)         sum[cnt][i] = 1e18;       else         sum[cnt][i] = sum[fa[cnt][i - 1]][i - 1] + sum[cnt][i - 1];     }   }   long long query(long long u, long long val) {     if (w[u] > val) return 0;     long long ans = 1;     val -= w[u];     for (int i = 20; i >= 0; i--) {       if (sum[u][i] <= val) {         val -= sum[u][i];         u = fa[u][i];         ans += (1 << i);       }     }     return ans;   }   int main() {     w[0] = 1e18;     w[1] = 0;     cnt = 1;     scanf( %lld , &Q);     for (int i = 0; i <= 20; i++) sum[1][i] = 1e18;     while (Q--) {       scanf( %lld %lld %lld , &op, &x, &y);       x ^= last;       y ^= last;       if (op == 1)         add(x, y);       else {         printf( %lld n , last = query(x, y));       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<int> a(n + 1);     for (int i = 1; i <= n; i++) cin >> a[i];     stack<int> lid, rid;     vector<int> mxl(n + 1), mxr(n + 1);     for (int i = 1; i <= n; i++) {       while (!lid.empty() && a[i] > a[lid.top()]) lid.pop();       mxl[i] = lid.empty() ? 1 : lid.top() + 1;       lid.push(i);     }     for (int i = n; i >= 1; i--) {       while (!rid.empty() && a[i] >= a[rid.top()]) rid.pop();       mxr[i] = rid.empty() ? n : rid.top() - 1;       rid.push(i);     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       ans += (long long)a[i] * (mxr[i] - i + 1) * (i - mxl[i] + 1);     }     vector<int> mil(n + 1), mir(n + 1);     while (!lid.empty()) lid.pop();     while (!rid.empty()) rid.pop();     for (int i = 1; i <= n; i++) {       while (!lid.empty() && a[i] < a[lid.top()]) lid.pop();       mil[i] = lid.empty() ? 1 : lid.top() + 1;       lid.push(i);     }     for (int i = n; i >= 1; i--) {       while (!rid.empty() && a[i] <= a[rid.top()]) rid.pop();       mir[i] = rid.empty() ? n : rid.top() - 1;       rid.push(i);     }     for (int i = 1; i <= n; i++) {       ans -= (long long)a[i] * (mir[i] - i + 1) * (i - mil[i] + 1);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   long long mod = 1000000007;   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   void solve() {     long long n;     cin >> n;     vector<long long> a(n);     priority_queue<pair<long long, long long> > pq;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] > 0) pq.push({a[i], i + 1});     }     vector<pair<long long, long long> > ans;     while (pq.size() >= 2) {       auto tp1 = pq.top();       pq.pop();       auto tp2 = pq.top();       pq.pop();       tp1.first--, tp2.first--;       ans.push_back({tp1.second, tp2.second});       if (tp1.first > 0) pq.push(tp1);       if (tp2.first > 0) pq.push(tp2);     }     cout << ans.size() << endl;     for (auto x : ans) cout << x.first <<     << x.second << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:66777216 )   using namespace std;   const int N = 100000 + 5;   int n, k;   char s[N];   int su[N];   int a[N];   bool check(int md) {     for (int i = 1; i <= (int)(n); ++i) {       int s = max(i - md, 1);       int f = min(i + md, n);       if (a[i] == 1 && su[f] - su[s - 1] >= k + 1) return true;     }     return false;   }   void solve() {     scanf( %d%d , &n, &k);     scanf( %s , s);     for (int i = 1; i <= (int)(n); ++i) {       a[i] = s[i - 1] -  0 ;       a[i] ^= 1;     }     for (int i = 1; i <= (int)(n); ++i) su[i] = su[i - 1] + a[i];     int st = 0, fi = n, re = n;     while (st <= fi) {       int md = (st + fi) / 2;       if (check(md)) {         re = md;         fi = md - 1;       } else         st = md + 1;     }     printf( %d n , re);   }   void testgen() {     FILE* f = fopen( input.txt ,  w );     fclose(f);   }   int main() {     cout << fixed;     cout.precision(10);     cerr << fixed;     cerr.precision(3);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int oo = 1e9, mxn = 100010;   int A[mxn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     string s;     cin >> s;     for (int i = (0); i < (n); ++i) cin >> A[i];     vector<bool> visited(n);     int curr = 0;     visited[0] = true;     while (true) {       curr += (s[curr] ==  >  ? A[curr] : -A[curr]);       if ((curr < 0 || curr >= n) || visited[curr]) break;       visited[curr] = true;     }     cout << ((curr < 0 || curr >= n) ?  FINITE  :  INFINITE ) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int NMAX = 100000;   vector<int> g[NMAX + 1];   int H[NMAX + 1], D[NMAX + 1], G[NMAX + 1], GS[NMAX + 1];   long long DS[NMAX + 1];   vector<int> data[NMAX + 1];   vector<long long> acc[NMAX + 1], psum[NMAX + 1];   int dfs1(int u, int p, int gp) {     G[u] = gp;     GS[gp]++;     for (int v : g[u])       if (v != p) H[u] = max(dfs1(v, u, gp) + 1, H[u]);     return H[u];   }   void dfs2(int u, int p, int pLen = 0) {     int m1 = -2, mi1 = -1, m2 = -2, mi2 = -1;     for (int v : g[u])       if (v != p) {         if (m2 < H[v]) m2 = H[v], mi2 = v;         if (m1 < m2) swap(m1, m2), swap(mi1, mi2);       }     D[u] = max(pLen, m1 + 1);     for (int v : g[u])       if (v != p) {         if (v == mi1)           dfs2(v, u, max(pLen + 1, m2 + 2));         else           dfs2(v, u, max(pLen + 1, m1 + 2));       }     while (data[G[u]].size() <= D[u]) data[G[u]].push_back(0);     data[G[u]][D[u]]++;     DS[G[u]] += D[u];   }   map<pair<int, int>, double> cache;   void query(int u, int v) {     u = G[u], v = G[v];     if (u == v) {       printf( -1 n );       return;     }     if (data[u].size() > data[v].size()) swap(u, v);     pair<int, int> P(u, v);     if (cache.count(P)) {       printf( %.10f n , cache[P]);       return;     }     long long S = 1LL * GS[u] * GS[v] + 1LL * GS[u] * DS[v] + 1LL * DS[u] * GS[v];     int bound = data[v].size() - 1;     for (int i = 0; i < data[u].size(); i++) {       int R = bound - i - 1;       if (0 <= R) {         R = min(R, (int)data[v].size() - 1);         long long terms = data[u][i] * acc[v][R];         S -= i * terms + psum[v][R] * data[u][i] + terms;         S += bound * terms;       }     }     printf( %.10f n , (double)S / GS[u] / GS[v]);   }   void process() {     int N, M, Q;     scanf( %d%d%d , &N, &M, &Q);     for (int i = 0; i < M; i++) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     int gps = 0;     for (int i = 1; i <= N; i++)       if (!G[i]) {         gps++;         dfs1(i, -1, gps);       }     for (int i = 1; i <= N; i++)       if (data[G[i]].empty()) {         dfs2(i, -1);       }     for (int i = 1; i <= gps; i++) {       acc[i] = psum[i] = vector<long long>(data[i].size());       acc[i][0] = data[i][0];       for (int j = 1; j < acc[i].size(); j++)         acc[i][j] = acc[i][j - 1] + data[i][j];       for (int j = 1; j < psum[i].size(); j++)         psum[i][j] = psum[i][j - 1] + 1LL * data[i][j] * j;     }     for (int i = 0; i < Q; i++) {       int u, v;       scanf( %d%d , &u, &v);       query(u, v);     }   }   int main() { process(); }
#include <bits/stdc++.h>   using namespace std;   void Get(int &T) {     char C;     bool F = 0;     for (; C = getchar(), C <  0  || C >  9 ;)       if (C ==  - ) F = 1;     for (T = C -  0 ; C = getchar(), C >=  0  && C <=  9 ; T = T * 10 + C -  0 )       ;     F && (T = -T);   }   void Get(long long &T) {     char C;     bool F = 0;     for (; C = getchar(), C <  0  || C >  9 ;)       if (C ==  - ) F = 1;     for (T = C -  0 ; C = getchar(), C >=  0  && C <=  9 ; T = T * 10 + C -  0 )       ;     F && (T = -T);   }   int U[50005];   int V[50005];   int W[50005];   int W2[50005];   int Order[50005];   int N, M;   bool Cmp(int X, int Y) { return W2[X] < W2[Y]; }   long long C1, C2;   void Init() {     Get(N);     Get(M);     Get(C1);     Get(C2);     for (int i = 1; i <= M; i++) {       Get(U[i]);       Get(V[i]);       Get(W[i]);       Get(W2[i]);       Order[i] = i;     }     std::sort(Order + 1, Order + M + 1, Cmp);   }   bool Delete[100005];   struct E {     int V, W, Next;   } Edge[100005];   int EdgeTop = 1;   int Head[205];   void Add(int U, int V, int W) {     Edge[++EdgeTop] = (E){V, W, Head[U]};     Head[U] = EdgeTop;   }   int Visited[205];   int Time;   int StackAns[205];   int StackX[205];   int Stacki[205];   multiset<int> Hash;   int DFS(int X, int Y) {     int Top = 1, i;     StackX[Top] = X;   Call:     X = StackX[Top];     if (X == Y) {       int Max = StackAns[1];       for (int i = 2; i < Top; i++) {         if (Edge[StackAns[i]].W > Edge[Max].W) Max = StackAns[i];       }       return Max;     }     Visited[X] = Time;     for (; Delete[Head[X]]; Head[X] = Edge[Head[X]].Next)       ;     for (i = Head[X]; i; i = Edge[i].Next) {       if (Visited[Edge[i].V] != Time) {         StackAns[Top] = i;         StackX[Top + 1] = Edge[i].V;         Stacki[Top] = i;         Top++;         goto Call;       Return:         Top--;         X = StackX[Top];         i = Stacki[Top];       }       for (; Delete[Edge[i].Next]; Edge[i].Next = Edge[Edge[i].Next].Next)         ;     }     if (Top >= 2) goto Return;     return -1;   }   long long Ans;   void Work() {     Ans = 0x2f2f2f2f2f2f2f2fll;     for (int k = 1, i; k <= M; k++) {       i = Order[k];       if (U[i] == V[i]) continue;       Time++;       int Max = DFS(U[i], V[i]);       if (Max == -1) {         Hash.insert(W[i]);         Add(U[i], V[i], W[i]);         Add(V[i], U[i], W[i]);       } else if (Edge[Max].W > W[i]) {         Delete[Max] = Delete[Max ^ 1] = 1;         Hash.erase(Hash.find(Edge[Max].W));         Hash.insert(W[i]);         Add(U[i], V[i], W[i]);         Add(V[i], U[i], W[i]);       }       if (Hash.size() == N - 1) {         long long Ans0 = *Hash.rbegin() * C1 + W2[i] * C2;         if (Ans > Ans0) Ans = Ans0;       }     }   }   void Output() {     if (Ans >= 0x2f2f2f2f2f2f2f2fll)       puts( -1 );     else       printf( %I64d n , Ans);   }   int main() {     Init();     Work();     Output();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const int inf = LONG_MAX;   const long long inf64 = LLONG_MAX;   const long double pi = 3.1415926535897932384626433832795;   bool cmp(const pair<int, int> a, const pair<int, int> b) {     return a.first > b.first;   }   int a[100100];   int c[100100];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     vector<pair<int, int> > v;     for (int i = 0; i < n; i++)       if (a[i] != 0)         v.push_back(make_pair(a[i], i));       else {         sort(v.begin(), v.end(), cmp);         if (v.size() > 0) c[v[0].second] = 1;         if (v.size() > 1) c[v[1].second] = 2;         if (v.size() > 2) c[v[2].second] = 3;         v.clear();       }     int count = 0;     for (int i = 0; i < n; i++)       if (a[i] != 0) {         switch (c[i]) {           case 0:             printf( pushBack n );             break;           case 1:             printf( pushStack n );             break;           case 2:             printf( pushQueue n );             break;           case 3:             printf( pushFront n );             break;         }         count++;       } else {         if (count == 0) printf( 0 n );         if (count == 1) printf( 1 popStack n );         if (count == 2) printf( 2 popStack popQueue n );         if (count >= 3) printf( 3 popStack popQueue popFront n );         count = 0;       }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 110;   vector<int> G[N];   int vis[N];   void DFS(int cur) {     vis[cur]++;     int sz = G[cur].size();     int v;     for (int i = 0; i < sz; i++) {       v = G[cur][i];       if (!vis[v]) DFS(v);     }   }   int main() {     int n, m, u, v;     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     DFS(1);     for (int i = 1; i <= n; i++)       if (!vis[i]) {         cout <<  NO ;         return 0;       }     if (n == m)       cout <<  FHTAGN! ;     else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, x, b, k, v[100005], v2[100005], m;   bool OK(int x) {     int lst = -1e9, nr = 0;     for (int i = 1; i <= n; i++) {       if (v[i] - lst >= x) {         nr++;         lst = v[i];       }     }     return nr <= k;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       cin >> v[i];     }     sort(v + 1, v + n + 1);     if (k >= n) {       cout << n <<   n ;       return 0;     }     for (int i = 2; i <= n; i++) {       v2[i - 1] = v[i] - v[i - 1];     }     sort(v2 + 1, v2 + n);     k--;     int ans = v[n] - v[1] + 1;     for (int i = n - 1; i >= 1 && k > 0; i--, k--) {       ans -= (v2[i] - 1);     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   const int N = 1005;   char grid[N][N];   int visited[N][N];   bool row[N];   bool col[N];   int drow[] = {0, 0, -1, 1};   int dcol[] = {1, -1, 0, 0};   bool fullrow = false;   bool fullcol = false;   bool inside(int x, int y) { return (1 <= x && x <= n && 1 <= y && y <= m); }   bool edge(int x, int y) { return (x == 1 || y == 1 || x == n || y == m); }   void ngang() {     for (int i = 1; i <= n; ++i) {       int cnt = 0;       for (int j = 1; j <= m; ++j) {         if (grid[i][j] ==  # ) {           if (grid[i][j - 1] !=  # ) ++cnt;         }         if (cnt > 1) {           cout << -1;           cerr <<   nngang ;           exit(0);         }       }       if (cnt == 0) {         fullrow = true;       }     }   }   void doc() {     for (int j = 1; j <= m; ++j) {       int cnt = 0;       for (int i = 1; i <= n; ++i) {         if (grid[i][j] ==  # ) {           if (grid[i - 1][j] !=  # ) ++cnt;         }         if (cnt > 1) {           cout << -1;           cerr <<   ndoc ;           exit(0);         }       }       if (cnt == 0) {         fullcol = true;       }     }   }   void input() {     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         cin >> grid[i][j];       }     }   }   void dfs(int i, int j) {     visited[i][j] = true;     for (int d = 0; d <= 3; ++d) {       int x = i + drow[d];       int y = j + dcol[d];       if (!inside(x, y)) continue;       if (grid[x][y] ==  # ) {         if (!visited[x][y]) dfs(x, y);       }     }   }   void solve() {     ngang();     doc();     if (fullrow != fullcol) {       cout << -1;       return;     }     int ans = 0;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (!visited[i][j] && grid[i][j] ==  # ) {           dfs(i, j);           ++ans;         }       }     }     cout << ans;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     input();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Block {     int r1, c1, r2, c2;     int state[8];     void get(int& r, int& c, int st) {       switch (st) {         case 7:         case 0:           r = r1;           c = c2;           break;         case 1:         case 2:           r = r2;           c = c2;           break;         case 3:         case 4:           r = r2;           c = c1;           break;         case 5:         case 6:           r = r1;           c = c1;           break;       }     }   };   string field[100];   int vis[100][100];   int R, C;   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   int q[100][100];   void go(int r, int c, int& r1, int& c1, int& r2, int& c2) {     r1 = min(r1, r);     r2 = max(r2, r);     c1 = min(c1, c);     c2 = max(c2, c);     vis[r][c] = true;     for (int i = (0); i < (4); ++i) {       int rr = r + dx[i];       int cc = c + dy[i];       if (rr < 0 || rr >= R || cc < 0 || cc >= C || field[r][c] != field[rr][cc])         continue;       if (!vis[rr][cc]) go(rr, cc, r1, c1, r2, c2);     }   }   int main() {     int moves;     cin >> R >> moves;     for (int i = (0); i < (R); ++i) cin >> field[i];     C = field[0].size();     vector<Block> vb;     memset(vis, 0, sizeof vis);     memset(q, -1, sizeof q);     for (int r = (0); r < (R); ++r)       for (int c = (0); c < (C); ++c)         if (field[r][c] !=  0  && !vis[r][c]) {           Block b;           b.r1 = b.r2 = r;           b.c1 = b.c2 = c;           go(r, c, b.r1, b.c1, b.r2, b.c2);           int id = vb.size();           for (int r = (b.r1); r <= (b.r2); ++r)             for (int c = (b.c1); c <= (b.c2); ++c) q[r][c] = id;           vb.push_back(b);         }     for (int i = (0); i < (vb.size()); ++i) {       int r, c;       for (int j = (0); j < (8); ++j) {         vb[i].get(r, c, j);         int rr = r + dy[j / 2];         int cc = c + dx[j / 2];         if (rr < 0 || rr >= R || cc < 0 || cc >= C || q[rr][cc] == -1 ||             q[rr][cc] == q[r][c])           vb[i].state[j] = -1;         else           vb[i].state[j] = q[rr][cc];       }     }     int curs = 0;     int id = 0;     for (int i = (0); i < (moves); ++i) {       if (vb[id].state[curs] == -1)         curs = (curs + 1) & 7;       else         id = vb[id].state[curs];     }     cout << field[vb[id].r1][vb[id].c1] << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   int n, m, num[5000007], inv[10000007], g[10000007], f[10000007], res;   int read() {     int num = 0;     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     while (c >=  0  && c <=  9 ) num = num * 10 + c -  0 , c = getchar();     return num;   }   int main() {     n = read();     for (int i = 1; i <= n; i++) num[i] = read(), m += num[i];     inv[0] = inv[1] = 1;     for (int i = 2; i <= max(n, m); i++)       inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;     g[1] = mod + 1 - n;     for (int i = 1; i < m; i++)       g[i + 1] = 1LL * (1LL * i * g[i] % mod - m + mod) % mod * (n - 1) % mod *                  inv[m - i] % mod;     for (int i = m; i >= 1; i--) f[i - 1] = (f[i] - g[i] + mod) % mod;     for (int i = 1; i <= n; i++) res = (res + f[num[i]]) % mod;     res = 1LL * (res - 1LL * (n - 1) * f[0] % mod + mod) % mod * inv[n] % mod;     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void dfs(int i, int j, vector<vector<char>>& grid,            vector<vector<char>>& visited) {     int n = grid.size();     if (i < 0 || i >= n || j < 0 || j >= n || visited[i][j] || grid[i][j] ==  0 )       return;     visited[i][j] = 1;     dfs(i - 1, j, grid, visited);     dfs(i, j - 1, grid, visited);   }   void solve() {     int n;     cin >> n;     int startx = -1, starty = -1;     vector<vector<char>> grid(n, vector<char>(n));     vector<vector<char>> visited(n, vector<char>(n));     char x;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         cin >> x;         grid[i][j] = x;         ;         visited[i][j] = 0;       }     }     for (int i = 0; i < n; i++) {       if (grid[n - 1][i] ==  1  && !visited[n - 1][i])         dfs(n - 1, i, grid, visited);     }     for (int i = 0; i < n; i++) {       if (grid[i][n - 1] ==  1  && !visited[i][n - 1])         dfs(i, n - 1, grid, visited);     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (grid[i][j] ==  1  && !visited[i][j]) {           cout <<  NO n ;           return;         }       }     }     cout <<  YES n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int a[N], b[N];   int d1[N][25], d2[N][25];   void RMQ1_init(int *A, int n) {     for (int i = 0; i < n; i++) d1[i][0] = A[i];     for (int j = 1; (1 << j) <= n; j++) {       for (int i = 0; i + (1 << j) - 1 < n; i++) {         d1[i][j] = max(d1[i][j - 1], d1[i + (1 << (j - 1))][j - 1]);       }     }   }   int RMQ1(int L, int R) {     int k = 0;     while ((1 << (k + 1)) <= R - L + 1) k++;     return max(d1[L][k], d1[R - (1 << k) + 1][k]);   }   void RMQ2_init(int *A, int n) {     for (int i = 0; i < n; i++) d2[i][0] = A[i];     for (int j = 1; (1 << j) <= n; j++) {       for (int i = 0; i + (1 << j) - 1 < n; i++) {         d2[i][j] = min(d2[i][j - 1], d2[i + (1 << (j - 1))][j - 1]);       }     }   }   int RMQ2(int L, int R) {     int k = 0;     while ((1 << (k + 1)) <= R - L + 1) k++;     return min(d2[L][k], d2[R - (1 << k) + 1][k]);   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     RMQ1_init(a, n);     RMQ2_init(b, n);     long long sum = 0;     for (int i = 0; i < n; i++) {       int l = i, r = n;       while (l < r) {         int mid = (l + r) / 2;         if (RMQ1(i, mid) < RMQ2(i, mid))           l = mid + 1;         else           r = mid;       }       int p1 = l;       l = i, r = n;       while (l < r) {         int mid = (l + r) / 2;         if (RMQ1(i, mid) <= RMQ2(i, mid))           l = mid + 1;         else           r = mid;       }       int p2 = l;       sum += p2 - p1;     }     cout << sum << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int k = 0, l = 0, i = 0, count = 0, x = -1, y = 0;     vector<int> alpha(26, 0);     string s;     cin >> k >> s;     char temp[105][105];     for (i = 0; s[i] !=   0 ; i++) {       alpha[s[i] -  a ] += 1;       if (alpha[s[i] -  a ] == 1) {         ++x;         y = 0;         temp[x][y] = s[i];       } else {         y++;         temp[x][y] = s[i];       }     }     for (i = 0; i < 26; i++) {       if (alpha[i] != 0) count++;     }     if (count >= k) {       cout <<  YES  << endl;       for (i = 0; i < k; i++) {         if (i != k - 1) {           cout << temp[i] << endl;         } else {           int j = 0;           for (j = i; j <= x; j++) cout << temp[j];         }       }     } else {       cout <<  NO ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ii = pair<int, int>;   using vi = vector<int>;   using vll = vector<long long>;   using vii = vector<pair<int, int>>;   int main() {     int n, m;     scanf( %d%d , &n, &m);     vector<tuple<int, int, int>> ve;     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d%d , &x, &y);       ve.emplace_back(x, -y, i);     }     sort((ve).begin(), (ve).end());     vii edge(m);     int last = 1;     set<pair<int, int>> cur;     cur.insert({last, last});     for (auto t : ve) {       int a, b, c;       tie(a, b, c) = t;       if (b) {         assert(cur.rbegin()->first == last && cur.rbegin()->second == last);         ++last;         edge[c] = {last - 1, last};         auto it = cur.end();         cur.erase(prev(it));         cur.insert({last, last - 1});         cur.insert({last, last});       } else {         int u = cur.begin()->first, v = cur.begin()->second;         if (u == last) {           puts( -1 );           return 0;         } else {           cur.erase(cur.begin());           ++u;           edge[c] = {v, u};           cur.insert({u, v});         }       }     }     for (auto e : edge) printf( %d %d n , e.first, e.second);   }