题意:

有一个\(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 分块的更多相关文章

  1. Codeforces 785E Anton and Permutation(分块)

    [题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...

  2. Codeforces 785E. Anton and Permutation

    题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...

  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. CKEditor插件开发

    以前做过一个教育项目,是有关在线考试的.其中对编辑器CKEditor做了扩充,增加了插入客观题.主观题.选择题和判断题的功能.这里记述下CKEditor插件开发的过程. CKEditor以前叫FCKE ...

  2. 【2017-07-01】Linux应用开发工程师面试问题记录之二:关于结构体的大小及内存对齐问题

    Tencent后台服务器开发有一道题是计算一个结构体的sizeof的大小: struct strData { int m_Int; char m_Char; short m_Short; char m ...

  3. C. Tanya and Toys_模拟

    C. Tanya and Toys time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. CNN中卷积的意义

    在传统的神经网络中,比如多层感知机(MLP),其输入通常是一个特征向量.需要人工设计特征,然后将用这些特征计算的值组成特征向量.在过去几十年的经验来看,人工找的特征并不总是好用.有时多了,有时少了,有 ...

  5. python将图像转化为矩阵

    Image.fromarray(matrix).show()

  6. path、classpath理解

    path.classpath最常见的场景:环境变量配置 path环境变量:设置path的作用是让操作系统可以找到JDK命令(指定了JDK命令搜索路径):path环境变量原来Windows里面就有,只需 ...

  7. System.Threading.Tasks

    前言: 我们之前介绍了两种构建多线程软件的编程技术(使用异步委托或通过System.Threading的成员).这两个可以在任何版本的.NET平台工作. 关于System.Threading 的介绍 ...

  8. Swift小记一

    1.输出地址 print(String(format: "%p", "temp")) 2.判断字符串是否为空串.是否为nil 为String添加一个分类 ext ...

  9. Servlet学习笔记02——什么是http协议?

    1.http协议 (了解) (1)什么是http协议? 是一种网络应用层协议,规定了浏览器与web服务器之间 如何通信以及相应的数据包的结构. 注: a.tcp/ip: 网络层协议,可以保证数据可靠的 ...

  10. Anaconda下安装 TensorFlow 和 keras 以及连接pycharm

    首先在官网下载Anaconda https://www.anaconda.com/download/ 安装时注意 勾选第一个,增加环境变量 安装好后再windows界面打开Anaconda Promp ...