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

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
ll MOD = 1000000007;

struct bit {
  vector<ll> v;
  bit(int n) : v(n + 1) {}
  ll sum(int i) {
    ll res = 0;
    for (; i > 0; i -= i & -i)
      res += v[i];
    return res;
  }
  void add(int i, ll x) {
    for (i++; i < v.size(); i += i & -i)
      v[i] += x;
  }
};

int B = 300;

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &p[i]);
    p[i]--;
  }
  vector<vector<int>> num(N / B + 1, vector<int>(N + 1));
  for (int i = 0; i < N; i++)
    num[p[i] / B][i + 1]++;
  for (int j = 0; j <= N / B; j++)
    for (int i = 0; i < N; i++)
      num[j][i + 1] += num[j][i];
  int Q;
  cin >> Q;
  vector<vector<int>> query(N);
  vector<int> l(Q), r(Q);
  for (int q = 0; q < Q; q++) {
    scanf("%d%d", &l[q], &r[q]);
    l[q]--;
    r[q]--;
    query[r[q]].push_back(q);
  }
  vector<int> index(N, -1);
  bit bt(N);
  vector<int> A(N / B + 1);
  vector<vector<int>> v(N / B + 1);
  vector<vector<ll>> sum(N / B + 1, vector<ll>(1));
  ll cur = 0;
  vector<ll> ans(Q);
  for (int i = 0; i < N; i++) {
    for (int x = N - 1; x > p[i];) {
      if (x % B == B - 1 && x - B >= p[i]) {
        A[x / B]++;
        cur += v[x / B].size();
        x -= B;
      } else {
        if (index[x] != -1) {
          bt.add(index[x], 1);
          cur++;
        }
        x--;
      }
    }
    int x = p[i];
    index[x] = i;
    ll prev = sum[x / B].back();
    v[x / B].push_back(i);
    sum[x / B].push_back(prev + A[x / B]);
    for (int k = 0; k < query[i].size(); k++) {
      int q = query[i][k];
      ans[q] = cur - bt.sum(l[q]);
      for (int i = 0; i <= N / B; i++) {
        int j = num[i][l[q]];
        ans[q] -= (ll)j * A[i] - sum[i][j];
      }
    }
  }
  for (int q = 0; q < Q; q++)
    printf("%lld\\n", ans[q]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define gep(i, g, j) for (int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it, c)                                                            \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define rng(a) a.begin(), a.end()
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define df(x) int x = in()
#define dame                                                                   \\
  {                                                                            \\
    puts("0");                                                                 \\
    return 0;                                                                  \\
  }
#define show(x) cout << #x << " = " << x << endl;
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline void priv(vi a) {
  rep(i, sz(a)) printf("%d%c", a[i], i == sz(a) - 1 ? '\\n' : ' ');
}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T> string join(vector<T> &v) {
  stringstream s;
  rep(i, sz(v)) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T> ostream &operator<<(ostream &o, vector<T> &v) {
  if (sz(v))
    o << join(v);
  return o;
}
template <typename T1, typename T2>
istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, pair<T1, T2> &v) {
  return o << v.fi << "," << v.se;
}

const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;

const int D = 200, E = 501;
// Binary Indexed Tree
struct bit {
  typedef int TT;
  vector<TT> d;
  int n;
  vi hist;
  bit() {}
  bit(int mx) : n(mx), d(mx) {}
  void add(int i, TT x = 1) {
    for (++i; i < n; i += i & -i) {
      hist.pb(i);
      d[i] += x;
    }
  }
  TT sum(int i) {
    TT x = 0;
    for (++i; i; i -= i & -i)
      x += d[i];
    return x;
  }
  void rev() {
    for (int i : hist)
      d[i] = 0;
    hist.clear();
  }
};
//

int n, a[MX];
ll dl[E][MX], dr[E][MX];

int main() {
  // preprocess
  scanf("%d", &n);
  rep(i, n) scanf("%d", &a[i]), a[i] = n - a[i];
  bit t(n + 2);
  for (int i = 0; i <= n; i += D) {
    int di = i / D;
    ll sum = 0;
    for (int j = i; j < n; ++j) {
      sum += t.sum(a[j]);
      dl[di][j] = sum;
      t.add(a[j]);
    }
    t.rev();
    sum = 0;
    for (int j = i - 1; j >= 0; --j) {
      sum += t.sum(n - a[j]);
      dr[di][j] = sum;
      t.add(n - a[j]);
    }
    t.rev();
  }

  // online query
  int q;
  scanf("%d", &q);
  rep(qi, q) {
    int l, r;
    scanf("%d%d", &l, &r);
    --l;
    ll ans = 0;
    if (r - l <= D) {
      for (int i = l; i < r; ++i) {
        ans += t.sum(a[i]);
        t.add(a[i]);
      }
      t.rev();
    } else {
      int li = l / D + 1, ri = r / D;
      ans += dl[li][r - 1];
      ans += dr[ri][l];
      ans -= dl[li][ri * D - 1];
      vp p;
      for (int i = li * D - 1; i >= l; --i)
        p.pb(P(a[i], 0));
      for (int i = ri * D; i < r; ++i)
        p.pb(P(a[i], 1));
      sort(rng(p));
      int s = 0;
      for (P &np : p) {
        if (np.se)
          ans += s;
        else
          s++;
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}