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位置).
代码:
//如果每询问一次就求一次顺序数很费时间,先求出最初的顺序数ans,,每转动一次时
//如果a+1~b位置的数大于a位置的数,ans--,如果小于a,ans++。hduoj时而TLE时而AC.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long A[];
int f[];
int Lowbit(int id) {return id&(-id);}
void Add(int id,int c)
{
while(id<=){
A[id]+=c;
id+=Lowbit(id);
}
}
long long query(int id)
{
long long s=;
while(id>){
s+=A[id];
id-=Lowbit(id);
}
return s;
}
int main()
{
int n,m,a,b;
char ch[];
while(scanf("%d",&n)==){
memset(A,,sizeof(A));
long long ans=;
for(int i=;i<n;i++){
scanf("%d",&f[i]);
Add(f[i],);
ans+=query(f[i]-);
}
scanf("%d",&m);
while(m--){
scanf("%s",ch);
if(ch[]=='Q') printf("%I64d\n",ans);
else{
scanf("%d%d",&a,&b);
int v=f[a];
for(int i=a;i<b;i++){
f[i]=f[i+];
if(f[i]>v) ans--;
else if(f[i]<v) ans++;
}
f[b]=v;
}
}
}
return ;
}

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. Python基础 之 数据类型

    数据类型 一.运算符 算数运算a = 10 * 10赋值运算a = a + 1 a+=1 布尔值:True 真 False 假 if True: pass while True: pass v = n ...

  2. python—IDLE的shell上下翻看历史

    Alt+p和Alt+n,分别向上(history previous)和向下(history next)调出使用过的历史命令.

  3. 关于mysql开元数据库的几个随想

    现在已经是凌晨了,昨天晚上写了我人生中的第一篇笔记,觉得没什么可写的,写了一个多小时都没写出什么,现在突然想写点东西了,这是一个比较有趣的问题,前两个月换了新工作,记得当初面试这份工作的时候面试到第三 ...

  4. 一个改变this指向bind的函数,vue源代码

    function bind(fn, ctx) { return function (a) { var l = arguments.length; return l ? l > 1 ? fn.ap ...

  5. Alpha发布文案加美工展示

    目录 团队简介 项目进展 组内分工 队员总结 后期计划 一.团队简介 二.项目进展 从选题发布到今天的Alpha发布,我们团队经历了许许多多的磨难.我们最终设计了如下的功能:首页.班级.个人.更多.打 ...

  6. 阅读 用P4对数据平面进行编程

    引言 关于题目,对数据平面进行编程,在之前读过the road to SDN,软件定义网络的思想在于数控分离,其对网络行为的编程暂时只局限于网络控制平面.其转发平面在很大程度上受制于功能固定的包处理硬 ...

  7. .net Mvc4 View—布局页与分部页

    ▲      一.Layout属性     1.1.@RenderPage():渲染制定的页面到占位符.     注意:@RenderPage()可以使用多次,这点与@RenderBody()不同   ...

  8. linux查看资源占用情况

    在Linux中查看占用空间大文件 查看当前目录总共占的容量.而不单独列出各子项占用的容量$ du -sh查看当前目录下一级子文件和子目录占用的磁盘容量.$ du -lh --max-depth=1结果 ...

  9. activeMQ 讲解及实战

    #### 软件架构项目中需要用到activeMQ 下载地址:http://activemq.apache.org/download.html #### 安装教程需要安装jdk环境activeMQ免安装 ...

  10. dpr dproj 扩展名区别,dprdproj

    这段时间用xe6,看了下目录下生成的一些文件,因为隐藏了扩展名,看到两个名字一样的文件,右键属性看了下,同名但扩展名不同,百度了下区别,没有找到答案,问群里的朋友才知道区别,特此记录下来: dpr:D ...