Rotate

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3709    Accepted Submission(s): 711

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.
 
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
5
1 2 3 4 5
3
Q
R 1 3
Q
 
Sample Output
10
8
 
Author
yifenfei
 
Source
 题意:
共n个数,m次操作,Q表示询问n个数有多少顺序数,R a,b表示区间[a,b]中的数转动一下(a+1~b每个数向前移动,a位置的数到b位置).
代码:
  1. //如果每询问一次就求一次顺序数很费时间,先求出最初的顺序数ans,,每转动一次时
  2. //如果a+1~b位置的数大于a位置的数,ans--,如果小于a,ans++。hduoj时而TLE时而AC.
  3. #include<iostream>
  4. #include<cstdio>
  5. #include<cstring>
  6. using namespace std;
  7. long long A[];
  8. int f[];
  9. int Lowbit(int id) {return id&(-id);}
  10. void Add(int id,int c)
  11. {
  12. while(id<=){
  13. A[id]+=c;
  14. id+=Lowbit(id);
  15. }
  16. }
  17. long long query(int id)
  18. {
  19. long long s=;
  20. while(id>){
  21. s+=A[id];
  22. id-=Lowbit(id);
  23. }
  24. return s;
  25. }
  26. int main()
  27. {
  28. int n,m,a,b;
  29. char ch[];
  30. while(scanf("%d",&n)==){
  31. memset(A,,sizeof(A));
  32. long long ans=;
  33. for(int i=;i<n;i++){
  34. scanf("%d",&f[i]);
  35. Add(f[i],);
  36. ans+=query(f[i]-);
  37. }
  38. scanf("%d",&m);
  39. while(m--){
  40. scanf("%s",ch);
  41. if(ch[]=='Q') printf("%I64d\n",ans);
  42. else{
  43. scanf("%d%d",&a,&b);
  44. int v=f[a];
  45. for(int i=a;i<b;i++){
  46. f[i]=f[i+];
  47. if(f[i]>v) ans--;
  48. else if(f[i]<v) ans++;
  49. }
  50. f[b]=v;
  51. }
  52. }
  53. }
  54. return ;
  55. }

HDU2688 树状数组(逆序数)的更多相关文章

  1. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  2. hdu2838Cow Sorting(树状数组+逆序数)

    题目链接:点击打开链接 题意描写叙述:给定一个长度为100000的数组,每一个元素范围在1~100000,且互不同样,交换当中的随意两个数须要花费的代价为两个数之和. 问怎样交换使数组有序.花费的代价 ...

  3. HDU5196--DZY Loves Inversions 树状数组 逆序数

    题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...

  4. HDU3465 树状数组逆序数

    Life is a Line Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)T ...

  5. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...

  6. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  7. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  8. Bzoj 2789: [Poi2012]Letters 树状数组,逆序对

    2789: [Poi2012]Letters Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 185[Submit][Stat ...

  9. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  10. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

随机推荐

  1. Java面试知多少

    1.谈谈&和&&的区别  1.&&是短路判断,在与其他语句一起判断时,第一个条件为假就不判断剩下的条件:   & 需要判断所有的条件  2.&是 ...

  2. leetcode个人题解——#24 Swap Nodes in Pairs

    因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...

  3. Python3 集合

    1.集合的表示 集合是一个无序不重复的元素序列 创建空集合 set() 2.集合的运算 a={1,2,3} b={2,3,4} print(a-b) #a中包含b中不包含 print(a|b) #a中 ...

  4. net::ERR_ABORTED ,引入js文件出现报错的解决方法

    在head头里面添加 <mvc:annotation-driven enable-matrix-variables="true"></mvc:annotation ...

  5. SGU 438 The Glorious Karlutka River =)(最大流)

    Description A group of Mtourists are walking along the Karlutka river. They want to cross the river, ...

  6. c# windows service 程序

    service服务程序:可以长时间运行可执行应用程序.没有用户界面.可以自动启动和手动启动.适用于在服务器上或需要干扰其他工作的用户可以在同一台计算机上长时间的运行此功能. C#创建service服务 ...

  7. windows下cudnn的安装过程

    在CUDA安装成功之后,系统环境变量中会有如下两个变量显示:CUDA_PATH和CUDA_PATH_8 在安装完CUDA之后,到官网下载与其版本对应的CUDNN        下载地址:https:/ ...

  8. ZooKeeper server &&client

    写了一个关于zookeepeer应用的简单demo 服务端定时的向zookeeper集群注册,客户端监听zookeeper服务节点变化,一旦变化,立刻响应,更新服务端列表 服务端代码: #includ ...

  9. css那些事儿4 背景图像

    background:背景颜色,图像,平铺方式,大小,位置 能够显示背景区域即为盒子模型的填充和内容部分,其中背景图像将会覆盖背景颜色.常见的水平或垂直渐变颜色背景通常使用水平或垂直渐变的背景图像在水 ...

  10. 如何实时获取DBGrid 中当前单元格输入的内容?

    如何获取DBGrid 中当前单元格输入的内容? 还没输入完成,我想实时获取 Cell中的内容,以便作其他处理, 用什么事件呢? 所以Field的Onchange事件是没用的. 这个问题简单啊,每输入1 ...