hdu 2688

OJ

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

#define PF(x) (scanf("%d",&x))
#define PT(x,y) (scanf("%d%d",&x,&y))
#define PR(x) (printf("%d\n",x))
#define PRT(x,y)(printf("%d %d\n",x,y))
#define M 3000005
#define N 10005
int n;
int a[M];
typedef __int64 LL;
LL ar[N];

int minx(int a,int b)
{
	return a>b?b:a;
}
int lowbit(int x)
{
	return x&(-x);
}
void  add(int x,LL c)
{
	for(;x<N;x+=lowbit(x))
	{
		ar[x] += c;
	}
}
LL sum(int x)
{
	LL s = 0;
	for(;x;x-=lowbit(x))
		s += ar[x];
	return s;
}
void init()
{
	while(scanf("%d",&n)!=EOF)
	{
		LL ans = 0;
		memset(ar,0,sizeof(ar));
		for(int i=0;i<n;i++)
		{
			int x;
			PF(x);
			a[i] = x;
			ans += sum(x-1);
			add(x,1LL);
		}
		int q;
		PF(q);
		 while(q--) {
            char str[10];
            scanf("%s", str);

            if(str[0] == 'Q') {
                printf("%I64d\n", ans);
            }else {
                int s, e;
                scanf("%d %d", &s, &e);
                if(s > e) {
                    int tmp = s;
                    s = e;
                    e = tmp;
                }
				int i;
                if(s != e) {
                    int v = a[s];
                    int lt = 0, bt = 0;
                    for(i = s; i < e; i++) {
                        a[i] = a[i+1];
                        if(v < a[i+1]) {
                            lt ++;
                        }
                        if(v > a[i+1]) {
                            bt ++;
                        }

                    }
                    a[e] = v;
                    ans = ans - lt + bt;
                }
            }
		
	}
	}
	return ;
}
int main()
{
	init();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值