Problem p02617 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define lowbit(x) ((x) & -(x))
#define lson l, mid, id << 1
#define rson mid + 1, r, id << 1 | 1
#define ls id << 1
#define rs id << 1 | 1
#define MID(l, r) ((l) + (((r) - (l)) >> 1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

using LL = long long;
// using LL = __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;

const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000;      // 1e5
const int e6 = 1000000;     // 1e6;
const int MOD = 1000000007; // 1e9 + 7
// const int MOD = 998244353;
// const int MOD = 911451407;

template <typename T = int> inline T read() {
  char c = '\\0';
  bool f = false;
  T x = 0;
  while (c < '0' || '9' < c) {
    if (c == '-')
      f = true;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = (x << 1) + (x << 3) + (c & 0xf);
    c = getchar();
  }
  if (f)
    return -x;
  return x;
}
template <typename T> inline void read(T &x) { x = read<T>(); }
template <typename T, typename... Args> inline void read(T &x, Args &...args) {
  read(x);
  read(args...);
}

template <typename T> inline void getmin(T &x, T y) {
  if (y < x)
    x = y;
}
template <typename T> inline void getmax(T &x, T y) {
  if (x < y)
    x = y;
}
template <typename X, typename Y>
inline void depair(X &x, Y &y, const pair<X, Y> &pr) {
  x = pr.fi;
  y = pr.se;
}

inline int sig(double x) { return x < -eps ? -1 : eps < x; }
LL fp(LL a, LL n, LL mod = MOD) {
  if (n < 0)
    a = fp(a, mod - 2, mod), n = -n;
  LL res = 1;
  for (; n; n >>= 1, a = a * a % mod)
    if (n & 1)
      res = res * a % mod;
  return res;
}

struct Mint {
  int x;
  Mint() : x(0) {}
  Mint(int _x) : x(_x) {
    if (x < 0 || x >= MOD)
      x %= MOD;
    if (x < 0)
      x += MOD;
  }
  Mint(LL _x) {
    if (_x < 0 || _x >= MOD)
      _x %= MOD;
    if (_x < 0)
      _x += MOD;
    x = _x;
  }
  Mint operator-() const { return Mint(MOD - x); }
  Mint operator+(const Mint &rhs) const {
    return Mint(x + rhs.x >= MOD ? x + rhs.x - MOD : x + rhs.x);
  }
  Mint operator-(const Mint &rhs) const {
    return Mint(x - rhs.x < 0 ? x - rhs.x + MOD : x - rhs.x);
  }
  Mint operator*(const Mint &rhs) const { return Mint((LL)x * rhs.x % MOD); }
  Mint operator/(const Mint &rhs) const {
    return Mint((LL)x * fp(rhs.x, -1) % MOD);
  }
  bool operator==(const Mint &rhs) const { return x == rhs.x; }
  bool operator!=(const Mint &rhs) const { return x != rhs.x; }
  Mint &operator+=(const Mint &rhs) {
    x += rhs.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  Mint &operator-=(const Mint &rhs) {
    x -= rhs.x;
    if (x < 0)
      x += MOD;
    return *this;
  }
  Mint &operator*=(const Mint &rhs) {
    x = (LL)x * rhs.x % MOD;
    return *this;
  }
  Mint &operator/=(const Mint &rhs) {
    x = (LL)x * fp(rhs.x, -1) % MOD;
    return *this;
  }
  friend ostream &operator<<(ostream &out, Mint &rhs) {
    return out << to_string(rhs.x);
  }
  Mint inv() { return Mint(fp(x, -1)); }
  Mint pow(int k) { return Mint(fp(x, k)); }
  Mint pow(const Mint &t) { return Mint(fp(x, t.x)); }
};

const int maxn = (int)2e5 + 20;
const int maxm = (int)2e6 + 20;

LL T[maxn << 2];
int f[maxn << 2];
int s[maxn << 2];

void build(int l, int r, int id) {
  T[id] = 0;
  f[id] = 0;
  s[id] = r - l + 1;

  if (l == r)
    return;
  int mid = MID(l, r);
  build(lson);
  build(rson);
}

void lazy(int id) {
  if (f[id]) {
    int v = f[id];
    f[id] = 0;

    f[ls] += v;
    T[ls] += (LL)v * s[ls];

    f[rs] += v;
    T[rs] += (LL)v * s[rs];
  }
}

void update(int L, int R, int v, int l, int r, int id) {
  if (L <= l && r <= R) {
    T[id] += (LL)v * s[id];
    f[id] += v;
    return;
  }
  lazy(id);

  int mid = MID(l, r);

  if (L <= mid)
    update(L, R, v, lson);
  if (mid < R)
    update(L, R, v, rson);

  T[id] = T[ls] + T[rs];
}

LL query(int L, int R, int l, int r, int id) {
  if (L <= l && r <= R)
    return T[id];

  lazy(id);
  int mid = MID(l, r);

  if (R <= mid)
    return query(L, R, lson);
  if (mid < L)
    return query(L, r, rson);
  return query(L, R, lson) + query(L, R, rson);
}

void work() {
  int n;
  cin >> n;
  vector<pii> e(n - 1);
  for (int i = 0; i < n - 1; i++)
    read(e[i].fi, e[i].se);

  vector<vector<int>> G(n + 1);
  for (auto pr : e) {
    int x, y;
    depair(x, y, pr);
    if (x < y)
      swap(x, y);
    G[x].pb(y);
  }
  for (int i = 1; i <= n; i++)
    sort(rall(G[i]));

  vector<int> dsu(n + 1);
  for (int i = 1; i <= n; i++)
    dsu[i] = i;

  function<int(int)> find_p = [&](int x) {
    if (dsu[x] == x)
      return x;
    return dsu[x] = find_p(dsu[x]);
  };

  build(1, n, 1);
  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    update(1, i, 1, 1, n, 1);

    for (auto x : G[i]) {
      int pi = find_p(i);
      int px = find_p(x);

      if (pi == px)
        continue;

      update(1, x, -1, 1, n, 1);
      dsu[pi] = px;
    }

    ans += query(1, i, 1, n, 1);
  }
  cout << ans << endl;
}

