Codeforces 785E. Anton and Permutation
题目链接: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的更多相关文章
- Codeforces 785E Anton and Permutation(分块)
[题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...
- CodeForces 785E Anton and Permutation 分块
题意: 有一个\(1 \sim n\)的排列\(A\),有\(q\)个询问: 交换任意两个元素的位置,求交换之后排列的逆序数 分析: 像这种不太容易用线段树,树状数组维护的可以考虑分块 每\(\sqr ...
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数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 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- Codeforces 593B Anton and Lines
LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output st ...
随机推荐
- jquery 页面分页的实现
<!DOCTYPE html> <html> <head> <title>分页</title> <link rel="s ...
- MySQL 主表与从表
通过上一篇随笔,笔者了解到,实体完整性是通过主键约束实现的,而参照完整性是通过外键约束实现的,两者都是为了保证数据的完整性和一致性. 主键约束比较好理解,就是主键值不能为空且不重复,已经强调好多次,所 ...
- shell =~ 引发的思考
=~不是按位取反 1.[[]] if [[]]中引用变量不用加 双引号(")了,而if[]中变量必须加双引号,如if[ -n "$test" ],不然一些特殊的地方,会出 ...
- Source Insight 如何将script等文件加入
点击菜单栏Options -> Document Options , 然后再弹出的对话框中找到File filter,文件过滤的设置,里面肯能有*.c;*.h 你在后面添加Makefile,注意 ...
- AngularJS之登录显示用户名
效果图:在这里会显示出来用户名 使用AngularJs进行这样效果 第一步:写ng-app // 定义模块: var app = angular.module("pinyougou" ...
- django模型的crud操作
一个建立好的模型,django会生成一套完整的API,以便对对象进行crud操作,下面就详细介绍crud操作. 先说一下操作环境: python2.7, django1.11.8, win7系统,借助 ...
- python之auto鼠标/键盘事件
mouse_key.py import os import time import win32gui import win32api import win32con from ctypes impor ...
- Java操作文件Util
package io.guangsoft.utils; import java.io.File; import java.io.FileInputStream; import java.io.File ...
- 运行android模拟器,emulator: ERROR: x86 emulation currently requires hardware acceleration!
运行android模拟器,emulator: ERROR: x86 emulation currently requires hardware acceleration! 问题: 运行android模 ...
- 06: 字典、顺序表、列表、hash树 实现原理
算法其他篇 目录: 1.1 python中字典对象实现原理 1.2 顺序表 1.3 python 列表(list) 1.1 python中字典对象实现原理返回顶部 注:字典类型是Python中最常 ...