HDU-2688-Rotate-(树状数组)

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

又是一道有毒的题,时间刚好在1000ms左右,同一个代码一会可以过,一会就TLE,我TLE20几次也没弄清楚到底是哪点差别导致这种情况,估计是编译机理的原因

题意:给一个序列有两种操作,Q:查询正序数(与逆序数相反),R(i,j):把a[i]放到a[j]后面,

先求出初始正序数,设置从i到j的循环把a[i]往后移动就行,移动过程中遇到的元素比a[i]大,那么把a[i]移动到后面之后正序数减少一个,反之,移动过程中遇到的元素比a[i]小,那么把a[i]移动到后面之后正序数增加一个,

与这道题目很像: 

HDU-1394-Minimum Inversion Number-(树状数组)

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
int tree[10001];
int a[3000000];
inline int lowbit(int i) {return i&(-i);}
void add(int i)
{
    while(i<=10001)
    {
        tree[i]+=1;
        i+=lowbit(i);
    }
}
ll sum(int i)
{
    ll res=0;
    while(i>0)
    {
        res+=tree[i];
        i-=lowbit(i);
    }
    return res;
}
int main()
{
    int n,m;
    int i,j,s,e,temp;
    char ch[3];
    ll cnt;
    while(scanf("%d",&n)!=EOF)
    {
        memset(tree,0,sizeof(tree));
        cnt=0;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            cnt+=sum(a[i]-1);
            add(a[i]);
        }
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",ch);
            if(ch[0]=='Q')
            {
                printf("%I64d\n",cnt);
            }
            else
            {
                scanf("%d%d",&s,&e);
                temp=a[s];
                for(j=s;j<=e-1;j++)
                {
                    a[j]=a[j+1];
                    if(temp<a[j]) cnt--;
                    else if(temp>a[j]) cnt++;
                }
                a[e]=temp;
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值