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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int a[2700000], b[2700000], d[2700000];
long long c, bit1[2700000], bit2[2700000], size_;
void add(int l, int r, long long x) {
  int R1 = l;
  while (R1 < size_) {
    bit1[R1] += (-1LL * x * (l - 1));
    R1 += (R1 & -R1);
  }
  int R2 = r + 1;
  while (R2 < size_) {
    bit1[R2] += x * r;
    R2 += (R2 & -R2);
  }
  int R3 = l;
  while (R3 < size_) {
    bit2[R3] += x;
    R3 += (R3 & -R3);
  }
  int R4 = r + 1;
  while (R4 < size_) {
    bit2[R4] += -x;
    R4 += (R4 & -R4);
  }
}
long long sums(long long r) {
  long long R1 = 0, S1 = r;
  while (S1 >= 1) {
    R1 += bit1[S1];
    S1 -= (S1 & -S1);
  }
  long long R2 = 0, S2 = r;
  while (S2 >= 1) {
    R2 += bit2[S2];
    S2 -= (S2 & -S2);
  }
  return R1 + R2 * r;
}
long long sum(long long l, long long r) { return sums(r) - sums(l - 1); }
int main() {
  long long n, m, p1, p2, p3, p4;
  cin >> n >> m >> p1 >> p2 >> p3 >> p4;
  a[0] = p1;
  for (int i = 1; i < n; i++) {
    a[i] = (a[i - 1] * 58 + p2) % (m + 1);
  }
  b[0] = p3;
  for (int i = 1; i < m; i++) {
    b[i] = (b[i - 1] * 58 + p4) % (n + 1);
  }
  sort(a, a + n);
  sort(b, b + m);
  size_ = m + 3;
  for (int i = 1; i <= m; i++) {
    if (b[i - 1] >= 1)
      add(i, i, b[i - 1]);
  }
  add(m + 1, m + 1, 1145141919);
  add(m + 2, m + 2, 1145141919);
  for (int i = 0; i < n; i++) {
    int pos1 = m - a[i] + 1;
    int L = 1, R = pos1 + 2, M;
    while (true) {
      M = (L + R) / 2;
      long long T1 = sum(pos1, pos1) - sum(M - 1, M - 1);
      long long T2 = sum(pos1, pos1) - sum(M, M);
      if ((T1 >= 1 && T2 <= 0) || (M == 1 && T1 <= 0)) {
        break;
      }
      if (T1 <= 0)
        R = M;
      if (T2 >= 1)
        L = M;
    }
    L = pos1 - 1, R = m + 1;
    int M2;
    while (true) {
      M2 = (L + R) / 2;
      long long T1 = sum(M2, M2) - sum(pos1, pos1);
      long long T2 = sum(M2 + 1, M2 + 1) - sum(pos1, pos1);
      if ((T1 <= 0 && T2 >= 1) || (M2 == m && T2 <= 0)) {
        break;
      }
      if (T2 <= 0)
        L = M2;
      if (T1 >= 1)
        R = M2;
    }
    // for (int i = 1; i <= m; i++)cout << sum(i, i) << ' '; cout << endl;
    add(M2 + 1, m, -1);
    if (sum(pos1, pos1) == 0) {
      c += (m - M2);
    } else {
      add(M, M + (a[i] - (m - M2)) - 1, -1);
      c += a[i];
    }
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int a[2700000], b[2700000], d[2700000];
long long c;
class BIT {
  long long size_;
  std::vector<long long> bit1, bit2;

public:
  BIT()
      : size_(0), bit1(std::vector<long long>()),
        bit2(std::vector<long long>()){};
  BIT(long long size__) : size_(size__) {
    bit1.resize(size_);
    bit2.resize(size_);
    for (int i = 0; i < size_; i++) {
      bit1[i] = 0;
      bit2[i] = 0;
    }
  }
  inline void add(int l, int r, long long x) {
    int R1 = l;
    while (R1 < size_) {
      bit1[R1] += (-1LL * x * (l - 1));
      R1 += (R1 & -R1);
    }
    int R2 = r + 1;
    while (R2 < size_) {
      bit1[R2] += x * r;
      R2 += (R2 & -R2);
    }
    int R3 = l;
    while (R3 < size_) {
      bit2[R3] += x;
      R3 += (R3 & -R3);
    }
    int R4 = r + 1;
    while (R4 < size_) {
      bit2[R4] += -x;
      R4 += (R4 & -R4);
    }
  }
  inline long long sums(long long r) {
    long long R1 = 0, S1 = r;
    while (S1 >= 1) {
      R1 += bit1[S1];
      S1 -= (S1 & -S1);
    }
    long long R2 = 0, S2 = r;
    while (S2 >= 1) {
      R2 += bit2[S2];
      S2 -= (S2 & -S2);
    }
    return R1 + R2 * r;
  }
  inline long long sum(long long l, long long r) {
    return sums(r) - sums(l - 1);
  }
};
int main() {
  long long n, m, p1, p2, p3, p4;
  cin >> n >> m >> p1 >> p2 >> p3 >> p4;
  a[0] = p1;
  for (int i = 1; i < n; i++) {
    a[i] = (a[i - 1] * 58 + p2) % (m + 1);
  }
  b[0] = p3;
  for (int i = 1; i < m; i++) {
    b[i] = (b[i - 1] * 58 + p4) % (n + 1);
  }
  sort(a, a + n);
  sort(b, b + m);
  BIT B(m + 3);
  for (int i = 1; i <= m; i++) {
    if (b[i - 1] >= 1)
      B.add(i, i, b[i - 1]);
  }
  B.add(m + 1, m + 1, 1145141919);
  B.add(m + 2, m + 2, 1145141919);
  for (int i = 0; i < n; i++) {
    int pos1 = m - a[i] + 1;
    int L = 1, R = pos1 + 2, M;
    while (true) {
      M = (L + R) / 2;
      long long T1 = B.sum(pos1, pos1) - B.sum(M - 1, M - 1);
      long long T2 = B.sum(pos1, pos1) - B.sum(M, M);
      if ((T1 >= 1 && T2 <= 0) || (M == 1 && T1 <= 0)) {
        break;
      }
      if (T1 <= 0)
        R = M;
      if (T2 >= 1)
        L = M;
    }
    L = pos1 - 1, R = m + 1;
    int M2;
    while (true) {
      M2 = (L + R) / 2;
      long long T1 = B.sum(M2, M2) - B.sum(pos1, pos1);
      long long T2 = B.sum(M2 + 1, M2 + 1) - B.sum(pos1, pos1);
      if ((T1 <= 0 && T2 >= 1) || (M2 == m && T2 <= 0)) {
        break;
      }
      if (T2 <= 0)
        L = M2;
      if (T1 >= 1)
        R = M2;
    }
    // for (int i = 1; i <= m; i++)cout << B.sum(i, i) << ' '; cout << endl;
    B.add(M2 + 1, m, -1);
    if (B.sum(pos1, pos1) == 0) {
      c += (m - M2);
    } else {
      B.add(M, M + (a[i] - (m - M2)) - 1, -1);
      c += a[i];
    }
  }
  cout << c << endl;
  return 0;
}