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

#include <bits/stdc++.h>
using namespace std;

template <typename T> class imos2 {
  int n, m;
  vector<vector<T>> v;

public:
  imos2(int n, int m) : n(n), m(m), v(n + 1, vector<T>(m + 1)) {}
  T &val(int i, int j) {
    assert(0 <= i && i < n && 0 <= j && j < m);
    return v[i + 1][j + 1];
  }
  void add(int li, int lj, int ri, int rj) {
    assert(0 <= li && li <= ri && ri <= n);
    assert(0 <= lj && lj <= rj && rj <= m);
    val(li, lj) += 1;
    if (rj < m)
      val(li, rj) -= 1;
    if (ri < n)
      val(ri, lj) -= 1;
    if (ri < n && rj < m)
      val(ri, rj) += 1;
  }
  void build() {
    for (int i = 1; i <= n; i++)
      for (int j = 0; j <= m; j++)
        v[i][j] += v[i - 1][j];
    for (int i = 0; i <= n; i++)
      for (int j = 1; j <= m; j++)
        v[i][j] += v[i][j - 1];
  }
  T sum(int li, int lj, int ri, int rj) {
    assert(0 <= li && li <= ri && ri <= n);
    assert(0 <= lj && lj <= rj && rj <= m);
    return v[ri][rj] - v[ri][lj] - v[li][rj] + v[li][lj];
  }
};

void press(vector<int> &v, int &size) {
  auto s = v;
  sort(s.begin(), s.end());
  s.erase(unique(s.begin(), s.end()), s.end());
  for (auto &val : v) {
    val = lower_bound(s.begin(), s.end(), val) - s.begin();
  }
  size = s.size();
}

int main() {
  int N, h, w;
  cin >> N;
  vector<int> x(N * 2), y(N * 2);
  for (int i = 0; i < N * 2; i++) {
    cin >> x[i] >> y[i];
  }
  press(x, h), press(y, w);
  imos2<int> im(h, w);
  for (int i = 0; i < N; i++) {
    im.add(x[i * 2], y[i * 2], x[i * 2 + 1], y[i * 2 + 1]);
  }
  im.build();
  int res = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      res = max(res, im.val(i, j));
    }
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

template <typename T> class node {
public:
  node<T> *l, *r;
  T val, add;
  node(T val_ = 0, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
      : l(l_), r(r_), val(val_), add(0) {}
};

template <typename T> class DynamicLazySegmentTree {
  const ll n;
  node<T> *root;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1ll;
    return res;
  }
  T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
    if (ub <= l || r <= lb)
      return 0;
    if (l <= lb && ub <= r)
      return n->val + n->add * (ub - lb);
    return n->add * (min(r, ub) - max(l, lb)) +
           (n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : 0) +
           (n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : 0);
  }
  void suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
    if (ub <= l || r <= lb)
      return;
    if (l <= lb && ub <= r) {
      n->add += val;
      return;
    }
    n->val += val * (min(r, ub) - max(l, lb));
    if (n->l == nullptr) {
      n->l = new node<T>();
    }
    if (n->r == nullptr) {
      n->r = new node<T>();
    }
    suc(l, r, n->l, lb, (lb + ub) / 2, val);
    suc(l, r, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree(ll n_) : n(size(n_)) { root = new node<T>(); }
  void add(ll l, ll r, T val) { suc(l, r + 1, root, 0, n, val); }
  T getSum(ll l, ll r) { return sub(l, r + 1, root, 0, n); }
};

template <typename T> class node2 {
public:
  node2<T> *l, *r;
  DynamicLazySegmentTree<T> val, add;
  node2(ll w, node2<T> *l_ = nullptr, node2<T> *r_ = nullptr)
      : l(l_), r(r_), val(w), add(w) {}
};

template <typename T> class DynamicLazySegmentTree2 {
  const ll h, w;
  node2<T> *root;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1ll;
    return res;
  }
  T sub(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub) {
    if (ub <= li || ri <= lb)
      return 0;
    if (li <= lb && ub <= ri)
      return n->val.getSum(lj, rj - 1) + n->add.getSum(lj, rj - 1) * (ub - lb);
    return n->add.getSum(lj, rj - 1) * (min(ri, ub) - max(li, lb)) +
           (n->l == nullptr ? 0
                            : sub(li, lj, ri, rj, n->l, lb, (lb + ub) / 2)) +
           (n->r == nullptr ? 0 : sub(li, lj, ri, rj, n->r, (lb + ub) / 2, ub));
  }
  void suc(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub, T val) {
    if (ub <= li || ri <= lb)
      return;
    if (li <= lb && ub <= ri) {
      n->add.add(lj, rj - 1, val);
      return;
    }
    n->val.add(lj, rj - 1, val * (min(ri, ub) - max(li, lb)));
    if (n->l == nullptr) {
      n->l = new node2<T>(w);
    }
    if (n->r == nullptr) {
      n->r = new node2<T>(w);
    }
    suc(li, lj, ri, rj, n->l, lb, (lb + ub) / 2, val);
    suc(li, lj, ri, rj, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree2(ll h_, ll w_) : h(size(h_)), w(size(w_)) {
    root = new node2<T>(w);
  }
  void add(ll li, ll lj, ll ri, ll rj, T val) {
    suc(li, lj, ri + 1, rj + 1, root, 0, h, val);
  }
  T getSum(ll li, ll lj, ll ri, ll rj) {
    return sub(li, lj, ri + 1, rj + 1, root, 0, h);
  }
};

int main() {
  cin.sync_with_stdio(false);
  int N;
  cin >> N;
  DynamicLazySegmentTree2<ll> dlst(1000ll, 1000ll);
  for (int i = 0, x1, x2, y1, y2; i < N; i++) {
    cin >> x1 >> y1 >> x2 >> y2;
    dlst.add(x1, y1, x2 - 1, y2 - 1, 1);
  }
  ll res = 0;
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      res = max(res, dlst.getSum(i, j, i, j));
    }
  }
  cout << res << endl;
  return 0;
}