Codeforces785E - Anton and Permutation
Description
对一个长度为\(n(n\leq2\times10^5)\)的数列\(a\)进行\(m(m\leq5\times10^4)\)次操作,数列初始时为\(\{1,2,...,n\}\)。每次操作交换数列中的两个数\(a_L\)和\(a_R\),并询问此时数列\(a\)中的逆序对数。
Solution
交换\(a_L\)和\(a_R\)时,会影响逆序对数的只有下标在\((L,R)\)之间,数值在\(a_L\)和\(a_R\)之间的数(钦定\(L<R\))。设这样的数有\(k\)个,那么若\(a_L<a_R\),逆序对数会增加\(2k+1\);若\(a_L>a_R\),逆序对数会减少\(2k+1\)。
所以只要使用一种数据结构,支持单点修改和查询区间内数值在某个范围内的数的个数。分块即可解决这个问题。维护每个块内的数有序,那么就可以通过二分查找以\(O(log\sqrt n)\)求出块内满足条件的数的个数。对于修改,修改之后将所在块再次排序即可。
时间复杂度\(O(m\sqrt nlog\sqrt n)。\)
Code
//Anton and Permutation
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long lint;
inline char gc()
{
static char now[1<<16],*S,*T;
if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
return *S++;
}
inline int read()
{
int x=0; char ch=gc();
while(ch<'0'||'9'<ch) ch=gc();
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x;
}
int const N=2e5+1e3;
int n,m,n0,a[N];
int blk[N],fr[N],to[N],b[N];
int query(int t,int x,int y)
{
int L=upper_bound(b+fr[t],b+to[t]+1,x)-b;
int R=lower_bound(b+fr[t],b+to[t]+1,y)-b-1;
return R-L+1;
}
void update(int t)
{
for(int i=fr[t];i<=to[t];i++) b[i]=a[i];
sort(b+fr[t],b+to[t]+1);
}
int main()
{
n=read(),m=read(); n0=sqrt(n);
for(int i=1;i<=n;i++) a[i]=b[i]=i;
for(int i=1;i<=n;i++) blk[i]=(i-1)/n0+1;
for(int i=1;i<=blk[n];i++) fr[i]=(i-1)*n0+1,to[i]=i*n0;
to[blk[n]]=n;
lint ans=0;
for(int i=1;i<=m;i++)
{
int L=read(),R=read(); if(L>R) swap(L,R);
if(L==R) {printf("%lld\n",ans); continue;}
int x=a[L],y=a[R]; lint res=0,f=1;
if(x>y) swap(x,y),f=-1;
if(blk[L]==blk[R]) for(int i=L;i<=R;i++) res+=(x<a[i]&&a[i]<y);
else
{
for(int i=L;i<=to[blk[L]];i++) res+=(x<a[i]&&a[i]<y);
for(int t=blk[L]+1;t<=blk[R]-1;t++) res+=query(t,x,y);
for(int i=fr[blk[R]];i<=R;i++) res+=(x<a[i]&&a[i]<y);
}
ans+=2*res*f; if(a[L]<a[R]) ans++; else ans--;
swap(a[L],a[R]); update(blk[L]),update(blk[R]);
printf("%lld\n",ans);
}
return 0;
}
P.S.
要用long long来保存逆序对数。
存在\(L=R\)的情况,可以特判一下。
Codeforces785E - Anton and Permutation的更多相关文章
- 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 ...
- Anton and Permutation
Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input standa ...
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...
- Codeforces 785E. Anton and Permutation
题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...
- [CF785E]Anton and Permutation
题目大意:有一串数为$1\sim n(n\leqslant2\times10^5)$,$m(m\leqslant5\times10^4)$次询问,每次问交换位置为$l,r$的两个数后数列中逆序对的个数 ...
- Codeforces 785E Anton and Permutation(分块)
[题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...
- CodeForces 785E Anton and Permutation 分块
题意: 有一个\(1 \sim n\)的排列\(A\),有\(q\)个询问: 交换任意两个元素的位置,求交换之后排列的逆序数 分析: 像这种不太容易用线段树,树状数组维护的可以考虑分块 每\(\sqr ...
- 【codeforces 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- 题解 CF785E 【Anton and Permutation】
考虑用分块解决这个题,一次交换对当前逆序对个数的影响是,加上两倍的在区间\([l+1,r-1]\)中比\(a_r\)小的元素个数,减去两倍的在区间\([l+1,r-1]\)中比\(a_l\)小的元素个 ...
随机推荐
- Linux Shell 编程语法
原文地址:http://www.cnblogs.com/fhefh/archive/2011/04/13/2014967.html.感谢作者的无私分享 编写代码 在计划好要程序干什么以及如何使用程序的 ...
- 流API--初体验
在JDK8新增的许多功能中,有2个功能最重要,一个是Lambda表达式,一个是流API.Lambda表达式前面我已经整理过了,现在开始整理流API.首先应该如何定义流API中的"流" ...
- 【转】sed 的参数
一.15个参数 1.r 从文件读入 [root@watchout2 ~]# cat file12345 [root@watchout2 ~]# cat newfile abcde [root@watc ...
- TCP粘包和拆包问题
问题产生 一个完整的业务可能会被TCP拆分成多个包进行发送,也有可能把多个小的包封装成一个大的数据包发送,这个就是TCP的拆包和封包问题. 下面可以看一张图,是客户端向服务端发送包: 1. 第一种情况 ...
- Eclipse运行Java简单实例
运行eclipse前首先配置好JDK环境变量等 双击这句话可跳转配置环境变量详细步骤 运行eclipse软件 1.File菜单-New - project 2.Java Project - Next ...
- SDK,API概念
什么是SDK什么是API? SDK 就是 Software Development Kit 的缩写,就是"软件开发工具包". 这是一个覆盖面相当广泛的名词,可以这么说:辅助开发某一 ...
- SQLServer2008修改sa密码的方法与SQL server 2008数据库的备份与还原
sa密码的修改转载自:http://blog.csdn.net/templar1000/article/details/20211191 SQL server 2008数据库的备份与还原转自 :htt ...
- javascipt中的DOM对象
1.HTML中DOM对象的概念 HTML Document Object Model(文档对象模型) HTML DOM定义了访问和操作HTML文档的标准方法 HTML DOM把HTML文档呈现为带有元 ...
- HTTP/1.1与HTTP/1.0的区别[转]
原文链接:http://blog.csdn.net/forgotaboutgirl/article/details/6936982 下面主要从几个不同的方面介绍HTTP/1.0与HTTP/1.1之间的 ...
- bzoj 2627: JZPKIL [伯努利数 Pollard-rho]
2627: JZPKIL 题意:求 \[ \sum_{i=1}^n (n,i)^x [i,n]^y,\ [i,n] = lcm(i,n) \] \(n \le 10^{18},\ x,y\le 300 ...