跳到正文

题解

题解:P11398 众数

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

文章信息

发布
2024年12月15日
收录
2026年11月10日
更新
2026年11月21日
篇幅
约 6 分钟
获赞
6
题目
P11398

版本

  1. v1当前

题目链接:P11398 众数

非常妙的一个题。

首先考虑如何暴力地求出每个位置的权值:

从左向右扫整个序列,开个桶记录 1∼n1\sim n 当前作为 bib_i 各出现了几次,再用 maxxmaxx 记录出现次数最多的是哪一个,加进来一个数就把那个数的桶 +a+a 并将新值和 maxxmaxx 比较。

但是可以发现,每次从 11 扫到 nn,很多时候是重复的。

因为 ∑k≤5×107\sum k \le 5 \times 10^7,所以很多时候 kk 都是较小的,那么较靠前的数就会被重复算很多遍。

为了减少不必要的时间损耗,我们可以钦定一些点并记录在它之前每个数作为 bib_i 的出现次数,这样我们在后续扫的时候就只要从这个点开始。

不难想到分块,令块长为 n\sqrt n,记录每个块之前所有块的 1∼n1\sim n 出现次数的总和。

那我们每次只需从最后一个块扫,如果没有得到答案,就再从倒数第二个块扫,⋯\cdots,以此类推,直到找到答案为止。

每次修改的复杂度是 O(n)O\left(\sqrt n\right)。令 mm 和 nn 同阶,时间复杂度为 O(nn+∑k)O\left(n\sqrt n+\sum k\right)。

但是,好像过不去。(或许写得好能过?)

考虑换一种块长,令块长从后向前分别为 1,2,4,8,⋯1,2,4,8,\cdots,共有 log⁡n\log n 块,修改的时间复杂度为 O(log⁡n)O\left(\log n\right),总时间复杂度 O(nlog⁡n+∑k)O\left(n\log n + \sum k\right),可过。

代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;

int read()
{
	int f = 1;
	char c = getchar();
	while (!isdigit(c))
	{
	    if (c == '-') f = -1;
	    c = getchar();
    }
	int x = 0;
	while (isdigit(c))
	{
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}

int buf[15];

void write(int x)
{
	int p = 0;
	if (x < 0)
	{
	    putchar('-');
	    x = -x;
	}
	if (x == 0) putchar('0');
	else
	{
		while (x)
		{
			buf[++p] = x % 10;
			x /= 10;
		}
		for (int i = p; i >= 1; i--)
			putchar('0' + buf[i]);
	}
}

int T, n, m, tot, a[300005], b[300005], belong[300005], cur[300005], curcnt[300005], cnt[35][300005];

struct node
{
	int l, r, maxx;
} block[35];

signed main()
{
	T = read(), n = read(), m = read();
	for (int i = 1; i <= n; i++)
		a[i] = read(), b[i] = read();
	int now = n, len = 1;
	while (now >= 1)
	{
		tot++;
		block[tot].r = now;
		now -= len;
		block[tot].l = now + 1;
		len *= 2;
	}
	block[tot].l = 1;
	reverse(block + 1, block + tot + 1);
	for (int i = 1; i <= tot; i++)
		for (int j = block[i].l; j <= block[i].r; j++)
			belong[j] = i;
	for (int i = 1; i <= tot; i++)
	{
		for (int j = 1; j <= n; j++)
			cnt[i][j] = cnt[i - 1][j];
		block[i].maxx = block[i - 1].maxx;
		for (int j = block[i].l; j <= block[i].r; j++)
		{
			cnt[i][b[j]] += a[j];
			if (cnt[i][block[i].maxx] < cnt[i][b[j]] || (cnt[i][block[i].maxx] == cnt[i][b[j]] && b[j] > block[i].maxx)) block[i].maxx = b[j];
		}
	}
	while (m--)
	{
		int op, x, y;
		op = read(), x = read();
		if (op == 1)
		{
			y = read();
			a[x] += y;
			for (int i = belong[x]; i <= tot; i++)
			{
				cnt[i][b[x]] += y;
				if (cnt[i][block[i].maxx] < cnt[i][b[x]] || (cnt[i][block[i].maxx] == cnt[i][b[x]] && b[x] > block[i].maxx)) block[i].maxx = b[x];
			}
		}
		else
		{
			int Tot = 0, B = 0;
			for (int i = tot - 1; i >= 0; i--)
			{
				int curmaxx = block[i].maxx;
				for (int j = block[i + 1].l; j <= block[i + 1].r; j++)
				{
					curcnt[b[j]] += a[j];
					if (cnt[i][curmaxx] + curcnt[curmaxx] < cnt[i][b[j]] + curcnt[b[j]] || (cnt[i][curmaxx] + curcnt[curmaxx] == cnt[i][b[j]] + curcnt[b[j]] && b[j] > curmaxx)) curmaxx = b[j];
					cur[j] = curmaxx;
				}
				for (int j = block[i + 1].r; j >= block[i + 1].l; j--)
				{
					Tot ^= (cur[j] * a[j]);
					if (Tot == x)
					{
						write(n - j + 1);
						putchar('\n');
						B = 1;
						break;
					}
				}
				for (int j = block[i + 1].l; j <= block[i + 1].r; j++)
					curcnt[b[j]] -= a[j];
				if (B) break;
			}
		}
	}
	return 0;	
}
原文