alexjercan/bugnet huggingface dataset:
Problem p00277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
int n, m, k, mx, i, j, h, g, y, x;
int a[1000001], b[1000002], c[1000001];
int ten[100001], cnt[100001], sz[100001], nb[100001];
typedef pair<int, int> P;
int main(void) {
  priority_queue<P> pri; // 降順
  P p;
  cin >> n >> m >> k;
  for (i = 1; i <= m; i++)
    cin >> a[i] >> b[i] >> c[i];
  for (i = 1; i <= n; i++)
    ten[i] = 0, cnt[i] = 0;
  b[m + 1] = k;
  i = 1;
  while (i <= m) {
    for (i; i <= m; i++) {
      cnt[a[i]] += c[i];
      pri.push(P(cnt[a[i]], a[i]));
      // cout<<"A"<<i<<endl;
      if (b[i] != b[i + 1])
        break;
    }
    // cout<<"I"<<" "<<i<<endl;
    x = -10000000;
    y = 100001;
    g = 0;
    while (pri.size() != 0) {
      p = pri.top();
      if (p.f != cnt[p.s])
        pri.pop();
      else if (y > p.s && x <= p.f) {
        y = p.s;
        x = p.f;
        pri.pop();
        sz[g] = p.f;
        nb[g] = p.s;
        g++;
      } else
        break;
    }
    for (j = 0; j < g; j++) {
      pri.push(P(sz[j], nb[j]));
    }
    // printf("%d %d\\n",b[i],b[i-1]);
    ten[y] += b[i + 1] - b[i];
    // cout<<p.f<<" "<<p.s<<endl;
    i++;
  }
  mx = 0;
  h = 1;
  // for(i=1;i<=n;i++) cout<<ten[i]<<endl;
  for (i = 1; i <= n; i++)
    if (mx < ten[i])
      mx = ten[i], h = i;
  printf("%d\\n", h);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
long long d[1000001], t[1000002], x[1000001], a[100001], b[100001];
int main(void) {
  int n, r, l, w, j, flg, h, g, m, i;
  scanf("%d %d %d", &n, &r, &l);
  for (i = 1; i <= r; i++) {
    scanf("%lld %lld %lld", &d[i], &t[i], &x[i]);
  }
  t[r + 1] = l;
  d[0] = 1;
  t[0] = 0;
  x[0] = 0;
  for (i = 1; i <= n; i++) {
    b[i] = 0;
    a[i] = 0;
  }
  h = 1;
  g = 0;
  for (i = 0; i <= r; i++) {
    a[d[i]] = a[d[i]] + x[i];
    w = a[1];
    flg = 1;
    for (j = 1; j <= n; j++) {
      if (w < a[j]) {
        w = a[j];
        flg = j;
      }
    }
    b[h] = b[h] + (t[i] - t[g]);
    g = i;
    h = flg;
  }
  w = 0;
  b[h] = b[h] + (l - t[g]);
  for (i = 1; i <= n; i++) {
    if (w < b[i]) {
      w = b[i];
      m = i;
    }
  }
  printf("%d\\n", m);
  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;

struct dat {
  int id, point;
};
#define MAX_N 1 << 17

int N, R, L;
int res[MAX_N + 1];
dat seg[2 * MAX_N];
int n;

void init() {
  //	printf("c");
  n = 1;
  while (n < N)
    n *= 2; // nはseg木の要素数
  //	printf("[n = %d]", n);
  for (int i = 0; i < 2 * n - 1; i++)
    seg[i].point = 0;
  //	printf("a");
  for (int j = 1; j <= N; j++) {
    seg[n + j].id = j;
  }
  //	printf("b");
}

void add(int p, int x) {
  p += n;
  seg[p].point += x;
  while (p > 0) {
    // printf("p = %d\\n", p);
    p = (p - 1) / 2;
    if (seg[p * 2 + 1].point >= seg[p * 2 + 2].point) {
      seg[p] = seg[p * 2 + 1];
    } else {
      seg[p] = seg[p * 2 + 2];
    }
  }
}

int main() {
  scanf("%d%d%d", &N, &R, &L);

  init();

  int lb = 0; // 左端
  int d, t, x;
  for (int i = 0; i < R; i++) {
    scanf("%d%d%d", &d, &t, &x);
    if (i > 0)
      res[seg[0].id] += t - lb;
    add(d, x);
    lb = t;
    //		for(int i = 1; i <= N; i++){
    //			printf("%d ", res[i]);
    //		}
    //		printf(": seg[0].id = %d\\n", seg[0].id);
  }
  res[seg[0].id] += L - t;

  int longest = 0; // 最長の映り時間
  for (int i = 1; i <= N; i++)
    longest = max(longest, res[i]);
  for (int i = 1; i <= N; i++) {
    if (res[i] == longest) {
      printf("%d\\n", i);
      return 0;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

struct dat {
  int id, point;
};
#define MAX_N 100000

int N, R, L;
int res[MAX_N + 1];
dat seg[MAX_N * 2];
int n;

void init() {
  n = 1;
  while (n < N)
    n *= 2; // nはseg木の要素数
  for (int i = 0; i < 2 * n - 1; i++)
    seg[i].point = 0;
  for (int j = 0; j <= N; j++) {
    seg[n + j].id = j;
  }
}

void add(int p, int x) {
  p += n;
  seg[p].point += x;
  while (p > 0) {
    // printf("p = %d\\n", p);
    p = (p - 1) / 2;
    if (seg[p * 2 + 1].point >= seg[p * 2 + 2].point) {
      seg[p] = seg[p * 2 + 1];
    } else {
      seg[p] = seg[p * 2 + 2];
    }
  }
}

int main() {
  scanf("%d%d%d", &N, &R, &L);

  init();

  int lb = 0; // 左端
  int d, t, x;
  for (int i = 0; i < R; i++) {
    scanf("%d%d%d", &d, &t, &x);
    if (i > 0)
      res[seg[0].id] += t - lb;
    add(d, x);
    lb = t;
    // for(int i = 1; i <= N; i++){
    //	printf("%d ", res[i]);
    // }
    // printf(": seg[0].id = %d\\n", seg[0].id);
  }
  res[seg[0].id] += L - t;

  int longest = 0; // 最長の映り時間
  for (int i = 1; i <= N; i++)
    longest = max(longest, res[i]);
  for (int i = 1; i <= N; i++) {
    if (res[i] == longest) {
      printf("%d\\n", i);
      return 0;
    }
  }
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define rep(i, x) for (int i = 0; i < x; i++)
#define sor(v) sort(v.begin(), v.end())
#define min_3(a, b, c) min(a, min(b, c))

int SEG[400010];
int siz;

void init(int x) {
  siz = 1;
  while (siz < x)
    siz *= 2;

  rep(i, siz * 2 - 1) { SEG[i] = 0; }
}

void add(int k, int x) {
  k += siz - 1;
  SEG[k] += x;
  while (k > 0) {
    k = (k - 1) / 2;
    SEG[k] = max(SEG[2 * k + 1], SEG[2 * k + 2]);
  }
}

int query(int l, int r, int k) {
  if (l == r - 1)
    return l;
  if (SEG[2 * k + 1] >= SEG[k])
    return query(l, (l + r) / 2, 2 * k + 1);
  return query((l + r) / 2, r, 2 * k + 2);
}

int main() {
  int n, r, l;
  cin >> n >> r >> l;

  init(n);

  static int ret[100010] = {0};

  int T = 0;
  rep(i, r) {
    int d, t, x;
    cin >> d >> t >> x;
    d--;

    if (t > T) {
      ret[query(0, siz, 0)] += t - T;
      T = t;
    }

    add(d, x);
  }
  ret[query(0, siz, 0)] += l - T;

  int RET = 0, MAX = 0;

  rep(i, n) {
    if (MAX < ret[i]) {
      RET = i;
      MAX = ret[i];
    }
  }

  cout << RET + 1 << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

#define rep(i, x) for (int i = 0; i < x; i++)
#define sor(v) sort(v.begin(), v.end())
#define min_3(a, b, c) min(a, min(b, c))
#define pque priority_queue
#define pu push
#define fr first
#define sc second
#define sa static

struct com {
  bool operator()(P x, P y) {
    if (x.fr == y.fr)
      return x.sc > y.sc;
    return x.fr < y.fr;
  }
};

int main() {
  sa int n, r, l;
  sa int d[100010], t[100010], x[100010];

  cin >> n >> r >> l;
  rep(i, r) scanf("%d%d%d", &d[i], &t[i], &x[i]);
  d[r] = 0;
  t[r] = l;
  x[r] = 0;

  sa int ret[100010] = {0};
  sa int p[100010] = {0};
  sa int T = 0;
  sa pque<P, vector<P>, com> Q;
  rep(i, n) Q.pu(P(0, i + 1));

  rep(i, r + 1) {
    while (p[Q.top().sc] != Q.top().fr)
      Q.pop();
    if (t[i] > T) {
      while (p[Q.top().sc] != Q.top().fr)
        Q.pop();
      ret[Q.top().sc] += t[i] - T; // cout<<Q.top().sc<<" "<<t[i]-T<<endl;
      T = t[i];
    }
    p[d[i]] += x[i];
    if (p[d[i]] > Q.top().fr)
      Q.pu(P(p[d[i]], d[i]));
  }

  int Ret = 0, Max = 0;
  rep(i, n) {
    // cout<<i+1<<" "<<ret[i+1]<<endl;
    if (ret[i + 1] > Max) {
      Ret = i + 1;
      Max = ret[i + 1];
    }
  }

  cout << Ret << endl;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[137510];
struct node {
  int id, point;
} node[1 << 21];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[125010];
struct node {
  int id, point;
} node[1 << 21];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}
Problem p00277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>

typedef std::pair<int, int> P;

int main() {

  int n, r, l;
  std::cin >> n >> r >> l;

  std::vector<int> point(n, 0);
  std::vector<int> time(n, 0);

  std::priority_queue<P> que;

  int betime = 0;
  for (int i = 0; i < r; i++) {
    int d, t, x;
    std::cin >> d >> t >> x;
    d--;

    while (!que.empty()) {
      P res = que.top();

      if (res.first == point[-res.second]) {
        time[-res.second] += (t - betime);
        break;
      }
      que.pop();
    }

    point[d] += x;
    que.push(P(point[d], -d));

    betime = t;
  }

  while (!que.empty()) {
    P res = que.top();

    if (res.first == point[-res.second]) {
      time[-res.second] += (l - betime);
      break;
    }
    que.pop();
  }

  int max = 0;
  for (int i = 1; i < n; i++) {
    if (time[i] > time[max])
      max = i;
  }

  std::cout << max + 1 << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {

  int n, r, l;
  std::cin >> n >> r >> l;

  std::vector<int> time(n, 0);
  std::vector<int> score(n, 0);

  int id_now = 0, time_before = 0;

  for (int i = 0; i < r; i++) {

    int d, t, x;
    std::cin >> d >> t >> x;

    d--;

    time[id_now] += (t - time_before);
    score[d] += x;

    id_now = 0;
    for (int j = 1; j < n; j++) {
      if (score[j] > score[id_now])
        id_now = j;
    }

    time_before = t;
  }

  time[id_now] += (l - time_before);

  int res = 0;

  for (int i = 1; i < n; i++) {
    if (time[i] > time[res])
      res = i;
  }

  std::cout << res + 1 << std::endl;

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define N_MAX 100001
typedef pair<int, int> P;

int main() {
  int N, R, L;
  int d, t, x;
  int time[N_MAX] = {0};
  int score[N_MAX] = {0};
  cin >> N >> R >> L;
  priority_queue<P> que;
  for (int i = 1; i <= N; i++) {
    que.push(P(0, N - i));
  }
  int now = 0;
  for (int j = 0; j < R; j++) {
    cin >> d >> t >> x;
    P p1 = que.top();
    while (score[N - p1.second] != p1.first) {
      que.pop();
      p1 = que.top();
    }
    time[N - p1.second] += t - now;
    now = t;
    score[d] += x;
    que.push(P(score[d], N - d));
  }
  P p1 = que.top();
  while (score[N - p1.second] != p1.first) {
    que.pop();
    p1 = que.top();
  }
  time[N - p1.second] += L - now;
  priority_queue<P> que2;
  for (int i = 1; i <= N; i++) {
    que2.push(P(time[i], N - i));
  }
  p1 = que2.top();
  cout << N - p1.second << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define N_MAX 100000
typedef pair<int, int> P;

int main() {
  int N, R, L;
  int d, t, x;
  int time[N_MAX] = {0};
  int score[N_MAX] = {0};
  cin >> N >> R >> L;
  priority_queue<P> que;
  for (int i = 1; i <= N; i++) {
    que.push(P(0, N - i));
  }
  int now = 0;
  for (int j = 0; j < R; j++) {
    cin >> d >> t >> x;
    P p1 = que.top();
    while (score[N - p1.second] != p1.first) {
      que.pop();
      p1 = que.top();
    }
    time[N - p1.second] += t - now;
    now = t;
    score[d] += x;
    que.push(P(score[d], N - d));
  }
  P p1 = que.top();
  while (score[N - p1.second] != p1.first) {
    que.pop();
    p1 = que.top();
  }
  time[N - p1.second] += L - now;
  priority_queue<P> que2;
  for (int i = 1; i <= N; i++) {
    que2.push(P(time[i], N - i));
  }
  p1 = que2.top();
  cout << N - p1.second << endl;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
#define MAX 1111111
#define MIN -1111111
typedef pair<int, int> P;
void init(int n);
void update(int k, int x);
P maxx(int k1, int k2);
P dat[4 * 100001];
int tim[100001] = {0};
int N;

int main() {
  int R, L;
  cin >> N >> R >> L;
  int n_tmp = N;
  init(N);
  memset(tim, 0, n_tmp + 1);
  int now = 0;
  dat[0] = P(MIN, 1);
  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    tim[dat[0].second] += t - now;
    update(d, x);
    now = t;
  }
  tim[dat[0].second] += L - now;
  int *ans = max_element(tim, tim + n_tmp + 1);
  cout << ans - tim << endl;
}

void init(int n) {
  N = 1;
  while (N < n)
    N *= 2;
  for (int i = 0; i < 2 * N - 1; i++) {
    dat[i] = P(MIN, MAX);
  }
  for (int i = N - 1; i < N - 1 + n; i++) {
    dat[i] = P(0, i - (N - 2));
  }
  return;
}

void update(int k, int x) {
  k += N - 2;
  P p = dat[k];
  int score = p.first;
  int team = p.second;
  // cout << score << " " << team << endl;
  dat[k] = P(score + x, team);
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = maxx(k * 2 + 1, k * 2 + 2);
    // cout << dat[k].first << " " << dat[k].second << endl;
  }
  return;
}

P maxx(int k1, int k2) {
  P p;
  if (dat[k1].first == dat[k2].first) {
    p = P(dat[k1].first, min(dat[k1].second, dat[k2].second));
  } else {
    p = max(dat[k1], dat[k2]);
  }
  return p;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
#define MAX 1111111
#define MIN -1111111
typedef pair<int, int> P;
void init(int n);
void update(int k, int x);
P maxx(int k1, int k2);
P dat[2 * 100001];
int tim[100001] = {0};
int N;

int main() {
  int R, L;
  cin >> N >> R >> L;
  int n_tmp = N;
  init(N);
  memset(tim, 0, n_tmp + 1);
  int now = 0;
  dat[0] = P(MIN, 1);
  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    tim[dat[0].second] += t - now;
    update(d, x);
    now = t;
  }
  tim[dat[0].second] += L - now;
  int *ans = max_element(tim, tim + n_tmp + 1);
  cout << ans - tim << endl;
}

void init(int n) {
  N = 1;
  while (N < n)
    N *= 2;
  for (int i = 0; i < 2 * N - 1; i++) {
    dat[i] = P(MIN, MAX);
  }
  for (int i = N - 1; i < N - 1 + n; i++) {
    dat[i] = P(0, i - (N - 2));
  }
  return;
}

void update(int k, int x) {
  k += N - 2;
  P p = dat[k];
  int score = p.first;
  int team = p.second;
  // cout << score << " " << team << endl;
  dat[k] = P(score + x, team);
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = maxx(k * 2 + 1, k * 2 + 2);
    // cout << dat[k].first << " " << dat[k].second << endl;
  }
  return;
}

P maxx(int k1, int k2) {
  P p;
  if (dat[k1].first == dat[k2].first) {
    p = P(dat[k1].first, min(dat[k1].second, dat[k2].second));
  } else {
    p = max(dat[k1], dat[k2]);
  }
  return p;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[1000010];
struct node {
  int id;
  long long point;
} node[1 << 22];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[100010];
struct node {
  int id;
  long long point;
} node[1 << 21];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[150010];
struct node {
  int id, point;
} node[1 << 21];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[100010];
struct node {
  int id, point;
} node[1 << 21];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[135010];
struct node {
  int id, point;
} node[1 << 20];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[130010];
struct node {
  int id, point;
} node[1 << 20];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}