题目链接:HDU 2688
题目大意:题目大意:给你一个n个数的全排列,求这n个数的正序数(序号比前面大,值也比前面大),然后给出个区间,这个区间顺时针循环转一次(半夜写题看成对称反转了 orz )。比如,输入R 1 3,下标(从0开始)为1到3的数顺时针循环一次,即(2 3 4 变成3 4 2)其余不动,所以1 2 3 4 5变成 1 3 4 2 5.当输入Q时,输出当前排列的正序数。
题目大意是这位老哥的:点我
分析:
和求逆序对一样线段树应该也能写,不过这里用树状数组写了.
代码:
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Fi first
#define Se second
#define ll long long
#define inf 0x3f3f3f3f
#define lowbit(x) (x&-x)
#define PLL pair<ll,ll>
#define PII pair<int,int>
#define in(s,b) scanf(s,&b)
#define l_inf 0x3f3f3f3f3f3f3f
#define mmin(a,b,c) min(a,min(b,c))
#define mmax(a,b,c) max(a,max(b,c))
#define debug(a) cout<<#a<<"="<<a<<endl;
#define debug2(a,b) cout<<#a<<"="<<a<<" "<<#b<<"="<<b<<endl;
using namespace std;
const int N=10010;
int a[3000010],s[N];
void update(int p)
{
while(p<N)
{
s[p]++;
p+=lowbit(p);
}
}
int query(int p)
{
int ans=0;
while(p)
{
ans+=s[p];
p-=lowbit(p);
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
ll ans=0;
memset(s,0,sizeof s);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
ans+=query(a[i]-1);
update(a[i]);
}
int q;scanf("%d",&q);
while(q--)
{
char c;
scanf(" %c",&c);
if(c=='Q')
{
printf("%lld\n",ans);
}
else
{
int l,r;
scanf("%d%d",&l,&r);
l++;r++;
int tmp=a[l];
for(int i=l;i<r;i++)
{
a[i]=a[i+1];
if(a[i]>tmp) ans--;
if(a[i]<tmp) ans++;
}
a[r]=tmp;
}
}
}
return 0;
}
/*
(n-1)*n/2;
2 3 45
1 4 3 2 5
1 4 4 5 3 5 2 5
1 3
1 2
1 5
*/