HDU2688-Rotate

http://acm.hdu.edu.cn/showproblem.php?pid=2688

#include<iostream>
#include<cstring>
using namespace std;
#define ll __int64
#define maxn 10001
int a[3000001],n,m;
ll c[maxn];
int lowbit(int x) 
{
    return x&(-x);
}
void add(int pos) 
{
    while(pos<maxn) 
    {
        c[pos]++;
        pos+=lowbit(pos);
    }
}
ll sum(int pos) 
{
    ll s=0;
    while(pos>0) 
    {
        s+=c[pos];
        pos-=lowbit(pos);
    }
    return s;
}
int main(void) 
{
    int i,s,e,t,fe,be,se;
    char op[10];
    while(scanf("%d",&n)!=EOF) 
    {
        memset(c,0,sizeof(c));
        ll val=0;
        for(i=0;i<n;i++) 
        {
            scanf("%d",&a[i]);
            val+=sum(a[i]-1);
            add(a[i]);
        }
        scanf("%d",&m);
        while(m--) 
        {
            scanf("%s",op);
            if(op[0]=='Q') 
               printf("%I64d\n",val);
            else 
            {
                scanf("%d%d",&s,&e);
                if(s>e) 
                {
                    t=s;
                    s=e;
                    e=t;
                }
                if(s!=e) 
                {
                    fe=a[s];
                    be=se=0;
                    for(i=s;i<e;i++) 
                    {
                        a[i]=a[i+1];
                        if(fe<a[i+1]) 
                           be++;
                        if(fe>a[i+1]) 
                           se++;
                    }
                    a[e]=fe;
                    val=val-be+se;
                }
            }
        }
    }
    return 0;
}
posted @ 2012-09-15 17:33  Yogurt Shen  阅读(191)  评论(0编辑  收藏  举报