1003(树状数组+逆序对)
来源:互联网 发布:淘宝买的入职体检报告 编辑:程序博客网 时间:2024/06/08 18:34
Problem Description
Recently yifenfei face such a problem that give you millions of positive integers,tell how many pairs i and j that satisfy F[i] smaller than F[j] strictly when i is smaller than j strictly. i and j is the serial number in the interger sequence. Of course, the problem is not over, the initial interger sequence will change all the time. Changing format is like this [S E] (abs(E-S)<=1000) that mean between the S and E of the sequece will Rotate one times.
For example initial sequence is 1 2 3 4 5.
If changing format is [1 3], than the sequence will be 1 3 4 2 5 because the first sequence is base from 0.
For example initial sequence is 1 2 3 4 5.
If changing format is [1 3], than the sequence will be 1 3 4 2 5 because the first sequence is base from 0.
Input
The input contains multiple test cases. Each case first given a integer n standing the length of integer sequence (2<=n<=3000000) Second a line with n integers standing F[i](0<F[i]<=10000) Third a line with one integer m (m < 10000) Than m lines quiry, first give the type of quiry. A character C, if C is ‘R’ than give the changing format, if C equal to ‘Q’, just put the numbers of satisfy pairs.
Output
Output just according to said.
Sample Input
51 2 3 4 53QR 1 3Q
Sample Output
108
求逆序对数,并且要让一个区间的数循环一遍,找出最大的逆序对数。
思路:
先按照模板把逆序对数求出来,然后把区间的数依次循环,求出每个数字位置改变后的新逆序对数。然后选出最大的。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int a[3000000];int c[10001];int lowbit(int x){ return x&(-x);}void add(int x,int v){ while(x<=10001) { c[x]+=v; x+=lowbit(x); }}long long sum(int x){ long long sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum;}int main(){ int n,m; while(~scanf("%d",&n)) { long long ans=0; memset(c,0,sizeof(c)); for(int i=0;i<n;i++) { scanf("%d",&a[i]); ans+=sum(a[i]-1); add(a[i],1); } scanf("%d",&m); char fi[2]; int q,w,vv; while(m--) { scanf("%s",fi); if(fi[0]=='Q') { printf("%I64d\n",ans); } else { scanf("%d%d",&q,&w); vv=a[q]; for(int i=q;i<w;i++) { a[i]=a[i+1]; if(vv>a[i])ans++; if(vv<a[i])ans--; } a[w]=vv; } } } return 0;}
阅读全文
0 0
- 1003(树状数组+逆序对)
- 树状数组逆序对
- 求逆序对 (树状数组版)
- POJ3067(树状数组逆序对)
- 逆序对(树状数组/归并)
- 洛谷P1908 逆序对(树状数组)
- hihoCoder 1524 : 逆序对 (树状数组)
- 逆序对的求法(树状数组)
- poj1007 树状数组逆序对
- 树状数组求逆序对
- 树状数组求逆序对
- 树状数组求逆序对
- 树状数组求逆序对
- 树状数组求逆序对
- 逆序对-树状数组-1
- 树状数组求逆序对
- 树状数组求逆序对
- 树状数组求逆序对
- ios中的锁(Lock) NSLock,NSConditionLock,NSRecursiveLock,NSCondition
- github基本使用
- gpu gems——第23章 头发动画和渲染
- MediaMetadataRetriever的使用 (一)getFrameAtTime 总是返回第一帧数
- OpenShift负责人谈PaaS、Docker、Kubernetes及与CloudFoundry的竞争
- 1003(树状数组+逆序对)
- 抽奖概率-三种算法
- 一个常见的编程题(C++版)
- 收缩转换 c++更安全的转换机制、constexpr
- 带括号的四则优先级运算的算法详细源码实现
- [Winform]js与webbrowser交互
- 求输入矩阵的对角线之和
- 关于下拉框append option后没有选项的原因
- REST架构分析