malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1086;   const int MAXT = 1586;   const long long R = 1000000;   struct query {     long long ts, tl;     long double ss, sl, p;     query() {}     query(long double _ss, long double _sl, long long _ts, long long _tl,           long double _p)         : ss(_ss), sl(_sl), ts(_ts), tl(_tl), p(_p) {}     void init() {       cin >> ss >> sl >> ts >> tl >> p;       ss *= R, sl *= R;     }   } Q[MAXN];   pair<long double, long double> dp[MAXN][MAXT];   inline bool cmp(const query &a, const query &b) {     return a.tl * (1 - b.p) * a.p < b.tl * (1 - a.p) * b.p;   }   void update(pair<long double, long double> &a,               pair<long double, long double> b) {     if (b.first > a.first || a.first == b.first && a.second > b.second) a = b;   }   int main() {     int i, j;     int m, n;     scanf( %d%d , &n, &m);     for (i = 0; i < n; ++i) Q[i].init();     sort(Q, Q + n, cmp);     for (i = 0; i < n; ++i)       for (j = 0; j <= m; ++j) dp[i][j].first = dp[i][j].second = -1;     dp[0][0].first = dp[0][0].second = 0;     for (i = 0; i < n; ++i) {       long long ts = Q[i].ts, tl = Q[i].tl;       long double ss = Q[i].ss, sl = Q[i].sl, p = Q[i].p;       for (j = 0; j <= m; ++j)         if (dp[i][j].first != -1) {           long double cs = dp[i][j].first, cp = dp[i][j].second;           update(dp[i + 1][j], dp[i][j]);           if (j + ts <= m)             update(dp[i + 1][j + ts],                    pair<long double, long double>(cs + ss, cp + ts));           if (j + ts + tl <= m)             update(dp[i + 1][j + ts + tl],                    pair<long double, long double>(                        cs + ss + (1 - p) * sl, ts + (j + tl) * (1 - p) + p * cp));         }     }     pair<long double, long double> res(0, 0);     for (i = 0; i <= m; ++i) update(res, dp[n][i]);     cout << setprecision(20) << res.first / R <<     << res.second << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, d;     cin >> n;     unordered_set<int> s;     int mx = n;     int c = 0;     for (int i = 0; i < n; i++) {       cin >> d;       if (d == mx) {         for (int i = 0; i < c; i++) cout <<   n ;         cout << mx;         c = 0;         int k = mx - 1;         while (s.find(k) != s.end()) {           cout <<     << k;           s.erase(k);           k--;         }         cout <<   n ;         mx = k;       } else {         s.insert(d);         c++;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool a[2][100010];   int n, q, nr, x, y;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> q;     for (int i = 1; i <= q; i++) {       cin >> x >> y;       x--;       if (a[x][y])         nr -= a[(x + 1) % 2][y - 1] + a[(x + 1) % 2][y] + a[(x + 1) % 2][y + 1],             a[x][y] = 0;       else         nr += a[(x + 1) % 2][y - 1] + a[(x + 1) % 2][y] + a[(x + 1) % 2][y + 1],             a[x][y] = 1;       if (!nr)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double n, h;   char c[10];   double ans;   double ksm(double x, long long y) {     double maretu = 1.0;     for (; y; y >>= 1) {       if (y & 1) {         maretu *= x;       }       x *= x;     }     return maretu;   }   int main() {     scanf( %s , c);     if (c[0] ==  A ) {       scanf( %lf%lf , &n, &h);       ans = n;       for (double i = 1; i <= h; ++i) {         for (double j = 1; j <= n; ++j) {           ans +=               (n - j) * (1.0 / (double)(1LL << (long long)(2 * i))) *               ksm(1.0 - (1.0 / (double)(1LL << (long long)i)),                   (long long)(j - 1.0)) *               ((double)(1LL << ((long long)i)) -                (double)(1LL << (long long)(i - 1)) *                    (1.0 + (j - 1.0) / ((double)(1LL << (long long)(i)) - 1.0)));         }       }       printf( %.10lf , ans);     } else {       scanf( %lf%lf , &n, &h);       printf( %lf , n);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t, cont[8] = {0}, i;     cin >> n;     for (int i = 0; i < n && cin >> t; i++) cont[t]++;     if (cont[1] != n / 3 || cont[5] + cont[7] || cont[4] > cont[2] ||         cont[3] > cont[6] || cont[2] + cont[3] != cont[4] + cont[6])       cout << -1;     else {       for (i = 0; i < cont[3]; i++) cout << 1 <<     << 3 <<     << 6 << endl;       for (i = 0; i < cont[4]; i++) cout << 1 <<     << 2 <<     << 4 << endl;       for (i = 0; i < cont[2] - cont[4]; i++)         cout << 1 <<     << 2 <<     << 6 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxm = 2e5 + 5;   vector<long long> g[maxm];   long long mark[maxm];   long long p[maxm];   long long c[maxm];   long long ans;   long long n;   void solve(long long x) {     vector<long long> s;     while (!mark[x]) {       s.push_back(x);       mark[x] = 1;       x = p[x];     }     long long len = s.size();     for (long long i = 1; i <= len; i++) {       if (len % i == 0) {         for (long long j = 0; j < i; j++) {           long long ok = 1;           for (long long k = j; k + i < len; k += i) {             if (c[s[k]] != c[s[k + i]]) {               ok = 0;               break;             }           }           if (ok) {             ans = min(ans, i);             return;           }         }       }     }   }   signed main() {     long long T;     cin >> T;     while (T--) {       cin >> n;       ans = n;       for (long long i = 1; i <= n; i++) cin >> p[i];       for (long long i = 1; i <= n; i++) cin >> c[i];       for (long long i = 1; i <= n; i++) mark[i] = 0;       for (long long i = 1; i <= n; i++)         if (!mark[i]) solve(i);       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int n = s.size();     int pref = 0;     int kol = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  # ) kol++;       if (s[i] ==  ( )         pref++;       else         pref--;       if (pref < 0) {         cout <<  -1 ;         return 0;       }     }     string t;     int sz = 0;     int r = 0;     while (sz < kol) {       if (s[r] ==  # ) {         sz++;         t +=  ) ;         if (sz == kol) {           for (int j = 0; j < pref; j++) t +=  ) ;         }       } else {         t += s[r];       }       r++;     }     while (r < n) {       t += s[r];       r++;     }     n = t.size();     int preff = 0;     for (int i = 0; i < n; i++) {       if (t[i] ==  ( )         preff++;       else         preff--;       if (preff < 0) {         cout <<  -1 ;         return 0;       }     }     for (int i = 0; i < kol - 1; i++) {       cout << 1 << endl;     }     cout << pref + 1;   }
#include <bits/stdc++.h>   using namespace std;   map<long long, long long> dist;   int main() {     long long n, m;     cin >> n >> m;     long long x;     queue<long long> q;     for (long long i = 0; i < n; i++) {       cin >> x;       dist[x] = 0;       q.push(x);     }     long long total = 0;     vector<long long> ans;     while (!q.empty()) {       x = q.front();       q.pop();       if (dist[x] != 0) {         ans.push_back(x);         total += (dist[x]);         if (ans.size() == m) break;       }       if (dist.find(x - 1) == dist.end()) {         q.push(x - 1);         dist[x - 1] = dist[x] + 1;       }       if (dist.find(x + 1) == dist.end()) {         q.push(x + 1);         dist[x + 1] = dist[x] + 1;       }     }     cout << total <<   n ;     for (long long i = 0; i < ans.size(); i++) {       cout << ans[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, A, B;   long long toi(string s) {     istringstream is(s);     long long x;     is >> x;     return x;   }   string tos(long long t) {     stringstream st;     st << t;     return st.str();   }   long long F[1000005], FP[1000005];   bool f(long long x) {     while (x > 0) {       if (x % 10 != A && x % 10 != B) return 0;       x /= 10;     }     return 1;   }   long long pow(long long a, long long b, long long c) {     long long x = 1, y = a;     while (b > 0) {       if (b & 1) x = (x * y) % c;       y = (y * y) % c;       b >>= 1;     }     return x;   }   int main() {     F[0] = 1;     for (int i = 1; i < 1000005; ++i) {       F[i] = i * F[i - 1];       if (F[i] >= 1000000007) F[i] %= 1000000007;     }     FP[1000005 - 1] = pow(F[1000005 - 1], 1000000007 - 2, 1000000007);     for (int i = 1000005 - 2; i >= 0; --i) {       FP[i] = FP[i + 1] * (i + 1);       if (FP[i] >= 1000000007) FP[i] %= 1000000007;     }     while (cin >> A >> B >> n) {       long long cnt = 0;       for (int i = 0; i <= n; ++i)         if (f(i * A + (n - i) * B)) {           long long num = F[n], den = FP[i] * FP[n - i];           if (den >= 1000000007) den %= 1000000007;           num *= den;           if (num >= 1000000007) num %= 1000000007;           cnt += num;           if (cnt >= 1000000007) cnt -= 1000000007;         }       cout << cnt << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   class qry {    public:     int l, r, idx;     qry(int l, int r, int idx) : l(l), r(r), idx(idx) {}   };   int a[500001], n, m, res[500001];   vector<qry> q;   int getans(vector<int> b) {     int res = 0;     for (int i = 0; i < b.size(); i++) {       if ((res ^ b[i]) > res) res ^= b[i];     }     return res;   }   void ins(vector<int>& b, int x) {     for (int i = 0; i < b.size(); i++) {       if ((b[i] ^ x) < x) x ^= b[i];     }     if (!x) return;     int t = 31 - __builtin_clz(x);     for (int i = 0; i < b.size(); i++) {       if (b[i] & (1 << t)) b[i] ^= x;     }     b.push_back(x);   }   vector<int> merge(vector<int>& a, vector<int>& b) {     vector<int> ret = a;     for (int i = 0; i < b.size(); i++) ins(ret, b[i]);     return ret;   }   void solve(vector<qry>& q, int L, int R) {     if (q.empty()) return;     if (q.size() <= 3 || R - L <= 3) {       for (int i = 0; i < q.size(); i++) {         int l = q[i].l, r = q[i].r;         vector<int> bs;         for (int j = l; j <= r; j++) ins(bs, a[j]);         res[q[i].idx] = getans(bs);       }       return;     }     vector<qry> l, r, my;     int mid = (L + R) / 2;     for (int i = 0; i < q.size(); i++) {       if (q[i].r <= mid)         l.push_back(q[i]);       else if (q[i].l >= mid + 1)         r.push_back(q[i]);       else         my.push_back(q[i]);     }     solve(l, L, mid);     solve(r, mid + 1, R);     sort(my.begin(), my.end(), [](const qry& i, const qry& j) {       if (i.l != j.l)         return i.l > j.l;       else         return i.r < j.r;     });     vector<int> lb;     int cur = 0, pt = mid;     vector<vector<int> > rb;     vector<int> t;     for (int i = mid + 1; i <= R; i++) {       ins(t, a[i]);       rb.push_back(t);     }     while (cur < my.size()) {       bool found = false;       while (my[cur].l <= pt) {         found = true;         ins(lb, a[pt--]);       }       res[my[cur].idx] = getans(merge(lb, rb[my[cur].r - mid - 1]));       cur++;     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       int l, r;       scanf( %d %d , &l, &r);       q.push_back(qry(l, r, i));     }     solve(q, 1, n);     for (int i = 1; i <= m; i++) printf( %d n , res[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1005, MAXM = 200005;   int n, m, q, f[MAXN][MAXN], head[MAXM], nxt[MAXM], x[MAXM], y[MAXM];   struct Q {     int l, r, s, t;   } s[MAXM];   bool ans[MAXM];   int main() {     scanf( %d%d%d , &n, &m, &q);     for (register int i = 1; i <= m; ++i) scanf( %d%d , x + i, y + i);     for (register int i = 1; i <= q; ++i) {       scanf( %d%d%d%d , &s[i].l, &s[i].r, &s[i].s, &s[i].t);       nxt[i] = head[s[i].r];       head[s[i].r] = i;     }     for (register int u, v, k = 1; k <= m; ++k) {       u = x[k];       v = y[k];       f[u][v] = f[v][u] = k;       for (register int i = 1; i <= n; ++i)         f[u][i] = f[v][i] = max(f[u][i], f[v][i]);       for (register int i = head[k]; i; i = nxt[i])         ans[i] = s[i].l <= f[s[i].t][s[i].s];     }     for (register int i = 1; i <= q; ++i) puts(ans[i] ?  Yes  :  No );   }
#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const long long int INF = (long long int)1e18;   const int N = 100005;   const long long int MOD = 1000000007;   const double EPS = 1e-7;   const double PI = acos(-1.0);   const int dx[] = {+1, -1, +0, +0};   const int dy[] = {+0, +0, +1, -1};   const int hx[] = {-2, -2, -1, -1, +1, +1, +2, +2};   const int hy[] = {+1, -1, +2, -2, +2, -2, +1, -1};   const int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0};   const int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0};   template <typename T>   inline T GCD(T a, T b) {     a = abs(a), b = abs(b);     return !b ? a : GCD(b, a % b);   }   template <typename T>   inline T LCM(T x, T y) {     T g = GCD(x, y);     return x * y / g;   }   template <typename T>   inline T BigMod(T b, T p, T mod) {     T res = 1 % mod, x = b % mod;     while (p) {       if (p & 1) res = (res * x) % mod;       x = (x * x) % mod;       p >>= 1;     }     return res;   }   template <typename T>   inline T ModInv(T val, T mod) {     return BigMod(val, mod - 2, mod);   }   template <typename T>   inline T bigmod(T b, T p, T mod) {     if (p == 0) return 1;     T x = bigmod(b, p / 2, mod);     x = (x * x) % mod;     if (p % 2 == 1) x = (b * x) % mod;     return x;   }   template <typename T>   inline T NumDigit(T num, T base) {     return ((T)(log10((num)) / log10((base)))) + 1;   }   template <typename T>   inline T Power(T b, T p) {     T res = 1, x = b;     while (p) {       if (p & 1) res = (res * x);       x = (x * x);       p >>= 1;     }     return res;   }   template <typename T>   inline T IsPrime(T x) {     if (x < 2) return false;     for (T i = 2; i * i <= x; i++) {       if (x % i == 0) return false;     }     return true;   }   template <typename T>   inline T SumDigit(T x) {     T ret = 0;     while (x) {       ret += x % 10, x /= 10;     }     return ret;   }   int main(int argc, char const *argv[]) {     int n;     scanf( %d , &n);     for (int i = 0; i * 4 <= n; i++) {       for (int j = 0; i * 4 + j * 7 <= n; j++) {         if (4 * i + j * 7 == n) {           for (int m = 1; m <= i; m++) printf( 4 );           for (int m = 1; m <= j; m++) printf( 7 );           return 0;         }       }     }     printf( -1 n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<int> E[100005];   vector<int> L[100005];   int True = 0;   int vst[100005];   int main() {     int n, m, h, t, i, u, v, _h, _t, j, k;     cin >> n >> m >> h >> t;     for (i = 0; i < (m); i++) {       scanf( %d%d , &u, &v);       L[u].push_back(v);       L[v].push_back(u);       E[u].insert(v);       E[v].insert(u);     }     for (i = (1); i <= (n); i++) {       u = i;       if (((int)(E[u]).size()) < h + 1) continue;       for (j = 0; j < (((int)(L[i]).size())); j++) {         v = L[i][j];         if (((int)(E[u]).size()) + ((int)(E[v]).size()) < h + t + 2) continue;         if (((int)(E[v]).size()) < t + 1) continue;         bool sp = 0;         if (((int)(E[u]).size()) > ((int)(E[v]).size())) {           swap(u, v);           sp = 1;         }         set<int>::iterator it = E[u].begin();         _h = 0;         vector<int> cmn;         True++;         for (k = 0; k < (((int)(L[u]).size())); k++) vst[L[u][k]] = True;         for (k = 0; k < (((int)(L[v]).size())); k++) {           if (vst[L[v][k]] == True) {             _h++;             cmn.push_back(L[v][k]);           }         }         if (sp) swap(u, v);         if (((int)(E[u]).size()) + ((int)(E[v]).size()) - _h < h + t + 2)           continue;         printf( YES n );         printf( %d %d n , u, v);         it = E[u].begin();         while (it != E[u].end()) {           if (E[v].find(*it) == E[v].end()) {             if (*it == v) {               it++;               continue;             }             h--;             printf( %d  , *it);           }           if (!h) break;           it++;         }         while (h--) {           printf( %d  , cmn[((int)(cmn).size()) - 1]);           cmn.pop_back();         }         printf(  n );         it = E[v].begin();         while (it != E[v].end()) {           if ((E[u].find(*it) == E[u].end())) {             if (*it == u) {               it++;               continue;             }             t--;             printf( %d  , *it);           }           if (!t) break;           it++;         }         while (t--) {           printf( %d  , cmn[((int)(cmn).size()) - 1]);           cmn.pop_back();         }         printf(  n );         return 0;       }     }     printf( NO n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int mod2 = 1e9 + 9;   inline int pow_(long long a, int n, int p = mod) {     int r = 1;     while (n) {       if (n & 1) r = r * a % p;       n >>= 1;       a = a * a % p;     }     return r;   }   int main(void) {     int n, l;     long long s;     cin >> n >> l >> s;     int w[n + 1], p[n + 1];     memset(p, 0, sizeof(p));     memset(w, 0, sizeof(w));     for (int i = 1; i <= n; i++) cin >> w[i];     for (int i = 2; i <= n; i++) cin >> p[i];     bool done[n + 1];     memset(done, false, sizeof(done));     for (int i = 1; i <= n; i++)       if (w[i] > s) return 0 * printf( -1 n );     int ans = 0;     for (int i = n; i >= 1; i--) {       if (done[i]) continue;       ans++;       int cur = i;       int L = l;       long long S = s;       while (cur and L > 0 and S >= w[cur]) {         done[cur] = true;         S -= w[cur];         L--;         cur = p[cur];       }     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int x, y, n, m, i, k, ans;   string s[30], s1[30];   int main() {     cin >> n >> m;     for (i = 0; i < n; i++) {       cin >> s[i];     }     for (i = 0; i < m; i++) {       cin >> s1[i];     }     cin >> k;     for (i = 0; i < k; i++) {       cin >> x;       if (x % n == 0)         cout << s[n - 1];       else         cout << s[x % n - 1];       if (x % m == 0)         cout << s1[m - 1];       else         cout << s1[x % m - 1];       cout << endl;     }   }
#include <bits/stdc++.h>   const int MAXN = 1e9 + 7;   const int INF = 1e9 + 7;   const signed long long MOD = 1e9 + 7;   using namespace std;   bool comp1(signed long long a, signed long long b) { return (sin(a) < sin(b)); }   signed long long binpow(signed long long a, signed long long b) {     signed long long res = 1;     while (b) {       if (b & 1) res = (res * a);       a = (a * a);       b >>= 1;     }     return res;   }   signed long long phi(signed long long n) {     signed long long result = n;     for (signed long long i = 2; i * i <= n; ++i)       if (n % i == 0) {         while (n % i == 0) n /= i;         result -= result / i;       }     if (n > 1) result -= result / n;     return result;   }   signed long long gcd(signed long long a, signed long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     for (size_t jj = 0; jj < t; jj++) {       signed long long n, k;       cin >> n >> k;       vector<signed long long> mas(n);       signed long long mx = -1e9 - 47;       for (size_t i = 0; i < n; i++) {         cin >> mas[i];         mx = max(mx, mas[i]);       }       signed long long mx1 = -1e9 - 47;       for (size_t i = 0; i < n; i++) {         mas[i] = mx - mas[i];         mx1 = max(mx1, mas[i]);       }       k--;       if (k % 2) {         for (size_t i = 0; i < n; i++) {           mas[i] = mx1 - mas[i];         }       }       for (size_t i = 0; i < n; i++) {         cout << mas[i] <<    ;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > ans;   bool is_prime(int v) {     if (v == 1 || v == 0) return 0;     for (int i = 2; i * i <= v; i++) {       if (v % i == 0) return 0;     }     return 1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     long long cnt = n - 1;     ans.push_back(make_pair(n, 1));     for (int i = 2; i <= n; i++) {       ans.push_back(make_pair(i, i - 1));     }     for (int i = 1; i <= n / 2; i++) {       if (is_prime(ans.size())) break;       ans.push_back(make_pair(i, i + n / 2));       ++cnt;     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) {       cout << ans[i].first <<     << ans[i].second << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 7;   const int N = 3e5 + 10;   int flag[N];   set<int> s[N];   int a[N];   int pos[N];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       pos[a[i]] = i;     }     while (m--) {       int x, y;       scanf( %d%d , &x, &y);       if (x == a[n]) continue;       if (y == a[n]) {         flag[x] = 1;       }       s[x].emplace(y);     }     int ans = n;     for (int i = n - 1; i > 0; i--) {       if (flag[a[i]]) {         int cnt = 0;         for (auto& x : s[a[i]]) {           if (pos[x] < i) continue;           if (pos[x] >= ans) continue;           cnt++;         }         if (cnt != ans - i - 1)           flag[a[i]] = 0;         else {           for (int j = i; j < ans; j++) {             int x = a[j], y = a[j + 1];             swap(a[j], a[j + 1]);             swap(pos[x], pos[y]);           }           ans--;         }       }     }     printf( %d n , n - ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rd(time(0));   vector<long long> v(100010), val(100010), con[100010], st(100010, 0),       en(100010, 0), d(100010, 0);   pair<long long, long long> t[20][100010];   long long cur = 0;   void dfs(long long node, long long par, long long dep) {     st[node] = ++cur;     d[st[node]] = dep;     for (auto i : con[node]) {       if (i != par) dfs(i, node, dep + 1);     }     en[node] = cur;   }   void build(long long c, long long l, long long r) {     if (l == r)       t[c][l] = {d[l], val[l]};     else {       long long mid = (l + r) / 2;       build(c + 1, l, mid);       build(c + 1, mid + 1, r);       long long i = l, j = mid + 1, k = l;       while (i <= mid && j <= r) {         if (t[c + 1][i].first < t[c + 1][j].first)           t[c][k++] = t[c + 1][i++];         else           t[c][k++] = t[c + 1][j++];       }       while (i <= mid) t[c][k++] = t[c + 1][i++];       while (j <= r) t[c][k++] = t[c + 1][j++];       for (i = l + 1; i <= r; i++)         t[c][i].second = min(t[c][i].second, t[c][i - 1].second);     }   }   long long query(long long c, long long l, long long r, long long l1,                   long long r1, long long k) {     if (r1 < l || l1 > r) return 1000000007;     if (l >= l1 && r <= r1) {       pair<long long, long long> temp = {k, 1000000007};       long long w = upper_bound(t[c] + l, t[c] + r + 1, temp) - (t[c]);       if (w == l) return 1000000007;       return t[c][w - 1].second;     } else {       long long mid = (l + r) / 2;       return min(query(c + 1, l, mid, l1, r1, k),                  query(c + 1, mid + 1, r, l1, r1, k));     }   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, r, m, l, x, k;     cin >> n >> r;     for (long long i = 1; i < n + 1; i++) cin >> v[i];     for (long long i = 0; i < n - 1; i++) {       long long u, v;       cin >> u >> v;       con[u].push_back(v);       con[v].push_back(u);     }     dfs(r, -1, 0);     for (long long i = 1; i <= n; i++) val[st[i]] = v[i];     build(1, 1, cur);     cin >> m;     l = 0;     while (m--) {       cin >> x >> k;       x = (x + l) % n + 1;       k = (k + l) % n;       l = query(1, 1, cur, st[x], en[x], k + d[st[x]]);       cout << l << endl;     }     return 0;   }