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


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;

int N, M, Q;

struct query {
  int T;
  int make; // 0:ques 1:make
  int A, B;
  int id;
};

vector<int> par;
int getPar(int x) {
  if (x == par[x])
    return x;
  return par[x] = getPar(par[x]);
}
void unite(int a, int b) {
  a = getPar(a);
  b = getPar(b);
  if (a == b) {
    return;
  }
  if (a > b) {
    par[b] = a;
  } else {
    par[a] = b;
  }
}

int main(void) {
  cin >> N >> M >> Q;
  vector<query> que;
  for (int i = 0; i < N; ++i) {
    par.push_back(i);
  }
  for (int i = 0; i < M; ++i) {
    int d, a, b;
    cin >> d >> a >> b;
    --a;
    --b;
    que.push_back({d, 1, a, b});
  }
  set<int> suk;
  for (int i = 0; i < N - 1; ++i) {
    suk.insert(i);
  }
  for (int i = 0; i < Q; ++i) {
    int e, s, t;
    cin >> e >> s >> t;
    --s;
    --t;
    que.push_back({e, 0, s, t, i});
  }
  sort(que.begin(), que.end(), [](const query &q1, const query &q2) {
    return make_pair(q1.T, q1.make) < make_pair(q2.T, q2.make);
  });
  vector<pair<int, bool>> ans;
  for (auto &q : que) {
    int T = q.T;
    int make = q.make;
    if (make == 1) {
      int A = q.A;
      int B = q.B;
      auto itr = suk.lower_bound(A);
      auto ptr = suk.lower_bound(B);
      while (itr != ptr) {
        int x = *itr;
        unite(x, x + 1);
        //++itr;
        itr = suk.erase(itr);
      }
    } else {
      int S = q.A;
      int T = q.B;
      int id = q.id;
      if (S >= T) {
        // cout << "Yes" << endl;
        ans.push_back({id, true});
      } else {
        int SS = getPar(S);
        if (SS >= T) {
          // cout << "Yes" << endl;
          ans.push_back({id, true});
        } else {
          // cout << "No" << endl;
          ans.push_back({id, false});
        }
      }
    }
  }
  sort(ans.begin(), ans.end());
  for (auto x : ans) {
    cout << (x.second ? "Yes" : "No") << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;

int N, M, Q;

struct query {
  int T;
  int make; // 0:ques 1:make
  int A, B;
  int id;
};

vector<int> par;
int getPar(int x) {
  if (x == par[x])
    return x;
  return par[x] = getPar(par[x]);
}
void unite(int a, int b) {
  a = getPar(a);
  b = getPar(b);
  if (a == b) {
    return;
  }
  if (a > b) {
    par[b] = a;
  } else {
    par[a] = b;
  }
}

int main(void) {
  cin >> N >> M >> Q;
  vector<query> que;
  for (int i = 0; i < N; ++i) {
    par.push_back(i);
  }
  for (int i = 0; i < M; ++i) {
    int d, a, b;
    cin >> d >> a >> b;
    --a;
    --b;
    que.push_back({d, 1, a, b});
  }
  set<int> suk;
  for (int i = 0; i < N - 1; ++i) {
    suk.insert(i);
  }
  for (int i = 0; i < Q; ++i) {
    int e, s, t;
    cin >> e >> s >> t;
    --s;
    --t;
    que.push_back({e, 0, s, t, i});
  }
  sort(que.begin(), que.end(), [](const query &q1, const query &q2) {
    return make_pair(q1.T, q1.make) < make_pair(q2.T, q2.make);
  });
  vector<pair<int, bool>> ans;
  for (auto &q : que) {
    int T = q.T;
    int make = q.make;
    if (make == 1) {
      int A = q.A;
      int B = q.B;
      auto itr = suk.lower_bound(A);
      auto ptr = suk.lower_bound(B);
      while (itr != ptr) {
        int x = *itr;
        unite(x, x + 1);
        ++itr;
      }
    } else {
      int S = q.A;
      int T = q.B;
      int id = q.id;
      if (S >= T) {
        // cout << "Yes" << endl;
        ans.push_back({id, true});
      } else {
        int SS = getPar(S);
        if (SS >= T) {
          // cout << "Yes" << endl;
          ans.push_back({id, true});
        } else {
          // cout << "No" << endl;
          ans.push_back({id, false});
        }
      }
    }
  }
  sort(ans.begin(), ans.end());
  for (auto x : ans) {
    cout << (x.second ? "Yes" : "No") << endl;
  }
  return 0;
}