题目链接: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. AtCoder Beginner Contest 069 ABCD题

    题目链接:http://abc069.contest.atcoder.jp/assignments A - K-City Time limit : 2sec / Memory limit : 256M ...

  2. python shutil模块简单介绍

    python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...

  3. JMX堆栈分析

    线程堆栈: 线程堆栈也称线程调用堆栈,是虚拟机中线程(包括锁)状态的一个瞬间快照,即系统在某一个时刻所有线程的运行状态,包括每一个线程的调用堆栈,锁的持有情况.虽然不同的虚拟机打印出来的格式有些不同, ...

  4. Django之富文本编辑器

    1.在虚拟环境中安装包. pip install django-tinymce==2.6.0 2.在配置文件中INSTALLED_APPS注册 3.配置富文本编辑器的宽高 4.配置编辑器url.

  5. webform 使用富文本编辑器

    <div class="form-group"> <label class="col-xs-2 control-label text-right&quo ...

  6. weblogic11,linux字符页面安装

    1.  安装前 1  确定weblogic版本被认证 首先请确认您要安装的Weblogic版本所在的平台已通过了BEA的认证,完整的认证平台列表请参考http://e-docs.bea.com/wls ...

  7. Windows下用cmd命令实例讲解yii2.0 的控制台定时任务

    Yii中的资源是和Web页面相关的文件,可为CSS文件,JavaScript文件,图片或视频等,资源放在Web可访问的目录下,直接被Web服务器调用. 有时候有些功能需要做到计划任务中去,因此就需要y ...

  8. TF-IDF原理与实现

    TF-IDF 原理与实现 目录 1.原理 2.伪代码 3.实现 1.原理 \[ TF-IDF = tf_{t,d} \times idf_{t}\\ tf_{t,d} = \frac{术语t在文档d中 ...

  9. linux 计划任务 crontab 简单用法

    添加计划任务: 方法1: crontab -e 方法2: vim /etc/crontab #这种方式进去的文件有个sample可供参考 查看已经启动的任务:crontab -l 查看运行状态:ser ...

  10. PHP 重置数组为连续数字索引的几种方式

    原文链接:https://blog.csdn.net/zhang197093/article/details/78606916 推荐的方式  array_values 方法 这样方式无论对普通数组还是 ...