hdu 2688

来源:互联网 发布:java集合框架总结 编辑:程序博客网 时间:2024/05/17 22:20

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 10005int 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;}