[CF785E]Anton and Permutation
题目大意:有一串数为$1\sim n(n\leqslant2\times10^5)$,$m(m\leqslant5\times10^4)$次询问,每次问交换位置为$l,r$的两个数后数列中逆序对的个数。
题解:发现交换位置为$l,r$的数后,逆序对的变化只和区间$(l,r)$内的数与$s_l,s_r$的大小关系有关,设$S_i$表示区间$(l,r)$中比$s_i$小的数,$B_i$表示区间$(l,r)$中比$s_i$大的数,$ans'=ans+S_r-B_r-S_l+B_l$。设$len=r-l-1$,$ans'=ans+S_r-(len-S_r)-S_l+(len-S_l)=ans+2(S_r-S_l)$。
考虑分块,设块大小为$S$,在块内排序,在边角处暴力,在整块处二分查找位置,询问的复杂度是$O(2S+\dfrac n S\log_2S)$;修改为二分处位置直接插入或删除,复杂度$O(4S)$,所以当$S$略大于$\sqrt n$时最优。(反正我懒得算,随便猜一个)
卡点:
1. 计算$2(S_r-S_l)$时使用了位运算,当$S_r-S_l$为负数时出锅
2. 最开始块大小设成了$512$,计算块时用了$2^8$
C++ Code:
#include <algorithm>
#include <cstdio>
#include <vector>
#define maxn 200010
const int BSZ = 1000, BNUM = maxn / BSZ + 10; int n, m;
long long ans;
int L[BNUM], R[BNUM], bel[maxn], s[maxn];
std::vector<int> V[BNUM]; int query(int l, int r, int x) {
if (l > r) return 0;
const int lb = bel[l], rb = bel[r];
int res = 0;
if (lb == rb) for (int i = l; i <= r; ++i) res += s[i] < x;
else {
for (int i = l; i <= R[lb]; ++i) res += s[i] < x;
for (int i = lb + 1; i < rb; ++i) res += std::upper_bound(V[i].begin(), V[i].end(), x) - V[i].begin();
for (int i = L[rb]; i <= r; ++i) res += s[i] < x;
}
return res;
} int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) V[bel[i] = i / BSZ + 1].push_back(s[i] = i);
const int B = bel[n];
for (int i = 1; i <= B; ++i) {
L[i] = (i - 1) * BSZ;
R[i] = L[i] + BSZ - 1;
}
L[1] = 1, R[B] = n;
while (m --> 0) {
int l, r;
scanf("%d%d", &l, &r);
if (l == r) {
printf("%lld\n", ans);
continue;
}
if (l > r) std::swap(l, r);
const int lb = bel[l], rb = bel[r]; ans += (query(l + 1, r - 1, s[r]) - query(l + 1, r - 1, s[l])) * 2;
ans += (s[l] < s[r]) ? 1 : -1;
printf("%lld\n", ans); if (lb != rb) {
V[lb].erase(std::lower_bound(V[lb].begin(), V[lb].end(), s[l]));
V[lb].insert(std::upper_bound(V[lb].begin(), V[lb].end(), s[r]), s[r]);
V[rb].erase(std::lower_bound(V[rb].begin(), V[rb].end(), s[r]));
V[rb].insert(std::upper_bound(V[rb].begin(), V[rb].end(), s[l]), s[l]);
}
std::swap(s[l], s[r]);
}
return 0;
}
[CF785E]Anton and Permutation的更多相关文章
- Codeforces785E - Anton and Permutation
Portal Description 对一个长度为\(n(n\leq2\times10^5)\)的数列\(a\)进行\(m(m\leq5\times10^4)\)次操作,数列初始时为\(\{1,2,. ...
- 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 ...
- Anton and Permutation
Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input standa ...
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...
- 题解 CF785E 【Anton and Permutation】
考虑用分块解决这个题,一次交换对当前逆序对个数的影响是,加上两倍的在区间\([l+1,r-1]\)中比\(a_r\)小的元素个数,减去两倍的在区间\([l+1,r-1]\)中比\(a_l\)小的元素个 ...
- Codeforces 785E. Anton and Permutation
题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...
- 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 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
随机推荐
- NB-IOT使用LWM2M移动onenet对接之MTU最大传输单元设置
1. 最近遇到的一个项目NB-IOT使用LWM2M移动onenet对接,要求设置传输的MTU,因此首先需要搞懂MTU是什么? 以太网的MTU值是1500 bytes,假设发送者的协议高层向IP层发送了 ...
- 「知识学习&日常训练」莫队算法(一)(Codeforce Round #340 Div.2 E)
题意 (CodeForces 617E) 已知一个长度为\(n\)的整数数列\(a[1],a[2],-,a[n]\),给定查询参数\(l,r\),问\([l,r]\)内,有多少连续子段满足异或和等于\ ...
- hdu1730Northcott Game(nim博弈)
Northcott Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Java小记(1)
return 关键字 package mytest; public class Test4 { public static void main(String[] args) { // TODO Aut ...
- 域名添加www之后(或域名后加端口)无法访问(阿里云服务器)
当时在阿里云服务器上部署了一个api接口,通过APP调用一直很正常,突然无法访问了,然后测试调查发现,只要在域名前加上www,再通过域名加端口的方式访问的话, 显示的都是 :502 错误:还一直以为是 ...
- [CF294B]Shaass and Bookshelf
问题描述 Shaass拥有n本书.他想为他的所有书制作一个书架,并想让书架的长宽尽量小.第i本书的厚度是t[i],且这本书的纸张宽度是w[i].书的厚度是1或2,所有书都有同样的高度(即书架的高是均匀 ...
- Java 消息对列
ActiveMQ入门实例 1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5. ...
- 使用Zabbix监控rabbitmq服务
添加rabbitmq脚本 [root@controller rabbitmq]# cd /etc/zabbix/script/rabbitmq [root@controller rabbitmq]# ...
- LeetCode - 326, 342, 231 Power of Three, Four, and Two
1. 问题 231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数 342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数 326. Po ...
- Python3 Tkinter-Label
1.创建 from tkinter import * root=Tk() root.title('Hello tkinter!') root.mainloop() 2.使用内置位图 from tkin ...