int main() {
#ifdef yukihana0416
  freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // yukihana0416

  int tc = 1;
  //    read(tc);
  for (int ca = 1; ca <= tc; ca++) {
    //        printf("Case #%d: ", ca);
    work();
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

using LL = long long;
// using LL = __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;

template <typename T = int> inline T read() {
  char c = '\\0';
  bool f = false;
  T x = 0;
  while (c < '0' || '9' < c) {
    if (c == '-')
      f = true;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = (x << 1) + (x << 3) + (c & 0xf);
    c = getchar();
  }
  if (f)
    return -x;
  return x;
}
template <typename T> inline void read(T &x) { x = read<T>(); }
template <typename T, typename... Args> inline void read(T &x, Args &...args) {
  read(x);
  read(args...);
}

template <typename T> inline void getmin(T &x, T y) {
  if (y < x)
    x = y;
}
template <typename T> inline void getmax(T &x, T y) {
  if (x < y)
    x = y;
}

#define lowbit(x) ((x) & -(x))
#define lson l, mid, id << 1
#define rson mid + 1, r, id << 1 | 1
#define ls id << 1
#define rs id << 1 | 1
#define MID(l, r) ((l) + (((r) - (l)) >> 1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000;      // 1e5
const int e6 = 1000000;     // 1e6;
const int MOD = 1000000007; // 1e9 + 7
// const int MOD = 998244353;
// const int MOD = 911451407;

inline int sig(double x) { return x < -eps ? -1 : eps < x; }
LL fp(LL a, LL n, LL mod = MOD) {
  if (n < 0)
    a = fp(a, mod - 2, mod), n = -n;
  LL res = 1;
  for (; n; n >>= 1, a = a * a % mod)
    if (n & 1)
      res = res * a % mod;
  return res;
}

struct Mint {
  int x;
  Mint() : x(0) {}
  Mint(int _x) : x(_x) {
    if (x < 0 || x >= MOD)
      x %= MOD;
    if (x < 0)
      x += MOD;
  }
  Mint(LL _x) {
    if (_x < 0 || _x >= MOD)
      _x %= MOD;
    if (_x < 0)
      _x += MOD;
    x = _x;
  }
  Mint operator-() const { return Mint(MOD - x); }
  Mint operator+(const Mint &rhs) const {
    return Mint(x + rhs.x >= MOD ? x + rhs.x - MOD : x + rhs.x);
  }
  Mint operator-(const Mint &rhs) const {
    return Mint(x - rhs.x < 0 ? x - rhs.x + MOD : x - rhs.x);
  }
  Mint operator*(const Mint &rhs) const { return Mint((LL)x * rhs.x % MOD); }
  Mint operator/(const Mint &rhs) const {
    return Mint((LL)x * fp(rhs.x, -1) % MOD);
  }
  bool operator==(const Mint &rhs) const { return x == rhs.x; }
  bool operator!=(const Mint &rhs) const { return x != rhs.x; }
  Mint &operator+=(const Mint &rhs) {
    x += rhs.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  Mint &operator-=(const Mint &rhs) {
    x -= rhs.x;
    if (x < 0)
      x += MOD;
    return *this;
  }
  Mint &operator*=(const Mint &rhs) {
    x = (LL)x * rhs.x % MOD;
    return *this;
  }
  Mint &operator/=(const Mint &rhs) {
    x = (LL)x * fp(rhs.x, -1) % MOD;
    return *this;
  }
  friend ostream &operator<<(ostream &out, Mint &rhs) {
    return out << to_string(rhs.x);
  }
  Mint inv() { return Mint(fp(x, -1)); }
  Mint pow(int k) { return Mint(fp(x, k)); }
  Mint pow(const Mint &t) { return Mint(fp(x, t.x)); }
};

const int maxn = (int)1e6 + 20;
const int maxm = (int)2e6 + 20;

void work() {
  int n, k;
  cin >> n >> k;
  vector<int> a, b;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x >= 0)
      a.pb(x);
    else
      b.pb(-x);
  }

  int ok = 0;
  for (int i = 0; i <= a.size(); i++) {
    int rem = k - i;
    if (rem <= b.size() && rem % 2 == 0) {
      ok = 1;
      break;
    }
  }

  if (ok == 1) {
    sort(rall(a));
    sort(rall(b));

    Mint ans = 1;
    int ap = 0, bp = 0;
    int f = 0;
    int zero = 0;
    while (k) {
      if (bp == b.size() || (ap < a.size() && a[ap] > b[bp])) {
        if (a[ap] == 0)
          zero++, ap++;
        else
          ans *= a[ap++];
      } else {
        ans *= -b[bp++];
        f ^= 1;
      }
      k--;
    }

    if (f) {
      ans /= -b[--bp];
      if (ap + 2 <= a.size() && bp + 2 <= b.size()) {
        LL ta = (LL)a[ap] * a[ap + 1];
        LL tb = (LL)b[bp] * b[bp + 1];

        ans *= max(ta, tb);
      } else if (ap + 2 <= a.size()) {
        ans = ans * a[ap] * a[ap + 1];
      } else {
        ans = ans * b[bp] * b[bp + 1];
      }
    }

    if (zero)
      ans *= 0;

    cout << ans << endl;
  } else {
    sort(all(a));
    sort(all(b));

    Mint ans = 1;
    int ap = 0, bp = 0;
    while (k--) {
      if (bp == b.size() || (ap < a.size() && a[ap] < b[bp])) {
        ans *= a[ap++];
      } else {
        ans *= -b[bp++];
      }
    }
    cout << ans << endl;
  }
}

int main() {
#ifdef yukihana0416
  freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // yukihana0416

  int tc = 1;
  //    read(tc);
  for (int ca = 1; ca <= tc; ca++) {
    //        printf("Case #%d: ", ca);
    work();
  }
  return 0;
}