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

思路:
  首先树状数组求出一开始的奇偶性,然后考虑每次翻转对答案的贡献。
  对于整个区间,我们可以把翻转转化成若干次交换。
  也就是交换(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 #525 (Div. 2)D. Ehab and another another xor problem

    D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...

  2. LwIP - 打开keepalive功能

    在服务器端打开keepalive功能 1.保证LWIP_TCP_KEEPALIVE被定义为1,(这样TCP_KEEPIDLE.TCP_KEEPINTVL和TCP_KEEPCNT 设置才有效) 2. i ...

  3. 在Ubuntu下编译WebKit源码--qt

    转载自:http://www.cnblogs.com/panderen/archive/2011/10/18/2216154.html 在朋友的介绍下有幸認识了WebKit这个让人心动的开源浏览器内核 ...

  4. Spring Framework框架解析(1)- 从图书馆示例来看xml文件的加载过程

    引言 这个系列是我阅读Spring源码后的一个总结,会从Spring Framework框架的整体结构进行分析,不会先入为主的讲解IOC或者AOP的原理,如果读者有使用Spring的经验再好不过.鉴于 ...

  5. flume高级组件及各种报错

    1,one source two channel 创建conf文件,内容如下: #定义agent名, source.channel.sink的名称 access.sources = r1 access ...

  6. codevs3304 水果姐逛水果街Ⅰ

    题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...

  7. Opencv 中透视变换函数对IplImage图像变换时出现的问题?

    最近一直在做视频稳像的项目,为了简化部分实现,使用了部分Opencv的函数,其中包括Opencv中对IplImage进行同时变换的函数cvWarpPerspective(src, dst,...) 发 ...

  8. XML解析代码

    import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.LinkedLi ...

  9. phpstorm调试环境XDebug搭建

    1.安装XDebug,通过phpinfo()可以看到xdebug是否已安装!!!如果未安装,可以先看我的另一篇关于XDebug安装的文章:http://www.cnblogs.com/cxscode/ ...

  10. python中的with与上下文管理器

    #转载请留言联系 很多人平时需要打开文件进行读取写入操作时,通常这样: f = open('文件路径','w') f.write(data) f.close 这样写有一个潜在的问题,如果在调用 wri ...