Problem p03915 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
struct node {
  int u, v;
  int64 c;
} E[MAXN * 2];
int N, Q, tot;
int64 val[MAXN * 2];
int fa[MAXN];
int getfa(int x) { return x == fa[x] ? x : getfa(fa[x]); }
void Solve() {
  read(N);
  read(Q);
  int a, b;
  int64 c;
  for (int i = 0; i < 2 * N; ++i)
    val[i] = 1e18;
  for (int i = 1; i <= Q; ++i) {
    read(a);
    read(b);
    read(c);
    E[++tot] = (node){a, b, c};
    val[a] = min(val[a], c + 1);
    val[b] = min(val[b], c + 2);
  }
  for (int i = 1; i < 2 * N; ++i) {
    val[i] = min(val[i - 1] + 2, val[i]);
  }
  for (int i = 0; i < N; ++i) {
    E[++tot] = (node){i, (i + 1) % N, min(val[i], val[i + N])};
  }
  sort(E + 1, E + tot + 1, [](node a, node b) { return a.c < b.c; });
  for (int i = 0; i < N; ++i)
    fa[i] = i;
  int64 ans = 0;
  for (int i = 1; i <= tot; ++i) {
    if (getfa(E[i].u) != getfa(E[i].v)) {
      fa[getfa(E[i].u)] = getfa(E[i].v);
      ans += E[i].c;
    }
  }
  out(ans);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
struct node {
  int u, v;
  int64 c;
} E[MAXN * 2];
int N, Q, tot;
int64 val[MAXN * 2];
int fa[MAXN];
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
void Solve() {
  read(N);
  read(Q);
  int a, b;
  int64 c;
  for (int i = 0; i < 2 * N; ++i)
    val[i] = 1e18;
  for (int i = 1; i <= Q; ++i) {
    read(a);
    read(b);
    read(c);
    E[++tot] = (node){a, b, c};
    val[a] = min(val[a], c + 1);
    val[b] = min(val[b], c + 2);
  }
  for (int i = 1; i < 2 * N; ++i) {
    val[i] = min(val[i - 1] + 2, val[i]);
  }
  for (int i = 0; i < N; ++i) {
    E[++tot] = (node){i, (i + 1) % N, min(val[i], val[i + N])};
  }
  sort(E + 1, E + tot + 1, [](node a, node b) { return a.c < b.c; });
  for (int i = 0; i < N; ++i)
    fa[i] = i;
  int64 ans = 0;
  for (int i = 1; i <= tot; ++i) {
    if (getfa(E[i].u) != getfa(E[i].v)) {
      fa[getfa(E[i].u)] = getfa(E[i].v);
      ans += E[i].c;
    }
  }
  out(ans);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
  return 0;
}