[CF911D]Inversion Counting
题目大意:
给你一个数列,翻转其中一个区间,问每次翻转过后逆序对个数的奇偶性。
思路:
首先树状数组求出一开始的奇偶性,然后考虑每次翻转对答案的贡献。
对于整个区间,我们可以把翻转转化成若干次交换。
也就是交换(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的更多相关文章
- Codeforces 911D. Inversion Counting (数学、思维)
题目链接:Inversion Counting 题意: 定义数列{ai|i=1,2,...,n}的逆序对如下:对于所有的1≤j<i≤n,若ai<aj,则<i,j>为一个逆序对. ...
- 题解 CF911D 【Inversion Counting】
这是一道看似复杂其实也不简单的思维题. 其实思路很明显. 因为这道题的数据范围比较大,有1e5的询问,如果暴力(像我考场上那样打平衡树)的话可以做到$mnlogn$. 但那样也是稳T. 经过思考之后我 ...
- 【Codeforces】CF 911 D. Inversion Counting(逆序对+思维)
题目 传送门:QWQ 分析 思维要求比较高. 首先我们要把原图的逆序对q算出来. 这个树状数组或归并排序都ok(树状数组不用离散化好评) 那么翻转$[l,r]$中的数怎么做呢? 暴力过不了,我试过了. ...
- 【Educational Codeforces Round 35 D】Inversion Counting
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 排列中交换任意两个数字. 排列的逆序对个数的奇偶性会发生变化. 翻转这个过程其实就是len/2对数字发生交换. 交换了偶数次的话,不 ...
- 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 ...
- Educational Codeforces Round 35 B/C/D
B. Two Cakes 传送门:http://codeforces.com/contest/911/problem/B 本题是一个数学问题. 有a个Ⅰ类球,b个Ⅱ类球:有n个盒子.将球放入盒子中,要 ...
- Educational Codeforces Round 35
Nearest Minimums 相同的数里最小的数里的最小距离 Solution Two Cakes Solution Three Garlands 瞎比试 Solution Inversion C ...
- codeforces 911D
D. Inversion Counting time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Gym - 102040B Counting Inversion (数位dp)
题意:求[a,b]区间内的数字中正序对的个数. 具体思路参考: https://blog.csdn.net/weixin_43135318/article/details/88061396 https ...
随机推荐
- [codechef MEXDIV]Mex division
题目链接:https://vjudge.net/contest/171650#problem/I 直接用set+dp水过去了... /* 设dp[i]表示前i个做划分满足条件的方案数 有一个显然的转移 ...
- npm install 权限的问题
用ctrl+r切换到对象的目录,以管理圆的身份执行 npm cache clean first. If that doesn’t fix things, take a look in %APPDATA ...
- Robot POJ - 1376
The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...
- 复杂的json分析
在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...
- springboot之mybatis别名的设置
mybatis别名设置 在具体的mapper.xml文件中,定义很多的statement,statement需要parameterType指定输入参数的类型.需要resultType指定输出结果的映射 ...
- 忘记mysq rootl密码后解决办法
如果mysql正在运行,/etc/init.d/mysqld stop 启动mysql(无需输入密码):bin/safe_mysqld –skip-grant-tables & 在bin目录下 ...
- iOS 后台运行执行代码(例如定位)
- Python阶段复习 - part 1 - Python基础练习题
1.实现1-100的所有的和 # 方法1: sum = 0 for i in range(1,101): sum += i print(sum) # 方法2: num1 = int(input('请输 ...
- 【MT8382/8121】为MTK的工厂测试添加测试项
摘要: 本文介绍添加MTK工厂测试项的步骤及调试技巧. 纲要: 1. 描述添加MTK工厂测试项的步骤 2.调试小技巧 1. 描述添加MTK工厂测试项的步骤 以添加红外测试为例: 1. mediatek ...
- python几个重要的函数(lambda,filter,reduce,map,zip)
一.匿名函数lambda lambda argument1,argument2,...argumentN :expression using arguments 1.lambda是一个表达式,而不是一 ...