CodeForces 785E Anton and Permutation 分块
题意:
有一个\(1 \sim n\)的排列\(A\),有\(q\)个询问:
交换任意两个元素的位置,求交换之后排列的逆序数
分析:
像这种不太容易用线段树,树状数组维护的可以考虑分块
每\(\sqrt{n}\)个元素划分为一块,然后两端的块可以直接扫出逆序数的变化,中间的块可以用二分计算逆序数
在更新块的时候,可以二分查找要插入或删除的位置
每次询问的复杂度为\(O(\sqrt{n}log\sqrt{n})=O(\sqrt{n}logn)\)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 200000 + 10;
const int maxsqrt = 500;
#define ALL(x) x.begin(), x.end()
int n, q, a[maxn];
int st[maxsqrt], ed[maxsqrt];
vector<int> b[maxsqrt];
int main()
{
scanf("%d%d", &n, &q);
int col = (int)sqrt(n);
for(int i = 0; i < n / col; i++) { st[i] = col * i; ed[i] = col * (i + 1); }
if(n % col > 0) { st[n / col] = n - (n % col); ed[n / col] = n; }
for(int i = 0; i < n; i++) {
a[i] = i;
b[i / col].push_back(i);
}
long long ans = 0;
while(q--) {
int l, r; scanf("%d%d", &l, &r);
l--; r--;
if(l == r) { printf("%lld\n", ans); continue; }
if(l > r) swap(l, r);
int idl = l / col, idr = r / col;
//update
for(int i = idl + 1; i < idr; i++) {
int size = b[i].size();
int p = lower_bound(ALL(b[i]), a[l]) - b[i].begin();
ans -= p;
ans += size - p;
p = lower_bound(ALL(b[i]), a[r]) - b[i].begin();
ans += p;
ans -= size - p;
}
for(int i = l + 1; i < ed[idl] && i < r; i++) {
if(a[i] > a[l]) ans++; else ans--;
if(a[i] > a[r]) ans--; else ans++;
}
for(int i = st[idr]; i < r && i > l; i++) {
if(a[i] > a[l]) ans++; else ans--;
if(a[i] > a[r]) ans--; else ans++;
}
//swap
if(idl < idr) {
b[idl].erase(lower_bound(ALL(b[idl]), a[l]));
b[idl].insert(lower_bound(ALL(b[idl]), a[r]), a[r]);
b[idr].erase(lower_bound(ALL(b[idr]), a[r]));
b[idr].insert(lower_bound(ALL(b[idr]), a[l]), a[l]);
}
if(a[l] < a[r]) ans++; else ans--;
swap(a[l], a[r]);
printf("%lld\n", ans);
}
return 0;
}
CodeForces 785E Anton and Permutation 分块的更多相关文章
- Codeforces 785E Anton and Permutation(分块)
[题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...
- Codeforces 785E. Anton and Permutation
题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...
- 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 ...
随机推荐
- u-boot分析(五)----I/D cache失效|关闭MMU和cache|关闭看门狗
u-boot分析(五) 上篇博文我们按照210的启动流程,对u-boot启动中的设置异常向量表,设置SVC模式进行了分析,今天我们继续按照u-boot的启动流程对以下内容进行分析. 今天我们会用到的文 ...
- mysql-5.7安装配置指导
mysql 安装 yum 安装mysql 源码编译安装mysql 通过yum安装 下载yum仓库配置安装包 MySQL Yum Repository http://dev.mysql.com/down ...
- 问题解决 Visual Studio 2015 无法复制文件“D:\swapfile.sys”
莫名其妙的问题..度娘 必应统统不给力.. 还是找了谷大爷 严重性 代码 说明 项目 文件 行 列 类别 源 项目级别 工具 禁止显示状态错误 无法复制文件“D:\swapfile.sys”,原因是找 ...
- Aizu The Maximum Number of Customers
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_5_A The Maximum Number of Customers Ide ...
- 设有三个进程A、B、C,其中A与B构成一对生产者与消费者(A为生产者,B为消费者),共享一个由n个缓冲块组成的缓冲池;B与C也构成一对生产者与消费者(此时B为生产者,C为消费者)共享另一个由m个缓冲块组成的缓冲池。用P、V操作描述它们之间的同步关系。
生产者消费者问题 设信号量mutex1, mutex2, full1, full2, empty1, empty2分别表示1和2号缓冲区的访问互斥, 是否满, 是否空 semaphore mutex1 ...
- currency 过滤器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 3、SpringBoot+Mybatis整合------主键回填
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870 ...
- ubuntu安装jdk6
我的ubuntu版本是12.04的64位的.由于ubuntu官方没有更新最新版的jdk6,所以我们只能主动去oracle公司网站去下载.网站地址:http://www.oracle.com/techn ...
- architecture x86_64(Error)
undefined symbols for architecture x86_64 错误如下 因为提示文件非第三方文件,最初尝试使用以下方式处理 iOS :undefined symbols for ...
- web攻击技术与防护
一.跨站脚本攻击(XSS) 跨站脚本攻击是指通过存在安全漏洞的Web网站注册用户的浏览器运行非法的HTML标签或JavaScript进行的一种攻击.动态创建的HTML部分有可能隐藏着安全漏洞.就这样, ...