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

#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  for (int y = 0; y < H; y++) {
    cin >> A[y] >> B[y];
    A[y]--;
  }

  set<pair<int, int>> S;
  multiset<int> T;
  for (int x = 0; x < W; x++) {
    S.insert({x, 0});
    T.insert(0);
  }

  for (int y = 0; y < H; y++) {
    auto s = S.lower_bound({A[y], 0});
    if (s != S.end()) {
      int t = 999999;
      while (s != S.end() && s->first < B[y]) {
        T.erase(T.find(s->second));

        t = min(t, s->second + (B[y] - s->first));
        s = S.erase(s);
      }
      if (B[y] < W && t < 999999) {
        S.insert({B[y], t});
        T.insert(t);
      }
    }

    if (T.empty())
      cout << -1 << endl;
    else
      cout << *T.begin() + y + 1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;

vector<int> naive(int H, int W, vector<int> A, vector<int> B) {
  vector<int> ans;

  int oo = 999999;
  vector<int> T(W);
  vector<int> P(W);
  for (int x = 0; x < W; x++)
    P[x] = x;

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (A[y] <= P[x] && P[x] < B[y]) {
        T[x] += B[y] - P[x];
        P[x] = B[y];
      }
      T[x]++;
    }

    int a = oo;
    for (int x = 0; x < W; x++)
      if (P[x] < W)
        a = min(a, T[x]);
    ans.push_back(a < oo ? a : -1);
  }
  return ans;
}

vector<int> solve(int H, int W, vector<int> A, vector<int> B) {
  vector<int> ans;

  multiset<pair<int, int>> S;
  multiset<int> T;
  for (int x = 0; x < W; x++) {
    S.insert({x, 0});
    T.insert(0);
  }

  for (int y = 0; y < H; y++) {
    auto s = S.lower_bound({A[y], 0});
    if (s != S.end()) {
      int t = 0;
      while (s != S.end() && s->first < B[y]) {
        T.erase(T.find(s->second));

        t = max(t, s->second + (B[y] - s->first));
        s = S.erase(s);
      }
      if (B[y] < W) {
        S.insert({B[y], t});
        T.insert(t);
      }
    }

    if (T.empty())
      ans.push_back(-1);
    else
      ans.push_back(*T.begin() + y + 1);
  }
  return ans;
}

int main() {
  /*
  while (true)
  {
      int H = 8;
      int W = 4;
      vector<int> A(H);
      vector<int> B(H);
      for (int y=0; y<H; y++)
      {
          A[y] = rand()%W;
          while (true)
          {
              B[y] = rand()%W;
              if (A[y]<=B[y])
                  break;
          }
      }

      vector<int> an = naive(H, W, A, B);
      vector<int> as = solve(H, W, A, B);
      if (an!=as)
      {
          cout<<H<<" "<<W<<endl;
          for (int y=0; y<H; y++)
              cout<<A[y]+1<<" "<<B[y]+1<<endl;
          for (int a: an)
              cout<<" "<<a;
          cout<<endl;
          for (int a: as)
              cout<<" "<<a;
          cout<<endl;
          break;
      }
  }
  */

  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  for (int y = 0; y < H; y++) {
    cin >> A[y] >> B[y];
    A[y]--;
  }
  vector<int> ans = naive(H, W, A, B);
  for (int a : ans)
    cout << a << endl;
}