Problem p00903 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// created [2020/03/09] 22:43:41
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
using uint = unsigned int;
using usize = std::size_t;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T, usize n> using arr = T (&)[n];
template <typename T, usize n> using c_arr = const T (&)[n];
template <typename T> using max_heap = std::priority_queue<T>;
template <typename T>
using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T> constexpr T popcount(const T u) {
  return u ? static_cast<T>(__builtin_popcountll(static_cast<u64>(u)))
           : static_cast<T>(0);
}
template <typename T> constexpr T log2p1(const T u) {
  return u ? static_cast<T>(64 - __builtin_clzll(static_cast<u64>(u)))
           : static_cast<T>(0);
}
template <typename T> constexpr T msbp1(const T u) { return log2p1(u); }
template <typename T> constexpr T lsbp1(const T u) {
  return __builtin_ffsll(u);
}
template <typename T> constexpr T clog(const T u) {
  return u ? log2p1(u - 1) : static_cast<T>(u);
}
template <typename T> constexpr bool ispow2(const T u) {
  return u and (static_cast<u64>(u) & static_cast<u64>(u - 1)) == 0;
}
template <typename T> constexpr T ceil2(const T u) {
  return static_cast<T>(1) << clog(u);
}
template <typename T> constexpr T floor2(const T u) {
  return u == 0 ? static_cast<T>(0) : static_cast<T>(1) << (log2p1(u) - 1);
}
template <typename T> constexpr bool btest(const T mask, const usize ind) {
  return static_cast<bool>((static_cast<u64>(mask) >> ind) &
                           static_cast<u64>(1));
}
template <typename T> void bset(T &mask, const usize ind) {
  mask |= (static_cast<T>(1) << ind);
}
template <typename T> void breset(T &mask, const usize ind) {
  mask &= ~(static_cast<T>(1) << ind);
}
template <typename T> void bflip(T &mask, const usize ind) {
  mask ^= (static_cast<T>(1) << ind);
}
template <typename T> void bset(T &mask, const usize ind, const bool b) {
  (b ? bset(mask, ind) : breset(mask, ind));
}
template <typename T> constexpr T bcut(const T mask, const usize ind) {
  return ind == 0 ? static_cast<T>(0)
                  : static_cast<T>((static_cast<u64>(mask) << (64 - ind)) >>
                                   (64 - ind));
}
template <typename T> bool chmin(T &a, const T &b) {
  return (a > b ? a = b, true : false);
}
template <typename T> bool chmax(T &a, const T &b) {
  return (a < b ? a = b, true : false);
}
constexpr unsigned int mod = 1000000007;
template <typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4;
template <typename Real>
constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
auto mfp = [](auto &&f) {
  return [=](auto &&...args) {
    return f(f, std::forward<decltype(args)>(args)...);
  };
};

template <typename T> T in() {
  T v;
  return std::cin >> v, v;
}
template <typename T, typename Uint, usize n, usize i>
T in_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type) {
  return in<T>();
}
template <typename T, typename Uint, usize n, usize i>
auto in_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type &szs) {
  const usize s = (usize)szs[i];
  std::vector<decltype(in_v<T, Uint, n, i + 1>(szs))> ans(s);
  for (usize j = 0; j < s; j++) {
    ans[j] = in_v<T, Uint, n, i + 1>(szs);
  }
  return ans;
}
template <typename T, typename Uint, usize n> auto in_v(c_arr<Uint, n> szs) {
  return in_v<T, Uint, n, 0>(szs);
}
template <typename... Types> auto in_t() {
  return std::tuple<std::decay_t<Types>...>{in<Types>()...};
}
struct io_init {
  io_init() {
    std::cin.tie(nullptr), std::ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(20);
  }
  void clear() { std::cin.tie(), std::ios::sync_with_stdio(true); }
} io_setting;

int out() { return 0; }
template <typename T> int out(const T &v) { return std::cout << v, 0; }
template <typename T> int out(const std::vector<T> &v) {
  for (usize i = 0; i < v.size(); i++) {
    if (i > 0) {
      std::cout << ' ';
    }
    out(v[i]);
  }
  return 0;
}
template <typename T1, typename T2> int out(const std::pair<T1, T2> &v) {
  return out(v.first), std::cout << ' ', out(v.second), 0;
}
template <typename T, typename... Args>
int out(const T &v, const Args... args) {
  return out(v), std::cout << ' ', out(args...), 0;
}
template <typename... Args> int outln(const Args... args) {
  return out(args...), std::cout << '\\n', 0;
}
template <typename... Args> int outel(const Args... args) {
  return out(args...), std::cout << std::endl, 0;
}
#define SHOW(...) static_cast<void>(0)
constexpr ull TEN(const usize n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; }

