codeforces 911D

时间:2024-01-18 13:04:44
D. Inversion Counting
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

Output

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.

Examples
Input
3
1 2 3
2
1 2
2 3
Output
odd
even
Input
4
1 2 4 3
4
1 1
1 4
1 4
2 3
Output
odd
odd
odd
even
Note

The first example:

  1. after the first query a = [2, 1, 3], inversion: (2, 1);
  2. after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2).

The second example:

  1. a = [1, 2, 4, 3], inversion: (4, 3);
  2. a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3);
  3. a = [1, 2, 4, 3], inversion: (4, 3);
  4. a = [1, 4, 2, 3], inversions: (3, 2), (4, 2).

(这一题思路很巧妙

题意:给n个不相同的数,每次询问都会将l到r的数翻转,判断每次翻转后所有数的逆序数和的奇偶性。

解题思路:因为每段序列的逆序数和最大的情况是该序列从大到小排序,此时逆序数和为max=(r-l+1)*(r-l)/2 。那么设序列的逆序数和为m,则其翻转后为max-m。若max为偶数,则m与max-m的奇偶性一致,也就是说翻转无影响。而若max为奇数,则m与max-m的奇偶性不同,则翻转会改变答案。

也就是说,只需要先求出整个序列的逆序数和的奇偶性,再根据每次翻转,判断是否会改变答案的奇偶性就行了。

ac代码如下:

 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 using namespace std;
5 const int maxn = 1500+10;
6 int nu[maxn];
7 int cnt=0;
8 int main()
9 {
10 int n;
11 scanf("%d",&n);
12 for(int i=1;i<=n;++i)
13 scanf("%d",&nu[i]);
14 for(int i=1;i<=n;++i)
15 {
16 for(int j=1;j<i;++j)
17 {
18 if(nu[j]>nu[i])
19 {
20 cnt++;
21 }
22 }
23 }
24 cnt%=2;
25 int m;
26 scanf("%d",&m);
27 int l,r;
28 for(int i=1;i<=m;++i)
29 {
30 scanf("%d%d",&l,&r);
31 if((r-l+1)*(r-l)/2%2==1) cnt^=1;
32 if(cnt) printf("odd\n");
33 else printf("even\n");
34 }
35 return 0;
36 }