樹狀數組專題(十一)之hdu2688

//本題大意:給出一個數組..然後給出m個處理
//Q表示輸出現在的遞增2元組有多少個..
//R a b表示對a-b這個區間進行循環
//題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2688
//處理方式..一次進行樹狀處理..然後因爲b-a比較小..
//所以可以直接模擬就OK了.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
const int MN = 3000000;
int a[MN+10];
int tree[11111];
int n;
int Lowbit(int x)
{
    int m =  x&(-x);
    return m;
}
void Updata(int x)
{
    for(int i = x ; i <= 11111  ; i += Lowbit(i))
      tree[i] += 1;
}
int Getsum(int x)
{
    int sum = 0;
    x--;
    while(x > 0)
    {
        sum += tree[x];
        x -= Lowbit(x);
    }
    return sum;
}
int main()
{
    while(scanf("%d",&n) != EOF)
    {
        memset(tree,0,sizeof(tree));
        __int64 ans = 0;
        for(int i = 1 ; i <= n ; i ++)
        {
            scanf("%d",&a[i]);
            Updata(a[i]);
            ans += Getsum(a[i]);
        }
        int m;
        scanf("%d",&m);
        for(int i = 1 ; i <= m ; i++)
        {
            char h[11];
            scanf("%s",h);
            if(h[0] == 'Q')
            {
                printf("%I64d\n",ans);
            }
            else
            {
                int x,y;
                scanf("%d%d",&x,&y);x++;y++;
                if(x > y)swap(x,y);
                int p = a[x];
                int q = a[x+1];
                int sum = 0;
                int sum1 = 0;
                if(a[x] < a[y])sum++;
                if(a[x] == a[y])sum1++;
                for(int i = x+1 ; i <= y ; i++)
                {
                   if(a[x] < a[i])ans--;
                   if(a[x] > a[i])ans++;
                   if(i != y)
                   a[i] = a[i+1];
                }
                a[x] = q;
                a[y] = p;
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章