Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAXN = 1e5 + 10;
- array<int, MAXN> v, bit;
- void update(int id, int val)
- {
- while (id < MAXN)
- {
- bit[id] += val;
- id += id&-id;
- }
- }
- int query(int id)
- {
- int resp = 0;
- while (id > 0)
- {
- resp += bit[id];
- id -= id&-id;
- }
- return resp;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int N; cin >> N;
- for (int i = 0; i < N; i++) cin >> v[i];
- long long resp = 0;
- for (int i = N-1; i >= 0; i--)
- {
- resp += query(v[i]-1);
- update(v[i], 1);
- }
- cout << resp << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment