CF 1422F Boring Queries

题目描述

https://www.luogu.com.cn/problem/CF1422F

Solution

我们考虑一下离线,右端点递增,用线段树维护左端点的答案

对于右端点移动,我们首先对于 $a[i]$ 质因数分解,考虑 $p^m$

能够发现这是一个单调栈,对于强制在线,我们直接把这东西扔到主席树上就行

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
#include <iostream>
#include <cstdio>
#include <vector>
#define maxn 100010
#define maxm 200010
#define INF 1000000000
#define ll long long
using namespace std;

const int p = 1000000007;

ll pow_mod(ll x, ll n) {
ll s = 1;
for (; n; n >>= 1) {
if (n & 1) s = s * x % p;
x = x * x % p;
}
return s;
}

int n, m, a[maxn];

#define lc T[i].ch[0]
#define rc T[i].ch[1]
#define Lc T[j].ch[0]
#define Rc T[j].ch[1]
struct zhuxi {
int ch[2], v = 1, mul = 1;
} T[maxn * 300]; int top, rt[maxn];
inline void maintain(int i) { T[i].v = T[lc].v * T[rc].v % p; }

void update(int &i, int j, int l, int r, int L, int R, int v) {
if (l > R || r < L) return ;
i = ++top; T[i] = T[j];
if (L <= l && r <= R) {
T[i].v = 1ll * T[i].v * v % p;
T[i].mul = 1ll * T[i].mul * v % p;
return ;
} int m = l + r >> 1;
update(lc, Lc, l, m, L, R, v); update(rc, Rc, m + 1, r, L, R, v);
}

int query(int i, int l, int r, int k, ll mul) {
if (l == r) return T[i].v * mul % p;
int m = l + r >> 1;
if (k <= m) return query(lc, l, m, k, mul * T[i].mul % p);
else return query(rc, m + 1, r, k, mul * T[i].mul % p);
}

vector<pair<int, int>> S[maxm];

void ins(int i, int p, int s) {
update(rt[i], rt[i], 1, n, S[p].back().first + 1, i, pow_mod(p, s));
while (s > S[p].back().second) {
update(rt[i], rt[i], 1, n, S[p][S[p].size() - 2].first + 1,
S[p].back().first, pow_mod(p, s - S[p].back().second));
S[p].pop_back();
} S[p].push_back({ i, s });
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 0; i < maxm; ++i) S[i].push_back({ 0, INF });
for (int i = 1; i <= n; ++i) {
rt[i] = rt[i - 1];
for (int p = 2; p * p <= a[i]; ++p)
if (a[i] % p == 0) {
int s = 0;
while (a[i] % p == 0) ++s, a[i] /= p;
ins(i, p, s);
}
if (a[i] > 1) ins(i, a[i], 1);
}
cin >> m;
for (int i = 1, lans = 0; i <= m; ++i) {
int x, y; cin >> x >> y;
x = (x + lans) % n + 1; y = (y + lans) % n + 1;
if (x > y) swap(x, y);
cout << (lans = query(rt[y], 1, n, x, 1)) << "\n";
}
return 0;
}