911D - Inversion Counting - CodeForces Solution


brute force math *1800

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <map>

using namespace std;

const int N = 2010;
int a[N], b[N], cnt[N];
int ans = 0;
int pos[N];
int n, m;
void merge_sort(int l, int r)
{
    if (l >= r) return;

    int mid = l + r >> 1;
    merge_sort(l, mid), merge_sort(mid + 1, r);

    int i = l, j = mid + 1, k = 0;
    while (i <= mid && j <= r) {
        if (a[i] <= a[j]) {
            b[k++] = a[i++];
        }
        else {         
            ans += mid - i + 1;
            pos[j] += mid - i + 1;
            b[k++] = a[j++];
        }
    }
    while (i <= mid) b[k++] = a[i++];
    while (j <= r) b[k++] = a[j++];

    for (int p = 0, q = l; p < k; p++, q++) a[q] = b[p];
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    merge_sort(1, n);

    int res = ans & 1;
    cin >> m;
    for (int i = 1; i <= m; i++) {
        int l, r;
        cin >> l >> r;
        int len = r - l + 1;
        if (len * (len - 1) / 2 & 1) res ^= 1;    
        if (res % 2 != 0) cout << "odd" << endl;
        else cout << "even" << endl;
    }

    system("pause");
    return 0;
}
    			 	 		 		      		     			


Comments

Submit
0 Comments
More Questions

85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why