Problem p02845 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \
  int i;                                                                       \
  cin >> i
#define readll(i)                                                              \
  ll i;                                                                        \
  cin >> i
#define readdouble(i)                                                          \
  double i;                                                                    \
  cin >> i
#define readstring(s)                                                          \
  string s;                                                                    \
  cin >> s

typedef long long ll;

using namespace std;

ll modd = 1000 * 1000 * 1000 + 7;

template <class T> T fastpower(T a, long long n) { // n must be >= 1
  if (n % 2 == 0) {
    T halfpower = fastpower<T>(a, n / 2);
    return (halfpower * halfpower) % modd;
  } else {
    if (n == 1) {
      return a;
    } else {
      T halfpower = fastpower<T>(a, n / 2);
      return (((halfpower * halfpower) % modd) * a) % modd;
    }
  }
};

int main() {

  ios_base::sync_with_stdio(false);

  cout.precision(17);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, 1000); // rand_gen(rng) gets the rand no

  //    auto start = chrono::steady_clock::now();

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readint(n);
    vector<ll> x;
    forr(i, 0, n) {
      readll(xx);
      x.push_back(xx);
    }

    multiset<int> last;
    ll ret = 1;
    forr(i, 0, n) {
      if (x[i] == 0) {
        ret *= (3 - last.size());
        last.insert(0);
      } else {
        int ct = last.count(x[i] - 1);
        ret *= ct;
        ret = ret % modd;
        last.erase(last.find(x[i] - 1));
        last.insert(x[i]);
      }
    }

    cout << ret << endl;
  }

  //    auto end = chrono::steady_clock::now();
  //    cout << chrono::duration_cast<chrono::milliseconds>(end - start).count()
  //    << endl;

  return 0;
}

Fixed submission:

#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \
  int i;                                                                       \
  cin >> i
#define readll(i)                                                              \
  ll i;                                                                        \
  cin >> i
#define readdouble(i)                                                          \
  double i;                                                                    \
  cin >> i
#define readstring(s)                                                          \
  string s;                                                                    \
  cin >> s

typedef long long ll;

using namespace std;

ll modd = 1000 * 1000 * 1000 + 7;

template <class T> T fastpower(T a, long long n) { // n must be >= 1
  if (n % 2 == 0) {
    T halfpower = fastpower<T>(a, n / 2);
    return (halfpower * halfpower) % modd;
  } else {
    if (n == 1) {
      return a;
    } else {
      T halfpower = fastpower<T>(a, n / 2);
      return (((halfpower * halfpower) % modd) * a) % modd;
    }
  }
};

int main() {

  ios_base::sync_with_stdio(false);

  cout.precision(17);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, 1000); // rand_gen(rng) gets the rand no

  //    auto start = chrono::steady_clock::now();

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readint(n);
    vector<ll> x;
    forr(i, 0, n) {
      readll(xx);
      x.push_back(xx);
    }

    multiset<int> last;
    ll ret = 1;
    forr(i, 0, n) {
      if (x[i] == 0) {
        ret *= (3 - last.size());
        last.insert(0);
      } else {
        int ct = last.count(x[i] - 1);
        ret *= ct;
        ret = ret % modd;
        if (ct == 0) {
          break;
        }
        last.erase(last.find(x[i] - 1));
        last.insert(x[i]);
      }
    }

    cout << ret << endl;
  }

  //    auto end = chrono::steady_clock::now();
  //    cout << chrono::duration_cast<chrono::milliseconds>(end - start).count()
  //    << endl;

  return 0;
}