BZOJ 2141 排队(树状数组套主席树)
解法很多的题,可以块套树状数组,可以线段树套平衡树。我用的是树状数组套主席树。
题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数。(n,m<=2e4).
对于没有交换操作的逆序对数,可以直接用树状数组直接统计。
考虑每次交换操作(l,r),那么逆序对数会有什么变化呢。
1.如果a[l]>a[r],ans--,如果a[l]<a[r],那么ans++.
2.剩下的就是a[l]和a[r]对区间[l+1,r-1]之内数字的影响了。
ans+=([l+1,r-1]内比a[l]大的数)-([l+1,r-1]内比a[l]小的数)+([l+1,r-1]内比a[r]小的数)-([l+1,r-1]内比a[r]大的数)。
查询区间内比指定数字小或者大的数字数目,可以用主席树来实现。
现在要求支持动态交换两个数,显然用树状数组套主席树可以实现。
时间复杂度O(mlognlogn).常数略大。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int N=;
//Code begin... int a[N], tree[N], ans;
int root[N], s[N*], ls[N*], rs[N*], sz, siz;
VI v; void add(int x){while (x<N) ++tree[x], x+=lowbit(x);}
int sum(int x){
int res=;
while (x) res+=tree[x], x-=lowbit(x);
return res;
}
void Update(int l, int r, int x, int &y, int L, int val){
y=++sz; s[y]=s[x]+val;
if (l==r) return ;
ls[y]=ls[x]; rs[y]=rs[x];
int mid=(l+r)>>;
if (L<=mid) Update(l,mid,ls[x],ls[y],L,val);
else Update(mid+,r,rs[x],rs[y],L,val);
}
int Query(int l, int r, int x, int L){
if (r<L) return ;
if (l>=L) return s[x];
int mid=(l+r)>>;
return Query(l,mid,ls[x],L)+Query(mid+,r,rs[x],L);
}
int Query2(int l, int r, int x, int L){
if (l>L) return ;
if (r<=L) return s[x];
int mid=(l+r)>>;
return Query2(l,mid,ls[x],L)+Query2(mid+,r,rs[x],L);
}
int Sum(int l, int x){
int res=;
while (l) res+=Query(,siz,root[l],x+), l-=lowbit(l);
return res;
}
int Sum2(int l, int x){
int res=;
while (l) res+=Query2(,siz,root[l],x-), l-=lowbit(l);
return res;
}
void Add(int x, int n, int l, int val){while (x<=n) Update(,siz,root[x],root[x],l,val), x+=lowbit(x);}
int main ()
{
int n, m, x, y;
scanf("%d",&n);
FOR(i,,n) scanf("%d",a+i), v.pb(a[i]);
sort(v.begin(),v.end());
siz=unique(v.begin(),v.end())-v.begin();
FOR(i,,n) {
a[i]=lower_bound(v.begin(),v.begin()+siz,a[i])-v.begin()+;
ans+=(i--sum(a[i])); add(a[i]);
Add(i,n,a[i],);
}
printf("%d\n",ans);
scanf("%d",&m);
while (m--) {
scanf("%d%d",&x,&y);
if (x>y) swap(x,y);
if (a[x]<a[y]) ++ans;
else if (a[x]>a[y]) --ans;
ans+=(Sum(y-,a[x])-Sum(x,a[x]))-(Sum(y-,a[y])-Sum(x,a[y]))+(Sum2(y-,a[y])-Sum2(x,a[y]))-(Sum2(y-,a[x])-Sum2(x,a[x]));
Add(x,n,a[x],-); Add(x,n,a[y],); Add(y,n,a[y],-); Add(y,n,a[x],);
swap(a[x],a[y]);
printf("%d\n",ans);
}
return ;
}
BZOJ 2141 排队(树状数组套主席树)的更多相关文章
- BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树
[题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...
- BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树
[题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...
- BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树
BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...
- ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...
- P2617 Dynamic Rankings(树状数组套主席树)
P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...
- [COGS257]动态排名系统 树状数组套主席树
257. 动态排名系统 时间限制:5 s 内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...
- 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】
题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...
- 【Luogu】P2617Dynamic Ranking(树状数组套主席树)
题目链接 树状数组套主席树有点难懂qwq 不好理解 树状数组套主席树的直观理解应该是:树状数组的每一个节点是一棵主席树. 普通区间修改我们是创建1个线段树,树状数组套主席树的时候我们就创建log个线段 ...
随机推荐
- 20155315庄艺霖第三次作业之Linux初体验
Linux初体验 安装Linux三两事 老师的作业要求基于VirtualBox安装Linux系统,我一开始下载了VB但是电脑运行不了,后来看网上的教程下载了VMware,才算开始了我的Linux之旅. ...
- 北京Uber优步司机奖励政策(4月15日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- MSP-EZ430U_02板子测试使用
1. 实物如下 2. 先上电,显示驱动没安装 3. 找到驱动的位置,不过实际上安装IAR for msp430之后,驱动就自动的识别了.
- [vijos1067]Warcraft III 守望者的烦恼
就是上次考得fyfy.竟然是原题... // It is made by XZZ #include<cstdio> #include<algorithm> #include&l ...
- 创建Springmvc项目时,特殊拦截器失效情况的原因及解决办法
最近开发一个新项目时,搭建springmvc框架时,遇到一个拦截器失效的情况困扰了两天.现在解决,特此记录一下. 拦截器不生效的情况描述: 设置登录拦截的时候,首先登录接口肯定是不用拦截的.所以需要在 ...
- 【转】微信小程序实现自动化测试
山雨欲来风满楼,最近微信小程序相关开发文章吹遍大江南北,亦有摧枯拉朽万象更新之势.问小程序形为何物,直教IT众生怡情悦性高潮迭起.作为一名有着远大理想“包袱”与互联网变革 “使命感”的测试工程师,我再 ...
- Phaser3 屏幕适配iPhoneX、iPhoneXs的坑 -- JavaScript Html5 游戏开发
PhaserJS 坑:在config内不要把 width 设为 window.innnerWidth在config内不要把 width 设为 window.innnerWidth在config内不 ...
- RabbitMQ各协议异同详解
一.官网介绍 Which protocols does RabbitMQ support? RabbitMQ supports several messaging protocols, directl ...
- openvpn部署
原文发表于cu:2016-03-29 参考文档: 安装:http://qicheng0211.blog.51cto.com/3958621/1575273 安装:http://www.ipython. ...
- JS对字符串编码的几种方式
函数 描述 encodeURI() 把字符串编码为 URI encodeURIComponent() 把字符串编码为 URI 组件 escape() 对字符串进行编码 上面是查询来自w3school的 ...