题目链接: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. HDU 1846 Brave Game (巴什博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1846 十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏& ...

  2. 在vim编辑器python实现tab补全功能

    在vim编辑器中实现python tab补全插件有Pydiction,Pydiction可以实现下面python代码的自动补全: 1. 简单python关键词补全 2. python函数补全带括号 3 ...

  3. The Little Prince-12/12

    The Little Prince-12/12 双十二,大家有没有买买买呢?宝宝双十一之后就吃土了,到现在,叶子都长出来了!!! 当你真的喜欢一个人的时候 就会想很多 会很容易办蠢事 说傻话 小王子要 ...

  4. 左移和右移运算符<< >>

    左移的符号为<< 首先来个简单的例子    求8左移两位的值  8<<2   首先 将十进制的8转为二进制的数    倒序输出----> 1000 1000  左移两位 ...

  5. MyEclipse与Eclipse配置清单

    MyEclipse与Eclipse配置清单 1.编码设置    workspace -> 设置全局编码utf-8    修改JSP编码(Encoding)为UTF-82.Java配置    格式 ...

  6. Python小项目四:实现简单的web服务器

    https://blog.csdn.net/u010103202/article/details/74002538 本博客是整理在学习实验楼的课程过程中记录下的笔记形成的,参考:https://www ...

  7. Java axis2.jar包详解及缺少jar包错误分析

    Java  axis2.jar包详解及缺少jar包错误分析 一.最小开发jar集 axis2 开发最小jar包集: activation-1.1.jar axiom-api-1.2.13.jar ax ...

  8. netty支持的各种socketchannel实现及比较

    性能上从低到高如下: OioSocketChannel:传统,阻塞式编程. NioSocketChannel:select/poll或者epoll,jdk 7之后linux下会自动选择epoll. E ...

  9. 深入浅出MyBatis-快速入门

    http://blog.csdn.net/hupanfeng/article/details/9068003/

  10. opencv学习之路(13)、图像阈值化threshold

    一.图像阈值化简介 二.固定阈值 三.自适应阈值 #include<opencv2/opencv.hpp> using namespace cv; void main(){ Mat src ...