题目链接:http://codeforces.com/problemset/problem/785/E


其实可以CDQ分治...

我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多少个就可以了。

考虑分块,每段区间之内有排序或者二分查询比一个数大的树的个数的操作。

复杂度${O(qn \sqrt n log_{2}^{\sqrt n})}$

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 1000100
#define llg int
#define LL long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,SIZE,belong[maxn],cnt,ne[maxn],a[maxn],T;
LL ans;
vector<llg>c[maxn]; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} void init()
{
SIZE=sqrt(n);
for (llg i=;i<=n;i++)
{
ne[i]=i+SIZE-; ne[i]=min(ne[i],n); cnt++;
c[cnt].push_back();
for (llg j=i;j<=ne[i];j++)
{
ne[j]=ne[i];
belong[j]=cnt;
c[cnt].push_back(j);
}
i=ne[i];
}
for (llg i=;i<=n;i++) a[i]=i;
} llg erfen(llg x,llg v)
{
llg l=,r=c[x].size()-,mid,wz;
while (l<=r)
{
mid=(l+r)>>;
if (c[x][mid]<=v) {l=mid+; wz=mid;}else r=mid-;
}
return c[x].size()-wz;
} llg more(llg l,llg r,llg v)
{
if (r<l) return ;
llg tot=;
llg stk=belong[l],endk=belong[r];
for (llg i=l;i<=r;i++)
{
if (belong[i]==stk || belong[i]==endk)
{
if (a[i]>v) tot++;
continue;
}
tot+=erfen(belong[i],v);
i=ne[i];
}
return tot;
} void change(llg x,llg v1,llg v2)
{
llg w=c[x].size();
for (llg i=;i<w;i++)
if (c[x][i]==v1)
{
c[x][i]=v2;
sort(c[x].begin(),c[x].end());
return ;
}
} int main()
{
// yyj("a");
cin>>n>>T;
init();
while (T--)
{
llg l,r;
l=getint(),r=getint();
if (l>r) swap(l,r);
if (l!=r)
{
llg morel=more(l+,r-,a[l]);
llg lessl=r--l-morel;
llg morer=more(l+,r-,a[r]);
llg lessr=r--l-morer;
ans+=morel-lessl+lessr-morer;
if (a[l]>a[r]) ans--;else ans++;
change(belong[l],a[l],a[r]); change(belong[r],a[r],a[l]);
swap(a[l],a[r]);
}
printf("%I64d\n",ans);
}
return ;
}

Codeforces 785E. Anton and Permutation的更多相关文章

  1. Codeforces 785E Anton and Permutation(分块)

    [题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...

  2. CodeForces 785E Anton and Permutation 分块

    题意: 有一个\(1 \sim n\)的排列\(A\),有\(q\)个询问: 交换任意两个元素的位置,求交换之后排列的逆序数 分析: 像这种不太容易用线段树,树状数组维护的可以考虑分块 每\(\sqr ...

  3. Codeforces 785 E. Anton and Permutation(分块,树状数组)

    Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...

  4. 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 ...

  5. Codeforces785E - Anton and Permutation

    Portal Description 对一个长度为\(n(n\leq2\times10^5)\)的数列\(a\)进行\(m(m\leq5\times10^4)\)次操作,数列初始时为\(\{1,2,. ...

  6. Anton and Permutation

    Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input standa ...

  7. 【codeforces 785E】Anton and Permutation

    [题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...

  8. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  9. Codeforces 593B Anton and Lines

    LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output st ...

随机推荐

  1. nfs共享文件搭建

    Linux NFS服务器的安装与配置详解 一.NFS服务简介  NFS是Network  File System(网络文件系统).主要功能是通过网络让不同的服务器之间可以共享文件或者目录.NFS客户端 ...

  2. A-作业01

    #1 简单作业 1. 系统的日志文件/var/log/secure /var/log/messages /var/log/cron会自动的进行轮询,系统是通过什么实现的? 2. 写出下面特殊符号在定时 ...

  3. jquery photoClip支持手机端,PC端 本地裁剪图片后上传插件

    支持手机,PC最好的是jquery photoClip插件,下载地址&示例:https://github.com/topoadmin/photoClip demo.html 代码: <! ...

  4. P2763 试题库问题(dinic)

    P2763 试题库问题 dinic 搞个虚拟源点和汇点,瞎建建边就好辣. 偷张图↓↓ 如果没满流就是无解辣 输出方案咋办呢? 枚举每种类型,蓝后枚举它们的边 如果该边被使用了(通过判断反向边的流量), ...

  5. openstack tap complete

    $ openstack complete > /etc/bash_completion.d/osc.bash_completion re-login and bash

  6. Android之socket服务端

    import java.io.DataInputStream; import java.io.IOException; import java.io.PrintWriter; import java. ...

  7. Computer Study

    2017.12.15日开始制定长期学习计划: 大二上: 目前短期计划: 1.每天一道PAT乙级(弄完PAT乙级)+校OJ(打基础).   2.先复习C++,17周后开始复习数据结构.(补任务  刚刚发 ...

  8. centOS6.8开放防火墙端口

    1.找到防火墙配置文件,/etc/sysconfig/iptables.如果是新装的linux系统,防火墙默认是被禁掉的,没有配置任何防火墙策略,所以不存在iptables.需要在控制台使用iptab ...

  9. topcoder srm 697 div1 -3

    1.给定长度为$n$ 的数组$b$,构造长度为$n$ 的且没有重复元素的数组$a$,令$p_{i}$表示$a$中除$a_{i}$外其他元素的乘积.构造出的$a$满足$a_{i}^{b_{i}}$能够被 ...

  10. JPA原理与实践、多数据源配置

    参考博客: https://segmentfault.com/a/1190000015047290?utm_source=Weibo&utm_medium=shareLink&utm_ ...