malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long int md = 1e9 + 7;   long long int __gcd(long long int a, long long int b) {     if (b == 0) return a;     return __gcd(b, a % b);   }   long long int poww(long long int a, long long int b) {     long long int res = 1;     while (b) {       if (b & 1) {         res *= a;         res %= md;       }       a *= a;       a %= md;       b >>= 1;     }     return res;   }   void ainp(long long int arr[], long long int n) {     for (long long int i = 1; i <= n; i++) cin >> arr[i];   }   const long long int N = 2e5 + 5;   void solve() {     long long int n;     cin >> n;     long long int t = n;     vector<long long int> bits;     while (n) {       bits.push_back(n % 3);       n /= 3;     }     bits.push_back(0);     long long int cnt2 = -1;     for (long long int i = 0; i < bits.size() - 1; i++) {       if (bits[i] == 2) {         cnt2 = i;       }     }     if (cnt2 == -1)       cout << t <<   n ;     else {       for (long long int i = 0; i < cnt2; i++) {         bits[i] = 0;       }       for (long long int i = cnt2; i < bits.size() - 1; i++) {         if (bits[i] == 2) {           bits[i] = 0;           bits[i + 1] += 1;         }       }       long long int ans = 0;       for (long long int i = 0; i < bits.size(); i++) {         ans += bits[i] * (long long int)pow(3, i);       }       cout << ans <<   n ;     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int t;     cin >> t;     while (t-- > 0) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, g = 0;     cin >> t;     for (int i = 0; i < t; i++) {       vector<int> p;       int n, x;       g = 0;       cin >> n;       if (n == 1) {         cin >> x;         cout <<  YES  << endl;         continue;       }       for (int l = 0; l < n; l++) {         int k;         cin >> k;         p.push_back(k);       }       sort(p.begin(), p.end());       for (int j = 0; j < p.size() - 1; j++) {         if (abs(p[j] - p[j + 1]) > 1) {           cout <<  NO  << endl;           g = 1;           break;         }       }       if (g == 0) cout <<  YES  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int n;     bool check, lucky;     string s;     cin >> n;     for (int i = 4; i < n + 1; i++) {       check = false;       s = to_string(i);       for (int j = 0; j < s.size(); j++)         if ((s[j] ==  4 ) || (s[j] ==  7 ))           check = true;         else {           check = false;           break;         }       if (check)         if (n % i == 0) {           lucky = true;           break;         } else {           lucky = false;         }     }     if (lucky)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve(int d) {     cout << fixed << setprecision(9);     if (d == 0) {       cout <<  Y   << 0.0 <<     << 0.0;     } else if (d >= 1 && d < 4)       cout <<  N ;     else {       double D = sqrt(d * (d - 4));       double a = (d + D) / 2.0;       double b = (d - D) / 2.0;       cout <<  Y   << a <<     << b;     }   }   int main() {     int t;     cin >> t;     while (t--) {       int d;       cin >> d;       solve(d);       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       vector<int> a(n, 0);       for (int i = 0; i < n; i++) scanf( %d , &a[i]);       int m;       scanf( %d , &m);       vector<pair<int, int> > h(m, {0, 0});       for (int i = 0; i < m; i++) scanf( %d%d , &h[i].first, &h[i].second);       sort(h.begin(), h.end());       vector<int> suf(m, 0);       suf[m - 1] = h[m - 1].second;       for (int i = m - 2; i >= 0; i--) suf[i] = max(suf[i + 1], h[i].second);       int ans = 0;       int bad = 0;       for (int i = 0; i < n;) {         int j = i;         int mx = 0;         while (j < n) {           mx = max(mx, a[j]);           int p = lower_bound(h.begin(), h.end(), make_pair(mx, -1)) - h.begin();           if (p < m && suf[p] >= j - i + 1)             j++;           else             break;         }         if (j <= i) {           bad = 1;           break;         }         ans++;         i = j;       }       if (bad)         printf( -1 n );       else         printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int arr[n + 1];       int i, s = 0;       for (i = 0; i < n; i++) {         cin >> arr[i];       }       int j;       for (i = 1; i < n - 1; i++) {         if (arr[i - 1] < arr[i] && arr[i + 1] < arr[i]) {           cout <<  YES  << endl << i <<     << i + 1 <<     << i + 2 << endl;           s = 1;           break;         }       }       if (s != 1) cout <<  NO  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 17;   int N, root[maxn], sz[maxn];   long long minv, maxv, val[maxn];   vector<int> tree[maxn];   vector<pair<long long, int> > nodes;   int find(int x) {     if (root[x] == x) return x;     return root[x] = find(root[x]);   }   long long merge(int first, int second) {     int x = find(first), y = find(second);     if (x == y) return 0LL;     long long res = 1LL * sz[x] * sz[y] * val[first];     sz[x] += sz[y];     root[y] = x;     return res;   }   int main() {     scanf( %d , &N);     for (int i = 1; i <= N; i++)       scanf( %I64d , val + i), nodes.push_back(make_pair(val[i], i));     for (int i = 1; i < N; i++) {       int A, B;       scanf( %d %d , &A, &B);       tree[A].push_back(B);       tree[B].push_back(A);     }     sort(nodes.begin(), nodes.end());     for (int i = 0; i <= N; i++) root[i] = i, sz[i] = 1;     for (int i = 0; i < N; i++)       for (int v : tree[nodes[i].second])         if (val[nodes[i].second] >= val[v]) maxv += merge(nodes[i].second, v);     for (int i = 0; i <= N; i++) root[i] = i, sz[i] = 1;     for (int i = N - 1; i >= 0; i--)       for (int v : tree[nodes[i].second])         if (val[nodes[i].second] <= val[v]) minv += merge(nodes[i].second, v);     printf( %I64d n , maxv - minv);     return 0;   }
/**     *  Author: dhruv_gheewala     *  Problem: A. Spy Detected!     *  Date:   12.04.2021     *  Time:   00:19:53   **/      #include  bits/stdc++.h    using namespace std;      #ifndef DEBUG      #define fundri 108   #define debug(...) 1729   #define endl   n       #endif      #define int int64_t      typedef pair<int, int> pii;   typedef vector<int> vi;      mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);}      bool in_range(int x, int l, int r) {return l <= x && x <= r;}   template<typename H, typename ...T>void inp(H &head) {cin >> head;}   template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);}   template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;}   template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;}      // Multi-Dimension Vector   // Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type())   template<int D, typename T> struct vec : public vector<vec<D - 1, T>> {       static_assert(D >= 1,  Vector dimensions must be greater than zero !! );       template<typename... Args>       vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)){}   };      template<typename T> struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val){}};      const int inf = 1e15;   const bool testcases = true;      const bool ready = []() -> bool {       ios_base::sync_with_stdio(false);       cin.tie(nullptr);      #ifdef DHRUV_GHEEWALA       freopen( in.txt ,  r , stdin);       freopen( out.txt ,  w , stdout);       freopen( debug.txt ,  w , stderr);   #endif          return true;   }();      void solve(int tc);   int32_t main(int32_t argc, char **argv) {       int TC = 1;       if(testcases) cin >> TC;          // Pre-Processing goes here...       for(int tc = 1; tc <= TC; ++tc) {           solve(tc);           fundri;       }       exit(0);   }      void solve(int tc) {       int n;       cin >> n;          vector<int> a(n);       cin >> a;          map<int, int> mp;       for(int x: a)           mp[x]++;          for(int i = 0; i < n; i++) {           if(mp[a[i]] == 1) {               cout << i + 1 << endl;               return;           }       }   }
#include <bits/stdc++.h>   using namespace std;   int n, arr[110], ans[110];   int primes[17] = {2,  3,  5,  7,  11, 13, 17, 19, 23,                     29, 31, 37, 41, 43, 47, 53, 59};   int fact[65];   int memo[110][1 << 17];   int find_min_cost(int ind, int mask) {     if (ind >= n) return 0;     if (memo[ind][mask] != -1) {       return memo[ind][mask];     }     memo[ind][mask] = 1 << 30;     for (int i = 1; i < 2 * arr[ind]; i++) {       if ((mask & fact[i]) == 0) {         memo[ind][mask] =             min(memo[ind][mask],                 abs(i - arr[ind]) + find_min_cost(ind + 1, (mask | fact[i])));       }     }     return memo[ind][mask];   }   void compute_ans(int ind, int mask, int min_cost) {     if (ind >= n) return;     for (int i = 1; i < 2 * arr[ind]; i++) {       if (((mask & fact[i]) == 0) and           (min_cost == abs(i - arr[ind]) + memo[ind + 1][(mask | fact[i])])) {         ans[ind] = i;         compute_ans(ind + 1, (mask | fact[i]), memo[ind + 1][(mask | fact[i])]);         return;       }     }   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> arr[i];     memset(fact, 0, sizeof(fact));     for (int i = 2; i <= 62; i++) {       for (int j = 0; j < 17; j++) {         if ((i % primes[j]) == 0) fact[i] = (fact[i] | (1 << j));       }     }     for (int i = 0; i < n; i++)       for (int j = 0; j < (1 << 17); j++) memo[i][j] = -1;     for (int i = 0; i < (1 << 17); i++) memo[n][i] = 0;     int min_cost = find_min_cost(0, 0);     compute_ans(0, 0, min_cost);     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int a;       cin >> a;       int k = a;       while (a--) {         cout << 1 <<    ;       }       cout << endl;     }   }
#include <bits/stdc++.h>   using namespace std;      int dx[4] = {0, 0, 1, -1};   int dy[4] = {-1, 1, 0, 0};      int lredge[600][599]; //n, m-1   int tdedge[599][600]; //n-1, m   int n, m, k;      int distance(int i1, int j1, int i2, int j2){    //next cell is above    if(i1 > i2){     return tdedge[i2][j1];    }    //next cell is below    else if(i2 > i1){     return tdedge[i1][j1];    }    //next cell is to the left    else if(j1 > j2){     return lredge[i1][j2];    }    //next cell is to the right    else if(j2 > j1){     return lredge[i1][j1];    }       assert(1 == 0);   }      int dp[510][510][25];      int32_t main(){    cin >> n >> m >> k;       for(int i=0; i<510; i++)     for(int j=0; j<510; j++)      for(int k=1; k<25; k++)       dp[i][j][k] = 1000000000;       for(int i=0; i<n; i++){     for(int j=0; j<m-1; j++){      cin >> lredge[i][j];     }    }       for(int i=0; i<n-1; i++){     for(int j=0; j<m; j++){      cin >> tdedge[i][j];     }    }       if(k%2 == 1){     for(int i=0; i<n; i++){      for(int j=0; j<m; j++)       cout << -1 <<    ;      cout <<   n ;     }     return 0;    }       // for(int i=0; i<n; i++){    //  for(int j=0; j<m; j++){    //   for(int l=0; l<4; l++){    //    int newx = i + dx[l];    //    int newy = j + dy[l];    //    if(0 <= newx && newx <= n-1 && 0 <= newy && newy <= m-1){    //     dp[i][j][1] = min(dp[i][j][1], distance(i, j, newx, newy));    //    }    //   }    //  }    // }       // for(int i=0; i<n; i++){    //  for(int j=0; j<m; j++){    //   cout << dp[i][j][1] <<    ;    //  }    //  cout <<   n ;    // }       // for(int i=0; i<n; i++){    //  for(int j=0; j<m; j++){    //   cout << dp[i][j][0] <<    ;    //  }    //  cout <<   n ;    // }       for(int l=2; l<=k; l+=2){     for(int i=0; i<n; i++){      for(int j=0; j<m; j++){       for(int r=0; r<4; r++){        int newx = i + dx[r];        int newy = j + dy[r];        if(0 <= newx && newx <= n-1 && 0 <= newy && newy <= m-1){         dp[i][j][l] = min(dp[i][j][l], dp[newx][newy][l-2]+2*distance(i, j, newx, newy));        }       }      }     }        // debug( AA );     // for(int i=0; i<n; i++){     // for(int j=0; j<m; j++){     //  cout << dp[i][j][l] <<    ;     // }     // cout <<   n ;    // }    // cout <<   n ;    }       for(int i=0; i<n; i++){     for(int j=0; j<m; j++){      cout << dp[i][j][k] <<    ;     }     cout <<   n ;    }   }
#include <bits/stdc++.h>   long long sum = 0, dd = 0;   int N, R, V[(1 << 18) + 233];   int main() {     scanf( %d%d , &N, &R);     dd = 1 << N;     for (int i = 0; i < dd; ++i) {       scanf( %d , V + i);       sum += V[i];     }     printf( %.10lf n , sum / (double)dd);     for (int i = 0; i < R; ++i) {       int z, g;       scanf( %d%d , &z, &g);       sum -= V[z];       V[z] = g;       sum += g;       printf( %.10lf n , sum / (double)dd);     }   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, q;     cin >> n >> q;     std::vector<vector<int>> v;     v.resize(2, vector<int>(n, 0));     int ans = 0;     while (q--) {       int change = 0;       int x, y;       cin >> x >> y;       x--;       y--;       if (v[x][y] == 0)         change++;       else         change--;       v[x][y] = 1 - v[x][y];       for (int i = -1; i <= 1; i++) {         if (y + i >= n && y + i <= 0) continue;         if (v[1 - x][y + i] == 1) ans += change;       }       if (ans == 0)         cout <<  YES n ;       else         cout <<  NO n ;     }   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   long long size(const T& x) {     return x.size();   }   template <class T>   T smod(T a, T b) {     return (a % b + b) % b;   }   long long n, k;   vector<long long> X;   vector<pair<long long, pair<long long, long long> > > A;   vector<vector<long long> > mem;   long long dp(int at, int chosen) {     if (at >= n) return 0;     if (mem[at][chosen] != -1) return mem[at][chosen];     long long mx = 0;     if (chosen < 2) {       mx = max(mx, A[at].first + dp(A[at].second.second + 1, chosen + 1));     }     mx = max(mx, dp(at + 1, chosen));     return mem[at][chosen] = mx;   }   void solve() {     cin >> n >> k;     mem = vector<vector<long long> >(n + 10, vector<long long>(3, -1));     X = vector<long long>(n);     A = vector<pair<long long, pair<long long, long long> > >(n);     for (long long i = (0); i < (n); ++i) {       cin >> X[i];     }     sort(X.begin(), X.end());     for (long long i = (0); i < (n); ++i) {       long long a;       cin >> a;     }     for (long long i = (0); i < (n); ++i) {       auto r = upper_bound(X.begin(), X.end(), X[i] + k);       int index = r - X.begin() - 1;       A[i] = pair<long long, pair<long long, long long> >(           index - i + 1, pair<long long, long long>(i, index));     }     cout << dp(0, 0) << endl;   }   int main() {     cin.sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     long long t;     cin >> t;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   int n, m, q, cnt, ans, w[13][100010], bi[20], g[13][100010], tot;   bitset<4096> f[2 * 100010];   int main() {     int i, s, a, b, j, k;     for (i = bi[0] = 1; i < 20; i++) bi[i] = bi[i - 1] << 1;     scanf( %d%d%d , &n, &m, &q);     tot = m;     for (i = 1; i <= m; i++) {       for (s = 1; s <= n; s++) scanf( %d , &w[i][s]);       for (s = 0; s < bi[m]; s++)         if (s & bi[i - 1]) f[i][s] = 1;     }     for (i = 1; i <= m; i++)       for (s = 1; s <= n; s++)         for (k = 1; k <= m; k++)           if (w[k][s] >= w[i][s]) g[i][s] |= bi[k - 1];     while (q--) {       scanf( %d%d%d , &k, &a, &b);       if (k == 1) f[++tot] = f[a] | f[b];       if (k == 2) f[++tot] = f[a] & f[b];       if (k == 3) {         ans = 0;         for (i = 1; i <= m; i++)           if (f[a][g[i][b]]) ans = max(ans, w[i][b]);         printf( %d n , ans);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000007;   int n, m;   int fa[200010];   struct Line {     int u, v, c;   } l[200010];   int p[200010];   bool cmp(int x, int y) { return l[x].c < l[y].c; }   bool intree[200010];   struct Edge {     int v, id, nxt;   } e[400010];   int tot;   int first[200010];   void build(int u, int v, int id) {     e[++tot] = (Edge){v, id, first[u]};     first[u] = tot;     return;   }   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   bool merge(int u, int v) {     u = find(u);     v = find(v);     if (u == v) return false;     fa[u] = v;     return true;   }   void init() {     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++) p[i] = i;     sort(p + 1, p + m + 1, cmp);     for (int i = 1; i <= m; i++) {       int x = p[i];       if (merge(l[x].u, l[x].v)) {         intree[x] = true;         build(l[x].u, l[x].v, x);         build(l[x].v, l[x].u, x);       }     }     return;   }   const int M = 20;   int f[200010][20];   int g[200010][20];   int h[200010][20];   int dep[200010];   int Id[200010];   void dfs(int u, int lst, int lste) {     Id[u] = lste;     dep[u] = dep[lst] + 1;     f[u][0] = lst;     g[u][0] = l[lste].c;     h[u][0] = INF;     for (int i = 1; i < M; i++) {       f[u][i] = f[f[u][i - 1]][i - 1];       g[u][i] = max(g[u][i - 1], g[f[u][i - 1]][i - 1]);       h[u][i] = INF;     }     for (int i = first[u]; i; i = e[i].nxt)       if (e[i].v != lst) dfs(e[i].v, u, e[i].id);     return;   }   int ans[200010];   void solve(int x) {     int u = l[x].u;     int v = l[x].v;     int c = l[x].c;     ans[x] = 0;     if (dep[u] < dep[v]) swap(u, v);     int need = dep[u] - dep[v], cur = 0;     while (need) {       if (need & 1) {         ans[x] = max(ans[x], g[u][cur]);         h[u][cur] = min(h[u][cur], c);         u = f[u][cur];       }       need >>= 1;       cur++;     }     if (u == v) return;     for (int i = M - 1; i >= 0; i--)       if (f[u][i] != f[v][i]) {         ans[x] = max(ans[x], g[u][i]);         ans[x] = max(ans[x], g[v][i]);         h[u][i] = min(h[u][i], c);         h[v][i] = min(h[v][i], c);         u = f[u][i];         v = f[v][i];       }     ans[x] = max(ans[x], g[u][0]);     ans[x] = max(ans[x], g[v][0]);     h[u][0] = min(h[u][0], c);     h[v][0] = min(h[v][0], c);     return;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) scanf( %d %d %d , &l[i].u, &l[i].v, &l[i].c);     init();     dfs(1, 0, 0);     for (int i = 1; i <= m; i++)       if (!intree[i]) solve(i);     for (int i = M - 1; i > 0; i--)       for (int u = 1; u <= n; u++) {         h[u][i - 1] = min(h[u][i - 1], h[u][i]);         h[f[u][i - 1]][i - 1] = min(h[f[u][i - 1]][i - 1], h[u][i]);       }     for (int u = 2; u <= n; u++) ans[Id[u]] = h[u][0];     for (int i = 1; i <= m; i++)       if (ans[i] != INF)         printf( %d  , ans[i] - 1);       else         printf( -1  );     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> e[400010], b[400010];   int ll[400010], rr[400010], son[400010], No, an[400010], now;   void dfs0(int x, int last) {     son[x] = 1;     ll[x] = No++;     for (int i = 0; i < e[x].size(); i++) {       if (e[x][i] == last) continue;       dfs0(e[x][i], x);       son[x] += son[e[x][i]];     }     rr[x] = No++;   }   int ac[400010 << 1], ha[400010 << 1];   void insert(int d[], int id, int L, int R, int L2, int R2, int v) {     if (L2 <= L && R <= R2) {       d[id] += v;       return;     }     int M = (L + R) >> 1;     if (R2 <= M)       insert(d, id << 1, L, M, L2, R2, v);     else if (M < L2)       insert(d, (id << 1) | 1, M + 1, R, L2, R2, v);     else {       insert(d, id << 1, L, M, L2, R2, v);       insert(d, (id << 1) | 1, M + 1, R, L2, R2, v);     }   }   int qq(int d[], int id, int L, int R, int x) {     if (L == x && x == R) {       return d[id];     }     int M = (L + R) >> 1;     if (x <= M)       return d[id] + qq(d, id << 1, L, M, x);     else       return d[id] + qq(d, (id << 1) | 1, M + 1, R, x);   }   int qq2(int d[], int id, int L, int R, int L2, int R2) {     if (L2 <= L && R <= R2) {       return d[id];     }     int M = (L + R) >> 1;     if (R2 <= M)       return qq2(d, id << 1, L, M, L2, R2);     else if (M < L2)       return qq2(d, (id << 1) | 1, M + 1, R, L2, R2);     else       return qq2(d, id << 1, L, M, L2, R2) +              qq2(d, (id << 1) | 1, M + 1, R, L2, R2);   }   void insert2(int d[], int id, int L, int R, int x, int v) {     d[id] += v;     if (L == x && x == R) return;     int M = (L + R) >> 1;     if (x <= M)       insert2(d, id << 1, L, M, x, v);     else       insert2(d, (id << 1) | 1, M + 1, R, x, v);   }   void dfs(int x, int last) {     vector<int> tmp, tmp2;     int i;     for (i = 0; i < b[x].size(); i++) {       if (qq(ac, 1, 1, No, ll[b[x][i]]))         continue;       else {         tmp.push_back(b[x][i]);         insert(ac, 1, 1, No, ll[b[x][i]], rr[b[x][i]], 1);         int get_v = qq2(ha, 1, 1, No, ll[b[x][i]], rr[b[x][i]]);         insert2(ha, 1, 1, No, ll[b[x][i]], son[b[x][i]] - get_v);         tmp2.push_back(son[b[x][i]] - get_v);         now += son[b[x][i]] - get_v;       }     }     an[x] = now;     for (int i = 0; i < e[x].size(); i++) {       if (e[x][i] == last) continue;       dfs(e[x][i], x);     }     for (i = 0; i < tmp.size(); i++) {       insert(ac, 1, 1, No, ll[tmp[i]], rr[tmp[i]], -1);       insert2(ha, 1, 1, No, ll[tmp[i]], -tmp2[i]);       now -= tmp2[i];     }   }   int main() {     No = 1;     int i, j, k, n, m;     scanf( %d%d , &n, &m);     for (i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       e[x].push_back(y);       e[y].push_back(x);     }     for (i = 0; i < m; i++) {       int x, y;       scanf( %d%d , &x, &y);       b[x].push_back(x);       b[x].push_back(y);       b[y].push_back(x);       b[y].push_back(y);     }     dfs0(1, 0);     dfs(1, 0);     for (i = 1; i <= n; i++) {       if (i != 1) printf(   );       printf( %d , an[i] > 0 ? (an[i] - 1) : 0);     }     puts(  );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, v;     cin >> n >> v;     if (n >= v) {       long long t = (n - v) * (n - v + 1);       t /= 2;       t += v - 1;       cout << t;     } else {       cout << n - 1;     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     t = 1;     while (t--) solve();     return 0;   }