oi

题解:P17849「RiOI-(-2)」houran 和序列

Posted by Vesperon on 2026-11-14
Estimated Reading Time 3 Minutes
Words 692 In Total
article views

喜报:比赛时因快读喜提95pts

题意

给你一个长度为 $n$ 的正整数序列 $a$ ,进行 $m$ 次单点修改,每次把 $a_x$ 改成 $y$

在修改前以及每次修改后,设整个序列的乘积为 $X$:

  • 若 $X < V$,记这次答案为 $X$
  • 否则记答案为 $0$

求这 $m+1$ 个答案的异或和

$1 \le n,m \le 5\times10^6$,$2 \le V \le 10^9$,$1 \le a_i,y \le V$

思路

注意到 $V$ 的最大值是 $10^9$,假设有 $n$ 个大于1的 $a$ ,那么就有
$$2^{30} = 1\,073\,741\,824 > 10^9 \ge V$$
由此可知,有效的乘积最多只有$29$个,如果超过$29$个,则答案一定为 $0$

于是我们只需要关心“哪些位置的值 $>1$”这一个集合:

  • 集合大小 $\ge 30$:答案 $0$;
  • 集合大小 $\le 29$:只有不到 $30$ 个数要乘,逐个乘、中途 $\ge V$ 就提前退出即可。

实现

如上所述,我们需要维护一个集合

需要支持的操作只有两种:

  1. 删除一个位置(它的值从 $>1$ 变成 $1$);
  2. 插入一个位置(它的值从 $1$ 变成 $>1$)。

用一个数组 idx 存所有 $>1$ 的位置,再用 pos[x] 记录位置 $x$ 在 idx 里的下标。删除时把 idx 末尾元素换到空位再 pop_back,这样插入和删除都是 $O(1)$。

逐次乘 $a[idx[i]]$ 直到$\ge V$停止,修改前要算一次,之后每次修改后再算一次,一共 $m+1$ 个答案,把它们全部异或起来。

复杂度:$O(n + m\log V)$

本题需要使用快读。

代码(经 dsh 修改快读部分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;

static char *ib;
static int ip;
static inline int read() {
while (ib[ip] < '0')
++ip;
int x = 0;
while (ib[ip] >= '0')
x = x * 10 + (ib[ip++] & 15);
return x;
}

static int a[5000005], pos[5000005];
namespace Vesperon {
void work() {
fseek(stdin, 0, SEEK_END);
int sz = ftell(stdin);
fseek(stdin, 0, SEEK_SET);
ib = (char *)malloc(sz + 1);
fread(ib, 1, sz, stdin);
ib[sz] = 0;

int n = read(), m = read();
long long V = read();

vector<int> idx;
idx.reserve(n);
for (int i = 1; i <= n; i++) {
a[i] = read();
if (a[i] > 1)
pos[i] = idx.size() + 1, idx.push_back(i);
}

auto calc = [&]() -> long long {
if (idx.size() >= 30)
return 0;
long long p = 1;
for (int i : idx) {
p *= a[i];
if (p >= V)
return 0;
}
return p;
};

long long xr = calc();
while (m--) {
int x = read(), y = read();
if (pos[x] && y == 1) {
int j = pos[x] - 1, last = idx.back();
idx[j] = last;
pos[last] = j + 1;
idx.pop_back();
pos[x] = 0;
} else if (!pos[x] && y > 1) {
pos[x] = idx.size() + 1;
idx.push_back(x);
}
a[x] = y;
xr ^= calc();
}
printf("%lld\n", xr);
}
} // namespace Vesperon

signed main() {
freopen("query.in", "r", stdin);
freopen("query.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return Vesperon::work(), 0;
}

如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !