SGU180:Inversions(树状数组)】的更多相关文章

There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j]. Input The first line of the input contains the number N. The second line contains N numbers A…
Inversions time limit per test: 0.25 sec. memory limit per test: 4096 KB input: standard output: standard There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A…
题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri]的逆序对数小于等于K,且ri的值最大.(ri对应代码中的cnt数组) 显然ri单调不降,我们可以通过用两个指针扫一遍,利用树状数组计算出r数组. 对于每个询问L,R,我们要计算的是∑i=LR[min(R,ri)−i+1] 由于ri具有单调性,那我们直接在上面二分即可,然后记一个前缀和(s数组).…
                                                                E. Infinite Inversions                                                                                          time limit per test 2 seconds                                            …
思路及代码参考:https://blog.csdn.net/u014800748/article/details/45420085 There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We performed n swap operations with this sequence. A swap(a, b) is an ope…
题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组求逆序数的理解:输入数据并利用树状数组求出前边比它小和等于它的数据有几个,用输入数据的总的个数减去比它小的数就是比它大的数res,将所有的res加起来就是要求的序列的逆序数. 如图: 把所有的res加起来就是答案了 代码: #include <iostream> #include <cstd…
Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N个数a[1],a[2] ... a[N],有M个操作,每个操作给出x,y两个数,你将a[x],a[y]交换,然后求交换后数组的逆序对个数.逆序对的意思是1 <= i < j <= N 且a[i] > a[j].…
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an integer sequence a.Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will ha…
A-Combination Lock  B-School Marks   C-Ice Cave   D-Bad Luck Island   E-Infinite Inversions E:Infinite Inversions 题意就是有一个序列1,2,3,4..... 现在有n次交换,每次都把ab交换求最终形成的序列的逆序数: 逆序数分为两部分.一部分是交换过位置的,另一部分是没有交换过的. 离散化后,利用树状数组求出交换过的位置的逆序数的个数. 第二部分: 看一个样例: 2 1 6 9 5…
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树状数组,但是发现那些没有交换的数也会产生逆序对数,但我没有算. 经明神提示, 把没有用到的数字段化成点.然后用树状数组算一下就好了. 然后我用一个数组记录每个点的长度.比如 <1,2><5,6>,1,2,3,4,5,6只有1,2,5,6用到了,那么离散化为1,2,3,4,5,f[1]=…