题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2688
题目大意:求顺序数对数,但是有转移操作
Rotate
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2539 Accepted Submission(s): 575
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
5 1 2 3 4 5 3 Q R 1 3 Q
Sample Output
10 8
Author
yifenfei
Source
这个代码可能过不去,看人品。998ms过得,未来优化
#include <cstdio>
#include <cstring>
#define maxn 3000010
int n;
int a[maxn],c[maxn];
int bit(int t){
return t&(-t);
}
void update(int pos){
while(pos<=10000){
c[pos]++;
pos+=bit(pos);
}
}
__int64 getsum(int pos){
__int64 ans=0;
while(pos>0){
ans+=c[pos];
pos-=bit(pos);
}
return ans;
}
int main()
{
__int64 ans;
while(scanf("%d",&n)!=EOF){
memset(c,0,sizeof(c));
ans=0;
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
ans+=getsum(a[i]-1);//避免有重复的a[i]
update(a[i]);
}
int m;
scanf("%d",&m);
while(m--){
char ch[3];
scanf("%s",ch);
if(ch[0]=='Q'){
printf("%I64d\n",ans);
}
else {
int s,e;
scanf("%d%d",&s,&e);
int cnt=a[s];
for(int i=s;i<e;i++){
a[i]=a[i+1];
if(a[i]>cnt)ans--;
else if(a[i]<cnt)ans++;
}
a[e]=cnt;
}
}
}
return 0;
}