forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
40 lines (36 loc) · 664 Bytes
/
E.cpp
File metadata and controls
40 lines (36 loc) · 664 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
int tree[100100], n;
void add(int x)
{
for (int i = x; i <= n; i += i & -i)
tree[i]++;
}
int get(int x)
{
int res = 0;
for (int i = x; i; i -= i & -i)
res += tree[i];
return res;
}
int main()
{
ios::sync_with_stdio(0);
int x;
cin >> n;
long long ans = 0;
vector < pair<int,int> > a;
for (int i = 1; i <= n; i++)
{
cin >> x;
if (x < i) a.push_back({x, -i});
else a.push_back({i, -x});
}
sort(a.begin(), a.end());
for (auto u : a)
{
ans += get(-u.second - 1) - get(u.first);
add(-u.second);
}
cout << ans << endl;
}