逆序数 技巧题

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

31 2 321 22 3

Output

oddeven

Input

41 2 4 341 11 41 42 3

Output

oddoddoddeven

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).

题目分析 : 让你求一下再经过 m 次操作后,序列中的逆序数是多少,会发现一个规律, n 个数的序列中最多有逆序数 (n - 1) * n / 2 , 那么当你交换此序列的顺序后逆序数会变成 sum - 原有的逆序数 , 然后呢 再看此题,它就是让你判断一个奇偶性 ,可以借着此规律搞搞 就出来了

代码示例 :

#define ll long long

int pre[1505];

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n, m;
    int l, r;

    cin >> n;
    for(int i = 1; i <= n; i++){
        scanf("%d", &pre[i]);
    }
    cin >> m;
    ll cnt = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j < i; j++){
            if (pre[i] < pre[j]) cnt++;
        }
    }
    for(int i = 1; i <= m; i++){
        scanf("%d%d", &l, &r);
        int t = r - l + 1;
        int y = (t-1)*t/2;
        cnt += 1ll*y;
        if (cnt % 2 == 0) printf("even\n");
        else printf("odd\n");
    }

    return 0;
}

原文地址:https://www.cnblogs.com/ccut-ry/p/8143864.html

时间: 2024-08-29 01:41:21

逆序数 技巧题的相关文章

51Nod 1019 逆序数(线段树)

题目链接:逆序数 模板题. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i, a, b) for (int i(a); i <= (b); ++i) 6 #define lson i << 1, L, mid 7 #define rson i << 1 | 1, mid + 1, R 8 9 const int N = 100010; 10 11 long long ans

hiho一下 第三十九周(逆序数)

题目连接:点击打开链接 解题思路: 逆序数模板题.注意此题坑点在于数据大,开成unsigned long long 完整代码: #include <algorithm> #include <iostream> #include <cstring> #include <complex> #include <cstdio> #include <string> #include <cmath> #include <queu

hdu 1394 Minimum Inversion Number 归并求逆序数

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12107    Accepted Submission(s): 7388 Problem Description The inversion number of a given number sequence a1, a2, ..., a

51Nod1019题(逆序数)

最开始不知道归并排序,于是这道题就各种花式T,最好的一次1015ms,也是醉了. 说下最开始的想法:   用<set>插入第一个数,随后输入的数依次比较<set>中的元素,如果与其中 <= 其中一个数, 则后面的数都可作为其逆序数,用  “<set>当前容量” 减去 “当前数的位置” 就是这个数所产生的逆序数对数,最后插入该数, 还是粘一下代码吧(TLE) #include <iostream> #include <cstdio> #inc

E - 归并排序 求逆序数

Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to

1019 逆序数

1019 逆序数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序,逆序数是4.给出一个整数序列,求该序列的逆序数. Input 第1行:N,N为序列的长度(n <= 50000) 第2 - N + 1行:序列中的元素(0 <= A[i] <= 10^9) Ou

POJ3067 Japan【树状数组】【逆序数】

题目链接: http://poj.org/problem?id=3067 题目大意: 有两排的城市,一排N个城市,编号为1~N,一排M个城市,编号为1~M.这两排城市之间有K条路. 路都是直线连接,问:这些路,有多少道路是相交的,并且焦点不是城市所在的点,求出交点个数. 思路: 树状数组的思想.参考网上的图,先将所有边(u,v)按u升序排列,如果u相同,则按v升序排列.可 以看出来,路(u1,v1)和路(u2,v2)如果有交点的话,u1 > u2 并且 v1 < v2,或者 u1 < u

HDU2689 Sort it【树状数组】【逆序数】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2689 题目大意: 求把一个具有N个不同元素的序列通过交换两个相邻的元素转换成升序序列需要进行的交换次数 是多少. 例如:1 2 3 5 4,只需要交换5和4,交换次数为1次. 思路: 典型的求逆序数题.其实可以直接暴力过.但是用树状数组效率比较高.对于值为a第i个元素, 需要交换次数为前i个元素中大于a的元素个数,即逆序数. 用树状数组来做,数组Tree[i]表示数字i是否在序列中出现过,如果数字

HDU 4911 Inversion 求逆序数对

点击打开链接 Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1106    Accepted Submission(s): 474 Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent num