Codeforces 785E Anton and Permutation(分块)

当交换 O ( a x , a y ) O(a_x,a_y) O(ax,ay) 时,讨论区间 ( x , y ) (x,y) (x,y) 对答案的贡献有:

  • 加上区间内比 a y a_y ay 小的个数
  • 加上区间内比 a x a_x ax 大的个数
  • 减去区间内比 a y a_y ay 大的个数
  • 减去区间内比 a x a_x ax 小的个数
  • 减去 [ a x > a y ] [a_x > a_y] [ax>ay]
  • 加上 [ a x &lt; a y ] [a_x &lt; a_y] [ax<ay]

这样就转化为求一个区间内比给定值小/大的元素个数

对于每个块开一个 v e c t o r \mathsf{vector} vector

修改是单点的,直接暴力重构块就行

时间复杂度 O ( n n   l o g   n ) O(n\sqrt {n~log~n}) O(nn log n )

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(s) freopen(s".in", "r", stdin), freopen(s."out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 200010 ;
const int M = 2010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

int len, num, n, m ; ll ans ;
int s[N], bl[N], l[M], r[M] ;
vi v[M] ;

void build() {
	len = sqrt(n), num = (n - 1) / len + 1 ;
	rep(i, 1, n) s[i] = i ;
	rep(i, 1, n) bl[i] = (i - 1) / len + 1 ;
	rep(i, 1, num) l[i] = (i - 1) * len + 1, r[i] = i * len ;
	r[num] = n ;
	rep(i, 1, n) v[bl[i]].pb(s[i]) ;
}

int query(int x, int y, int w) {
	if (x > y) return 0 ;
	int L = bl[x], R = bl[y], res = 0 ;
	if (L == R) {
		rep(i, x, y) res += s[i] < w ;
	} else {
		rep(i, x, r[L]) res += s[i] < w ;
		rep(i, L - 1, R - 1) res += ub(all(v[i]), w) - v[i].begin() ;
		rep(i, l[R], y) res += s[i] < w ;
	}
	return res ;
}

void modify(int l, int r) {
	if (bl[l] != bl[r]) {
		int L = bl[l], R = bl[r] ;
		v[L].erase (lb(all(v[L]), s[l])) ;
		v[L].insert(ub(all(v[L]), s[r]), s[r]) ;
		v[R].erase (lb(all(v[R]), s[r])) ;
		v[R].insert(ub(all(v[R]), s[l]), s[l]) ;
	}
	swap(s[l], s[r]) ;
}

void solve(int l, int r) {
	if (l == r) {
		printf("%lld\n", ans) ; // 巧妙的优化
		return ;
	}
	if (l > r) swap(l, 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) ;
}

signed main(){
    scanf("%d%d", &n, &m) ;
    build() ;
	while (m--) {
		int l, r ; scanf("%d%d", &l, &r) ;
		solve(l, r) ;
		modify(l, r) ;
	}

	return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值