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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;

#define Seg_Max_N (1 << 19)

using Value = long long int;
const Value ini = -1e18;
struct segtree {
  int N;
  vector<Value> dat;

  segtree() {}
  segtree(int n) : dat(2 * Seg_Max_N) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++) {
      dat[i] = ini;
    }
  }
  Value connect(const Value &l, const Value &r) { return max(l, r); }
  // update k th element
  void update(int k, Value a) {
    k += N - 1;
    dat[k] = a;

    while (k > 0) {
      k = (k - 1) / 2;
      const Value al(dat[k * 2 + 1]);
      const Value ar(dat[k * 2 + 2]);
      dat[k] = connect(al, ar);
    }
  }
  // min [a, b)
  Value query(int a, int b) { return query(a, b, 0, 0, N); }
  Value query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return ini;
    if (a <= l and r <= b)
      return dat[k];
    const int m = (l + r) / 2;
    const Value al(query(a, b, k * 2 + 1, l, m));
    const Value ar(query(a, b, k * 2 + 2, m, r));
    return connect(al, ar);
  }
};

int main() {
  int N;
  cin >> N;

  // size,value
  vector<pair<long long int, long long int>> ps;
  vector<long long int> v;
  for (int i = 0; i < N; ++i) {
    long long int a, b;
    scanf("%lld %lld", &a, &b);
    ps.emplace_back(a, b);
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  map<long long int, int> mp;
  vector<long long int> revmp(v.size());
  for (int i = 0; i < v.size(); ++i) {
    mp[v[i]] = i;
    revmp[i] = v[i];
  }
  for (auto &p : ps) {
    p.first = mp[p.first];
  }

  vector<long long int> sas(v.size() - 1);
  for (int i = 0; i < v.size() - 1; ++i) {
    sas[i] = revmp[i + 1] - revmp[i];
  }
  vector<long long int> arts(v.size());
  for (auto &p : ps) {
    arts[p.first] += p.second;
  }

  segtree seg(v.size());
  seg.update(0, 0);
  vector<long long int> as(N);
  for (int i = 0; i < v.size() - 1; ++i) {
    long long int x = seg.query(i, i + 1) + arts[i + 1] - sas[i];
    seg.update(i + 1, x);
    as[i + 1] = x;
  }

  long long int ans = 0;
  for (int i = 0; i < v.size(); ++i) {
    ans = max(ans, arts[i] + seg.query(i, v.size()) - as[i]);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;

#define Seg_Max_N (1 << 19)

using Value = long long int;
const Value ini = -1e18;
struct segtree {
  int N;
  vector<Value> dat;

  segtree() {}
  segtree(int n) : dat(2 * Seg_Max_N) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++) {
      dat[i] = ini;
    }
  }
  Value connect(const Value &l, const Value &r) { return max(l, r); }
  // update k th element
  void update(int k, Value a) {
    k += N - 1;
    dat[k] = a;

    while (k > 0) {
      k = (k - 1) / 2;
      const Value al(dat[k * 2 + 1]);
      const Value ar(dat[k * 2 + 2]);
      dat[k] = connect(al, ar);
    }
  }
  // min [a, b)
  Value query(int a, int b) { return query(a, b, 0, 0, N); }
  Value query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return ini;
    if (a <= l and r <= b)
      return dat[k];
    const int m = (l + r) / 2;
    const Value al(query(a, b, k * 2 + 1, l, m));
    const Value ar(query(a, b, k * 2 + 2, m, r));
    return connect(al, ar);
  }
};

int main() {
  int N;
  cin >> N;

  // size,value
  vector<pair<long long int, long long int>> ps;
  vector<long long int> v;
  for (int i = 0; i < N; ++i) {
    long long int a, b;
    scanf("%ld %ld", &a, &b);
    ps.emplace_back(a, b);
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  map<long long int, int> mp;
  map<int, long long int> revmp;
  for (int i = 0; i < v.size(); ++i) {
    mp[v[i]] = i;
    revmp[i] = v[i];
  }
  for (auto &p : ps) {
    p.first = mp[p.first];
  }

  vector<long long int> sas(v.size() - 1);
  for (int i = 0; i < v.size() - 1; ++i) {
    sas[i] = revmp[i + 1] - revmp[i];
  }
  vector<long long int> arts(v.size());
  for (auto p : ps) {
    arts[p.first] += p.second;
  }

  segtree seg(v.size());
  seg.update(0, 0);
  vector<long long int> as(N);
  for (int i = 0; i < v.size() - 1; ++i) {
    long long int x = seg.query(i, i + 1) + arts[i + 1] - sas[i];
    seg.update(i + 1, x);
    as[i + 1] = x;
  }

  long long int ans = 0;
  for (int i = 0; i < v.size(); ++i) {
    ans = max(ans, arts[i] + seg.query(i, v.size()) - as[i]);
  }
  cout << ans << endl;

  return 0;
}