A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).
You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].
After each query you have to determine whether the number of inversions is odd or even.
The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct.
The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process.
Then m lines follow, i-th line containing two integers li, ri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.
Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.
3 1 2 3 2 1 2 2 3
odd even
4 1 2 4 3 4 1 1 1 4 1 4 2 3
odd odd odd even
The first example:
- after the first query a = [2, 1, 3], inversion: (2, 1);
- after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2).
The second example:
- a = [1, 2, 4, 3], inversion: (4, 3);
- a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3);
- a = [1, 2, 4, 3], inversion: (4, 3);
- a = [1, 4, 2, 3], inversions: (3, 2), (4, 2).
题意:有一个大小为n的数组,其中的元素从1-n,输入l和r,把l和r区间内的数翻转,求数组中符合下列条件的个数:i > j && a[i] < a[j];
思路:把[l,r]区间内的数全部翻转,也就相当于把r翻转r-1次,r-1翻转r-2次....一次类推,[l,r]区间内的数共翻转(r-l+1)*(r-l)/ 2次,如果该数为偶数,则奇偶性没有改变,否则奇偶性改变。
#include <bits/stdc++.h>
using namespace std;
int a[1100],n,m,ret;
int main(){
scanf("%d",&n);
for(int i = 1; i <= n; i ++) scanf("%d",&a[i]);
for(int i = 1; i <= n; i ++){
for(int j = 1; j < i; j ++){
if(a[j] > a[i]) ret ^= 1;
}
}
scanf("%d",&m);
int l,r;
while(m --){
scanf("%d%d",&l,&r);
int detal = (r - l + 1) * (r - l) / 2;
if(detal & 1) ret ^= 1;
puts(ret&1 ? "odd" : "even");
}
return 0;
}