Rotate(hdu 2688)(树状数组+暴力)

Rotate

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

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

ZJFC 2009-3 Programming Contest

先树状数组求正序对数ans,每次进行翻转的时候,ans的改变量只与【L,R】的正逆序对数有关,题目中提到更改区间长度小于等于1000,直接暴力。注意:update函数中的区间上限是f[i]中的最大值,而不是n!!!

#include<bits/stdc++.h>
using namespace std;
#define MAX 10000
int n,f[(int)3e6+5],c[10005];
int lowbit(int x)
{
    return x&(-x);
}
int sum(int pos)
{
    int ans=0;
    while(pos>0)
    {
        ans+=c[pos];
        pos-=lowbit(pos);
    }
    return ans;
}
void update(int pos,int val)
{
    while(pos<=MAX)
    {
        c[pos]+=val;
        pos+=lowbit(pos);
    }
}
int main()
{
    int m,a,b;
    char ch[4];
    while(scanf("%d",&n)==1&&n)
    {
        memset(c,0,sizeof(c));
        long long ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&f[i]);
            update(f[i],1);
            ans+=(long long)sum(f[i]-1);
        }
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",ch);
            if(ch[0]=='Q')
            {
                printf("%lld\n",ans);
                continue;
            }
            scanf("%d%d",&a,&b);
            int t=f[a];
            for(int i=a+1;i<=b;i++)
            {
                if(t<f[i]) ans--;
                else if(t>f[i]) ans++;
                f[i-1]=f[i];
            }
            f[b]=t;
        }

    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值