malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const int inf = 0x3f3f3f3f;   const int Minf = 0x7f7f7f7f;   const long long INF = 0x3f3f3f3f3f3f3f3fll;   const long long MINF = 0x7f7f7f7f7f7f7f7fll;   const int N = 2e5 + 50;   vector<int> t[N];   int dfn[N], rig[N];   int fa[N][20], dep[N];   void dfs(int now, int f) {     fa[now][0] = f;     dfn[now] = ++dfn[0];     dep[now] = dep[f] + 1;     for (int i = (1); i <= 19; ++i) fa[now][i] = fa[fa[now][i - 1]][i - 1];     for (int to : t[now]) {       if (to == f) continue;       dfs(to, now);     }     rig[now] = dfn[0];   }   struct node {     int l, r;     int tag;     int mxn;     int cnt;   };   struct seg {     node t[N << 2];     void updata(int now) {       t[now].mxn = max(t[(now << 1)].mxn, t[(now << 1 | 1)].mxn);       t[now].cnt = 0;       if (t[now].mxn == t[(now << 1)].mxn) t[now].cnt += t[(now << 1)].cnt;       if (t[now].mxn == t[(now << 1 | 1)].mxn)         t[now].cnt += t[(now << 1 | 1)].cnt;     }     void bt(int now, int l, int r) {       t[now].l = l;       t[now].r = r;       if (l == r) {         t[now].mxn = 0;         t[now].cnt = 1;         return;       }       bt((now << 1), l, ((t[now].l + t[now].r) >> 1));       bt((now << 1 | 1), ((t[now].l + t[now].r) >> 1) + 1, r);       updata(now);     }     void pushdown(int now) {       if (!t[now].tag) return;       t[(now << 1)].mxn += t[now].tag;       t[(now << 1)].tag += t[now].tag;       t[(now << 1 | 1)].mxn += t[now].tag;       t[(now << 1 | 1)].tag += t[now].tag;       t[now].tag = 0;     }     void change(int now, int l, int r, int v) {       if (l <= t[now].l && t[now].r <= r) {         t[now].mxn += v;         t[now].tag += v;         return;       }       pushdown(now);       if (l <= ((t[now].l + t[now].r) >> 1)) change((now << 1), l, r, v);       if (r > ((t[now].l + t[now].r) >> 1)) change((now << 1 | 1), l, r, v);       updata(now);     }   } sgt;   bool in_tree(int y, int x) { return dfn[y] <= rig[x]; }   int main() {     int n, q, x, y;     scanf( %d%d , &n, &q);     for (int i = (2); i <= n; ++i)       scanf( %d%d , &x, &y), t[x].push_back(y), t[y].push_back(x);     dfs(1, 0);     sgt.bt(1, 1, n);     map<pair<int, int>, int> vis;     int link = 0, v = 0;     for (int i = (1); i <= q; ++i) {       scanf( %d%d , &x, &y);       if (dfn[x] > dfn[y]) swap(x, y);       if (vis[make_pair(x, y)])         vis[make_pair(x, y)] = 0, v = -1, --link;       else         vis[make_pair(x, y)] = 1, v = 1, ++link;       if (in_tree(y, x)) {         int u = y;         for (int i = (19); i >= 0; --i)           if (dep[fa[u][i]] > dep[x]) u = fa[u][i];         sgt.change(1, 1, n, v);         sgt.change(1, dfn[u], rig[u], -1 * v);         sgt.change(1, dfn[y], rig[y], v);       } else {         sgt.change(1, dfn[x], rig[x], v);         sgt.change(1, dfn[y], rig[y], v);       }       node ans = sgt.t[1];       if (ans.mxn == link) {         printf( %d n , ans.cnt);       } else         puts( 0 );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     double r, x1, y1, x2, y2;     cin >> r >> x1 >> y1 >> x2 >> y2;     long long x =         (long long)sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2 / r;     if (x * 2.0 * r < sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)))       cout << x + 1;     else       cout << x;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, i, j, x;   long long m, k, x1, u1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6;   bool t = 0;   int main() {     cin >> x1 >> u1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4 >> x5 >> y5 >> x6 >> y6;     m = (x2 - x1) * (y2 - u1);     if (x3 < x2 && y3 < y2 && x4 > x1 && y4 > u1) {       if (x3 < x1) x3 = x1;       if (y3 < u1) y3 = u1;       if (x4 > x2) x4 = x2;       if (y4 > y2) y4 = y2;       k = (x4 - x3) * (y4 - y3);       m -= k;     }     if (x5 < x2 && y5 < y2 && x6 > x1 && y6 > u1) {       if (x5 < x1) x5 = x1;       if (y5 < u1) y5 = u1;       if (x6 > x2) x6 = x2;       if (y6 > y2) y6 = y2;       k = (x6 - x5) * (y6 - y5);       m -= k;     }     if (x3 < x6 && x4 > x5 && y3 < y6 && y5 < y4) {       if (x3 < x5) x3 = x5;       if (x4 > x6) x4 = x6;       if (y3 < y5) y3 = y5;       if (y4 > y6) y4 = y6;       k = (x4 - x3) * (y4 - y3);       m += k;     }     if (m > 0)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   unsigned seed = chrono::system_clock::now().time_since_epoch().count();   mt19937 eng(seed);   long long random2() { return (1ll << 31ll) * eng() + eng(); }   struct FT {     vector<long long> s;     FT(int n) : s(n) {}     void update(int pos, long long dif) {       for (; pos < (int)(s).size(); pos |= pos + 1) s[pos] += dif;     }     long long query(int pos) {       long long res = 0;       for (; pos > 0; pos &= pos - 1) res += s[pos - 1];       return res;     }     int lower_bound(long long sum) {       if (sum <= 0) return -1;       int pos = 0;       for (int pw = 1 << 25; pw; pw >>= 1) {         if (pos + pw <= (int)(s).size() && s[pos + pw - 1] < sum)           pos += pw, sum -= s[pos - 1];       }       return pos;     }   };   long long n, m, k, q, T;   const long long big = 1000000007;   const long long big2 = 1000000009;   const long long mod = 998244353;   const int MAXN = 600001;   const int SQRT = 500;   vector<int> smalls;   vector<long long> A;   long long amod[SQRT] = {0};   long long bigs = 0;   long long brute(long long i) {     long long res = 0;     for (int c1 = 0; c1 < (i + 1); ++c1) {       for (int c2 = 0; c2 < (i + 1); ++c2) {         res += A[c1] % A[c2];       }     }     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long a, b, c, d, e;     cin >> n;     FT ft1(MAXN);     FT ft2(MAXN);     long long upscore = 0;     long long ans = 0;     for (int c1 = 0; c1 < (n); ++c1) {       cin >> a;       A.push_back(a);       if (a < SQRT) {         ans += amod[a];         ans += ft2.query(a) + a * bigs;         for (auto& y : smalls) {           ans += a % y;         }         smalls.push_back(a);       } else {         long long temp1 = upscore;         long long a2 = a;         while (1) {           temp1 -= (long long(c1) - ft1.query(a2)) * a;           if (a2 > MAXN / 2) break;           a2 += a;         }         ans += temp1;         ans += ft2.query(a) + a * bigs;         for (auto& y : smalls) {           ans += a % y;         }         a2 = a;         while (1) {           ft2.update(a2 - 1, -a);           if (a2 > MAXN / 2) break;           a2 += a;         }         bigs++;       }       ft1.update(a, 1);       for (int c2 = 1; c2 < (SQRT); ++c2) {         amod[c2] += a % c2;       }       upscore += a;       cout << ans <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > v;   vector<long long> p;   int main() {     long long n, i, j, x;     long long m;     cin >> n;     cin >> m;     for (i = 0; i < n; i++) {       cin >> x;       p.push_back(x);     }     long long z = *max_element(p.begin(), p.end()), sum = 0;     for (i = 0; i < n; i++) {       sum += z - p[i];     }     if (sum >= m) {       cout << z <<     << *max_element(p.begin(), p.end()) + m << endl;     } else {       long long k = m - sum;       long long y = k % n;       long long q = k / n;       if (y != 0)         cout << z + q + 1 <<     << *max_element(p.begin(), p.end()) + m << endl;       else         cout << z + q <<     << *max_element(p.begin(), p.end()) + m << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, b, c, d, i, j = 1, k, l = 0, n, m, t, s = 0, p, r, ans,                                  w = 0, y, x, lcm, gcd, q, count;     string str;     vector<string> vc;     vector<string> vc2;     map<long long int, long long int> mp;     set<string> stringstream;     cin >> n >> m;     for (j = n + 1;; j++) {       l = 0;       for (i = 2; i <= sqrt(j); i++) {         if (j % i == 0) {           l = 1;           break;         }       }       if (l == 0) {         if (j == m)           cout <<  YES  << endl;         else           cout <<  NO  << endl;         return 0;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   int n;   vector<pair<int, int> > v[maxn];   int size[maxn];   long long mi, mx;   void dfs(int first, int fa) {     size[first] = 1;     for (pair<int, int> i : v[first]) {       if (i.first == fa) continue;       dfs(i.first, first);       if (size[i.first] & 1) mi += 1LL * i.second;       size[first] += size[i.first];       mx += 1LL * min(n - size[i.first], size[i.first]) * i.second;     }   }   void solve() {     scanf( %d , &n);     n = n * 2;     for (int i = 1; i <= n; i++) v[i].clear();     for (int i = 1, first, second, z; i < n; i++) {       scanf( %d%d%d , &first, &second, &z);       v[first].push_back({second, z});       v[second].push_back({first, z});     }     mi = 0, mx = 0;     dfs(1, 0);     printf( %lld %lld n , mi, mx);   }   int T;   int main() {     scanf( %d , &T);     while (T--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 7;   const int MOD = 1e9 + 7;   const int LEN = 8, MAGIC = 1e8;   int a[N], b[N];   int n;   char bignum[N];   long long m[N];   int tot;   int f[N], g[N];   int add(int a, int b) {     a += b;     if (a < 0) a += MOD;     if (a >= MOD) a -= MOD;     return a;   }   int divide(int d) {     long long remain = 0;     for (int i = tot - 1; i >= 0; i--) {       remain = remain * MAGIC + m[i];       m[i] = remain / d;       remain = remain % d;     }     while (tot > 1 && m[tot - 1] == 0) tot--;     return remain;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scanf( %d , &a[i]);     }     a[n] = 1e9 + 7;     for (int i = 1; i <= n; i++) {       scanf( %d , &b[i]);     }     {       scanf( %s , bignum);       tot = 0;       int cur = 0, l = strlen(bignum);       for (int i = l - 1; i >= 0; i -= LEN) {         cur = 0;         for (int j = max(0, i - LEN + 1); j <= i; j++) {           cur = cur * 10 + bignum[j] -  0 ;         }         m[tot++] = cur;       }     }     int carry = 0;     f[0] = 1;     for (int i = 1; i <= n; i++) {       int new_carry = carry + b[i], s = 0;       fill(g, g + new_carry + 1, 0);       for (int j = 0; j <= new_carry; j++) {         s = add(s, f[j]);         if (j > b[i]) {           s = add(s, -f[j - b[i] - 1]);         }         g[j] = s;       }       fill(f, f + carry + 1, 0);       if (a[i] > 1) {         int remainder = divide(a[i]);         for (int ii = remainder, jj = 0; ii <= new_carry; ii += a[i], jj++) {           f[jj] = g[ii];         }         carry = (new_carry - remainder) / a[i];       } else if (a[i] == 1) {         for (int i = 0; i <= new_carry; i++) f[i] = g[i];         carry = new_carry;       } else {         assert(false);       }     }     if (tot == 1 && m[0] == 0) {       printf( %d n , f[0]);     } else {       puts( 0 );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 100005;   bool black[MAX];   vector<int> vec[MAX];   bool old_black[MAX];   const int MAX_BLOCK_SIZE = 600;   int t[MAX], v[MAX];   bool used[MAX];   vector<pair<pair<int, int>, int>> v2[MAX];   int push[MAX];   bool clear[MAX];   void dfs_1(int pos, int prev = -1, int white = 0, int dist = 0) {     if (used[pos]) {       if (prev != -1) v2[prev].push_back(make_pair(make_pair(pos, white), dist));       for (int a : vec[pos]) dfs_1(a, pos, 0, 0);     } else {       if (!black[pos]) white++;       for (int a : vec[pos]) dfs_1(a, prev, white, dist + 1);     }   }   void make_1(int pos) {     if (!black[pos]) {       black[pos] = true;       return;     }     push[pos]++;     for (auto a : v2[pos])       if (a.first.second + 1 <= push[pos]) make_1(a.first.first);   }   void make_2(int pos) {     black[pos] = false;     push[pos] = 0;     clear[pos] = true;     for (auto &a : v2[pos]) {       a.first.second = a.second;       make_2(a.first.first);     }   }   void dfs_2(int pos, int p = 0, bool cl = false) {     if (used[pos]) {       p = push[pos];       cl |= clear[pos];     } else {       black[pos] = old_black[pos];       if (cl) black[pos] = false;       if (!black[pos] && p) {         black[pos] = true;         p--;       }     }     for (int a : vec[pos]) dfs_2(a, p, cl);   }   int main() {     ios_base::sync_with_stdio();     cin.tie(0);     cout.tie(0);     int n, q;     cin >> n >> q;     for (int i = 2; i <= n; i++) {       int a;       cin >> a;       vec[a].push_back(i);     }     for (int i = 1; i <= q; i++) cin >> t[i] >> v[i];     int root = 1;     for (int i = 1; i <= q; i += MAX_BLOCK_SIZE) {       for (int j = 1; j <= n; j++) {         used[j] = false;         v2[j].clear();         old_black[j] = black[j];         push[j] = 0;         clear[j] = false;       }       for (int j = 0; j < MAX_BLOCK_SIZE && i + j <= q; j++)         used[v[i + j]] = true;       dfs_1(root);       for (int j = 0; j < MAX_BLOCK_SIZE && i + j <= q; j++) {         int t = ::t[i + j];         int v = ::v[i + j];         if (t == 1)           make_1(v);         else if (t == 2)           make_2(v);         else           cout << (black[v] ?  black  :  white ) << endl;       }       dfs_2(root);     }   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int mod = 1e9 + 7;   const double eps = 1e-6;   int x[8], y[8];   bool check(int a, int b) {     return a >= min({x[0], x[1], x[2], x[3]}) &&            a <= max({x[0], x[1], x[2], x[3]}) &&            b >= min({y[0], y[1], y[2], y[3]}) &&            b <= max({y[0], y[1], y[2], y[3]});   }   int main() {     for (int i = 0; i < 8; i++) scanf( %d%d , x + i, y + i);     int tx = (x[4] + x[6]) / 2;     int ty = (y[4] + y[6]) / 2;     int d = abs(x[4] - tx) + abs(y[4] - ty);     for (int xx = -100; xx <= 100; xx++)       for (int yy = -100; yy <= 100; yy++)         if (abs(xx - tx) + abs(yy - ty) <= d)           if (check(xx, yy)) return cout <<  YES , 0;     cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0), eps = 1e-4;   const int inf = ~0U >> 2;   int mod = 1e9 + 7;   int Pow(int x, long long t) {     int r = 1;     for (; t; t >>= 1, x = (long long)x * x % mod)       if (t & 1) r = (long long)r * x % mod;     return r;   }   const long long linf = (long long)inf * inf;   const int N = 5, M = 101010;   int xcost[N][M], ycost[N][M];   int n, m;   long long f[2][1 << N];   void Update(long long &x, long long d) {     if (x > d) x = d;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m - 1; ++i)       for (int j = 0; j < n; ++j) scanf( %d , &xcost[j][i]);     for (int i = 0; i < m; ++i)       for (int j = 0; j < n; ++j) scanf( %d , &ycost[j][i]);     int c = 0;     for (int i = 0; i < 1 << n; ++i) f[c][i] = linf;     f[c][(1 << n) - 1] = 0;     for (int i = 1; i < m; ++i)       for (int j = 0; j < n; ++j) {         c ^= 1;         for (int i = 0; i < 1 << n; ++i) f[c][i] = linf;         for (int mask = 0; mask < 1 << n; ++mask) {           int newmask = mask & (~(1 << j));           long long Cost = 0;           if (j >= 1) Cost += mask >> (j - 1) & 1 ? 0 : ycost[j - 1][i];           if (j == n - 1) Cost += mask & 1 ? 0 : ycost[j][i];           Update(f[c][newmask | (1 << j)], f[c ^ 1][mask] + Cost);           Cost = mask >> j & 1 ? xcost[j][i - 1] : 0;           if (j >= 1) Cost += mask >> (j - 1) & 1 ? ycost[j - 1][i] : 0;           if (j == n - 1) Cost += mask & 1 ? ycost[j][i] : 0;           Update(f[c][newmask], f[c ^ 1][mask] + Cost);         }       }     cout << f[c][0] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   int a[1005];   int main() {     int x, y, n;     cin >> x >> y;     int a[10] = {-x, -y, x - y, x, y, y - x};     cin >> n;     int ans = (n + 2) % 6;     a[ans] %= mod;     if (a[ans] < 0) a[ans] += mod;     cout << a[ans] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int M = 1e9 + 7;   long long int cdiv(long long int a, long long int b) {     return (a % b == 0) ? a / b : a / b + 1;   }   long long int myMod(long long int a, long long int b) {     long long int r = a % b;     return r < 0 ? r + b : r;   }   long long int ModPow(long long int a, long long int b, long long int M) {     if (M == 1) return 0;     a %= M;     long long int ans = 1, t = 1;     while (t > 0 && t <= b) {       if (t & b) {         ans *= a;         ans %= M;       }       t <<= 1;       a *= a;       a %= M;     }     return ans;   }   struct seg {     long long int l, r, u, d;   };   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int n;     cin >> n;     seg a[n];     for (long long int i = 0; i < n; i++)       cin >> a[i].l >> a[i].d >> a[i].r >> a[i].u;     long long int mxll[n], mxlr[n], mnrl[n], mnrr[n];     mxll[0] = a[0].l, mxlr[n - 1] = a[n - 1].l, mnrl[0] = a[0].r,     mnrr[n - 1] = a[n - 1].r;     for (long long int i = 1; i < n; i++) {       mxll[i] = max(mxll[i - 1], a[i].l);       mnrl[i] = min(mnrl[i - 1], a[i].r);     }     for (long long int i = n - 1 - 1; i >= 0; i--) {       mxlr[i] = max(mxlr[i + 1], a[i].l);       mnrr[i] = min(mnrr[i + 1], a[i].r);     }     long long int mxdl[n], mxdr[n], mnul[n], mnur[n];     mxdl[0] = a[0].d, mxdr[n - 1] = a[n - 1].d, mnul[0] = a[0].u,     mnur[n - 1] = a[n - 1].u;     for (long long int i = 1; i < n; i++) {       mxdl[i] = max(mxdl[i - 1], a[i].d);       mnul[i] = min(mnul[i - 1], a[i].u);     }     for (long long int i = n - 1 - 1; i >= 0; i--) {       mxdr[i] = max(mxdr[i + 1], a[i].d);       mnur[i] = min(mnur[i + 1], a[i].u);     }     if (mnrr[1] - mxlr[1] >= 0 && mnur[1] - mxdr[1] >= 0) {       cout << mxlr[1] <<     << mxdr[1] <<   n ;       return 0;     }     for (long long int i = 1; i < n - 1; i++) {       long long int cix =           min(mnrl[i - 1], mnrr[i + 1]) - max(mxll[i - 1], mxlr[i + 1]);       long long int ciy =           min(mnul[i - 1], mnur[i + 1]) - max(mxdl[i - 1], mxdr[i + 1]);       if (cix >= 0 && ciy >= 0) {         cout << max(mxll[i - 1], mxlr[i + 1]) <<                  << max(mxdl[i - 1], mxdr[i + 1]);         return 0;       }     }     if (mnrl[n - 2] - mxll[n - 2] >= 0 && mnul[n - 2] - mxdl[n - 2] >= 0) {       cout << mxll[n - 2] <<     << mxdl[n - 2] <<   n ;       return 0;     }     return 0;   }
#include <bits/stdc++.h>   const int MAX = 111;   using namespace std;   int A[MAX];   int main() {     int N, a, b;     scanf( %d%d%d , &N, &a, &b);     for (int i = 0; i < (a); ++i) {       int(x);       scanf( %d , &x);       A[x] = 1;     }     for (int i = 0; i < (b); ++i) {       int(x);       scanf( %d , &x);       if (!A[x]) A[x] = 2;     }     for (int i = (1); i <= (N); ++i) printf( %d  , A[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, T;     cin >> t;     for (T = 0; T < t; T++) {       long long l, i;       string s, s1, s2;       cin >> s;       l = s.size();       for (i = 0; i < l / 2; i++) {         s1.push_back(s[i]);       }       for (i = l / 2; s[i] !=   0 ; i++) {         s2.push_back(s[i]);       }       if (s1 == s2)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[150], b[150];   void put(int x, int y[]) {     if (x == 1)       y[1] = 1;     else if (x == 2) {       y[1] = 3;       y[2] = 4;     } else {       for (int i = 1; i <= x - 1; i++) y[i] = 2;       y[x] = x - 2;     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     put(n, a);     put(m, b);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         printf( %d , a[i] * b[j]);         if (j == m)           printf(  n );         else           printf(   );       }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   vector<T>& operator--(vector<T>& v) {     for (auto& i : v) --i;     return v;   }   template <typename T>   vector<T>& operator++(vector<T>& v) {     for (auto& i : v) ++i;     return v;   }   template <typename T>   istream& operator>>(istream& is, vector<T>& v) {     for (auto& i : v) is >> i;     return is;   }   template <typename T>   ostream& operator<<(ostream& os, vector<T>& v) {     for (auto& i : v) os << i <<    ;     return os;   }   template <typename T, typename U>   istream& operator>>(istream& is, pair<T, U>& p) {     is >> p.first >> p.second;     return is;   }   template <typename T, typename U>   ostream& operator<<(ostream& os, pair<T, U>& p) {     os << p.first <<     << p.second;     return os;   }   template <typename T, typename U>   pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {     return make_pair(a.first - b.first, a.second - b.second);   }   template <typename T, typename U>   pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {     return make_pair(a.first + b.first, a.second + b.second);   }   template <typename T, typename U>   void umin(T& a, U b) {     if (a > b) a = b;   }   template <typename T, typename U>   void umax(T& a, U b) {     if (a < b) a = b;   }   int main() {     int T;     cin >> T;     while (T--) {       long long a, b, p, q;       cin >> a >> b >> p >> q;       long long t = min(b - a + 1, (long long)sqrt(b - a) + 5);       vector<pair<long long, int>> v;       auto g = [&](long long x) { return p * x * 2 % (q * 2); };       auto f = [&](long long x) { return abs(g(x) - q); };       42;       ;       for (int i = 0; i < t; ++i) {         v.emplace_back(g(i), i);       }       long long ans = a;       sort(v.begin(), v.end());       42;       ;       for (long long i = a; i + t <= b + 1; i += t) {         long long need = (q - g(i) + q * 2) % (q * 2);         int ind = (lower_bound((v).begin(), (v).end(), (make_pair(need, -1))) -                    (v).begin());         for (int j = ind - 3; j <= ind + 3; ++j) {           int k = (j + v.size()) % v.size();           long long fn = f(i + v[k].second);           if (fn < f(ans) || (fn == f(ans) && i + v[k].second < ans))             ans = i + v[k].second;         }         ind = (upper_bound((v).begin(), (v).end(), (make_pair(need, -1))) -                (v).begin());         for (int j = ind - 3; j <= ind + 3; ++j) {           int k = (j + v.size()) % v.size();           long long fn = f(i + v[k].second);           if (fn < f(ans) || (fn == f(ans) && i + v[k].second < ans))             ans = i + v[k].second;         }       }       for (long long i = b; i >= max(b - t - 5, a); --i) {         long long fn = f(i);         if (fn < f(ans) || (fn == f(ans) && i < ans)) ans = i;       }       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   vector<vector<int>> adj;   vector<bool> vis;   vector<int> dep;   vector<int> sz;   vector<int> dif;   void dfsd(int v, int c) {     vis[v] = true;     c++;     for (auto it = adj[v].begin(); it != adj[v].end(); ++it) {       if (vis[*it] != true) {         dfsd(*it, c);       }     }     dep[v] = c;   }   void dfss(int v) {     vis[v] = false;     for (auto it = adj[v].begin(); it != adj[v].end(); ++it) {       if (vis[*it] != false) {         dfss(*it);       }     }     for (auto it = adj[v].begin(); it != adj[v].end(); ++it) {       sz[v] += sz[*it];     }     sz[v]++;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, i, k, p, q, j;     long long cnt = 0;     cin >> n >> k;     adj.resize(n);     vis.resize(n);     dep.resize(n);     sz.resize(n, 0);     dif.resize(n);     for (i = 0; i < n - 1; i++) {       cin >> p >> q;       adj[p - 1].push_back(q - 1);       adj[q - 1].push_back(p - 1);     }     dfsd(0, 0);     dfss(0);     for (i = 0; i < n; i++) {       dif[i] = sz[i] - dep[i];     }     sort(dif.begin(), dif.end(), greater<int>());     for (i = 0; i < n - k; i++) {       cnt += dif[i];     }     cout << cnt;   }