莫队过不了吗
查看原帖
莫队过不了吗
494504
Sansyen楼主2021/10/6 12:49

洛谷提交不了,vjWA了几次,测了挺多数据没找出bug

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
ll belong[N], d[N], inp[N], cnt[N], typ[N], ans[N], book[N];
ll now;
struct query {
	int l;
	int r;
	int id;
}q[N];
bool cmp(query& a, query& b) {
	return belong[a.l] ^ belong[b.l] ? belong[a.l] < belong[b.l] : belong[a.l] & 1 ? a.r < b.r : a.r > b.r;
}
void add(int pos) {
	book[cnt[typ[pos]]]--;
	cnt[typ[pos]]++;
	book[cnt[typ[pos]]]++;
	now = max(now, cnt[typ[pos]]);
};
void del(int pos) {
	book[cnt[typ[pos]]]--;
	if (book[cnt[typ[pos]]] == 0 && cnt[typ[pos]] == now)now--;
	cnt[typ[pos]]--;
	book[cnt[typ[pos]]]++;
};
int main() {
	int n, m;
	while (scanf("%d", &n)) {
		if (n == 0)break;
		now = 0;
		memset(cnt, 0, sizeof(cnt));
		memset(book, 0, sizeof(book));
		scanf("%d", &m);
		int size = sqrt(n);
		for (int i = 1; i <= n; i++)
		{
			belong[i] = (i - 1) / size + 1;
		}
		for (int i = 1; i <= n; i++) {
			scanf("%lld", &d[i]);
			inp[i] = d[i];
		}
		int tot = unique(inp + 1, inp + n + 1) - inp - 1;
		for (int i = 1; i <= n; i++)typ[i] = lower_bound(inp + 1, inp + tot + 1, d[i]) - inp;
		for (int i = 1; i <= m; i++) {
			scanf("%d%d", &q[i].l, &q[i].r);
			q[i].id = i;
		}
		sort(q + 1, q + m + 1, cmp);
		int l = 1, r = 0;
		for (int i = 1; i <= m; i++) {
			int ql = q[i].l, qr = q[i].r;
			//cout << q[i].id << ' ' << q[i].l << ' ' << q[i].r;
			while (l < ql)del(l++);
			while (l > ql)add(--l);
			while (r < qr)add(++r);
			while (r > qr)del(r--);
			ans[q[i].id] = now;
			//cout << ' ' << now << endl;
		}

		for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]);
	}
	return 0;
}
2021/10/6 12:49
加载中...