[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;
}

  

原文地址:https://www.cnblogs.com/Memory-of-winter/p/10329505.html

时间: 2024-10-22 06:21:17

[CF785E]Anton and Permutation的相关文章

#404 (div2)Anton and Permutation (分块处理)

Anton likes permutations, especially he likes to permute their elements. Note that a permutation of n elements is a sequence of numbers {a1,?a2,?...,?an}, in which every number from 1 to n appears exactly once. One day Anton got a new permutation and

Codeforces 785 E. Anton and Permutation(分块,树状数组)

Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求将索引为l,r的两个数交换位置,并给出交换后数组中的逆序对数. 思路:此题用到了分块的思想,即将这组数分为bsz块,在每一块上建Fenwick树,对于每次查询,只需要处理l,r中间的块和l,r所在块受影响的部分.具体实现见代码及注释. #include<iostream> #include<

codeforces785E Anton and Permutation

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! 题目链接:codeforces785E 正解:分块 解题报告: 考虑每次的交换,只会影响到$[l,r]$这个区间的数,那么分块维护每次的$update$操作就好了,又暴力又好写. //It is made by ljh2000 //有志者,事竟成,破釜沉舟

CodeForces 785E Anton and Permutation

分块,暴力. 将序列分成$sqrt(n)$块,每块$sqrt(n)$个元素,每块内排序. 每次操作要计算这个区间中比$a[p1]$大的有几个,小的有几个,比$a[p2]$大的有几个,小的有几个,端点的块内暴力找,中间的块内二分找. 交换完数字之后,可以直接重新$sort$排个序. 总体时间复杂度$O(m*log(sqrt(n))*sqrt(n))$. #include <cstdio> #include <cmath> #include <cstring> #inclu

CF785CAnton and Permutation(分块 动态逆序对)

Anton likes permutations, especially he likes to permute their elements. Note that a permutation of n elements is a sequence of numbers {a1, a2, ..., an}, in which every number from 1 to n appears exactly once. One day Anton got a new permutation and

【Codeforces Round #404 (Div. 2)】题解

A. Anton and Polyhedrons 直接统计+答案就可以了. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define maxn 1000 #define LL long long using namespace std; char s[10000]; int main() { int n; LL sum=0; scanf("%d&qu

31. Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme

Permutation Sequence

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3

Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme