# LOJ 510: 「LibreOJ NOI Round #1」北校门外的回忆

### 题意简述：

add(x, v) 操作执行的是：
$x \le n$ 时，执行 s[x] ^= v，并将 $x$ 变为 $x + \mathrm{lowbit}(x)$，并继续循环。

query(x) 操作执行的是：
$ans$ 的初始值为 $0$
$x > 0$ 时，执行 ans ^= s[x]，并将 $x$ 变为 $x - \mathrm{lowbit}(x)$，并继续循环。

### 题解：

$K = 3, x = 1$ 时，$x$ 的变化为（十进制下）：$1 \to 2 \to 4 \to 5 \to 7 \to 8 \to 10 \to \cdots$

#include <cstdio>

const int HASH = 19260817, NUMS = 10000005;
int h[HASH], nxt[NUMS], to[NUMS], tot;
inline int Hasher(int val, int typ) {
int cyp = (val ^ val >> 1) % HASH;
for (int i = h[cyp]; i; i = nxt[i]) if (to[i] == val) return i;
return typ ? nxt[++tot] = h[cyp], to[tot] = val, h[cyp] = tot : 0;
}

typedef long long LL;
const int MK = 200005;

int N, K, Q, V[6], C;
inline void lowbit(int x, int &y, int &z) {
int s = 1, t = C;
while (t) { if (x % V[t] == 0) x /= V[t], s *= V[t]; --t; }
y = x % K, z = y * s;
}
int ltrs[MK][30], ntrs[MK][30];
LL lsum[MK][30], nsum[MK][30];
int arr[NUMS];

int main() {
scanf("%d%d%d", &N, &Q, &K);
for (LL x = K; x <= N; x *= x) V[++C] = x;
for (int i = 1; i < K; ++i) if ((i & -i) >= (K & -K))
ltrs[i * 2 % K][0] = lsum[i * 2 % K][0] = i,
ntrs[i][0] = i * 2 % K, nsum[i][0] = i;
for (int j = 0; j < 29; ++j)
for (int i = 1; i < K; ++i) if ((i & -i) >= (K & -K))
ltrs[i][j + 1] = ltrs[ltrs[i][j]][j],
lsum[i][j + 1] = lsum[i][j] + lsum[ltrs[i][j]][j],
ntrs[i][j + 1] = ntrs[ntrs[i][j]][j],
nsum[i][j + 1] = nsum[i][j] + nsum[ntrs[i][j]][j];
for (int i = 1; i <= Q; ++i) {
int op, x, y, z, v, d;
scanf("%d%d", &op, &x);
if (op == 1) {
scanf("%d", &v);
while (x <= N) {
lowbit(x, y, z);
if ((y & -y) >= (K & -K)) {
int id = 0, X = x, Y = y, w = z / y;
for (int j = 29; j >= 0; --j)
if (lsum[Y][j] < X / w)
id |= 1 << j,
X -= lsum[Y][j] * w,
Y = ltrs[Y][j];
++id;
for (int j = 0; x <= N; ++j) if (id >> j & 1) {
arr[Hasher(x, 1)] ^= v;
x += nsum[y][j] * w;
y = ntrs[y][j];
id += 1 << j;
}
break;
}
arr[Hasher(x, 1)] ^= v;
x += z;
}
} else {
int Ans = 0;
while (x) {
lowbit(x, y, z);
if ((y & -y) >= (K & -K)) {
int id = 0, X = x, Y = y, w = z / y;
for (int j = 29; j >= 0; --j)
if (lsum[Y][j] < X / w)
id |= 1 << j,
X -= lsum[Y][j] * w,
Y = ltrs[Y][j];
++id;
X = x, Y = y;
for (int j = 0; id; ++j, id >>= 1) if (id & 1) {
if ((d = Hasher(X, 0))) Ans ^= arr[d];
X -= lsum[Y][j] * w;
Y = ltrs[Y][j];
}
} else if ((d = Hasher(x, 0))) Ans ^= arr[d];
x -= z;
}
printf("%d\n", Ans);
}
}
return 0;
}

posted @ 2019-11-23 00:09  粉兔  阅读(...)  评论(...编辑  收藏