HDU_2688_Rotate

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define N 10005
#define LL long long
int c[N],a[3000005];
int sum(int x)
{
    int ret=0;
    while(x)
    {
        ret+=c[x];
        x-=x&-x;
    }
    return ret;
}
void add(int x)
{
    while(x<=N)
    {
        c[x]++;
        x+=x&-x;
    }
}
int main()
{
  int n,i,j,k,l,r,m;
  char ch[3];
  while(~scanf("%d",&n))
  {
      memset(c,0,sizeof(c));
      LL ans=0;
      for(i=0;i<n;++i)
      {
          scanf("%d",&a[i]);
          add(a[i]);
          ans+=sum(a[i]-1); //先求正序数
      }
      scanf("%d",&m);
      while(m--)
      {
          scanf("%s",ch);
          if(ch[0]=='Q')
              printf("%I64d\n",ans); //开头没用LL,就WA了
          else 
          {
              scanf("%d%d",&l,&r);
              int re=a[l];
              for(i=l;i<r;++i) //反正数组要更新,顺便更新ans
              {
                  a[i]=a[i+1];
                  if(a[i]<re) ans++;
                  else if(a[i]>re) ans--;
              }
                 a[r]=re;
          }
      }
  }
  return 0;
}

 

posted @ 2013-05-24 15:51  小仪在努力~  阅读(174)  评论(0编辑  收藏  举报