题目链接: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. flask框架----设置配置文件的几种方式

    设置配置文件的几种方式 ==========方式一:============ app.config['SESSION_COOKIE_NAME'] = 'session_lvning' #这种方式要把所 ...

  2. 怎样从外网访问内网RESTful API?

    本地部署了RESTful API,只能在局域网内访问,怎样从外网也能访问到本地的RESTful API呢?本文将介绍具体的实现步骤. 准备工作 部署并启动RESTful API服务端 默认部署的RES ...

  3. linux apidoc的安装和使用

    1.先去官网下载已编译好的安装包 以Centos7.4 64位为例, 下载地址: https://nodejs.org/dist/v8.1.2/node-v8.1.2-linux-x64.tar.xz ...

  4. 安装虚拟环境virtualenvwrapper和django

    以下操作在windows平台进行 1.安装虚拟环境virtualenvwrapper 首先需要安装python管理工具pip,安装完python3.7之后自带了pip,可通过where pip查看管理 ...

  5. Java操作文件Util

    package io.guangsoft.utils; import java.io.File; import java.io.FileInputStream; import java.io.File ...

  6. golang中tcp socket粘包问题和处理

    转自:http://www.01happy.com/golang-tcp-socket-adhere/ 在用golang开发人工客服系统的时候碰到了粘包问题,那么什么是粘包呢?例如我们和客户端约定数据 ...

  7. 【题解】Luogu P5068 [Ynoi2015]我回来了

    众所周知lxl是个毒瘤,Ynoi道道都是神仙题,这道题极其良心,题面好评 原题传送门 我们先珂以在\(O(n^2)\)的时间内bfs求出任意两点距离 我们考虑如何计算从一个点到所有点的最短路长度小于等 ...

  8. Android之Sqlite数据库

    数据库访问完毕后,游标必须也记得关闭 import com.huangzhong.love_power_model.UserInfoDto; import java.util.ArrayList; i ...

  9. python简说(二)list

    一.list # 1.list 列表 数组a = ['A', 'B', 'C', 'D']# 0 1 2# 2.空list# a = []# a = list()# 3.下标 角标 索引# print ...

  10. js通过 URL下载文件

    页面上一个button,点击之后触发一个function去请求数据,返回 pdf/epub 的URL,然后下载这个文件. 本来是直接用 a 写的,href里放资源地址,target设为'_blank' ...