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 }

codeforces 911D的更多相关文章

  1. Codeforces 911D. Inversion Counting (数学、思维)

    题目链接:Inversion Counting 题意: 定义数列{ai|i=1,2,...,n}的逆序对如下:对于所有的1≤j<i≤n,若ai<aj,则<i,j>为一个逆序对. ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. uni-app开发经验分享十八:对接第三方h5

    1.uni-app中对接第三方为了防止跳出app使用了webview <template> <view> <web-view :src="url" @ ...

  2. 常用的hadoop和yarn的端口总结

    节点 默认端口 用途说明 HDFS DataNode 50010 datanode服务端口,用于数据传输 50075 http服务的端口 50475 https服务的端口 50020 ipc服务的端口 ...

  3. cc 攻击

    糟糕,系统又被攻击了 原创 二马读书 二马读书 10月16日 几年前,我们的电商平台遭遇过一次CC攻击(一种分布式网络攻击,后面有详细介绍

  4. The WebSocket Protocol 1000

    https://tools.ietf.org/html/rfc6455 https://tools.ietf.org/html/rfc6455 7.4.1. Defined Status Codes ...

  5. https://www.cnblogs.com/AloneSword/p/3209653.html

    proc/sys/net/ipv4/下各项的意义 - 孤剑 - 博客园 https://www.cnblogs.com/AloneSword/p/3209653.html

  6. OpenSSL 常见对称加密算法特性分析

    在选择加密算法,面对一大长串的选项时,大家都有这样的疑问,究竟哪种加密方式是最好的呢? 对于加密方式.算法来说,一般安全性与性能呈负相关,越是安全的,对性能要求则更高. 现在主流的加密协议的安全性均能 ...

  7. 用好Java中的枚举真的没有那么简单

    1.概览 在本文中,我们将看到什么是 Java 枚举,它们解决了哪些问题以及如何在实践中使用 Java 枚举实现一些设计模式. enum关键字在 java5 中引入,表示一种特殊类型的类,其总是继承j ...

  8. LOJ10098

    USACO 2006 Jan. Gold 为了从F个草场中的一个走到另一个,贝茜和她的同伴们不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会 ...

  9. Spring5源码,Spring DispatecherServlet的生命周期

    一.前端控制器模式 二.DispatcherServlet的执行链 三.DispatcherServlet 1.策略初始化 2.请求预处理 3.请求处理 4.视图解析 5.处理调度请求 - 视图渲染 ...

  10. 将将List json 转成List<?>实体

    package TestJson; import java.util.ArrayList; import java.util.List; import java.util.Map; import ne ...