template <typename T, typename Uint, usize n, usize i>
auto make_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type,
            const T &v = T{}) {
  return v;
}
template <typename T, typename Uint, usize n, usize i>
auto make_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type szs,
            const T &v = T{}) {
  const usize s = (usize)szs[i];
  return std::vector<decltype(make_v<T, Uint, n, i + 1>(szs, v))>(
      s, make_v<T, Uint, n, i + 1>(szs, v));
}
template <typename T, typename Uint, usize n>
auto make_v(c_arr<Uint, n> szs, const T &t = T{}) {
  return make_v<T, Uint, n, 0>(szs, t);
}

template <typename Cost = usize> struct edge {
  using cost_type = Cost;
  usize u, v;
  Cost c;
  edge(const usize u, const usize v) : u{u}, v{v}, c{1} {}
  edge(const usize u, const usize v, const Cost &c) : u{u}, v{v}, c{c} {}
  operator usize() const { return v; }
  usize from() const { return u; }
  usize to() const { return v; }
  Cost cost() const { return c; }
  friend std::ostream &operator<<(std::ostream &os, const edge &e) {
    return os << e.u << "->" << e.v << ":" << e.c;
  }
};
template <typename Edge> class base_graph {
public:
  base_graph(const usize n) : v{n}, es(n), res(n) {}
  void add_edge(const usize u, const usize v, const bool bi = false) {
    es[u].emplace_back(u, v), res[v].emplace_back(v, u);
    if (bi) {
      es[v].emplace_back(v, u), res[u].emplace_back(u, v);
    }
  }
  template <typename Cost>
  void add_edge(const usize u, const usize v, const Cost &c,
                const bool bi = false) {
    es[u].emplace_back(u, v, c), res[v].emplace_back(v, u, c);
    if (bi) {
      es[v].emplace_back(v, u, c), res[u].emplace_back(u, v, c);
    }
  }
  std::vector<Edge> &operator[](const usize u) { return es[u]; }
  const std::vector<Edge> &operator[](const usize u) const { return es[u]; }
  std::vector<Edge> &from(const usize u) { return es[u]; }
  const std::vector<Edge> &from(const usize u) const { return es[u]; }
  std::vector<Edge> &to(const usize v) { return res[v]; }
  const std::vector<Edge> &to(const usize v) const { return res[v]; }
  usize size() const { return v; }
  friend std::ostream &operator<<(std::ostream &os, const base_graph &g) {
    for (usize i = 0; i < g.v; i++) {
      for (const auto &e : g.es[i]) {
        os << e << '\\n';
      }
    }
    return os;
  }

private:
  usize v;
  std::vector<std::vector<Edge>> es, res;
};
template <typename Edge> using base_tree = base_graph<Edge>;
using graph = base_graph<edge<>>;
using tree = base_graph<edge<>>;
template <typename Cost> using cost_graph = base_graph<edge<Cost>>;
template <typename Cost> using cost_tree = base_graph<edge<Cost>>;
int main() {
  while (true) {
    auto N = in<int>(), M = in<int>();
    if (N == 0 and M == 0) {
      break;
    }
    std::vector<int> ds(N, 0), es(N, 0);
    es[0] = 0, es[N - 1] = 1000;
    std::vector<std::vector<int>> vss(1001);
    std::vector<std::map<int, int>> mps(1001);
    cost_graph<int> fg(N), bg(N);
    for (int i = 1; i < N - 1; i++) {
      ds[i] = in<int>(), es[i] = in<int>();
    }
    for (int i = 0; i < N; i++) {
      vss[es[i]].push_back(i);
    }
    for (int i = 0; i < M; i++) {
      const auto a = in<int>() - 1, b = in<int>() - 1, c = in<int>();
      if (es[a] == es[b]) {
        fg.add_edge(a, b, c, false);
        bg.add_edge(b, a, c, false);
      } else if (es[a] < es[b]) {
        fg.add_edge(a, b, c, false);
      } else {
        bg.add_edge(b, a, c, false);
      }
    }
    for (int h = 0; h <= 1000; h++) {
      const auto &vs = vss[h];
      for (int i = 0; i < vs.size(); i++) {
        mps[h][vs[i]] = i;
      }
    }

    using pii = std::pair<int, int>;
    using ppi = std::pair<pii, int>;
    using ippi = std::pair<int, ppi>;
    auto dsss = make_v({N, N, 1 << 10}, inf_v<int>);
    min_heap<ippi> Q;
    dsss[0][0][1] = 0;
    Q.push({0, {{0, 0}, 1}});
    while (not Q.empty()) {
      const auto p = Q.top();
      Q.pop();
      const int d = p.first;
      const int fp = p.second.first.first;
      const int bp = p.second.first.second;
      const int mask = p.second.second;
      if (dsss[fp][bp][mask] < d) {
        continue;
      }
      const int fe = es[fp], be = es[bp];
      const int Me = std::max(fe, be);
      for (const auto &e : fg[fp]) {
        const int nfp = e.to();
        const int cost = e.cost();
        if (es[nfp] < Me) {
          continue;
        } else if (es[nfp] == Me) {
          const int nlind = mps[es[nfp]][nfp];
          const int nmask = mask | (1 << nlind);
          const int nd =
              dsss[fp][bp][mask] + cost + (btest(mask, nlind) ? 0 : ds[nfp]);
          if (dsss[nfp][bp][nmask] <= nd) {
            continue;
          }
          dsss[nfp][bp][nmask] = nd, Q.push({nd, {{nfp, bp}, nmask}});
        } else {
          const int nlind = mps[es[nfp]][nfp];
          const int nmask = (1 << nlind);
          const int nd = dsss[fp][bp][mask] + cost + ds[nfp];
          if (dsss[nfp][bp][nmask] <= nd) {
            continue;
          }
          dsss[nfp][bp][nmask] = nd, Q.push({nd, {{nfp, bp}, nmask}});
        }
      }
      for (const auto &e : bg[bp]) {
        const int nbp = e.to();
        const int cost = e.cost();
        if (es[nbp] < Me) {
          continue;
        } else if (es[nbp] == Me) {
          const int nlind = mps[es[nbp]][nbp];
          const int nmask = mask | (1 << nlind);
          const int nd =
              dsss[fp][bp][mask] + cost + (btest(mask, nlind) ? 0 : ds[nbp]);
          if (dsss[fp][nbp][nmask] <= nd) {
            continue;
          }
          dsss[fp][nbp][nmask] = nd, Q.push({nd, {{fp, nbp}, nmask}});
        } else {
          const int nlind = mps[es[nbp]][nbp];
          const int nmask = (1 << nlind);
          const int nd = dsss[fp][bp][mask] + cost + ds[nbp];
          if (dsss[fp][nbp][nmask] <= nd) {
            continue;
          }
          dsss[fp][nbp][nmask] = nd, Q.push({nd, {{fp, nbp}, nmask}});
        }
      }
    }
    const int ans = dsss[N - 1][N - 1][1];
    outln(ans == inf_v<int> ? -1 : ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(kbrni, n) cout << " " << a[kbrni];                                       \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(kbrni, v) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

ll A, B, C = ~(1 << 31), M = (1 << 16) - 1;

ll r() {
  A = 36969 * (A & M) + (A >> 16);
  B = 18000 * (B & M) + (B >> 16);
  return (C & ((A << 16) + B)) % 1000000;
}

set<P> st[300005];
vector<P> v[1000001];

bool contain(int cri, P &nw) {
  auto it = st[cri].lower_bound(P(nw.fi - 1, nw.se));
  if (it != st[cri].begin()) {
    --it;
    return it->se < nw.se;
  } else {
    return false;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    int n, m;
    cin >> m >> n >> A >> B;
    rep(i, n + m + 1) { st[i].clear(); }
    rep(i, 1000000) { v[i].clear(); }
    if (n == 0 && m == 0 && A == 0 && B == 0) {
      break;
    }
    rep(i, m) {
      int s, t, u;
      cin >> s >> t >> u;
      v[s].pb(P(t, u));
    }
    rep(i, n) { v[r()].pb(P(r(), r())); }
    int mx = -1;
    rep(i, 1000001) {
      if (len(v[i])) {
        sort(all(v[i]), [&](const P &p, const P &q) {
          return (p.fi == q.fi) ? (p.se > q.se) : (p.fi > q.fi);
        });
        each(it, v[i]) {
          int l = -1, r = mx + 1;
          while (r - l > 1) {
            int mid = (l + r) / 2;
            if (contain(mid, it)) {
              l = mid;
            } else {
              r = mid;
            }
          }
          auto it2 = st[r].upper_bound(it);
          if (it2 != st[r].begin()) {
            --it2;
            if (it2->se <= it.se) {
              continue;
            }
            ++it2;
          }
          while (1) {
            if (it2 != st[r].end()) {
              if (it2->se >= it.se) {
                it2 = st[r].erase(it2);
              } else {
                cmx(mx, r);
                st[r].insert(it);
                break;
              }
            } else {
              cmx(mx, r);
              st[r].insert(it);
              break;
            }
          }
        }
      }
    }
    cout << mx + 1 << "\\n";
  }
  return 0;
}