Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <queue>
- #include <iostream>
- #include <vector>
- #include <cstring>
- #include <iostream>
- #include <set>
- #include <cstring>
- #include <stack>
- #include <algorithm>
- #include <map>
- #include <cmath>
- //#include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = 100005;
- const ll INF = 3e16 + 10;
- int segment_tree[3 * maxn];
- void build_tree(int L, int R, int node) {
- if(L == R) {
- segment_tree[node] = 0;
- }
- else {
- int mid = (L + R) / 2;
- build_tree(L, mid, 2 * node);
- build_tree(mid + 1, R, 2 * node + 1);
- segment_tree[node] = segment_tree[2 * node] + segment_tree[2 * node + 1];
- }
- }
- void update(int L, int R, int node, int idx, int new_value) {
- if(L == R) {
- segment_tree[node] = new_value;
- return;
- }
- int mid = (L + R) / 2;
- if(idx <= mid) {
- update(L, mid, 2 * node, idx, new_value);
- }
- else {
- update(mid + 1, R, 2 * node + 1, idx, new_value);
- }
- segment_tree[node] = segment_tree[2 * node] + segment_tree[2 * node + 1];
- }
- int query(int L, int R, int node, int i, int j) {
- // L R i L R j L R
- if(i <= L and R <= j) {
- return segment_tree[node];
- }
- if(R < i or j < L) {
- return 0;
- }
- int mid = (L + R) / 2;
- return query(L, mid, 2 * node, i, j) + query(mid + 1, R, 2 * node + 1, i, j);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- vector<int> v(n);
- vector<pair<int, int> > cords;
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- cords.push_back(make_pair(v[i], i));
- }
- sort(cords.begin(), cords.end());
- vector<int> compressed;
- for(int i = 0; i < n; i++) {
- compressed.push_back(cords[i].second);
- }
- build_tree(0, n, 1);
- vector<ll> right_smaller(n);
- for(int i = n - 1; i >= 0; i--) {
- update(0, n, 1, compressed[i], 1);
- right_smaller[i] = query(0, n, 1, 0, compressed[i] - 1);
- }
- vector<ll> left_bigger(n);
- build_tree(0, n, 1);
- for(int i = 0; i < n; i++) {
- update(0, n, 1, compressed[i], 1);
- left_bigger[i] = query(0, n, 1, compressed[i] + 1, n);
- }
- ll res = 0;
- for(int i = 0; i < n; i++) {
- res += right_smaller[i] * left_bigger[i];
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement