「BZOJ 4105」平方运算-线段树

现有一个长度为 $n$ 的序列 ${x_1, x_2, \cdots, x_n}$,要求支持两种操作:

  1. 0 l r 表示将 $i \in [l, r], x_i \leftarrow x_i^2 \bmod p$
  2. 1 l r 询问 $\sum\limits_{i = l} ^ r x_i$

链接

BZOJ 4105

题解

我们可以通过打表发现题目所给的模数,很快就能够使 $x_i \rightarrow x_i ^ 2 \bmod p$ 发生循环(至多 $11$ 步),同时每个环的长度都小于 $60$。

所以我们可以用线段树来维护,先预处理出循环,每次平方操作就相当于在环上走一步,对于不在环上的先暴力走,至多 $11$ 步就会进入循环。

在线段树上分解区间,如果区间里面的所有数都在环中,那么这个区间节点维护所有数的环的 $\text{lcm}$,环上的第 $i$ 个节点维护若这个区间进行 $i$ 次平方操作,和是多少。

时间复杂度为 $O(kn \log n)$,$k$ 为循环长度。

代码

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* Copyright (c) 2017, xehoth
* All rights reserved.
* 「BZOJ 4105」平方运算 12-10-2017
* 线段树
* @author xehoth
*/
#include <bits/stdc++.h>

namespace IO {

inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
return s == t ? -1 : *s++;
}

template <typename T>
inline bool read(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return false;
c == '-' ? iosig = true : 0;
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
iosig ? x = -x : 0;
return true;
}

inline void read(char &c) {
while (c = read(), isspace(c) && c != -1)
;
}

inline int read(char *buf) {
register int s = 0;
register char c;
while (c = read(), isspace(c) && c != -1)
;
if (c == -1) {
*buf = 0;
return -1;
}
do
buf[s++] = c;
while (c = read(), !isspace(c) && c != -1);
buf[s] = 0;
return s;
}

const int OUT_LEN = 1000000;

char obuf[OUT_LEN], *oh = obuf;

inline void print(char c) {
oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
*oh++ = c;
}

template <typename T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
x < 0 ? (print('-'), x = -x) : 0;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
while (cnt) print((char)buf[cnt--]);
}
}

inline void print(const char *s) {
for (; *s; s++) print(*s);
}

inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }

struct InputOutputStream {
template <typename T>
inline InputOutputStream &operator>>(T &x) {
read(x);
return *this;
}

template <typename T>
inline InputOutputStream &operator<<(const T &x) {
print(x);
return *this;
}

~InputOutputStream() { flush(); }
} io;
}

namespace {

using IO::io;

const int MAXN = 100000;
const int MAX_MOD = 9977;
bool inCircle[MAX_MOD + 1];
int n, m, next[MAX_MOD + 1], mod, a[MAXN + 1];

inline int lcm(int a, int b) { return a / std::__gcd(a, b) * b; }

struct Node {
Node *lc, *rc;
int sum, len, pos, tag, circle[62];
bool in;
Node();

inline void *operator new(size_t);

inline void init(const int v) {
sum = v;
if ((in = inCircle[v])) {
len = 1, circle[pos = 0] = v;
for (register int u = next[v]; u != v; u = next[u])
circle[len++] = u;
}
}

inline void maintain() {
sum = lc->sum + rc->sum;
if ((in = lc->in & rc->in)) {
len = lcm(lc->len, rc->len);
register int tx = lc->pos, ty = rc->pos;
for (register int i = 0; i < len; i++) {
circle[i] = lc->circle[tx++] + rc->circle[ty++];
(tx == lc->len) ? tx = 0 : 0;
(ty == rc->len) ? ty = 0 : 0;
}
pos = 0;
}
}

inline void pushDown() {
if (tag) {
lc->tag += tag, rc->tag += tag, lc->pos += tag, rc->pos += tag;
(lc->pos >= lc->len) ? lc->pos %= lc->len : 0;
lc->sum = lc->circle[lc->pos];
(rc->pos >= rc->len) ? rc->pos %= rc->len : 0;
rc->sum = rc->circle[rc->pos];
tag = 0;
}
}
} pool[MAXN * 4 + 1], *null = pool, *cur = pool + 1, *root;

inline void *Node::operator new(size_t) { return cur++; }

Node::Node() : lc(null), rc(null) {}

void build(Node *&p, int l, int r) {
p = new Node();
if (l == r) {
p->init(a[l]);
return;
}
register int mid = l + r >> 1;
build(p->lc, l, mid), build(p->rc, mid + 1, r), p->maintain();
}

void modify(Node *p, int l, int r, int s, int t) {
if (l == s && t == r && p->in) {
p->tag++, p->pos++;
(p->pos == p->len) ? p->pos = 0 : 0;
p->sum = p->circle[p->pos];
return;
}
if (l == r) {
p->init(next[p->sum]);
return;
}
p->pushDown();
register int mid = l + r >> 1;
if (t <= mid)
modify(p->lc, l, mid, s, t);
else if (s > mid)
modify(p->rc, mid + 1, r, s, t);
else
modify(p->lc, l, mid, s, mid), modify(p->rc, mid + 1, r, mid + 1, t);
p->maintain();
}

int query(Node *p, int l, int r, int s, int t) {
if (l == s && t == r) return p->sum;
p->pushDown();
register int mid = l + r >> 1;
if (t <= mid)
return query(p->lc, l, mid, s, t);
else if (s > mid)
return query(p->rc, mid + 1, r, s, t);
return query(p->lc, l, mid, s, mid) + query(p->rc, mid + 1, r, mid + 1, t);
}

inline void init() {
for (register int i = 1; i <= n; i++) io >> a[i];
for (register int i = 0; i < mod; i++)
next[i] = i * i % mod, inCircle[i] = true;
static bool vis[MAX_MOD + 1];
for (register int i = 0, y; i < mod; i++) {
if (!vis[i]) {
for (y = i; !vis[y];) vis[y] = true, y = next[y];
for (register int j = i; j != y; j = next[j]) inCircle[j] = false;
}
}
}

inline void solve() {
io >> n >> m >> mod;
init();
build(root, 1, n);
for (register int cmd, l, r; m--;) {
io >> cmd >> l >> r;
switch (cmd) {
case 0: {
modify(root, 1, n, l, r);
break;
}
case 1: {
io << query(root, 1, n, l, r) << '\n';
break;
}
}
}
}
}

int main() {
#ifdef DBG
freopen("sample/1.in", "r", stdin);
#endif
solve();
return 0;
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×