题意:

有一个\(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. 微信公众平台开发——helloworld

    威信公众平台有两种模式:编辑模式 和 开发模式. 普通的功能可以通过编辑模式来搞定.开发模式具有更多的功能.让我们来使用开发模式开发helloword吧 步骤如下: 1.先注册一个公众号(https: ...

  2. ubuntu linux double tab

    在terminal中,输入部分指令,再按两下Tab键,可以显示以相关的指令

  3. 正则表达式转换python2的print为python3风格

    直接查找 print ([^\n\(]*)替换为 print($1)

  4. Javascript作业—封装type函数,返回较详细的数据类型

    Javascript作业—封装type函数,返回较详细的数据类型 思路: 1 取typeof的值,如果是数字.函数等非对象类型,直接取类型 2 如果是object类型,则调用Object.protot ...

  5. ABI and compiler

    http://stackoverflow.com/questions/2171177/what-is-application-binary-interface-abi ABIs cover detai ...

  6. 【HDU4676】Sum Of Gcd(莫队+欧拉函数)

    点此看题面 大致题意: 多组询问,求\(\sum_{i=L}^R\sum_{j=i+1}^Rgcd(i,j)\). 推式子 这道题我们可以考虑,每个因数\(d\)被统计答案的次数,肯定与其出现次数有关 ...

  7. HDU(1754),线段树,单点替换,区间最值

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 线段树模板题,update功能是单点替换,query是访问区间最大值. #include < ...

  8. 2018.9.7 ArrayList

    ArrayList简介 ArrayList核心源码 ArrayList源码分析 System.arraycopy()和Arrays.copyOf()方法 两者联系与区别 ArrayList核心扩容技术 ...

  9. EF问题集合

    1. 在使用数据迁移的过程中,如果手工删除了本地数据库之后,再次尝试连接被删除的数据库,会有以下提示: System.Data.SqlClient.SqlException (0x80131904): ...

  10. python剑指offer最小的K个数

    题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 使用快排中的partition思想. ①我们设定part ...