Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <cstring>
- #include <algorithm>
- #include <fstream>
- using namespace std;
- const int maxn = 1e6 + 5;
- int segment_tree[3 * maxn];
- void build_tree(int L, int R, int position) {
- if(L == R) {
- segment_tree[position] = 0;
- }
- else {
- int middle = (L + R) / 2;
- build_tree(L, middle, 2 * position);
- build_tree(middle + 1, R, 2 * position + 1);
- segment_tree[position] = segment_tree[2 * position] + segment_tree[2 * position + 1];
- }
- }
- int query(int L, int R, int position, int i, int j) {
- // L R i L R j L R
- if(i <= L and R <= j) {
- return segment_tree[position];
- }
- if(L > j or R < i) {
- return 0;
- }
- int middle = (L + R) / 2;
- return query(L, middle, 2 * position, i, j) + query(middle + 1, R, 2 * position + 1, i, j);
- }
- void update(int L, int R, int position, int idx) {
- if(L == R) {
- segment_tree[position] = 1;
- return;
- }
- int middle = (L + R) / 2;
- if(idx <= middle) {
- update(L, middle, 2 * position, idx);
- }
- else {
- update(middle + 1, R, 2 * position + 1, idx);
- }
- segment_tree[position] = segment_tree[2 * position] + segment_tree[2 * position + 1];
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- vector<int> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- build_tree(0, n, 1);
- long long result = 0;
- for(int i = n - 1; i >= 0; i--) {
- update(0, n, 1, v[i]);
- result += query(0, n, 1, 0, v[i] - 1);
- }
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement