题目大意:
  给你一个数列,翻转其中一个区间,问每次翻转过后逆序对个数的奇偶性。

思路:
  首先树状数组求出一开始的奇偶性,然后考虑每次翻转对答案的贡献。
  对于整个区间,我们可以把翻转转化成若干次交换。
  也就是交换(l,r),(l+1,r-1)...
  总共有(r-l+1)/2次。
  考虑每一次交换对答案的影响。
  设当前交换的数为(a,b),那么对于a,改变的逆序对数为区间[a,b]大于a的数与小于a的数之差,对于b同理。
  由于只要求奇偶性,那么加和减都差不多,所以我们可以都变成加法,那么a和b造成影响的总和是2(b-a),肯定是偶数。
  再算上(a,b)本身一对,肯定是奇数。
  这样我们就对答案有(r-l+1)/2次奇数的变化,看一下(2-l+1)/2是奇数还是偶数,然后和答案异或起来即可。

 #include<cstdio>
#include<cctype>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
int n;
class FenwickTree {
private:
int val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p) {
while(p<=n) {
val[p]++;
p+=lowbit(p);
}
}
int query(int p) const {
int ret=;
while(p) {
ret+=val[p];
p-=lowbit(p);
}
return ret;
}
};
FenwickTree t;
int main() {
n=getint();
int cnt=;
for(register int i=;i<=n;i++) {
const int x=getint();
cnt+=t.query(n-x+);
t.modify(n-x+);
}
bool ans=cnt&;
for(register int m=getint();m;m--) {
const int l=getint(),r=getint();
ans^=(r-l+)>>&;
puts(ans?"odd":"even");
}
return ;
}

[CF911D]Inversion Counting的更多相关文章

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

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

  2. 题解 CF911D 【Inversion Counting】

    这是一道看似复杂其实也不简单的思维题. 其实思路很明显. 因为这道题的数据范围比较大,有1e5的询问,如果暴力(像我考场上那样打平衡树)的话可以做到$mnlogn$. 但那样也是稳T. 经过思考之后我 ...

  3. 【Codeforces】CF 911 D. Inversion Counting(逆序对+思维)

    题目 传送门:QWQ 分析 思维要求比较高. 首先我们要把原图的逆序对q算出来. 这个树状数组或归并排序都ok(树状数组不用离散化好评) 那么翻转$[l,r]$中的数怎么做呢? 暴力过不了,我试过了. ...

  4. 【Educational Codeforces Round 35 D】Inversion Counting

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 排列中交换任意两个数字. 排列的逆序对个数的奇偶性会发生变化. 翻转这个过程其实就是len/2对数字发生交换. 交换了偶数次的话,不 ...

  5. Educational Codeforces Round 35 (Rated for Div. 2)A,B,C,D

    A. Nearest Minimums time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Educational Codeforces Round 35 B/C/D

    B. Two Cakes 传送门:http://codeforces.com/contest/911/problem/B 本题是一个数学问题. 有a个Ⅰ类球,b个Ⅱ类球:有n个盒子.将球放入盒子中,要 ...

  7. Educational Codeforces Round 35

    Nearest Minimums 相同的数里最小的数里的最小距离 Solution Two Cakes Solution Three Garlands 瞎比试 Solution Inversion C ...

  8. codeforces 911D

    D. Inversion Counting time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. Gym - 102040B Counting Inversion (数位dp)

    题意:求[a,b]区间内的数字中正序对的个数. 具体思路参考: https://blog.csdn.net/weixin_43135318/article/details/88061396 https ...

随机推荐

  1. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  2. java的多构造函数的处理方式

    /** * */ package P; import java.awt.List; import java.lang.reflect.Array; import java.util.ArrayList ...

  3. POJ 3179 Corral the Cows

    Corral the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1352   Accepted: 565 De ...

  4. Error in deleting blocks.

    2014-08-24 22:15:21,714 WARN org.apache.hadoop.hdfs.server.datanode.DataNode: Error processing datan ...

  5. html与地图

    html:<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta ...

  6. xcode 10 新特性

    这里主要介绍一下Xcode10 版本主要更新的内容.随着iOS12的发布,Xcode10已经可以从Mac App Store下载.Xcode10包含了iOS12.watchOS 5.macOS10.1 ...

  7. 【FJWC2017】交错和查询 [线段树]

    交错和查询 Time Limit: 10 Sec  Memory Limit: 256 MB Description 无限循环数字串S由长度为n的循环节s构成.设s为12345(n=5),则数字串S为 ...

  8. #学习方法2打印为空,说明#延迟加载#解决方案:将nameField等设置改在viewDidLoad中设置

    #学习方法#error调试#NSLog调试之无法进行数据传输,Edit无法现实之前编辑 的内容             https://www.evernote.com/shard/s227/sh/c ...

  9. CVE-2017-5521: Bypassing Authentication on NETGEAR Routers(Netgear认证绕过漏洞)

    SpiderLabs昨天发布的漏洞, 用户访问路由器的web控制界面尝试身份验证,然后又取消身份验证,用户就会被重定向到一个页面暴露密码恢复的token.然后通过passwordrecovered.c ...

  10. python3 内置函数(转)

    http://www.runoob.com/python/python-built-in-functions.html divmod(7,2) # 返回(3,1)商和余的元组 frozenset() ...