malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-10;   const long long N = 2e5 + 10;   ;   long long n, p[N], t[N], a[N], b[N], Ma[N], Mi[N], T;   long long check(double m) {     double M = 0.0;     for (long long i = 1, j = 1; i <= n; i++) {       while (j <= n && p[b[j]] < p[b[i]])         M = max(M, (1.0 - m * Mi[b[j]] / T) * p[b[j]]), j++;       if ((1.0 - m * Ma[b[i]] / T) * p[b[i]] < M) return 0;     }     return 1;   }   signed main() {     scanf( %lld n , &n);     for (long long i = 1; i <= n; i++) scanf( %lld , &p[i]), a[i] = i;     for (long long i = 1; i <= n; i++) scanf( %lld , &t[i]), b[i] = i;     sort(a + 1, a + n + 1,          [&](long long i, long long j) { return p[i] * t[j] > p[j] * t[i]; });     sort(b + 1, b + n + 1, [&](long long i, long long j) { return p[i] < p[j]; });     for (long long i = 1, j, s; i <= n; i = j) {       for (j = i + 1, s = 0; j <= n; j++)         if (p[a[i]] * t[a[j]] != p[a[j]] * t[a[i]]) break;       for (long long k = i; k < j; k++) s += t[a[k]];       for (long long k = i; k < j; k++) Mi[a[k]] = T + t[a[k]], Ma[a[k]] = T + s;       T += s;     }     double l = 0, r = 1;     while (r - l > eps) {       double mid = (l + r) / 2.0;       if (!check(mid))         r = mid;       else         l = mid;     }     printf( %.10f n , l);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string A;   bool isPalindrome(int left, int right) {     while (left < right) {       if (A[left] != A[right]) return false;       left++, right--;     }     return true;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     cin >> A;     int N = A.size();     int bonus = 0;     int ret = 0;     if (N % 2 == 1) bonus = 1;     N /= 2;     while (N > 0) {       if (isPalindrome(0, N - 1)) {         if (N % 2 == 1) {           N--;           bonus = 1;         } else           N /= 2;       } else {         ret = 1 + bonus;         break;       }     }     if (ret == 0)       cout <<  Impossible ;     else       cout << ret;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long o = 1e6 + 5;   long n, i, j, h, k, sl, g, kq = 0;   long a[o], b[o], c[o], A, B;   string s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     i = 0, j = 0, h = 0, k = 0;     for (long p = 1; p <= n; p++) {       cin >> s >> g;       if (s ==  00 ) {         i++;         a[i] = g;       } else if (s ==  01 ) {         j++;         b[j] = g;       } else if (s ==  10 ) {         h++;         c[h] = g;       } else if (s ==  11 ) {         k++;         kq += g;       }     }     if (k == 0 && (j == 0 || h == 0)) {       cout << 0;       return 0;     }     sort(a + 1, a + 1 + i);     sort(b + 1, b + 1 + j);     sort(c + 1, c + 1 + h);     a[0] = b[0] = c[0] = 0;     A = B = 0;     sl = min(h, j);     while (A < sl) {       A++;       kq += c[h];       h--;     }     while (B < sl) {       B++;       kq += b[j];       j--;     }     sl = 2 * (k + sl) - (k + 2 * sl);     for (long p = 1; p <= sl; p++) {       long ma = max(a[i], max(b[j], c[h]));       if (ma == 0) break;       kq += ma;       if (ma == a[i])         i--;       else if (ma == b[j])         j--;       else if (ma == c[h])         h--;     }     cout << kq;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long ans;   struct arr {     int n;     int v;   } a[200001], b[200001];   int n, m, i, j;   bool cmp(arr a, arr b) { return a.n < b.n; }   int main() {     cin >> n;     for (i = 1; i <= n; i++) cin >> a[i].n >> a[i].v;     cin >> m;     for (i = 1; i <= m; i++) cin >> b[i].n >> b[i].v;     sort(a + 1, a + n + 1, cmp);     sort(b + 1, b + m + 1, cmp);     i = j = 1;     while (i <= n && j <= m) {       if (a[i].n == b[j].n)         ans += max(a[i].v, b[j].v), i++, j++;       else if (a[i].n < b[j].n)         ans += a[i].v, i++;       else         ans += b[j].v, j++;     }     if (i > n) {       for (i = j; i <= m; i++) ans += b[i].v;     } else {       for (j = i; j <= n; j++) ans += a[j].v;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void fft(vector<complex<double> > &a, bool invert) {     int n = a.size(), lg = 0;     while (1 << lg < n) ++lg;     assert(1 << lg == n);     for (int i = 0; i < n; ++i) {       int rev = 0;       for (int j = 0; j < lg; ++j)         if ((i & 1 << j) != 0) rev |= 1 << (lg - j - 1);       if (i < rev) swap(a[i], a[rev]);     }     vector<complex<double> > roots(n);     for (int i = 0; i < n; ++i) {       double a = 2 * M_PI * i / n * (invert ? -1 : 1);       roots[i] = complex<double>(cos(a), sin(a));     }     for (int len = 2; len <= n; len *= 2)       for (int i = 0; i < n; i += len)         for (int j = 0; j < len / 2; ++j) {           complex<double> u = a[i + j],                           v = a[i + j + len / 2] * roots[n / len * j];           a[i + j] = u + v;           a[i + j + len / 2] = u - v;         }     if (invert)       for (int i = 0; i < n; ++i) a[i] /= n;   }   vector<int> multiply(const vector<int> &a, const vector<int> &b) {     int m = a.size(), n = b.size(), p = 2;     while (p < m + n - 1) p <<= 1;     vector<complex<double> > pa(p), pb(p);     for (int i = 0; i < m; ++i) pa[i] = complex<double>(a[i], 0);     for (int i = 0; i < n; ++i) pb[i] = complex<double>(b[i], 0);     fft(pa, false);     fft(pb, false);     for (int i = 0; i < p; ++i) pa[i] *= pb[i];     fft(pa, true);     vector<int> res(p);     for (int i = 0; i < p; ++i) res[i] = (int)round(real(pa[i]));     return res;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<bool> hasBag(m + 1);     vector<int> p(m + 1);     for (int i = 0; i < n; ++i) {       int w;       cin >> w;       p[w] = 1;       hasBag[w] = true;     }     p = multiply(p, p);     for (int i = 1; i <= m; ++i)       if (p[i] > 0) {         if (!hasBag[i]) return cout <<  NO , 0;         hasBag[i] = false;       }     printf( YES n%d n , count(hasBag.begin(), hasBag.end(), 1));     for (int i = 1; i <= m; ++i)       if (hasBag[i] == 1) printf( %d  , i);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e15;   long long q[300005];   long long d[300005];   long long dd[300005];   struct st {     long long x, w, id;   };   vector<st> v[300005];   st t;   vector<long long> ans;   long long sum;   long long n, m;   long long x, y, z;   inline void dij(long long st) {     for (long long i = 1; i <= n; ++i) d[i] = inf;     set<pair<long long, long long> > s;     d[st] = 0;     s.insert(make_pair(0, st));     while (!s.empty()) {       long long x = s.begin()->second;       s.erase(s.begin());       for (long long i = 0; i < v[x].size(); ++i) {         long long to = v[x][i].x;         long long len = d[x] + v[x][i].w;         if (d[to] > len) {           s.erase(make_pair(d[to], to));           d[to] = len;           s.insert(make_pair(d[to], to));         }       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (long long i = 1; i <= m; ++i) {       cin >> x >> y >> z;       t.x = y;       t.w = z;       t.id = i;       v[x].push_back(t);       t.x = x;       v[y].push_back(t);     }     long long stt;     cin >> stt;     dij(stt);     for (long long i = 1; i <= n; ++i) {       long long mx = 1e9;       for (long long j = 0; j < v[i].size(); ++j) {         long long to = v[i][j].x;         if (d[to] != d[i] - v[i][j].w) continue;         mx = min(mx, v[i][j].w);       }       for (long long j = 0; j < v[i].size(); ++j) {         long long to = v[i][j].x;         if (d[to] != d[i] - v[i][j].w) continue;         if (v[i][j].w == mx) {           ans.push_back(v[i][j].id);           sum += v[i][j].w;           break;         }       }     }     cout << sum <<   n ;     for (long long i = 0; i < ans.size(); ++i) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(), cout.tie();     int n;     cin >> n;     int temp = 1;     int a[] = {4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777};     for (int i = 0; i < 14; i++) {       if (n % a[i] == 0) {         cout <<  YES  << endl;         temp = 0;         break;       }     }     if (temp) {       cout <<  NO  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000;   const int INF = 0x3f3f3f3f;   int dis[MAXN + 10], pos[MAXN + 10], f[MAXN + 10], Max, m, n;   bool check(const int& x) {     if (x >= dis[1] - 1) return true;     if (2 * x < dis[1] - 1) return false;     f[1] = 0;     for (int i = 2; i <= n; i++) {       f[i] = -INF;       if (f[i - 1] >= pos[i] - 1) f[i] = pos[i] + x;       if (f[i - 1] >= pos[i] - 1 - x) f[i] = max(f[i], pos[i]);       if (i > 2 && f[i - 2] >= pos[i] - 1 - x) f[i] = max(pos[i - 1] + x, f[i]);     }     if (f[n] + x >= m - 1) return true;     f[1] = x, f[2] = max(x, pos[2]);     for (int i = 3; i <= n; i++) {       f[i] = -INF;       if (f[i - 1] >= pos[i] - 1) f[i] = pos[i] + x;       if (f[i - 1] >= pos[i] - 1 - x) f[i] = max(f[i], pos[i]);       if (f[i - 2] >= pos[i] - 1 - x) f[i] = max(pos[i - 1] + x, f[i]);     }     if (f[n] >= min(m - 1, pos[2] + m - 1 - x)) return true;     return false;   }   int tmp[MAXN + 10];   void _rotate(int* arr, int mid, int len) {     int cnt = 0;     for (int i = mid; i <= len; i++) tmp[++cnt] = arr[i];     for (int i = 1; i < mid; i++) tmp[++cnt] = arr[i];     for (int i = 1; i <= len; i++) arr[i] = tmp[i];   }   int main() {     scanf( %d%d , &m, &n);     if (n == 1) {       printf( %d n , m - 1);       return 0;     }     for (int i = 1; i <= n; i++) scanf( %d , &pos[i]);     sort(pos + 1, pos + 1 + n);     pos[0] = pos[n];     int p;     for (int i = 1; i <= n; i++) {       dis[i] = (pos[i] - pos[i - 1] + m) % m;       if (dis[i] > Max) p = i;       Max = max(Max, dis[i]);     }     _rotate(pos, p, n);     _rotate(dis, p, n);     int eff = pos[1];     for (int i = 1; i <= n; i++) pos[i] = (pos[i] - eff + m) % m;     int L = 1, R = Max - 1, ans = 0;     while (L <= R) {       int mid = (L + R) >> 1;       if (check(mid)) {         ans = mid;         R = mid - 1;       } else         L = mid + 1;     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     vector<pair<long long, long long> > v(n);     for (int i = 0; i < n; i++) {       int a;       cin >> a;       v[i] = {a, 0};     }     if (n == 1) {       cout <<  T n ;       return;     }     while (1) {       sort(v.begin(), v.end(), greater<pair<long long, long long> >());       int t_idx = 0, hl_idx = 0;       while (t_idx < n && v[t_idx].second == 1) t_idx++;       if (t_idx == n || v[t_idx].first == 0) {         cout <<  HL n ;         return;       }       v[t_idx].first--;       while (hl_idx < n && hl_idx == t_idx) hl_idx++;       if (hl_idx == n || v[hl_idx].first == 0) {         cout <<  T n ;         return;       }       v[hl_idx].first--;       for (int i = 0; i < n; i++) {         if (i == hl_idx)           v[i].second = 1;         else           v[i].second = 0;       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string a, b;       a +=  M , b +=  M ;       string tmp1, tmp2;       cin >> tmp1 >> tmp2;       a += tmp1, b += tmp2;       for (int i = (1); i < n + 1; i++) a[i] -=  0 , b[i] -=  0 ;       vector<int> ans;       int pos = 1;       for (int i = (1); i < n + 1; i++) {         if (b[n - i + 1] == (a[pos] + i - 1) % 2) ans.push_back(1);         ans.push_back(n - i + 1);         if (pos <= n / 2)           pos = n - pos + 1;         else           pos = n - pos + 2;       }       cout << ans.size() <<    ;       for (auto p : ans) cout << p <<    ;       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a, l, r, c(0), d(0);     scanf( %d%d , &n, &m);     while (n--) {       scanf( %d , &a);       if (a == 1) {         c += 1;       } else {         d += 1;       }     }     int up = min(c, d) * 2;     while (m--) {       scanf( %d%d , &l, &r);       printf((r - l) % 2 == 1 && r - l < up ?  1 n  :  0 n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int maxx(int a[2][102], int n, int t) {     int m = 0;     for (int i = 0; i <= n; i++)       if (a[t][i] > m) m = a[t][i];     return m;   }   int main() {     char s[102];     ifstream in( input.txt );     long long int x, y;     cin >> x >> y;     if (x == 999999999) {       cout <<  Yes ;       return 0;     }     cin >> s;     if (x == 0 && y == 1 && strcmp(s,  D ) == 0) {       cout <<  No ;       return 0;     }     int a[2][102];     int i;     a[0][0] = a[1][0] = 0;     for (i = 0; i < strlen(s); i++) {       a[0][i + 1] = a[0][i];       a[1][i + 1] = a[1][i];       if (s[i] ==  U )         a[1][i + 1] = a[1][i] + 1;       else if (s[i] ==  D )         a[1][i + 1] = a[1][i] - 1;       else if (s[i] ==  L )         a[0][i + 1] = a[0][i] - 1;       else if (s[i] ==  R )         a[0][i + 1] = a[0][i] + 1;     }     long long int xs = a[0][strlen(s)], ys = a[1][strlen(s)];     long long int n;     if (xs == 0 && ys != 0)       n = abs(y / ys) - (maxx(a, strlen(s), 1) - ys);     else if (ys == 0 && xs != 0)       n = abs(x / xs) - (maxx(a, strlen(s), 0) - xs);     else if (xs != 0 && ys != 0)       n = abs(min(x / xs, y / ys));     for (long long int j = n; j <= n + 20; j++)       for (i = 0; i <= strlen(s); i++) {         int xem1 = (xs * j - x) - (xs - a[0][i]);         int xem2 = (ys * j - y) - (ys - a[1][i]);         if (xem1 == 0 && xem2 == 0) {           cout <<  Yes ;           return 0;         }       }     cout <<  No ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false), cin.tie(0);     ;     long long n, k, i;     cin >> n >> k;     long long a[n], pr[n + 1];     map<long long, long long> f;     for (i = 0; i < n; i++) {       cin >> a[i];       f[a[i]]++;     }     int fl = 0;     for (auto e : f) {       if (e.second >= k) {         cout <<  0 n ;         fl = 1;         break;       }     }     if (!fl) {       sort(a, a + n);       pr[0] = a[0];       for (i = 1; i < n; i++) pr[i] = pr[i - 1] + a[i];       long long ans = 1e18 + 5;       long long left, right, ex, costl, costr;       for (i = 0; i < n; i++) {         left = lower_bound(a, a + n, a[i]) - a;         right = n - (upper_bound(a, a + n, a[i]) - a);         ex = n - left - right;         costl = left * (a[i] - 1);         if (left) costl -= pr[left - 1];         costr = pr[n - 1] - right * (a[i] + 1);         if ((n - right - 1) >= 0) costr -= pr[n - right - 1];         if (left + ex >= k) ans = min(ans, costl + k - ex);         if (right + ex >= k) ans = min(ans, costr + k - ex);         ans = min(ans, costl + costr + k - ex);       }       cout << ans <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 105, M = N * N;   int n;   int a[N];   int f[N][M];   vector<pair<int, int> > b;   int main() {     scanf( %d , &n);     int sum = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , a + i);       sum += a[i];     }     sort(a + 1, a + n + 1);     b.push_back(pair<int, int>(a[1], 1));     for (int i = 2; i <= n; i++) {       if (a[i] == b.back().first) {         b.back().second++;       } else {         b.push_back(pair<int, int>(a[i], 1));       }     }     int m = b.size();     if (m == 1) {       printf( %d n , n);     } else {       f[0][0] = 1;       int sumc = 0;       for (int i = 0; i < m; i++) {         int x = b[i].first, y = b[i].second;         sumc += y;         for (int c = sumc; c; c--) {           for (int j = sum; j >= x; j--) {             for (int k = 1; k * x <= j && k <= y && k <= c && f[c][j] < 2; k++) {               f[c][j] = min(2, f[c][j] + f[c - k][j - x * k]);             }           }         }       }       if (m == 2) {         int ans = 0;         for (int i = 0; i < m; i++) {           int x = b[i].first, y = b[i].second;           for (int j = 1; j <= y; j++) {             if (f[j][j * x] == 1) {               if (j < y) {                 ans = max(ans, j);               } else {                 ans = n;               }             }           }         }         printf( %d n , ans);       } else {         int ans = 0;         for (int i = 0; i < m; i++) {           int x = b[i].first, y = b[i].second;           for (int j = 1; j <= y; j++) {             if (f[j][j * x] == 1) {               ans = max(ans, j);             }           }         }         printf( %d n , ans);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 5048;   vector<int> adj[MAX];   bool used[MAX];   int dist[MAX][MAX];   void solve(const int test_num) {     (void)test_num;     int v, e;     cin >> v >> e;     for (int ee = 0, a, b; ee < e; ++ee) {       cin >> a >> b;       adj[a].push_back(b);     }     memset(dist, 0, sizeof(dist));     queue<int> que;     for (int st = 1; st <= v; ++st) {       que.push(st);       while (!que.empty()) {         const int at = que.front();         que.pop();         for (const int to : adj[at]) {           if (dist[st][to] == 0) {             dist[st][to] = dist[st][at] + 1;             que.push(to);           }         }       }     }     memset(used, false, sizeof(used));     int res = v;     for (int at = 1; at <= v; ++at) {       if (used[at]) continue;       used[at] = true;       int min_cycle = dist[at][at];       bool is_sink = true;       for (int to = 1; to <= v; ++to) {         if (dist[at][to] && dist[to][at]) {           used[to] = true;           min_cycle = min(min_cycle, dist[to][to]);         }         if (dist[at][to] && !dist[to][at]) {           is_sink = false;         }       }       if (is_sink && min_cycle) {         res += 998 * min_cycle + 1;       }     }     cout << res << endl;   }   void init() {}   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     srand(1223);     init();     int tests = 1;     for (int test = 1; test <= tests; ++test) {       solve(test);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int ans = 1e9;   int f[1000010];   int main() {     int n, i, j;     scanf( %d , &n);     for (i = 2; i <= n; i++) {       if (!f[i]) {         for (j = 2 * i; j <= n; j += i) {           f[j] = i;         }       }       f[i] = i - f[i] + 1;     }     for (i = f[n]; i <= n; i++) {       if (f[i] < ans) ans = f[i];     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n, m;     cin >> n >> m;     long long int st = n / 2;     if (n & 1) st++;     for (long long int i = (st); i < n + 1; i++) {       if (i % m == 0) {         cout << i;         return;       }     }     cout << -1;   }   signed main() {     ios::sync_with_stdio(0), cin.tie(0);     long long int tt = 1;     for (long long int i = (0); i < tt; i++) solve(), cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long func(long long arr[], long long n) {     long long ans = 0;     for (long long i = 2; i < n; i++) {       if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {         ans++;       } else if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) {         ans++;       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     long long arr[n + 1];     for (long long i = 1; i <= n; i++) {       cin >> arr[i];     }     cout << func(arr, n) << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int available[1000000];     int aux;     int aux2;     for (int i = 0; i < 1000000; ++i) {       available[i] = -2;     }     for (int i = 0; i < n; ++i) {       int k = 0;       cin >> aux;       cin >> aux2;       if (!(aux % 2)) {         if ((i - available[2]) > aux2) {           k++;         }         available[2] = i;         if ((i - available[aux / 2]) > aux2) {           k++;         }         available[aux / 2] = i;       }       for (int j = 1; j <= sqrt(aux); j++) {         if (!(aux % j)) {           if ((i - available[j]) > aux2) {             k++;           }           available[j] = i;           if ((i - available[aux / j]) > aux2) {             k++;           }           available[aux / j] = i;         }       }       printf( %d n , k);     }   }
#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1);   const int N = 1e6 + 10, M = 1e7 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f, ha = 137;   int n, q;   int mx[N], a[N];   int s[M], ls[M], rs[M];   int rt[N];   int cnt;   int update(int loc, int pre, int l, int r) {     int now = ++cnt;     s[now] = s[pre] + 1;     ls[now] = ls[pre];     rs[now] = rs[pre];     if (l == r) return now;     int mid = l + r >> 1;     if (loc <= mid)       ls[now] = update(loc, ls[pre], l, mid);     else       rs[now] = update(loc, rs[pre], mid + 1, r);     return now;   }   int query(int a, int b) {     int x = rt[a - 1], y = rt[b];     int ans = 0;     int l = 0, r = n;     while (l < r) {       int mid = l + r >> 1;       if (a <= mid)         ans += s[rs[y]] - s[rs[x]], x = ls[x], y = ls[y], r = mid;       else {         l = mid + 1, x = rs[x], y = rs[y];       }     }     return ans;   }   int tot;   struct dat {     int s[N << 2], lazy[N << 2];     void update(int a, int b, int x, int k, int l, int r) {       if (a == l && b == r) {         s[x] += k;         lazy[x] += k;         return;       }       int mid = l + r >> 1, ls = x << 1, rs = x << 1 | 1;       if (lazy[x]) {         update(l, mid, ls, lazy[x], l, mid);         update(mid + 1, r, rs, lazy[x], mid + 1, r);         lazy[x] = 0;       }       if (b <= mid)         update(a, b, ls, k, l, mid);       else if (a > mid)         update(a, b, rs, k, mid + 1, r);       else {         update(a, mid, ls, k, l, mid);         update(mid + 1, b, rs, k, mid + 1, r);       }       s[x] = max(s[ls], s[rs]);     }     int query(int k) {       int x = 1;       int l = 1, r = n;       while (l < r) {         int mid = l + r >> 1, ls = x << 1, rs = x << 1 | 1;         if (lazy[x]) {           update(l, mid, ls, lazy[x], l, mid);           update(mid + 1, r, rs, lazy[x], mid + 1, r);           lazy[x] = 0;         }         if (s[rs] >= k)           x = rs, l = mid + 1;         else           x = ls, r = mid;       }       return l;     }   } tr;   int f[N];   int ss;   int main() {     cin >> n >> q;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       int k = i - a[i];       if (k < 0) {         rt[i] = update(a[i] = 0, rt[i - 1], 0, n);         continue;       }       int t = tr.query(k);       a[i] = f[t];       if (k == 0) f[++tot] = i, a[i] = i, t = tot;       if (k > ss) {         a[i] = 0, t = 0;       } else         ss++;       if (min(tot, t)) tr.update(1, min(tot, t), 1, 1, 1, n);       rt[i] = update(a[i], rt[i - 1], 0, n);     }     int l, r;     while (q--) {       scanf( %d%d , &l, &r);       printf( %d n , query(l, n - r));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 200005;   int P[maxN];   int color[maxN];   int cycle_count = 0;   void dfs(int s) {     color[s] = 1;     if (color[P[s]] == 0)       dfs(P[s]);     else if (color[P[s]] == 1)       ++cycle_count;     color[s] = 2;   }   int main() {     int n;     cin >> n;     int res = 1;     for (int i = 1; i <= n; ++i) scanf( %d , &P[i]);     for (int i = 1; i <= n; ++i) {       int b;       scanf( %d , &b);       res ^= b;     }     for (int i = 1; i <= n; ++i)       if (color[i] == 0) dfs(i);     if (cycle_count > 1) res += cycle_count;     cout << res << endl;     return 0;   }