ACDream - Dynamic Inversions II
先上题目:
A - Dynamic Inversions II
Problem Description
逆序对的意思是1 <= i < j <= N 且a[i] > a[j].
Input
多组数据,每组数据:
两个数N,M,接下来一行有N个数a[1]... a[N]
最后M行每行两个数x,y
1 <= N,M <= 10^5, 1 <= x < y <= N,1 <= a[i] <= N
Output
Sample Input
2 1
1 2
1 2
Sample Output
0
1 因为结果要求的是逆序对的二进制最低位是多少,所以我们需要分析一下变换了位置以后的就变化情况。
先求一次逆序对。
再分析情况,发现逆序对的奇偶性变化只有两个数之间的数会带来变化。
① ······大····小······ --> ······小····大······
中间的数有三种情况:a.比大的大 b.比大的小,比小的大 c.比小的小
对于三种情况逆序对的变化情况:
| 小 | 大 | ||||
| 大 | 减少 | 减少 | 增加 | ||
| 小 | 增加 | 减少 | 减少 |
其中减少和增加的量是相等的,那就是说这样变化的结果是偶数对-1对。
② ······小····大······ --> ······大····小······
中间的数有三种情况:a.比大的大 b.比大的小,比小的大 c.比小的小
对于三种情况逆序对的变化情况:
| 小 | 大 | ||||
| 大 | 增加 | 增加 | 减少 | ||
| 小 | 减少 | 增加 | 增加 |
其中减少和增加的量是相等的,那就是说这样变化的结果是偶数对+1对。 ③ ······a····a······ -->不变 所以我们需要做的是判断交换的两个数是不是相等,如果是相等就不变化奇偶性,否则奇偶性变化一次。 上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) (x & (-x))
#define MAX 100002
#define LL long long
using namespace std; int n,m; int c[MAX],a[MAX]; void add(int x){
for(;x<=n;x+=lowbit(x)) c[x]++;
} LL sum(int x){
LL ans=;
for(;x>;x-=lowbit(x)) ans+=c[x];
return ans;
} int main()
{
int x,y;
LL s;
while(scanf("%d %d",&n,&m)!=EOF){
memset(c,,sizeof(c));
s=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
add(a[i]);
s+=sum(n)-sum(a[i]);
}
bool f=s&;
if(f) puts("");
else puts("");
for(int i=;i<m;i++){
scanf("%d %d",&x,&y);
if(x!=y && a[x]!=a[y]) f=f^;
swap(a[x],a[y]);
if(f) puts("");
else puts("");
}
}
return ;
}
Dynamic InversionsII
ACDream - Dynamic Inversions II的更多相关文章
- Dynamic Inversions II 逆序数的性质 树状数组求逆序数
Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...
- Dynamic Inversions 50个树状数组
Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- [Express] Level 3: Massaging User Data
Flexible Routes Our current route only works when the city name argument matches exactly the propert ...
- Kooboo中怎么新增一个关联的Details 动态页面。
Kooboo中怎么新增一个关联的Details 动态页面. 有几个要点: 1. Sub Page的Parent Page 必须是英文书写.如果是中文会出现找不到页面 500错误 2. 要在Page M ...
- 国外成熟的程序交易系统的思路 z
波涛(1998)在<系统交易方法>中提出,一个设计良好的交易系统,必须对投资决策的各个相关环节做出相应明确的规定,同时还必须符合使用者的心理特征.投资对象的统计特征以及投资资金的风险特征. ...
- Reading task(Introduction to Algorithms. 2nd)
Introduction to Algorithms 2nd ed. Cambridge, MA: MIT Press, 2001. ISBN: 9780262032933. Introduction ...
- oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)
LAST UPDATE: 1 Dec 15, 2016 APPLIES TO: 1 2 3 4 Oracle Database - Enterprise Edition - Versi ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- Why is try {…} finally {…} good; try {…} catch{} bad?
http://stackoverflow.com/questions/128818/why-is-try-finally-good-try-catch-bad The big difference i ...
- bzoj4977 跳伞求生——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4977 今天讲的贪心题,真神奇啊: 首先,要得到尽量多选队友的解: 把队友按 a[i] 从小到 ...
- Coursera Algorithms week3 归并排序 练习测验: Merging with smaller auxiliary array
题目原文: Suppose that the subarray a[0] to a[n-1] is sorted and the subarray a[n] to a[2*n-1] is sorted ...
- JDK5.0新特性(静态导入、自动装箱/拆箱、增强for循环、可变参数、枚举、泛形)
JDK5中新增了很多新的java特性,利用这些新语法可以帮助开发人员编写出更加高效.清晰,安全的代码. 这些新特性主要有:1.静态导入2.自动装箱/拆箱3.增强for循环4.可变参数5.枚举6.泛型7 ...
- z-index 、层叠上下文、层叠级别、z-index失效
一.z-index z-index默认处于非激活状态,只有定位元素(即position:relative/absolute/fixed时)才会被激活. z-index与层叠上下文关联. 当z-inde ...
- bzoj1588: [HNOI2002]营业额统计(权值线段树)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 16863 Solved: 6789[Submit][Sta ...
- python 端口扫描程序
#! /usr/bin/env python3 #-*- coding:utf-8 -*- import socket import threading OPEN_COUNT = 0 lock = t ...
- mysql数据库的介绍及安装
一.什么是数据库 1.什么是数据(Data) 描述事物的符号记录成为数据,描述事物的符号既可以是文字.图片.图像.声音.语言等,数据有多种表现形式,他们都可以经过数字化后存入计算机 在计算机中描述一个 ...
- Elasticsearch之更新(全部更新和局部更新)
前面的基础, Elasticsearch之curl创建索引库 Elasticsearch之curl创建索引 Elasticsearch之curl创建索引库和索引时注意事项 Elasticsearch之 ...
- android黑科技系列——Xposed框架实现拦截系统方法详解
一.前言 关于Xposed框架相信大家应该不陌生了,他是Android中Hook技术的一个著名的框架,还有一个框架是CydiaSubstrate,但是这个框架是收费的,而且个人觉得不怎么好用,而Xpo ...