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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

struct segtree {

  bool check(set<P> &T, int a, int b) {
    set<P>::iterator it;
    it = T.lower_bound(P(a, a));
    if (it != T.end() && it->first <= b)
      return true;
    if (it != T.begin() && a <= (--it)->second)
      return true;
    return false;
  }

  void insert(set<P> &T, int a, int b) {
    set<P>::iterator it;
    while (1) {
      it = T.lower_bound(P(a, a));
      if (it != T.end() && it->first <= b) {
        a = min(a, it->first);
        b = max(b, it->second);
        T.erase(it);
      } else if (it != T.begin() && a <= (--it)->second) {
        a = min(a, it->first);
        b = max(b, it->second);
        T.erase(it);
      } else
        break;
    }
    T.insert(P(a, b));
  }

  set<P> t[(1 << 19)], u[(1 << 19)];

  void init() {
    for (int i = 0; i < (1 << 19); i++)
      t[i].clear(), u[i].clear();
  }

  bool check(int a, int b, int c, int d, int k, int l, int r) {
    if (b <= l || r <= a)
      return false;
    if (check(t[k], c, d))
      return true;
    if (a <= l && r <= b)
      return check(u[k], c, d);
    int m = (l + r) / 2;
    return check(a, b, c, d, k * 2 + 1, l, m) ||
           check(a, b, c, d, k * 2 + 2, m, r);
  }

  void insert(int a, int b, int c, int d, int k, int l, int r) {
    if (b <= l || r <= a)
      return;
    insert(u[k], c, d);
    if (a <= l && r <= b) {
      insert(t[k], c, d);
      return;
    }
    int m = (l + r) / 2;
    insert(a, b, c, d, k * 2 + 1, l, m);
    insert(a, b, c, d, k * 2 + 2, m, r);
  }
};

segtree T;

#define MAX_N 100005
int N;
int ax[MAX_N], ay[MAX_N], bx[MAX_N], by[MAX_N];

int main() {
  scanf("%d", &N);
  vector<int> X(N + N), Y(N + N);

  for (int i = 0; i < N; i++) {
    scanf("%d %d %d %d", &ax[i], &ay[i], &bx[i], &by[i]);
    if (ax[i] > bx[i])
      swap(ax[i], bx[i]);
    if (ay[i] > by[i])
      swap(ay[i], by[i]);

    X[i] = ax[i], X[i + N] = bx[i];
    Y[i] = ay[i], Y[i + N] = by[i];
  }

  sort(X.begin(), X.end());
  X.erase(unique(X.begin(), X.end()), X.end());

  sort(Y.begin(), Y.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());

  for (int i = 0; i < N; i++) {
    ax[i] = lower_bound(X.begin(), X.end(), ax[i]) - X.begin() + 1;
    ay[i] = lower_bound(Y.begin(), Y.end(), ay[i]) - Y.begin() + 1;
    bx[i] = lower_bound(X.begin(), X.end(), bx[i]) - X.begin() + 1;
    by[i] = lower_bound(Y.begin(), Y.end(), by[i]) - Y.begin() + 1;
  }

  for (int i = 0; i < N; i++) {
    if (T.check(ax[i], bx[i] + 1, ay[i], by[i], 0, 0, (1 << 18))) {
      printf("0\\n");
    } else {
      T.insert(ax[i], bx[i] + 1, ay[i], by[i], 0, 0, (1 << 18));
      printf("1\\n");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 200005

struct BIT {
  unordered_map<ll, ll> dat;
  void Add(ll i, ll val) {
    while (0 < i && i < MAX) {
      dat[i] += val;
      i += (i & -i);
    }
  }
  ll Sum(ll i) {
    ll res = 0;
    while (i) {
      if (dat.count(i) > 0)
        res += dat[i];
      i -= (i & -i);
    }
    return res;
  }
};

struct RSQ {
  BIT A, B;
  void Add(ll i, ll val) {
    //    A.Add(1,val);
    A.Add(i, -val);
    B.Add(i, val * i);
  }
  ll Sum(ll i) { return A.Sum(i) * i + B.Sum(i); }
};

struct BIT2 {
  unordered_map<ll, RSQ> dat;
  void Add(ll x, ll y, ll val) {
    while (0 < x && x < MAX) {
      dat[x].Add(y, val);
      x += (x & -x);
    }
  }
  ll Sum(ll x, ll y) {
    ll res = 0;
    while (x) {
      if (dat.count(x) > 0)
        res += dat[x].Sum(y);
      x -= (x & -x);
    }
    return res;
  }
};

struct RSQ2 {
  BIT2 A, B;
  void Add(ll x, ll y, ll val) {
    //    A.Add(1,y,val);
    A.Add(x, y, -val);
    B.Add(x, y, val * x);
  }
  ll Sum(ll x, ll y) { return A.Sum(x, y) * x + B.Sum(x, y); }
};

#define MAX_N 100005

RSQ2 T;
int N;
int ax[MAX_N], ay[MAX_N], bx[MAX_N], by[MAX_N];

ll Sum(int ax, int ay, int bx, int by) {
  ll res = 0;
  res += T.Sum(ax - 1, ay - 1);
  res += T.Sum(bx, by);
  res -= T.Sum(ax - 1, by);
  res -= T.Sum(bx, ay - 1);
  return res;
}

void Add(int ax, int ay, int bx, int by, ll val) {
  T.Add(ax - 1, ay - 1, val);
  T.Add(bx, by, val);
  T.Add(ax - 1, by, -val);
  T.Add(bx, ay - 1, -val);
}

int main() {

  scanf("%d", &N);
  vector<int> X(N + N), Y(N + N);

  for (int i = 0; i < N; i++) {
    scanf("%d %d %d %d", &ax[i], &ay[i], &bx[i], &by[i]);
    if (ax[i] > bx[i])
      swap(ax[i], bx[i]);
    if (ay[i] > by[i])
      swap(ay[i], by[i]);

    X[i] = ax[i], X[i + N] = bx[i];
    Y[i] = ay[i], Y[i + N] = by[i];
  }

  sort(X.begin(), X.end());
  X.erase(unique(X.begin(), X.end()), X.end());

  sort(Y.begin(), Y.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());

  for (int i = 0; i < N; i++) {
    ax[i] = lower_bound(X.begin(), X.end(), ax[i]) - X.begin() + 2;
    ay[i] = lower_bound(Y.begin(), Y.end(), ay[i]) - Y.begin() + 2;
    bx[i] = lower_bound(X.begin(), X.end(), bx[i]) - X.begin() + 2;
    by[i] = lower_bound(Y.begin(), Y.end(), by[i]) - Y.begin() + 2;
  }

  for (int i = 0; i < N; i++) {
    if (Sum(ax[i], ay[i], bx[i], by[i])) {
      printf("0\\n");
    } else {
      printf("1\\n");
      Add(ax[i], ay[i], bx[i], by[i], 1);
    }
  }
  return 0;
}