alexjercan/bugnet huggingface dataset:
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k, mod = 1e9 + 7;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  long long ans = 1;
  int l = 0, r = n - 1, sign = 1;
  if (k % 2 == 1) {
    ans = a[r];
    r--;
    if (ans < 0)
      sign = -1;
    k--;
  }
  while (k) {
    long long x = (a[l] * 1ll * a[l + 1]), y = (a[r - 1] * 1ll * a[r]);
    if (sign == 1) {
      if (x < y) {
        ans = y % mod * ans % mod;
        r -= 2;
      } else {
        ans = x % mod * ans % mod;
        l += 2;
      }
    } else {
      if (x > y) {
        ans = y % mod * ans % mod;
        r -= 2;
      } else {
        ans = x % mod * ans % mod;
        l += 2;
      }
    }
    k -= 2;
  }
  cout << (ans + mod) % mod << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k, mod = 1e9 + 7;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  long long ans = 1;
  if (a[n - 1] <= 0) {
    for (int i = n - 1; i >= n - k; i--) {
      ans *= a[i];
      ans %= mod;
    }
    cout << (ans + mod) % mod << endl;
  } else {
    sort(a.begin(), a.end(), [&](int a, int b) { return abs(a) > abs(b); });
    int negs = 0;
    for (int i = 0; i < k; i++) {
      if (a[i] < 0)
        negs++;
    }
    if (negs & 1) {
      int l_nega, l_posi;
      int f_nega, f_posi;
      for (int i = 0; i < k; i++) {
        if (a[i] < 0)
          l_nega = i;
        if (a[i] > 0)
          l_posi = i;
      }
      for (int i = n - 1; i > k; i--) {
        if (a[i] < 0)
          f_nega = i;
        if (a[i] > 0)
          f_posi = i;
      }
      if (a[f_posi] * 1ll * a[l_posi] >= a[f_nega] * 1ll * a[l_nega])
        swap(a[f_posi], a[l_nega]);
      else
        swap(a[l_posi], a[f_nega]);
    }
    for (int i = 0; i < k; i++) {
      ans *= a[i];
      ans %= mod;
    }
    cout << (ans + mod) % mod;
  }
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 1000000007;
priority_queue<int, vector<int>, greater<int>> que;
signed main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);

  for (int i = n - 1; i >= 0; i--)
    if (que.size() < k && a[i] > 0)
      que.push(a[i]);
  if ((que.size() == 0 && k % 2 == 1) || n == k) {
    int ans2 = 1;
    for (int i = 0; i < k; i++) {
      ans2 = (ans2 * (MOD + a[n - 1 - i]) % MOD) % MOD;
    }
    cout << ans2 << endl;
    return 0;
  }
  if (que.size() < k && (k - que.size()) % 2 == 1)
    que.pop();
  int x = 0;
  for (int i = 0; i * 2 + 1 < n; i++) {
    // cerr<<x<<' ';
    if (que.size() + x == k && que.size() <= 1)
      break;
    if (a[i * 2] <= 0 && a[i * 2 + 1] <= 0) {
      if (que.size() + x < k) {
        x += 2;

        continue;
      }
      int p = que.top();
      que.pop();
      if (p * que.top() < a[i * 2] * a[i * 2 + 1]) {
        que.pop();
        x += 2;
        continue;
      } else {
        que.push(p);
        break;
      }
    }
  }
  int ans = 1;
  for (int i = 0; i < x; i++)
    ans = (ans * (-1 * a[i])) % MOD;
  while (!que.empty()) {
    //  cerr<<que.top()<<endl;
    ans = (ans * que.top()) % MOD;
    que.pop();
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 1000000007;

signed main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);
  priority_queue<int, vector<int>, greater<int>> que;
  for (int i = n - 1; i >= 0; i--)
    if (que.size() < k && a[i] > 0)
      que.push(a[i]);
  if (que.size() == 0 && k % 2 == 1) {
    int ans2 = 1;
    for (int i = 0; i < k; i++) {
      ans2 = (ans2 * (MOD + a[n - 1 - i])) % MOD;
    }
    cout << ans2 << endl;
    return 0;
  }
  if (que.size() < k && (k - que.size()) % 2 == 1)
    que.pop();
  int x = 0;
  for (int i = 0; i * 2 + 1 < n; i++) {
    // cerr<<x<<' ';
    if (a[i * 2] <= 0 && a[i * 2 + 1] <= 0) {
      if (que.size() + x < k) {
        x += 2;

        continue;
      }
      int p = que.top();
      que.pop();
      if (p * que.top() < a[i * 2] * a[i * 2 + 1]) {
        que.pop();
        x += 2;
        continue;
      } else {
        que.push(p);
        break;
      }
    }
  }
  int ans = 1;
  for (int i = 0; i < x; i++)
    ans = (ans * (-1 * a[i])) % MOD;
  while (!que.empty()) {
    //  cerr<<que.top()<<endl;
    ans = (ans * que.top()) % MOD;
    que.pop();
  }
  cout << ans << endl;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  sort(A.begin(), A.end());

  ll ans = 1;
  int l = 0, r = N - 1;
  if (K % 2) {
    ans *= A[r];
    --r;
    --K;
    if (ans < 0) {
      while (K > 0) {
        ans = ans * A[r] % MOD;
        --r;
        --K;
      }
      cout << (MOD + ans) % MOD << endl;
      return 0;
    }
  }
  while (K > 0) {
    ll x = (ll)A[l] * A[l + 1];
    ll y = (ll)A[r] * A[r - 1];
    if (x > y) {
      ans = ans * (x % MOD) % MOD;
      l += 2;
    } else {
      ans = ans * (y % MOD) % MOD;
      r -= 2;
    }
    K -= 2;
  }
  cout << (MOD + ans) % MOD << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  sort(A.begin(), A.end(), greater<int>());

  ll ans = 1;
  int l = 0, r = N - 1;
  if (K % 2) {
    ans *= A[l];
    ++l;
    --K;
    if (ans < 0) {
      while (K > 0) {
        ans = ans * A[l] % MOD;
        ++l;
        --K;
      }
      cout << (MOD + ans) % MOD << endl;
      return 0;
    }
  }
  while (K > 0) {
    ll x = (ll)A[l] * A[l + 1];
    ll y = (ll)A[r] * A[r - 1];
    if (x > y) {
      ans = ans * (x % MOD) % MOD;
      l += 2;
    } else {
      ans = ans * (y % MOD) % MOD;
      r += 2;
    }
    K -= 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>

using namespace std;

int n;

int head[200010], to[400010], nxt[400010], cnt;

inline void AddEdge(int u, int v) {
  ++cnt, to[cnt] = v, nxt[cnt] = head[u], head[u] = cnt;
}

long long ans;

inline void Dfs(int u, int p) {
  for (int e = head[u]; e; e = nxt[e]) {
    int v = to[e];
    if (v == p)
      continue;
    if (v < u)
      ans += 1ll * v * (u - v);
    else
      ans += 1ll * (v - u) * (n - v + 1);
    Dfs(v, u);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    AddEdge(u, v), AddEdge(v, u);
  }

  Dfs(1, 0);

  printf("%lld\\n", ans + n);

  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int n;

int head[200010], to[400010], nxt[400010], cnt;

inline int AddEdge(int u, int v) {
  ++cnt, to[cnt] = v, nxt[cnt] = head[u], head[u] = cnt;
}

long long ans;

inline void Dfs(int u, int p) {
  for (int e = head[u]; e; e = nxt[e]) {
    int v = to[e];
    if (v == p)
      continue;
    if (v < u)
      ans += 1ll * v * (u - v);
    else
      ans += 1ll * (u - v) * (n - v + 1);
    Dfs(v, u);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    AddEdge(u, v), AddEdge(v, u);
  }

  Dfs(1, 0);

  printf("%lld\\n", ans);

  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;

int n;
long long ans = 0;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.in" , "r" , stdin) ;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);
    ans -= 1LL * u * (n - v + 1LL);
  }

  for (int i = 1; i <= n; i++) {
    ans += 1LL * (i * (n - i + 1LL));
  }
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;
int n;

vector<int> adj[N];
int up[N][20], mnt[N][20], mxt[N][20];
int hei[N];
int st[N], en[N];
int t;
long long ans = 0;

int dfs(int x, int p) {
  st[x] = t++;
  up[x][0] = p;
  hei[x] = hei[p] + 1;
  for (int i = 1; i < 20; i++) {
    up[x][i] = up[up[x][i - 1]][i - 1];
    mnt[x][i] = min(mnt[x][i - 1], mnt[up[x][i - 1]][i - 1]);
    mxt[x][i] = max(mnt[x][i - 1], mxt[up[x][i - 1]][i - 1]);
  }

  for (auto u : adj[x]) {
    if (u == p)
      continue;
    mnt[u][0] = mxt[u][0] = x;
    dfs(u, x);
  }
  en[x] = t++;
}

bool upper(int u, int v) { return (st[v] >= st[u] && en[v] <= en[u]); }

int lca(int u, int v) {
  if (upper(u, v))
    return u;
  if (upper(v, u))
    return v;
  for (int i = 19; i >= 0; i--) {
    if (!upper(up[u][i], v)) {
      u = up[u][i];
    }
  }
  return up[u][0];
}

int m = 1e9, M = 0;

void lift(int u, int v) {
  int dst = hei[u] - hei[v];
  for (int i = 0; i < 20; i++) {
    if (dst & (1 << i)) {
      m = min(m, mnt[u][i]);
      M = max(M, mxt[u][i]);
      u = up[u][i];
    }
  }
  return;
}

int getM(int u, int v) {
  int lc = lca(u, v);
  int ret = 1e9;
  m = 1e9;
  M = 0;
  lift(u, lc);
  lift(v, lc);
  m = min(m, min(u, v));
  M = max(M, max(u, v));
  ans -= m * (n - M + 1);
  // cout<< u<<" " << v <<" " << m <<" " << M <<" " << lc <<"...\\n" ;
  return M;
}

int main() {
  ios_base::sync_with_stdio(0);
  // freopen("in.in" , "r" , stdin) ;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(1, 1);

  for (int i = 1; i <= n; i++) {
    ans += i * (n - i + 1);
  }

  for (int i = 1; i < n; i++) {
    getM(i, i + 1);
  }

  cout << ans;

  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  rep(i, n) { ans += (n - i + 1) * (n - i) / 2; }
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);
    ans -= u * (n - v + 1);
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

void solve() {
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
    g[v].pb(u);
  }
  ll ans = 0;
  rep(i, n) {
    ll cnt = 0;
    REP(j, i, n) {
      ++cnt;
      for (auto x : g[j]) {
        if (x < j && x >= i) {
          --cnt;
        }
      }
      ans += cnt;
      //	cout<<i<<" "<<j<<" "<<cnt<<"\\n";
    }
  }
  cout << ans << "\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Runtime Error

/*input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define UQ(x) (x).resize(distance((x).begin(), unique(x.begin(), x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2) {
      ans *= a;
      ans %= (mod);
    }
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

void solve() {
  ll i, j, _;
  ll n;
  cin >> n;
  ll ans = 0, k;
  rep(i, 1, n + 1) ans += (n - i + 1) * i;
  rep(i, 0, n - 1) {
    cin >> j >> k;
    if (j > k)
      swap(j, k);
    ans -= (n - k + 1) * j;
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

/*input
3
1 3
2 3
*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define UQ(x) (x).resize(distance((x).begin(), unique(x.begin(), x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2) {
      ans *= a;
      ans %= (mod);
    }
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

ll who[200001];

ll find(ll i) {
  if (who[i] == i)
    return i;
  return who[i] = find(who[i]);
}

ll Union(ll i, ll j) {
  i = find(i), j = find(j);
  if (j > i)
    who[j] = i;
  else
    who[i] = j;
}

void solve() {
  ll i, j, _;
  ll n;
  cin >> n;
  rep(i, 0, n) who[i] = i;
  vpll v[n];
  ll k;
  ll ans = 0;
  rep(i, 0, n - 1) {
    cin >> j >> k;
    --j, --k;
    if (j > k)
      swap(j, k);
    v[k - j].pb({j, k});
  }
  k = 0;
  rep(i, 1, n + 1) ans += (n - i + 1) * i;
  rep(i, 1, n) {
    for (auto j : v[i]) {
      if (find(j.F) == find(j.S))
        continue;
      Union(j.F, j.S);
      k++;
    }
    ans -= k;
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int INF = 1e9;
const int S = 3e5;

vector<int> adj[S];
int n;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += i * (n - i + 1);
  }
  for (ll i = 0; i < n; i++) {
    for (ll it : adj[i]) {
      if (it < i) {
        ans -= (it + 1) * (n - i);
        // cout<<i+1<<" "<<it+1<<": "<<ans<<'\\n';
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int INF = 1e9;
const int S = 1e4;

vector<int> adj[S] = {{0}};
int n;

// int DSU[S];

// int find(int a){
// 	if (a == DSU[a]){
// 		return a;
// 	}
// 	return DSU[a] = find(DSU[a]);
// }

// int ccnt;
// void combine(int a, int b){
// 	a = find(a);
// 	b = find(b);
// 	if (a == b){
// 		return;
// 	}else{
// 		ccnt--;
// 		DSU[a] = b;
// 	}
// }
int cnt[S] = {0}; // cnt[i] = no. of neighbors less than i

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  // iota(DSU, DSU+S, 0);

  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    // adj[u][v] = 1;
    // adj[v][u] = 1;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int it : adj[i]) {
      // cout<<i+1<<' '<<it+1<<'\\n';
      if (it < i) {
        cnt[i]++;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    int ccnt = 0;
    // ans++;
    for (int j = i; j < n; j++) {
      ccnt++;
      ccnt -= cnt[j];
      ans += ccnt;
      // cout<<i+1<<' '<<j+1<<": "<<ccnt<<'\\n';
      // cout<<cnt[j]<<'\\n';
    }
    for (int it : adj[i]) {
      if (i < it) {
        cnt[it]--;
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 5;
const long long inf = 0x3f3f3f3f;
long long ans, u[maxn], v[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  long long n;
  cin >> n;
  for (long long i = 1; i < n; i++)
    cin >> u[i] >> v[i];
  for (long long i = 1; i <= n; i++)
    ans += i * (n - i + 1);
  for (long long i = 1; i <= n - 1; i++) {
    if (u[i] > v[i])
      swap(u[i], v[i]);
    ans -= u[i] * (n - v[i] + 1);
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int ans, u[maxn], v[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++)
    cin >> u[i] >> v[i];
  for (int i = 1; i <= n; i++)
    ans += i * (n - i);
  for (int i = 1; i <= n - 1; i++) {
    if (u[i] > v[i])
      swap(u[i], v[i]);
    ans -= u[i] * (n - v[i] + 1);
  }
  cout << ans << endl;
  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
using namespace std;
#define rep_1(i, m, n) for (int i = m; i <= n; i++)
#define mem(st) memset(st, 0, sizeof st)
inline int read() {
  int num = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    num = (num << 3) + (num << 1) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -num : num;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  //	for(ll i=0; i<=n; i++)
  //		ans+=(n-i)*(i+1);
  for (ll i = 1; i <= n; i++) {
    //		for(int j=i;j<=n;j++)
    //			ans+=j-i+1;
    ans += (i + n) * (n - i + 1) / 2 + (n - i + 1) - i * (n - i + 1);
  }
  for (ll i = 1; i <= n - 1; i++) {
    ll l, r;
    cin >> l >> r;
    if (l > r)
      swap(l, r);
    ans -= l * (n - r + 1);
  }
  cout << ans << endl;
}
signed main() {
  int t = 1;
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
using namespace std;
#define rep_1(i, m, n) for (int i = m; i <= n; i++)
#define mem(st) memset(st, 0, sizeof st)
inline int read() {
  int num = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    num = (num << 3) + (num << 1) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -num : num;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  //	for(ll i=0; i<=n; i++)
  //		ans+=(n-i)*(i+1);
  for (ll i = 1; i <= n; i++)
    for (int j = i; j <= n; j++)
      ans += j - i + 1;
  for (ll i = 1; i <= n - 1; i++) {
    ll l, r;
    cin >> l >> r;
    if (l > r)
      swap(l, r);
    ans -= l * (n - r + 1);
  }
  cout << ans << endl;
}
signed main() {
  int t = 1;
  while (t--)
    solve();
  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF 100000000000007
using namespace std;
using pii = pair<int, int>;
using LL = int64_t;

int main() {
  LL n;
  cin >> n;
  LL ans = 0;
  for (LL i = 1; i <= n; i++) { // 頂点iを含むsの数
    ans += i * (n - i + 1);
  }

  REP(i, n - 1) {
    LL u, v;
    cin >> u >> v;

    LL a = min(u, v);
    LL b = max(u, v);
    /*if(u>v){
        swap(u,v);
    }*/
    ans = ans - a * (n - b + 1);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF 100000000000007
using namespace std;
using pii = pair<int, int>;
using LL = int64_t;

int main() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) { // 頂点iを含むsの数
    for (int l = 1; l <= n; l++) {
      for (int r = l; r <= n; r++) { // s=l以上r以下
        if (l <= i && i <= r) {
          ans++;
        }
      }
    }
  }
  REP(i, n - 1) {
    int u, v;
    cin >> u >> v;
    int number = 0; // 辺uvを含むsの数
    for (int l = 1; l <= n; l++) {
      for (int r = l; r <= n; r++) { // s=l以上r以下
        if (l <= u && u <= r && l <= v && v <= r) {
          number++;
        }
      }
    }
    ans -= number;
  }

  cout << ans << endl;

  return 0;
}