树状数组_Rotate HDU2688
来源:互联网 发布:java怎么反序列化 编辑:程序博客网 时间:2024/05/18 02:37
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.
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题意:给定一个区间,求满足(ai<aj && i<j)的序对的个数.遇到Q输出答案,遇到R对一个子区间进行rotate.比如,子区间a,b,c进行rotate后变成b,c,a.值得注意的是给定R 1 3,实际子区间是[2,4].分析:用树状数组能很好的求出原序列中正序对的个数ans,然后只需对子区间进行rotate并更新ans值.这题时间卡得有点无语,963ms过的.并且有时会过不了.AC代码:#include <cstdio>#include <cstring>using namespace std;#define lowbit(x) (x&-x)#define maxn 3000030typedef long long ll;ll n,c[10005],a[maxn];ll sum(ll x){ ll res=0; while(x>0) { res+=c[x]; x-=lowbit(x); } return res;}void add(ll x,ll d){ while(x<=10005) { c[x]+=d; x+=lowbit(x); }}int main(){ while(scanf("%lld",&n)!=EOF) { for(ll i=1;i<=n;i++) scanf("%lld",&a[i]); memset(c,0,sizeof(c)); ll ans=0; for(ll i=1;i<=n;i++) { add(a[i],1); ans+=sum(a[i]-1); } ll t; scanf("%lld",&t); while(t--) { char ch; getchar(); scanf("%c",&ch); if(ch=='R') { ll l,r; scanf("%lld%lld",&l,&r); l++,r++; ll tmp=a[l]; for(ll i=l+1;i<=r;i++) { if(tmp<a[i]) ans--; else if(tmp>a[i]) ans++; a[i-1]=a[i]; } a[r]=tmp; } else if(ch=='Q') { printf("%lld\n",ans); } } } return 0;}
阅读全文
0 0
- 树状数组_Rotate HDU2688
- hdu2688 Rotate (树状数组)
- hdu2688树状数组
- hdu2688 Rotate(树状数组)
- hdu2688 Rotate(树状数组)
- 树状数组专题(十一)之hdu2688
- hdu 2688_Rotate(树状数组,求顺序对数)
- 09-09 HDU_Steps5.3 树状数组,LCA HDU1166 HDU2492 HDU3584 HDU2586 HDU2874 HDU3486 HDU2688
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 还记得你的第一个“对象”吗?
- mongo 常用语句
- 高通RPM部分简介
- 这是一篇最通熟易懂的Hadoop HDFS实践攻略!
- gym100971
- 树状数组_Rotate HDU2688
- 国外牛人总结的机器学习领域的框架、库以及软件
- Property xx cannot be found in forward class object "XXXX"
- 单linux服务器同时拨多条ADSL和挂多个固定公网ip通过squid实现代理池方案
- 编辑平台图层IP占用后解决办法
- MySQL中文乱码问题
- 2017软考考前准备必看
- iOS 开发中常见Property关键字解读
- MongoDB 之 用户管理