题解:P11398 众数
题目链接:P11398 众数
非常妙的一个题。
首先考虑如何暴力地求出每个位置的权值:
从左向右扫整个序列,开个桶记录
但是可以发现,每次从
因为
为了减少不必要的时间损耗,我们可以钦定一些点并记录在它之前每个数作为
不难想到分块,令块长为
那我们每次只需从最后一个块扫,如果没有得到答案,就再从倒数第二个块扫,
每次修改的复杂度是
但是,好像过不去。(或许写得好能过?)
考虑换一种块长,令块长从后向前分别为
代码:
#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;
}