CF455D.Serega and Fun

给你一个序列,在线地支持两个操作:

  • 将一个区间循环移位。

  • 查询一个区间中某个数出现的次数。

两个方法

  • 分块 维护块内元素出现次数,修改和更新用双向队列都很显然。$O((n+m)\sqrt{n})=1700ms$
  • 平衡树,第一个操作很好解决,对于第二个操作,首先我们只能维护$t_i$这个数字平衡树出现的相对位置,而不能维护绝对位置。但是我们可以通过一开始构造原序列的平衡树的映射,就可以知道某个值在原序列第几个位置,这里需要$O(\log n)$。我想知道$[1,r]$之间$i$出现了几次,平衡树上通过这个查询二分即可。总复杂度$O(n+m\log^2 n)=300ms$
平衡树
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

#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
const int N = 4e5 + 10;
const int mod = 1e9 + 7;

int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
struct FHQ
{
int ls[N], rs[N];
int siz[N], key[N], val[N];
int fa[N];
int cor[N];
// ll sum[N];
int tcnt;
int root;
int Newnode(int x)
{
siz[++tcnt] = 1;
val[tcnt] = x;
key[tcnt] = rnd(); //精髓所在

fa[tcnt] = tcnt;
ls[tcnt] = rs[tcnt] = 0;
return tcnt;
}
void pushup(int pos)
{
siz[pos] = siz[ls[pos]] + siz[rs[pos]] + 1;
// sum[pos] = sum[ls[pos]] + sum[rs[pos]] + val[pos];
ls[pos] ? (fa[ls[pos]] = pos) : 0;
rs[pos] ? (fa[rs[pos]] = pos) : 0;
}

void split(int pos, int v, int &ll, int &rr)
{
if (!pos)
{
ll = 0, rr = 0;
return;
}

if (siz[ls[pos]] + 1 <= v)
{
ll = pos;
split(rs[pos], v - siz[ls[pos]] - 1, rs[pos], rr);
}
else
{
rr = pos;
split(ls[pos], v, ll, ls[pos]);
}
fa[ll] = ll, fa[rr] = rr; //保证头节点
pushup(pos);
}
int merge(int l, int r)
{
if (!l || !r)
return l + r;

if (key[l] < key[r])
{
rs[l] = merge(rs[l], r);
pushup(l);
return l;
}
else
{

ls[r] = merge(l, ls[r]);
pushup(r);
return r;
}
}
// int find(int x)
// {
// return fa[x] == x ? x : find(fa[x]);
// }
int insert(int &rt, int x)
{
int pos = Newnode(x);
rt = merge(rt, pos);
return pos;
}
int kth(int x)
{
int res = siz[ls[x]] + 1;
while (fa[x] != x)
{
if (rs[fa[x]] == x)
{
res += siz[ls[fa[x]]] + 1;
}
x = fa[x];
}
return res;
}

} t;

int rt[N];

int calc(int k, int r)
{
int o = rt[k];
int res = 0;
while (o)
{
if (t.kth(t.cor[o]) <= r)
{
res += t.siz[t.ls[o]] + 1;
o = t.rs[o];
}
else
o = t.ls[o];
}
return res;
}
int query(int l, int r, int k)
{
return calc(k, r) - calc(k, l - 1);
}

void update(int l, int r)
{
int a, b, c, d;
t.split(rt[0], l - 1, a, b);
t.split(b, r - l + 1, b, d);
t.split(b, r - l, b, c);

int k = t.val[c];
rt[0] = t.merge(a, t.merge(c, t.merge(b, d)));
int ls = calc(k, l - 1);
int rs = calc(k, r);
//cout << k << " " << ls << " " << rs << endl;

t.split(rt[k], ls, a, b);
t.split(b, rs - ls, b, d);
t.split(b, rs - ls - 1, b, c);

rt[k] = t.merge(a, t.merge(c, t.merge(b, d)));
}

int a[N];
int main()
{
//freopen("1", "r", stdin);
int n = read();
for (int i = 1; i <= n; i++)
{
a[i] = read();
int x = t.insert(rt[0], a[i]);
int y = t.insert(rt[a[i]], a[i]);
t.cor[x] = y;
t.cor[y] = x;
}

int lst = 0;
int q = read();
while (q--)
{
int op = read(), l = read(), r = read();

if (op == 1)
{
l = (l + lst - 1) % n + 1, r = (r + lst - 1) % n + 1;
//cout << op << " " << l << " " << r << " " << endl;
if (l > r)
swap(l, r);
if (l == r)
continue;
update(l, r);
}
else
{
int k = read();
l = (l + lst - 1) % n + 1, r = (r + lst - 1) % n + 1, k = (k + lst - 1) % n + 1;
// cout << op << " " << l << " " << r << " "
// << " " << k << endl;
if (l > r)
swap(l, r);

printf("%d\n", lst = query(l, r, 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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, int>
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const int M = 400;

int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}

struct Bo
{
int block, tot, bl[N], br[N], fa[N];
int cnt[M][N];
int a[N];
deque<int> q[M];
ll tag[N];

void replace(int x, int y, int w)
{
cnt[x][y] += w;
}
void build(int n)
{
block = sqrt(n);
tot = n / block + (n % block != 0);

for (int i = 1; i <= tot; i++)
bl[i] = (i - 1) * block + 1, br[i] = i * block;

br[tot] = n;

for (int i = 1; i <= n; i++)
fa[i] = (i - 1) / (block) + 1;
for (int i = 1; i <= tot; i++)
{
for (int j = bl[i]; j <= br[i]; j++)
{
q[i].push_back(a[j]);
replace(i, a[j], 1);
}
}
}
void work(int lx, int rx, int l, int r)
{

stack<int> le, ri;
for (int i = 1; i <= l - bl[lx]; i++)
{
le.push(q[lx].front());
q[lx].pop_front();
}

for (int i = 1; i <= br[rx] - r; i++)
{
ri.push(q[rx].back());
q[rx].pop_back();
}
int u = q[rx].back();
replace(rx, u, -1);
q[rx].pop_back();
replace(lx, u, 1);
q[lx].push_front(u);
while (!le.empty())
{
q[lx].push_front(le.top());
le.pop();
}
while (!ri.empty())
{
q[rx].push_back(ri.top());
ri.pop();
}
}
int calc(int lx, int rx, int l, int r, int k)
{

stack<int> le, ri;
for (int i = 1; i <= l - bl[lx]; i++)
{
le.push(q[lx].front());
q[lx].pop_front();
}

for (int i = 1; i <= br[rx] - r; i++)
{
ri.push(q[rx].back());
q[rx].pop_back();
}
int res = 0;
for (int x : q[lx])
if (x == k)
res++;
if (lx != rx)
{
for (int x : q[rx])
if (x == k)
res++;
}

while (!le.empty())
{
q[lx].push_front(le.top());
le.pop();
}
while (!ri.empty())
{
q[rx].push_back(ri.top());
ri.pop();
}
return res;
}

void update(int l, int r)
{
if (fa[l] == fa[r])
{
work(fa[l], fa[l], l, r);
return;
}
int lx = fa[l], rx = fa[r];
for (int i = lx; i < rx; i++)
{
int x = q[i].back();
q[i].pop_back();
replace(i, x, -1);
q[i + 1].push_front(x);
replace(i + 1, x, 1);
}
work(lx, rx, l, r);
}
int query(int l, int r, int k)
{
if (fa[l] == fa[r])
{
return calc(fa[l], fa[l], l, r, k);
}
int lx = fa[l], rx = fa[r];
int res = 0;
for (int i = lx + 1; i < rx; i++)
{
res += cnt[i][k];
}
res += calc(lx, rx, l, r, k);
return res;
}
} t;

int main()
{
// freopen("1", "r", stdin);
int n = read();
for (int i = 1; i <= n; i++)
t.a[i] = read();
t.build(n);
int q = read();
int lst = 0;
while (q--)
{
int op = read(), l = read(), r = read();

if (op == 1)
{
l = (l + lst - 1) % n + 1, r = (r + lst - 1) % n + 1;
//cout << op << " " << l << " " << r << " " << endl;
if (l > r)
swap(l, r);
if (l == r)
continue;
t.update(l, r);
}
else
{
int k = read();
l = (l + lst - 1) % n + 1, r = (r + lst - 1) % n + 1, k = (k + lst - 1) % n + 1;
// cout << op << " " << l << " " << r << " "
// << " " << k << endl;
if (l > r)
swap(l, r);

printf("%d\n", lst = t.query(l, r, k));
}
}
}