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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, k, n) for (int i = k; i < (int)n; i++)
#define rei(n) for (int i = 0; i < (int)n; i++)
#define rel(n) for (int l = 0; l < (int)n; l++)
// #define int long long
#define al(s) s.begin(), s.end()
#define INT_MAX 2147483647
typedef pair<int, int> pi;
typedef pair<pi, pi> ppi;
typedef pair<int, ppi> pipp;
typedef pair<int, pi> pip;
typedef pair<long long, pi> llpi;

struct SegTree {
  int N;
  vector<int> Seg;
  void init(int n) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < N * 2; i++) {
      Seg.push_back(INT_MAX);
    }
  }

  void update(int k, int a) {
    k = k - 1 + N;
    Seg[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      Seg[k] = min(Seg[2 * k + 1], Seg[2 * k + 2]);
    }
  }
  int find_min(int a, int b) { return query(a, b, 0, 0, N); }

  int query(int a, int b, int k, int l, int r) {
    if (r <= a || l >= b)
      return INT_MAX;
    if (l >= a && r <= b) {
      return Seg[k];
    } else {
      return min(query(a, b, k * 2 + 1, l, (r + l) / 2),
                 query(a, b, k * 2 + 2, (r + l) / 2, r));
    }
  }
};

SegTree ss[4]; // min,max

vector<llpi> s;
vector<long long> a;
int main() {
  long long ans = 0;
  long long n, d;
  cin >> n >> d;
  rei(4) ss[i].init(n);
  for (int i = 0; i < n; i++) {
    long long b;
    int x, y;
    cin >> x >> y >> b;
    s.push_back(llpi(b, pi(x, y)));
  }
  sort(al(s));
  for (int i = 0; i < s.size(); i++)
    a.push_back(s[i].first);
  for (int i = 0; i < n; i++) {
    ss[0].update(i, s[i].second.first);
    ss[2].update(i, s[i].second.first * (-1));

    ss[1].update(i, s[i].second.second);
    ss[3].update(i, s[i].second.second * (-1));
  }
  // bool end = false;
  for (vector<long long>::iterator i = a.begin();;) {
    vector<long long>::iterator E;
    E = upper_bound(al(a), (long long)(*i + d));
    int AA, BB;
    AA = i - a.begin();
    BB = E - a.begin();
    i = upper_bound(al(a), (long long)(*i));
    // cout << AA << " " << BB << endl;
    ans = max(ans, (long long)((-1) * ss[2].find_min(AA, BB) -
                               ss[0].find_min(AA, BB)) *
                       (long long)((-1) * ss[3].find_min(AA, BB) -
                                   ss[1].find_min(AA, BB)));
    if (E == a.end())
      break;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, k, n) for (int i = k; i < (int)n; i++)
#define rei(n) for (int i = 0; i < (int)n; i++)
#define rel(n) for (int l = 0; l < (int)n; l++)
// #define int long long
#define al(s) s.begin(), s.end()
#define INT_MAX 2147483647
typedef pair<int, int> pi;
typedef pair<pi, pi> ppi;
typedef pair<int, ppi> pipp;
typedef pair<int, pi> pip;
typedef pair<long long, pi> llpi;

struct SegTree {
  int N;
  vector<int> Seg;
  void init(int n) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < N * 2; i++) {
      Seg.push_back(INT_MAX);
    }
  }

  void update(int k, int a) {
    k = k - 1 + N;
    Seg[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      Seg[k] = min(Seg[2 * k + 1], Seg[2 * k + 2]);
    }
  }
  int find_min(int a, int b) { return query(a, b, 0, 0, N); }

  int query(int a, int b, int k, int l, int r) {
    if (r <= a || l >= b)
      return INT_MAX;
    if (l >= a && r <= b) {
      return Seg[k];
    } else {
      return min(query(a, b, k * 2 + 1, l, (r + l) / 2),
                 query(a, b, k * 2 + 2, (r + l) / 2, r));
    }
  }
};

SegTree ss[4]; // min,max

vector<llpi> a;
// set<long long> s;
vector<long long> s;
int main() {
  long long ans = 0;
  long long n, d;
  cin >> n >> d;
  rei(4) ss[i].init(n);
  for (int i = 0; i < n; i++) {
    long long b;
    int x, y;
    cin >> x >> y >> b;
    a.push_back(llpi(b, pi(x, y)));
    s.push_back(b);
  }
  sort(al(s));
  for (vector<long long>::iterator i = s.begin() + 1; i != s.end();) {
    if (*i == *(i - 1)) {
      i = s.erase(i);
    } else {
      i++;
    }
  }
  for (int i = 0; i < n; i++) {
    int jj = 0;
    vector<long long>::iterator it = lower_bound(al(s), a[i].first);
    jj = it - s.begin();
    /*for (vector<long long>::iterator l = s.begin(); l != it; l++)
    {
            jj++;
    }
    */

    ss[0].update(jj, min(a[i].second.first, ss[0].Seg[jj + ss[0].N - 1]));
    ss[2].update(jj,
                 min(a[i].second.first * (-1), ss[2].Seg[jj + ss[0].N - 1]));

    ss[1].update(jj, min(a[i].second.second, ss[1].Seg[jj + ss[0].N - 1]));
    ss[3].update(jj,
                 min(a[i].second.second * (-1), ss[3].Seg[jj + ss[0].N - 1]));
  }
  bool end = false;
  for (int i = 0; i < s.size(); i++) {
    if (end)
      break;
    vector<long long>::iterator it, ne;
    it = s.begin() + i;
    //	for (int l = 0; l < i; l++)it++;
    ne = upper_bound(al(s), *it + d);
    if (ne == s.end())
      end = true;
    ne--;
    int coun = ne - it;
    // for (; it != ne; it++)coun++;
    coun += i;
    coun++;
    ans = max(ans, (long long)((-1) * ss[2].find_min(i, coun) -
                               ss[0].find_min(i, coun)) *
                       (long long)((-1) * ss[3].find_min(i, coun) -
                                   ss[1].find_min(i, coun)));
  }
  cout << ans << endl;
}