Language: C++

#include<bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

ll bit[131072];
ll sum(int i){
	ll res = 0;
	for(; i; i -= i & -i) res += bit[i];
	return res;
}
void add(int i, int x){
	for(; i < 131072; i += i & -i) bit[i] += x;
}

const int B = 500;
int n, p[100000];
ll ans[200000];

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	cin >> n;
	rep(i, n) cin >> p[i];
	int q; cin >> q;
	
	vector<pair<pi, int>> v; //l/B, r, l
	rep(i, q){
		int l, r; cin >> l >> r; l--;
		v.pb(mp(mp(l / B, r), mp(l, i)));
	}
	int cnt = 0;
	sort(all(v));
	int L = 0, R = 0; ll res = 0;
	for(auto &i: v){
		int r = i.first.second, l = i.second.first;
		while(R < r) { assert(L <= R); res += R - L - sum(p[R]);         add(p[R++], 1); }
		while(L > l) { assert(L <= R); res += sum(p[--L]);               add(p[L], 1); }
		while(L < l) { assert(L < R); res -= sum(p[L]) - 1;             add(p[L++], -1); }
		while(R > r) { assert(L < R); res -= R - L; res += sum(p[--R]); add(p[R], -1); }
		ans[i.second.second] = res;
	}
	rep(i, q) printf("%lld\n", ans[i]);
	
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:41:21: error: no matching function for call to 'std::vector<std::pair<std::pair<int, int>, int> >::push_back(std::pair<std::pair<int, int>, std::pair<int, int> >)'
   41 |                 v.pb(mp(mp(l / B, r), mp(l, i)));
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<std::pair<int, int>, int>; _Alloc = std::allocator<std::pair<std::pair<int, int>, int> >; value_type = std::pair<std::pair<int, int>, int>]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'std::pair<std::pair<int, int>, std::pair<int, int> >' to 'const std::vector<std::pair<std::pair<int, int>, int> >::value_type&' {aka 'const std::pair<std::pair<int, int>, int>&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<std::pair<int, int>, int>; _Alloc = std::allocator<std::pair<std::pair<int, int>, int> >; value_type = std::pair<std::pair<int, int>, int>]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'std::pair<std::pair<int, int>, std::pair<int, int> >' to 'std::vector<std::pair<std::pair<int, int>, int> >::value_type&&' {aka 'std::pair<std::pair<int, int>, int>&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
a.cc:47:54: error: request for member 'first' in 'i.std::pair<std::pair<int, int>, int>::second', which is of non-class type 'int'
   47 |                 int r = i.first.second, l = i.second.first;
      |                                                      ^~~~~
a.cc:52:30: error: request for member 'second' in 'i.std::pair<std::pair<int, int>, int>::second', which is of non-class type 'int'
   52 |                 ans[i.second.second] = res;
      |                              ^~~~~~