逆序数 技巧题

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

 题目分析 : 让你求一下再经过 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值