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 }

最新文章

  1. JavaScript - 原型
  2. 移动前端不得不了解的html5 head 头标签
  3. IOS网络第六天 ASI (略)
  4. Laravel 5.3 请求处理管道详解
  5. 标准C++中的string类的用法总结
  6. HTML DOM Document 对象
  7. 隐藏左侧快速导航除DMS导航树之外的其他区域
  8. RPC和Socket,RMI和RPC之间的关系
  9. vijos1144(小胖守皇宫)
  10. Chrome浏览器插件
  11. 转&lt;&lt;C#集合Dictionary中按值的降序排列
  12. 尽量不要用工具频繁去查询排名结果_seo优化禁忌
  13. EasyUI DataGrid View
  14. ZOJ3675:Trim the Nails
  15. 51nod 1244 莫比乌斯函数之和(杜教筛)
  16. Java语言Socket接口用法详解
  17. Winform控件输入的字母转换成大写
  18. Linux怎么查看软件安装路径 查看mysql安装在哪
  19. zookeeper入门系列 : 分布式事务
  20. Linux记录-安装LAMP和R环境

热门文章

  1. SpringBoot 好“吃”的启动原理
  2. Windows下的python虚拟环境设置
  3. k8s之PV、PVC、StorageClass详解
  4. iDRAC RAC0218 最大会话数
  5. __new__() to create it, and __init__() to customize it 类方法 实例方法
  6. socket创建和结束
  7. OPC UA 统一架构) (二)
  8. TRUNK与VTP
  9. 项目action:前台传多个dataWrap给后台
  10. python中字符串的翻转(方法总结)