LUOGU P4119 Ynoi2018 未来日记
题意
有一个长度为
1 l r x y
,把区间内所有 变成 ; 2 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
using namespace std;
const int maxn = 100000, maxvs = 316;
const int maxs = 360, maxc = 278;
int cnt;
int a[maxn + 5];
int lp[maxc + 5], rp[maxvs + 5];
int bl[maxn + 5];
int vlp[maxvs + 5];
int vbl[maxn + 5];
int sum[maxc + 5][maxn + 5];
int blsum[maxc + 5][maxvs + 5];
int rep[maxc + 5][maxn + 5];
int col[maxc + 5][maxs + 5];
int id[maxn + 5];
int ccnt[maxc + 5];
void buildUnion(int bid) {
for (int i = 1; i <= ccnt[bid]; i++)
rep[bid][col[bid][i]] = 0;
ccnt[bid] = 0;
for (int i = lp[bid]; i <= rp[bid]; i++) {
if (rep[bid][a[i]] == 0) {
rep[bid][a[i]] = ++ccnt[bid];
col[bid][ccnt[bid]] = a[i];
}
id[i] = rep[bid][a[i]];
}
}
void reset(int bid) {
for (int i = lp[bid]; i <= rp[bid]; i++)
a[i] = col[bid][id[i]];
}
void build(int n) {
int siz = min(n, int(sqrt(1.3 * n)));
cnt = ceil(1. * n / siz);
for (int i = 1; i <= n; i++)
bl[i] = (i - 1) / siz + 1;
for (int i = 1; i <= cnt; i++) {
lp[i] = (i - 1) * siz + 1;
rp[i] = min(n, i * siz);
}
for (int i = 1; i <= maxn; i++)
vbl[i] = (i - 1) / maxvs + 1;
for (int i = 1; i <= maxvs + 1; i++)
vlp[i] = (i - 1) * maxvs + 1;
for (int i = 1; i <= cnt; i++) {
for (int j = 1; j <= maxn; j++)
sum[i][j] = sum[i - 1][j];
for (int j = 1; j <= maxvs + 1; j++)
blsum[i][j] = blsum[i - 1][j];
for (int j = lp[i]; j <= rp[i]; j++) {
sum[i][a[j]]++;
blsum[i][vbl[a[j]]]++;
}
buildUnion(i);
}
}
int tmp[maxn + 5], bltmp[maxvs + 5];
void add(int l, int r, int delta) {
for (int i = l; i <= r; i++) {
tmp[a[i]] += delta;
bltmp[vbl[a[i]]] += delta;
}
}
int query(int l, int r, int k) {
int lb = bl[l], rb = bl[r];
int res;
if (lb == rb) {
reset(lb);
copy(a + l, a + r + 1, tmp + l);
nth_element(tmp + l, tmp + l + k - 1, tmp + r + 1);
res = tmp[l + k - 1];
fill(tmp + l, tmp + r + 1, 0);
} else {
reset(lb);
reset(rb);
add(l, rp[lb], 1);
add(lp[rb], r, 1);
int i = 1, delta;
for (; k - (delta = bltmp[i] + blsum[rb - 1][i] - blsum[lb][i]) > 0;
i++)
k -= delta;
int j = vlp[i];
for (; k - (delta = tmp[j] + sum[rb - 1][j] - sum[lb][j]) > 0; j++)
k -= delta;
res = j;
add(l, rp[lb], -1);
add(lp[rb], r, -1);
}
return res;
}
void change(int l, int r, int x, int y, int bid) {
int chcnt = 0;
for (int i = l; i <= r; i++) {
if (a[i] == x) {
a[i] = y;
chcnt++;
}
}
sum[bid][x] -= chcnt;
sum[bid][y] += chcnt;
blsum[bid][vbl[x]] -= chcnt;
blsum[bid][vbl[y]] += chcnt;
}
void changeBlock(int l, int r, int x, int y, int bid) {
reset(bid);
change(l, r, x, y, bid);
buildUnion(bid);
}
void modify(int l, int r, int x, int y) {
int lb = bl[l], rb = bl[r];
if (x == y || sum[rb][x] - sum[lb - 1][x] == 0)
return;
for (int i = cnt; i >= lb; i--) {
sum[i][x] -= sum[i - 1][x];
sum[i][y] -= sum[i - 1][y];
blsum[i][vbl[x]] -= blsum[i - 1][vbl[x]];
blsum[i][vbl[y]] -= blsum[i - 1][vbl[y]];
}
if (lb == rb)
changeBlock(l, r, x, y, lb);
else {
changeBlock(l, rp[lb], x, y, lb);
changeBlock(lp[rb], r, x, y, rb);
for (int i = lb + 1; i < rb; i++) {
if (sum[i][x] == 0)
continue;
if (sum[i][y] == 0) {
col[i][rep[i][x]] = y;
swap(rep[i][y], rep[i][x]);
blsum[i][vbl[y]] += sum[i][x];
blsum[i][vbl[x]] -= sum[i][x];
sum[i][y] = sum[i][x];
sum[i][x] = 0;
} else
changeBlock(lp[i], rp[i], x, y, i);
}
}
for (int i = lb; i <= cnt; i++) {
sum[i][x] += sum[i - 1][x];
sum[i][y] += sum[i - 1][y];
blsum[i][vbl[x]] += blsum[i - 1][vbl[x]];
blsum[i][vbl[y]] += blsum[i - 1][vbl[y]];
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
build(n);
for (int i = 1; i <= m; i++) {
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
if (op == 1) {
int x, y;
scanf("%d%d", &x, &y);
modify(l, r, x, y);
} else {
int k;
scanf("%d", &k);
printf("%d\n", query(l, r, k));
}
}
}
LUOGU P4119 Ynoi2018 未来日记