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 <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = 1e6 + 10;
- int segment_tree[maxn];
- void build(int node, int L, int R) {
- if(L == R) {
- segment_tree[node] = 0;
- }
- else {
- int mid = (L + R) / 2;
- build(2 * node, L, mid);
- build(2 * node + 1, mid + 1, R);
- segment_tree[node] = segment_tree[2 * node] + segment_tree[2 * node + 1];
- }
- }
- int query(int node, int L, int R, 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(2 * node, L, mid, i, j) + query(2 * node + 1, mid + 1, R, i, j);
- }
- void update(int node, int L, int R, int idx) {
- if(L == R) {
- segment_tree[node] += 1;
- return;
- }
- int mid = (L + R) / 2;
- if(idx <= mid) {
- update(2 * node, L, mid, idx);
- }
- else {
- update(2 * node + 1, mid + 1, R, idx);
- }
- segment_tree[node] = segment_tree[2 * node] + segment_tree[2 * node + 1];
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- vector<int> v(n);
- vector<pair<int, int> > compressed;
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- compressed.push_back(make_pair(v[i], i));
- }
- sort(compressed.begin(), compressed.end());
- vector<int> tmp;
- for(int i = 0; i < n; i++) {
- tmp.push_back(compressed[i].second);
- }
- reverse(tmp.begin(), tmp.end());
- build(1, 0, n);
- vector<ll> smaller;
- for(int i = 0; i < n; i++) {
- update(1, 0, n, tmp[i]);
- smaller.push_back(query(1, 0, n, 0, tmp[i] - 1));
- }
- build(1, 0, n);
- vector<ll> bigger;
- for(int i = n - 1; i >= 0; i--) {
- update(1, 0, n, tmp[i]);
- bigger.push_back(query(1, 0, n, tmp[i] + 1, n));
- }
- reverse(bigger.begin(), bigger.end());
- ll res = 0;
- for(int i = 0; i < n; i++) {
- res += bigger[i] * smaller[i];
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement