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

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

    hadoop服务器更换硬盘操作步骤(datanode hadoop目录${HADOOP_HOME}/bin    日志位置:/var/log/hadoop)1.登陆服务器,切换到mapred用户,执行 ...

  2. tomcat内存配置及配置参数详解

    1.jvm内存管理机制: 1)堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟 ...

  3. 请把<ul><li>第1行</li><li>第2行</li>...</ul>(ul之间有10个li元素)插入body里面,注意:需要考虑到性能问题。

    var html = [],i;for(i = 0; i < 10; i++){ html.push('<ul><li>第' + (i+1) + '行</li> ...

  4. React 开发常见报错解决方法

    1. 使用 redux 的异步 action 时浏览器报错: Error: Actions must be plain objects. Use custom middleware for async ...

  5. io缓冲为何可以提高效率

    问题 据我了解,运用FileInputStream读写一段数据是一个字节一个字节的读取,如果有10个字节大小的文件,就要调用10次系统调用,每次将读取的数据赋值给变量,然后程序使用变量. 缓冲区可以看 ...

  6. Xcode5根控制器使用xib展示的步骤

    #error:Xcode5根控制器使用xib展示,步骤 ⓵取消mainInterface ⓶右击file's owner对xib进行view-view连线,否则: Terminating app du ...

  7. HDU1166(线段树单点更新区间查询)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. UVA 10303 How Many Trees? (catlan)

    刚开始没看出时卡特兰数列.直接套高精度版 #include <map> #include <set> #include <list> #include <cm ...

  9. DRF的异常处理

    默认情况下,DRF框架通过内置的 exception_handler 方法,处理了如下异常: django内置异常 Http404 PermissionDenied DRF框架异常 APIExcept ...

  10. nginx之旅:安装及简单部署

    安装之前最好了解一下nginx,参考nginx百度百科吧,下面这一句话基本概括了nginx的基本功能 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理  ...