BZOJ3289: Mato的文件管理
来源:互联网 发布:java删除sftp文件夹 编辑:程序博客网 时间:2024/05/26 05:53
每次询问区间[l,r]内相邻交换,排好序的最小交换次数
这个东西等于区间内的逆序对数(易证)
然后用莫队就行了,树状数组每次
总复杂度
code:
#include<set>#include<map>#include<deque>#include<queue>#include<stack>#include<cmath>#include<ctime>#include<bitset>#include<string>#include<vector>#include<cstdio>#include<cstdlib>#include<cstring>#include<climits>#include<complex>#include<iostream>#include<algorithm>#define ll long long#define lowbit(x) x&(-x)using namespace std;inline void read(int &x){ char c; while(!((c=getchar())>='0'&&c<='9')); x=c-'0'; while((c=getchar())>='0'&&c<='9') (x*=10)+=c-'0';}const int maxn = 51000;struct A{ int c,i;}c[maxn];inline bool cmpc(const A x,const A y) {return x.c<y.c;}int a[maxn];int s[maxn],n;void upd(int x,const int c){for(;x<=n;x+=lowbit(x))s[x]+=c;}int query(int x){int re=0;for(;x;x-=lowbit(x))re+=s[x];return re;}struct node{ int l,r,i; node(){} node(const int _l,const int _r,const int _i){l=_l;r=_r;i=_i;}}q[maxn]; int m;int N,id[maxn],L[maxn];inline bool cmp(node x,node y){return id[x.l]==id[y.l]?x.r<y.r:x.l<y.l;}ll ans[maxn];void solve(){ sort(q+1,q+m+1,cmp); int pos=1; int l=1,r=0; ll tmp=0ll,s=0ll; for(int i=1;i<=id[n];i++) { while(pos<=m&&q[pos].l<L[i+1]) { while(r<q[pos].r) tmp+=(s++)-(ll)query(a[++r]),upd(a[r],1); while(l>q[pos].l) tmp+=(ll)query(a[--l]-1),upd(a[l],1),s++; while(r>q[pos].r) tmp-=s-(ll)query(a[r]),upd(a[r--],-1),s--; while(l<q[pos].l) tmp-=(ll)query(a[l]-1),upd(a[l++],-1),s--; ans[q[pos++].i]=tmp; } }}int main(){ read(n); N=sqrt(n); for(int i=1;i<=n;i++) id[i]=(i-1)/N+1; id[n+1]=id[n]+1; for(int i=1;i<=id[n];i++) L[i]=(i-1)*N+1; L[id[n]+1]=n+1; for(int i=1;i<=n;i++) read(c[i].c),c[i].i=i,a[i]=c[i].c; sort(c+1,c+n+1,cmpc); c[0].c=c[1].c-1; for(int i=1,k=0;i<=n;i++) { if(c[i].c!=c[i-1].c) k++; a[c[i].i]=k; } read(m); for(int i=1;i<=m;i++) { int l,r; read(l); read(r); q[i]=node(l,r,i); } solve(); for(int i=1;i<=m;i++) printf("%lld\n",ans[i]); return 0;}
0 0
- bzoj3289: Mato的文件管理
- bzoj3289 : Mato的文件管理
- Bzoj3289: Mato的文件管理
- [bzoj3289]Mato的文件管理
- bzoj3289 Mato的文件管理
- 【BZOJ3289】 Mato的文件管理
- 【bzoj3289】 Mato的文件管理
- bzoj3289 Mato的文件管理
- bzoj3289 Mato的文件管理
- bzoj3289 Mato的文件管理
- BZOJ3289: Mato的文件管理
- BZOJ3289[Mato的文件管理]
- bzoj3289 Mato的文件管理
- BZOJ3289: Mato的文件管理 莫队
- BZOJ3289——Mato的文件管理
- [题解]bzoj3289 Mato的文件管理
- [BZOJ3289] Mato的文件管理 && 莫队算法
- bzoj3289 Mato的文件管理 莫队算法
- JAVA的静态绑定与动态绑定
- Problem B: 类的初体验(II)
- Problem C: 类的初体验(III)
- Problem C: 分数类的输出
- Github全面详解-09 使用issues
- BZOJ3289: Mato的文件管理
- Problem D: 类的初体验(IV)
- python中read() readline()以及readlines()对比
- Problem E: 类的初体验(V)
- hdu杭电1003 dp 连续子序列最大值
- opencv HSV 颜色模型(H通道取值 && CV_BGR2HSV_FULL)
- opencv中hsv
- 数位模17密码
- Codeforces777C 技巧