题解 CF785E 【Anton and Permutation】
考虑用分块解决这个题,一次交换对当前逆序对个数的影响是,加上两倍的在区间\([l+1,r-1]\)中比\(a_r\)小的元素个数,减去两倍的在区间\([l+1,r-1]\)中比\(a_l\)小的元素个数,再根据\(a_l\)和\(a_r\)的大小关系决定这两个位置对答案的影响。
可以用\(vector\)来维护每个块内元素有序,然后就可以支持询问了。
\(code:\)
#include<bits/stdc++.h>
#define maxn 200010
#define lower(a,x) lower_bound(ve[a].begin(),ve[a].end(),x)
#define upper(a,x) upper_bound(ve[a].begin(),ve[a].end(),x)
using namespace std;
typedef long long ll;
template<typename T> inline void read(T &x)
{
x=0;char c=getchar();bool flag=false;
while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
if(flag)x=-x;
}
int n,k,S;
int bel[maxn],a[maxn];
ll ans;
vector<int> ve[maxn];
void change(int l,int r)
{
if(bel[l]!=bel[r])
{
ve[bel[l]].erase(lower(bel[l],a[l])),ve[bel[l]].insert(upper(bel[l],a[r]),a[r]);
ve[bel[r]].erase(lower(bel[r],a[r])),ve[bel[r]].insert(upper(bel[r],a[l]),a[l]);
}
swap(a[l],a[r]);
}
int query(int l,int r,int v)
{
if(l>r) return 0;
int cnt=0;
for(int i=l;i<=min(S*bel[l],r);++i) cnt+=(a[i]<v);
if(bel[l]==bel[r]) return cnt;
for(int i=S*(bel[r]-1)+1;i<=r;++i) cnt+=(a[i]<v);
for(int i=bel[l]+1;i<=bel[r]-1;++i) cnt+=lower(i,v)-ve[i].begin();
return cnt;
}
int main()
{
read(n),read(k),S=sqrt(n);
for(int i=1;i<=n;++i) a[i]=i,bel[i]=(i-1)/S+1,ve[bel[i]].push_back(a[i]);
while(k--)
{
int l,r;
read(l),read(r);
if(l>r) swap(l,r);
if(l==r)
{
printf("%lld\n",ans);
continue;
}
ans+=2*(query(l+1,r-1,a[r])-query(l+1,r-1,a[l]));
if(a[l]<a[r]) ans++;
else ans--;
change(l,r),printf("%lld\n",ans);
}
return 0;
}
题解 CF785E 【Anton and Permutation】的更多相关文章
- [CF785E]Anton and Permutation
题目大意:有一串数为$1\sim n(n\leqslant2\times10^5)$,$m(m\leqslant5\times10^4)$次询问,每次问交换位置为$l,r$的两个数后数列中逆序对的个数 ...
- 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 ...
- Codeforces785E - Anton and Permutation
Portal Description 对一个长度为\(n(n\leq2\times10^5)\)的数列\(a\)进行\(m(m\leq5\times10^4)\)次操作,数列初始时为\(\{1,2,. ...
- 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/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...
- 【codeforces 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- Codeforces 785E. Anton and Permutation
题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...
- LeetCode题解之 Letter Case Permutation
1.题目描述 2.问题分析 可以使用递归的方法解决,参考了别人的答案才写出来的. 3.代码 vector<string> letterCasePermutation(string S) { ...
随机推荐
- 黎活明8天快速掌握android视频教程--12_文件的保存与读取
1.当前是把文件保存当前手机的app的data目录下 我们来看看操作保存文件的业务类 package contract.test.savafileapplication; import android ...
- css样式学习笔记
视频参见php中文网玉女心经视频教程 讲解的相当的清楚和明白 第1章 :css快速入门 1.1 什么是css 改变html框架的样式. 1.2 css的三种引入形式 第一种形式 ...
- JavaWeb网上图书商城完整项目--day02-21.退出功能的实现
1.当用户点击退出的时候,跳转到登陆页面 当用户点击退出的时候,需要将session中保存的登陆的用户销毁掉 当用户点击退出的时候,调用UserServlet的quit方法 退出按钮在top.jsp中 ...
- 一个比CBitmap更优秀的类 -- CImage类
Visual C++的CBitmap类的功能是比较弱的,它只能显示出在资源中的图标.位图.光标以及图元文件的内容,而不像VB中的Image控件可以显示出绝大多数的外部图像文件(BMP.GIF.JPEG ...
- Elasticsearch从入门到放弃:分词器初印象
Elasticsearch 系列回来了,先给因为这个系列关注我的同学说声抱歉,拖了这么久才回来,这个系列虽然叫「Elasticsearch 从入门到放弃」,但只有三篇就放弃还是有点过分的,所以还是回来 ...
- 【Xamarin.Forms 1】App的创建与运行
引言 本篇文章将从介绍Xamarin.Forms创建开始. 开发环境 Visual Studio 2019 16.6.2 Xamarin.Forms 4.6.0.726 Android 5.0 (AP ...
- 1.记我的第一次python爬虫爬取网页视频
It is my first time to public some notes on this platform, and I just want to improve myself by reco ...
- 二.2vueadmin-template反向代理/路由配置,idc增查删
一.反向代理: (1)F:\devops\data\web\vueAdmin-template\config\index.js ---让别人也能访问我的vue前端 host: '0.0.0.0', ( ...
- CentOS 关闭暂不需要的系统服务
需要保留的服务:crond.iptables.irqbalance.microcode_ctl.network.random.sshd.syslog.local 一 .使用命令:ntsysv 打开选项 ...
- debian10 安装 详解
准备 下载debian 下载页面,有3个iso,选择第一个,其他两个软件包,我们不需要. 制作启动盘 debian官方提供了一个工具,可以轻松制作启动盘,也可以用opensuse官方提供的一个工具ru ...