社区讨论

求助大佬一堆RE和TLE

P4168[Violet] 蒲公英参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mixa47xc
此快照首次捕获于
2025/12/08 23:01
2 个月前
此快照最后确认于
2025/12/11 21:05
2 个月前
查看原帖
CPP
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;

int n, m, a[40005], pos, num[40005], b[40005], len, tot, f[205][205], id[40005], lc[205], rc[205], sum[205][40005], arr[40005];
int fd(int x)
{
	return lower_bound(b + 1, b + pos + 1, x) - b;
}
int ask(int x, int ql, int qr)
{
	int ans = sum[id[qr + 1] - 1][x] - sum[id[ql - 1]][x];
	for (int pos = ql; pos <= rc[id[ql - 1]]; pos++) if (arr[pos] == x) ans++;
	for (int pos = lc[id[qr + 1]]; pos <= qr; pos++) if (arr[pos] == x) ans++;
	return ans;
}
int query(int l, int r)
{
	int ql, qr, tms = 0, c, d;
	if (l % len == 1) ql = -1, c = id[l];
	else ql = rc[id[l]], c = id[l] + 1;
	if (r % len == 0) qr = -1, d = id[r];
	else qr = lc[id[r]], d = id[r] - 1;
	if (id[r] - id[l] <= 1)
	{
		int fin = 0;
		for (int j = l; j <= r; j++) num[arr[j]] = 0;
		for (int j = l; j <= r; j++)
		{
			num[arr[j]]++;
			if ((num[arr[j]] > num[arr[fin]]) || (num[arr[j]] == num[arr[fin]] && a[j] < fin)) fin = a[j];
		}
		return fin;
	}
	//cout << "c:" << c << ' ' << "?" << d << endl;
	int tmp = f[c][d];
	//cout << "tmp:" << tmp << endl;
	tms = ask(fd(tmp), l, r);
	//cout << "tms:" << tms << endl;
	if (ql != -1)
	{
		for (int q = l; q <= ql; q++)
		{
			int qask = ask(arr[q], l, r);
			if ((tms < qask) || (tms == qask && a[q] < tmp))
			{
				tms = qask;
				tmp = a[q];
			}
		}
	}
	if (qr != -1)
	{
		for (int q = qr; q <= r; q++)
		{
			int qask = ask(arr[q], l, r);
			if ((tms < qask) || (tms == qask && a[q] < tmp))
			{
				tms = qask;
				tmp = a[q];
			}
		}
	}
	return tmp;
}
int main()
{
	cin >> n >> m;
	len = int(sqrt(n));
	tot = (n + len - 1) / len;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		id[i] = int((i - 1) / len) + 1;
		cout << id[i] << ' ';
		b[i] = a[i];
		if (i % len == 1) lc[id[i]] = i;
		if (i % len == 0) rc[id[i]] = min(i, n);
		cout << lc[id[i]] << ' ' << rc[id[i]] << endl;
	}
	sort(b + 1, b + n + 1);
	pos = unique(b + 1, b + n + 1) - b - 1;
	for (int i = 1; i <= tot; i++)
	{
		memset(num, 0, sizeof(num));
		int maxn = 0, temp = 1e9;
		for (int j = lc[i]; j <= n; j++)
		{
			if (!arr[j]) arr[j] = fd(a[j]);
			int k = arr[j];
			num[k]++;
			if ((maxn < num[k]) || (maxn == num[k] && a[j] < temp))
			{
				maxn = num[k];
				temp = a[j];
			}
			if (j % len == 0) 
			{
				f[i][id[j]] = temp;
				//cout << "f" << temp << endl;
			}
		}
	}
	for (int i = 1; i <= tot; i++)
	{
		for (int j = 1; j <= n; j++) sum[i][arr[j]] = sum[i - 1][arr[j]];
		for (int j = lc[i]; j <= rc[i]; j++) sum[i][arr[j]]++;
	}
	int x = 0, pl, pr;
	//cin >> x >> pl >> pr;
	//cout << ask(x, pl, pr) << endl << endl;
	for (int i = 1; i <= m; i++)
	{
		int l0, r0, l, r;
		cin >> l0 >> r0;
		//l = l0, r = r0; 
		l = ((l0 + x - 1) % n) + 1, r = ((r0 + x - 1) % n) + 1;
		if (l > r) swap(l, r);
		//cout << l << ' ' << r << endl;
		x = query(l, r);
		cout << x << endl; 
	} 
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...