malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int n;     cin >> n;     vector<long long int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     sort(v.begin(), v.end());     if (n == 1) {       int q;       cin >> q;       for (int i = 0; i < q; i++) {         long long int l, r;         cin >> l >> r;         cout << r - l + 1 <<    ;       }       return 0;     }     vector<long long int> use(n - 1);     for (int i = 1; i < n; i++) {       use[i - 1] = v[i] - v[i - 1];     }     sort(use.begin(), use.end());     vector<long long int> pref(n - 1);     pref[0] = use[0];     for (int i = 1; i < n - 1; i++) {       pref[i] = pref[i - 1] + use[i];     }     int q;     cin >> q;     for (int i = 0; i < q; i++) {       long long int l, r;       cin >> l >> r;       long long int ans = 0;       long long int val = r - l + 1;       long long int where =           upper_bound(use.begin(), use.end(), val) - use.begin();       if (where) ans = ans + pref[where - 1];       long long int extra = (val) * (n - 1 - where);       ans = ans + extra;       ans = ans + val;       cout << ans <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     char tt =  a ;     int k = s.size(), c = 0;     for (int i = 0; i < k; i++) {       int x = int(tt);       int y = int(s[i]);       int a = abs(x - y);       int b = 26 - a;       b = min(b, a);       c += b;       tt = s[i];     }     cout << c << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) { cerr << endl; }   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a <<   ,  ;     err(++it, args...);   }   namespace fastIO {   bool IOerror = 0;   inline char nc() {     static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;     if (p1 == pend) {       p1 = buf;       pend = buf + fread(buf, 1, 100000, stdin);       if (pend == p1) {         IOerror = 1;         return -1;       }     }     return *p1++;   }   inline bool blank(char ch) {     return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ;   }   inline void read(int &x) {     char ch;     while (blank(ch = nc()))       ;     if (IOerror) return;     for (x = ch -  0 ; (ch = nc()) >=  0  && ch <=  9 ; x = x * 10 + ch -  0 )       ;   }   }  // namespace fastIO   using namespace fastIO;   const double PI = acos(-1.0);   const double eps = 1e-8;   const int mod = 1e9 + 7;   long long qp(long long b, long long n) {     long long r = 1;     if (n < 0) n += mod - 1;     b %= mod;     for (; n; n >>= 1, b = b * b % mod)       if (n & 1) r = r * b % mod;     return r;   }   int sgn(double x) {     if (x < -eps) return -1;     return x > eps ? 1 : 0;   }   const int maxn = 3e5 + 7;   const int N = 1e5 + 10;   int n;   int sum[N], dif[N], a[N];   pair<int, int> p[N];   int main() {     scanf( %d , &n);     long long ans = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       sum[a[i]]++;       ans += (1LL * a[i] * (n - a[i] + 1));     }     for (int i = 1; i <= n; ++i) sum[i] += sum[i - 1];     for (int i = 1; i < n; ++i) {       p[i].first = a[i];       p[i].second = a[i + 1];       if (p[i].first > p[i].second) swap(p[i].first, p[i].second);       ans -= 1LL * p[i].first * (n - p[i].second + 1);     }     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void checkmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   inline void checkmax(T &a, T b) {     if (b > a) a = b;   }   int N, K;   char cs[100002];   int main() {     scanf( %d%d , &N, &K);     for (int i = (0); i < (int)(N); i++) cs[i] =  1 ;     for (int i = (0); i < (int)(K); i++) cs[i] =  0 ;     cs[N] = 0;     if (K > 1 && K != N) {       int cnt = (N - K) / 2;       for (int i = 0; i < N; i += cnt + 1) {         for (int j = 0; j < cnt + 1 && i + j < N; j++) {           if (j == 0)             cs[i + j] =  0 ;           else             cs[i + j] =  1 ;         }       }     }     printf( %s n , cs);   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2025;   const int MOD = 998244353;   int n, k;   namespace Task1 {   int ans;   int f[MAXN][MAXN];   int sum[MAXN][MAXN];   void solve() {     f[0][n] = 1;     for (int i = n; i >= 1; i--) sum[0][i] = 1;     for (int i = 1; i <= n; i++)       for (int j = i; j >= 1; j--) {         f[i][j] = sum[i - j][j];         (ans += f[i][j]) %= MOD;         sum[i][j] = (sum[i][j + 1] + f[i][j]) % MOD;       }     printf( %d n , ans);   }   }  // namespace Task1   namespace Task2 {   int ans, N;   vector<int> f[MAXN][70];   vector<int> sum[MAXN][70];   void solve() {     N = min(n, 65);     for (int i = 0; i <= n; i++) {       f[i][0].resize(MAXN);       sum[i][0].resize(MAXN);       for (int j = 1; j <= N; j++) {         f[i][j].resize(i / j + 2);         sum[i][j].resize(i / j + 2);       }     }     f[0][0][n] = 1;     for (int i = n; i >= 1; i--) sum[0][0][i] = 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= N; j++)         for (int k = i / j; k >= 1; k--) {           if (sum[i - j * k][j - 1].size() > k)             f[i][j][k] = sum[i - j * k][j - 1][k];           (ans += f[i][j][k]) %= MOD;           sum[i][j][k] = (sum[i][j][k + 1] + f[i][j][k]) % MOD;         }     printf( %d n , ans);   }   }  // namespace Task2   namespace Task3 {   int ans;   vector<int> a;   bool chk(vector<int> t) {     int K = k;     while (K--) {       vector<int> tt;       int sum = 0;       for (int i = 0; i < t.size(); i++) sum += t[i];       if (sum > n) return 0;       tt.resize(sum);       int tot = 0;       for (int i = t.size() - 1; i >= 0; i--)         for (int j = 1; j <= t[i]; j++) tt[tot++] = i + 1;       swap(tt, t);     }     return 1;   }   void dfs(int d) {     if (d > 1 && !chk(a)) return;     if (d > 1) ans++;     int t = d > 1 ? a.back() : n;     for (int i = 1; i <= t; i++) {       a.push_back(i);       int tmp = ans;       dfs(d + 1);       a.pop_back();       if (ans == tmp) return;     }   }   void solve() {     dfs(1);     printf( %d n , ans);   }   }  // namespace Task3   int main() {     scanf( %d%d , &n, &k);     if (k == 1) Task1::solve();     if (k == 2) Task2::solve();     if (k >= 3) Task3::solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   const int inf = 0x3f3f3f3f;   int n;   vector<int> vec[N];   int dis(int s, int t) {     if (t >= s) return t - s;     return t - s + n;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int m;     cin >> n >> m;     int num = 0;     for (int i = 1; i <= m; i++) {       int a, b;       cin >> a >> b;       vec[a].push_back(dis(a, b));       num = max(num, (int)vec[a].size());     }     for (int i = 1; i <= n; i++) {       sort(vec[i].begin(), vec[i].end());     }     for (int st = 1; st <= n; st++) {       int ans = 0;       for (int i = 1; i <= n; i++) {         if (vec[i].size() > 0)           ans = max(ans, dis(st, i) + ((int)vec[i].size() - 1) * n + vec[i][0]);       }       cout << ans <<    ;     }     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     vector<int> silver, gold;     int g = 0, mxg = -1;     ;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  S ) {         gold.push_back(g);         if (g > mxg) mxg = g;         g = 0;         silver.push_back(i);       } else         g++;     }     if (g) gold.push_back(g);     if (g > mxg) mxg = g;     for (int i = 0; i < silver.size(); i++) {       if (i == gold.size() - 1) break;       if (gold[i] > 0 && gold[i + 1]) {         int g = gold[i] + gold[i + 1];         bool found = 0;         for (int p = 0; p < gold.size(); p++) {           if (p != i && p != i + 1 && gold[p] > 0) {             found = true;             break;           }         }         if (found) g++;         if (g > mxg) mxg = g;       } else if (gold[i] > 0) {         int g = gold[i];         bool found = 0;         for (int p = 0; p < gold.size(); p++) {           if (p != i && gold[p] > 0) {             found = true;             break;           }         }         if (found) g++;         if (g > mxg) mxg = g;       } else if (gold[i + 1] > 0) {         int g = gold[i + 1];         bool found = 0;         for (int p = 0; p < gold.size(); p++) {           if (p != i + 1 && gold[p] > 0) {             found = true;             break;           }         }         if (found) g++;         if (g > mxg) mxg = g;       }     }     cout << mxg << endl;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 1e5 + 10;   const long long MAXL = 20;   long long n, m;   long long v[MAXN], s[MAXN];   long long rmq[MAXL][MAXN];   long long resp[MAXN];   long long best[MAXN];   void buildRMQ() {     for (long long k = 0; k < n; k++) rmq[0][k] = s[k];     for (long long i = 1; i < MAXL; i++)       for (long long k = 0; k < MAXN; k++)         if (k + (1 << i) <= n)           rmq[i][k] = max(rmq[i - 1][k], rmq[i - 1][k + (1 << i - 1)]);   }   long long RMQ(long long a, long long b) {     long long size = b - a + 1;     long long log = 31 - __builtin_clz(size);     return max(rmq[log][a], rmq[log][b - (1 << log) + 1]);   }   long long bs(long long ini, long long fim, long long pos) {     while (ini < fim) {       long long mid = (ini + fim + 1) / 2;       if (RMQ(mid, pos - 1) >= s[pos])         ini = mid;       else         fim = mid - 1;     }     return ini;   }   int main() {     scanf( %lld %lld , &n, &m);     for (long long i = 0; i < n; i++) scanf( %lld , &v[i]);     s[0] = 0;     for (long long i = 1; i < n; i++) s[i] = abs(v[i] - v[i - 1]);     buildRMQ();     while (m--) {       long long p, q;       scanf( %lld %lld , &p, &q);       p--;       q--;       long long ans = 0;       stack<pair<long long, long long> > st;       for (long long i = p; i <= q; i++) {         while (st.size() && st.top().first <= s[i]) st.pop();         if (st.size())           best[i] = st.top().second;         else           best[i] = 0;         st.push(pair<long long, long long>(s[i], i));       }       for (long long i = p; i <= q; i++) {         resp[i] = 0;         long long bt = best[i];         if (s[bt] >= s[i]) {           resp[i] = s[i] * (i - bt) + resp[bt];         } else {           resp[i] = s[i] * (i - p);         }         ans += resp[i];       }       printf( %lld n , ans);     }   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 201;   const long long K = 201;   const long long X = 201;   long long dp[N][X];   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n, k, x;     cin >> n >> k >> x;     vector<long long> picBeauty;     picBeauty.push_back(-1);     for (long long i = 1; i <= n; i++) {       long long x;       cin >> x;       picBeauty.push_back(x);     }     long long minimumPosts = n / k;     if (x < minimumPosts) {       cout << -1 << endl;       return 0;     }     for (long long i = 0; i <= n; i++) {       for (long long j = 0; j <= x; j++) {         dp[i][j] = 0;       }     }     for (long long i = 1; i <= k; i++) {       dp[i][1] = picBeauty[i];     }     long long flag = 0;     for (long long i = 1; i <= n; i++) {       for (long long j = 2; j <= i && j <= x; j++) {         long long tempFlag = 0;         long long backAllowed = i - k;         for (long long y = i - 1; y >= 1 && y >= backAllowed; y--) {           if (dp[y][j - 1] > 0)             dp[i][j] = max(dp[i][j], dp[y][j - 1] + picBeauty[i]);         }       }     }     long long ans = 0;     for (long long i = n - k + 1; i <= n; i++) {       ans = max(ans, dp[i][x]);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Q {     int l, r, id;     bool operator<(const Q a) const {       if (l == a.l) return r < a.r;       return l < a.l;     }   } q[400003];   int a[400003], b[400003];   int pos[400003];   int m, sum[400003];   int lb(int k) { return k & -k; }   void add(int k, int v) {     while (k <= m) {       sum[k] += v;       k += lb(k);     }   }   int s(int k) {     int re = 0;     while (k) {       re += sum[k];       k -= lb(k);     }     return re;   }   int L[400003];   int main() {     int i, j, n;     while (~scanf( %d , &n)) {       memset(sum, 0, sizeof(sum));       m = 0;       for (i = 0; i < n; i++) {         scanf( %d%d , &q[i].l, &q[i].r);         q[i].id = i;         b[m++] = q[i].l, b[m++] = q[i].r;       }       sort(b, b + m);       m = unique(b, b + m) - b;       for (i = 0; i < n; i++) {         int id = lower_bound(b, b + m, q[i].r) - b + 1;         add(id, 1);       }       sort(q, q + n);       int ans = 0;       for (i = 0; i < n; i++) {         int l = lower_bound(b, b + m, q[i].l) - b + 1;         int r = lower_bound(b, b + m, q[i].r) - b + 1;         add(r, -1);         ans = s(r) - s(l - 1);         L[q[i].id] = ans;       }       for (i = 0; i < n; i++) {         printf( %d n , L[i]);       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string s;       cin >> s;       string w;       for (int i = 0; i <= 2 * n - 2; i = i + 2) {         w = w + s[i];       }       cout << w << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > p;   int main() {     int tc;     cin >> tc;     while (tc--) {       p.clear();       int n, a, b;       cin >> n;       bool value = true;       for (int i = 0; i < n; ++i) {         cin >> a >> b;         if (a < b) value = false;         if (p.empty())           p.push_back({a, b});         else {           if (a < p.back().first || b < p.back().second ||               a - p.back().first < b - p.back().second)             value = false;           p.push_back({a, b});         }       }       if (value)         cout <<  YES n ;       else         cout <<  NO n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 2e3 + 5;   const long long MOD = 1e9 + 7;   long long nCr[N][N];   void precompute() {     nCr[0][0] = 1;     for (long long i = 1; i < N; i++) {       nCr[i][0] = 1;       for (long long j = 1; j <= i; j++) {         nCr[i][j] = nCr[i - 1][j - 1] + nCr[i - 1][j];         nCr[i][j] %= MOD;       }     }   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     precompute();     long long n, m, k;     cin >> n >> m >> k;     long long ans = nCr[n - 1][2 * k] * nCr[m - 1][2 * k];     ans %= MOD;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a[110][110], i, j, maxi, f, maxid[110], c = 0, poi;     scanf( %d%d , &n, &m);     for (i = 0; i < m; i++) {       for (j = 0; j < n; j++) scanf( %d , &a[i][j]);     }     for (i = 0; i < m; i++) {       maxi = a[i][0];       maxid[i] = 0;       for (j = 0; j < n; j++) {         if (maxi < a[i][j]) {           maxi = a[i][j];           maxid[i] = j;         }       }     }     f = 0;     sort(maxid, maxid + m);     poi = maxid[0];     for (i = 1; i < m; i++) {       if (maxid[i] != maxid[i - 1]) {         if (f < c) {           f = c;           poi = maxid[i - 1];         }         c = 0;       } else         c++;     }     printf( %d n , poi + 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const double eps = 1e-11;   template <class T>   T sqr(const T &x) {     return x * x;   }   template <class T>   T lowbit(const T &x) {     return (x ^ (x - 1)) & x;   }   inline long long mlog2(long long n) {     long long c = 0;     c += 64 - __builtin_clzll(n);     return c - 1;   }   void fun(int a, int b) {     for (b = 0; b < 5; b++) a++;   }   bool arr[15][15] = {0};   int k;   bool check(int a, int b, int c, int d) {     int i, j, tp = 0;     for (i = a; i <= b; i++)       for (j = c; j <= d; j++) {         if (arr[i][j] == 1) tp++;         if (tp >= k) return 1;       }     return 0;   }   int main() {     int r, c, n, i, j, p, q, cou = 0, x, y;     cin >> r >> c >> n >> k;     for (i = 0; i < n; i++) {       cin >> x >> y;       x--;       y--;       arr[x][y] = 1;     }     for (i = 0; i < r; i++) {       for (j = i; j < r; j++) {         for (p = 0; p < c; p++) {           for (q = p; q < c; q++) {             if (check(i, j, p, q)) cou++;           }         }       }     }     cout << cou;   }
#include <bits/stdc++.h>   using namespace std;   string st;   long ans, tans, n, ttl, qpos;   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     cin >> st;     for (int i = 0; i < st.size(); i++)       if (st[i] ==  H ) ++ttl;     ans = 1000000;     for (int stp = 0; stp < n; stp++) {       tans = 0;       qpos = stp;       for (int i = 1; i <= ttl; i++) {         if (st[qpos] ==  T ) ++tans;         ++qpos;         if (qpos == n) qpos = 0;       }       ans = min(ans, tans);     }     cout << ans << endl;     cin.get();     cin.get();     return 0;   }
#include <bits/stdc++.h>   const int N = 60;   const int M = 11;   const int MOD = 1e9 + 7;   int binom[N + 1][N + 1], n, a[N], parent[N], inDegree[N], v0[N], v1[N], msk1[N],       dp[N + 1][1 << M];   int findRoot(int u) {     if (parent[u] != u) {       parent[u] = findRoot(parent[u]);     }     return parent[u];   }   void add(int &x, int a) {     x += a;     if (x >= MOD) {       x -= MOD;     }   }   int solve(int n0, int n1) {     assert(n0 <= M);     for (int j = 0; j < n1; ++j) {       msk1[j] = 0;     }     for (int i = 0; i < n0; ++i) {       for (int j = 0; j < n1; ++j) {         if (v1[j] % v0[i] == 0) {           msk1[j] |= 1 << i;         }       }     }     for (int i = 1; i <= n1; ++i) {       for (int msk = 0; msk < 1 << n0; ++msk) {         dp[i][msk] = 0;       }     }     for (int i = 0; i < n1; ++i) {       add(dp[1][msk1[i]], 1);     }     for (int i = 1; i < n1; ++i) {       for (int msk = 0; msk < 1 << n0; ++msk) {         if (dp[i][msk]) {           int cnt = 0;           for (int j = 0; j < n1; ++j) {             if ((msk1[j] & msk) == msk1[j]) {               cnt++;             } else if (msk1[j] & msk) {               add(dp[i + 1][msk | msk1[j]], dp[i][msk]);             }           }           if (i < cnt) {             add(dp[i + 1][msk], 1LL * (cnt - i) * dp[i][msk] % MOD);           }         }       }     }     return dp[n1][(1 << n0) - 1];   }   int main() {     for (int i = 0; i <= N; ++i) {       binom[i][0] = 1;       for (int j = 1; j <= i; ++j) {         binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % MOD;       }     }     while (scanf( %d , &n) == 1) {       for (int i = 0; i < n; ++i) {         scanf( %d , a + i);         parent[i] = i;         inDegree[i] = 0;       }       for (int i = 0; i < n; ++i) {         for (int j = 0; j < n; ++j) {           if (i != j && a[j] % a[i] == 0) {             if (findRoot(i) != findRoot(j)) {               parent[findRoot(i)] = findRoot(j);             }             inDegree[j]++;           }         }       }       int result = 1, size = 0;       for (int group = 0; group < n; ++group) {         if (parent[group] == group) {           int n0 = 0, n1 = 0;           for (int i = 0; i < n; ++i) {             if (findRoot(i) == group) {               if (inDegree[i]) {                 v1[n1++] = a[i];               } else {                 v0[n0++] = a[i];               }             }           }           if (n1) {             size += n1 - 1;             result =                 1LL * result * binom[size][n1 - 1] % MOD * solve(n0, n1) % MOD;           }         }       }       printf( %d n , result);     }   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2010;   const long long mod = 998244353;   long long pw(long long a, long long b) {     if (b == 0) return 1;     long long k = pw(a, b / 2);     k = (k * k) % mod;     if (b & 1) k = (k * a) % mod;     return k;   }   long long n, m;   long long dp[maxn][maxn], cnt[maxn], f[maxn][maxn];   string s;   signed main() {     s +=  . ;     string ss;     cin >> ss;     n = ss.size();     s += ss;     for (long long i = 1; i <= n; i++) cnt[i] = cnt[i - 1] + (s[i] ==  ? );     for (long long t = 2; t <= n; t++)       for (long long l = 1; l + t - 1 <= n; l++) {         long long r = l + t - 1;         if (t == 2) f[l][r] = (s[l] !=  ) );         if (s[l] ==  ) ) {           dp[l][r] = dp[l + 1][r];           f[l][r] = f[l + 1][r];           continue;         }         if (s[r] ==  ( ) {           dp[l][r] = dp[l][r - 1];           continue;         }         if (t == 2) {           dp[l][r] = 1;           continue;         }         long long K = pw(2, cnt[r - 1] - cnt[l]);         f[l][r] = K + dp[l + 1][r - 1];         if (s[l] ==  ? ) f[l][r] += f[l + 1][r];         f[l][r] %= mod;         if (s[l] ==  (  && s[r] ==  ) ) dp[l][r] = (dp[l + 1][r - 1] + K) % mod;         if (s[l] ==  ?  && s[r] ==  ) )           dp[l][r] = (dp[l + 1][r] + dp[l + 1][r - 1] + K) % mod;         if (s[l] ==  (  && s[r] ==  ? )           dp[l][r] = (dp[l][r - 1] + dp[l + 1][r - 1] + K) % mod;         if (s[l] ==  ?  && s[r] ==  ? )           dp[l][r] = (dp[l][r - 1] + dp[l + 1][r] + K) % mod;       }     cout << dp[1][n];   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 1e5 + 10;   const int MaxL = 18;   const int INF = 1e9;   const int MOD = 1e9 + 7;   int up[MaxN][MaxL], ds[MaxN];   vector<int> graph[MaxN];   int n, m, sz[MaxN];   long long sum[MaxN], ups[MaxN];   void dfsLca(int v, int p) {     up[v][0] = p;     for (int i = 1; i < MaxL; ++i) {       up[v][i] = up[up[v][i - 1]][i - 1];     }     sz[v] = 1;     for (int i = 0; i < (int)graph[v].size(); ++i) {       int to = graph[v][i];       if (to == p) {         continue;       }       ds[to] = ds[v] + 1;       dfsLca(to, v);       sz[v] += sz[to];       sum[v] += sz[to] + sum[to];     }   }   void dfsUp(int v, int p) {     if (p != -1) {       ups[v] = (sum[p] - (sum[v] + sz[v]) + ups[p]) + (n - sz[v]);     }     for (int i = 0; i < (int)graph[v].size(); ++i) {       int to = graph[v][i];       if (to == p) {         continue;       }       dfsUp(to, v);     }   }   int getLca(int x, int y) {     if (ds[x] > ds[y]) {       swap(x, y);     }     for (int i = MaxL - 1; i >= 0; --i) {       if (ds[up[y][i]] >= ds[x]) {         y = up[y][i];       }     }     if (x == y) {       return x;     }     for (int i = MaxL - 1; i >= 0; --i) {       if (up[x][i] != up[y][i]) {         x = up[x][i];         y = up[y][i];       }     }     return up[x][0];   }   int getKth(int x, int k) {     for (int i = MaxL - 1; i >= 0; --i) {       if (k & (1 << i)) {         x = up[x][i];       }     }     return x;   }   double solve(int x, int y) {     int l = getLca(x, y);     int len = ds[x] + ds[y] - 2 * ds[l];     double ans = 0;     if (x != l && y != l) {       long long total = 1LL * sz[x] * sz[y];       long long cnt =           1LL * sz[x] * sum[y] + 1LL * sz[y] * sum[x] + 1LL * total * len + total;       ans = 1.0 * cnt / total;     } else {       if (ds[x] > ds[y]) {         swap(x, y);       }       int xy = getKth(y, len - 1);       long long total = 1LL * (n - sz[xy]) * sz[y];       long long cnt = 1LL * (n - sz[xy]) * sum[y] +                       1LL * (ups[xy] - (n - sz[xy])) * sz[y] + 1LL * total * len +                       total;       ans = 1.0 * cnt / total;     }     return ans;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n - 1; ++i) {       int x, y;       scanf( %d%d , &x, &y);       x--;       y--;       graph[x].push_back(y);       graph[y].push_back(x);     }     dfsLca(0, 0);     dfsUp(0, -1);     for (int i = 0; i < m; ++i) {       int x, y;       scanf( %d%d , &x, &y);       x--;       y--;       printf( %.9lf n , solve(x, y));     }     return 0;   }