跳到正文

个人记录

【信友队2026寒假】Day1 总结

Limit_Yank_Peak约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年2月4日
收录
2026年7月29日
更新
2026年11月9日
篇幅
约 6 分钟

版本

  1. v1当前

曼哈顿距离转切比雪夫距离

一个坐标系旋转 45∘45^\circ,其坐标由 (x,y)(x,y) 转变为 (x+y2,x−y2)(\frac{x+y}{2},\frac{x-y}{2})。

曼哈顿转切比雪夫:

坐标 (x,y)(x,y) 转变为 (x+y,x−y)(x+y,x-y)。

使用场景:

  • 需要处理菱形的:例如将坐标系全部旋转 45∘45^{\circ} 使得所有的菱形转换为正方形的。

    例题:信友队集训 - 好三角形。

区间查询MEX

例题:P4137 Rmq Problem / mex - 洛谷

MEX:最小未出现过的自然数。

对于多次询问,我们考虑莫队。通过桶和分块数组维护区间使用。

Code:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace florr{
	inline int read()
	{
		int x = 0, f = 1;
		char ch = getchar();
		while (ch<'0' || ch > '9')
		{
			if (ch == '-')
				f = -1;
			ch = getchar();
		}
		while (ch >= '0' && ch <= '9')
			x = x * 10 + ch - '0', ch = getchar();
		return x * f;
	}
	void write(int x)
	{
		if (x < 0)
			putchar('-'), x = -x;
		if (x == 0) {putchar('0'); return;}
		if (x > 9)
			write(x / 10);
		putchar(x % 10 + '0');
	}
}
using namespace florr;

const int MAXN = 2e5 + 10;
const int MAX_A = 2e5;
int n, m, a[MAXN];

struct Ques {
	int l, r, ans, idx;
} ques[MAXN];
int block_size; 
int bel[MAXN];

int cnt[MAX_A + 10];
int B; 
bool full[(MAX_A + 10) / 450 + 20]; 

bool cmp_mo(Ques x, Ques y) {
	if (bel[x.l] != bel[y.l]) return bel[x.l] < bel[y.l];
	return bel[x.l] % 2 ? x.r < y.r : x.r > y.r;
}

bool cmp_idx(Ques x, Ques y) {
	return x.idx < y.idx;
}

void init_block() {
	B = sqrt(MAX_A) + 1; 
	memset(full, 0, sizeof(full));
	memset(cnt, 0, sizeof(cnt));
}

void update(int x, int add) {
	if (x > MAX_A) return;
	if (add) {
		if (cnt[x] == 0) {
			cnt[x]++;
			int k = x / B;
			bool is_full = true;
			for (int i = k * B; i <= min((k + 1) * B - 1, MAX_A); i++) {
				if (cnt[i] == 0) {
					is_full = false;
					break;
				}
			}
			full[k] = is_full;
		} else {
			cnt[x]++;
		}
	} else {
		if (cnt[x] == 1) {
			cnt[x]--;
			int k = x / B;
			full[k] = false; 
		} else if (cnt[x] > 1) {
			cnt[x]--;
		}
	}
}

int get_mex() {
	int k = 0;
	while (k * B <= MAX_A && full[k]) k++;
	int start = k * B;
	for (int x = start; x <= min((k + 1) * B - 1, MAX_A); x++) {
		if (cnt[x] == 0) {
			return x;
		}
	}
	return MAX_A + 1;
}

signed main() {
	n = read();
	m = read();
	for (int i = 1; i <= n; i++) {
		a[i] = read();
	}
	for (int i = 1; i <= m; i++) {
		ques[i].l = read();
		ques[i].r = read();
		ques[i].idx = i;
		ques[i].ans = 0;
	}
	
	block_size = sqrt(n) + 1;
	for (int i = 1; i <= n; i++) {
		bel[i] = (i - 1) / block_size + 1;
	}
	sort(ques + 1, ques + 1 + m, cmp_mo);
	init_block();
	
	int cur_l = 1, cur_r = 0;
	for (int i = 1; i <= m; i++) {
		int l = ques[i].l, r = ques[i].r;
		while (cur_r < r) update(a[++cur_r], 1);
		while (cur_l > l) update(a[--cur_l], 1);
		while (cur_r > r) update(a[cur_r--], 0);
		while (cur_l < l) update(a[cur_l++], 0);
		ques[i].ans = get_mex();
	}
	
	sort(ques + 1, ques + 1 + m, cmp_idx);
	for (int i = 1; i <= m; i++) {
		write(ques[i].ans);
		putchar('\n');
	}
	
	return 0;
}
原文