JZOJ 5555 Password

打表发现对于 \(i \ge 2\)\(a_i\)\(a_{i+2}\) 两个序列完全相同。
所以只要维护前三行。

第二行就是前 \(i\) 个数中 \(a_{1,i}\) 出现多少次。
分块解决,\(f_{i,j}\) 表示前 \(i\) 个块中 \(j\) 出现多少次。

第三行就是前 \(i\) 个数中有多少种数出现次数不小于前 \(i\) 个数中 \(a_{1,i}\) 的出现次数。
设一个 \(g_{i,j}\) 表示前 \(i\) 个块中出现次数超过 \(j\) 的有多少种数。

修改的话,注意到 \(f\) 改变一位对 \(g\) 的影响只有一位,于是可以 \(O(\frac nT)\) 解决,其中 \(T\) 为块大小。
然后就做完了。
(话说我让 \(T = 500\) 就变成最优解了?)

代码:

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
#include <cstdio> 
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1e5;
const int V = 1e4;
const int BLK = 500;
const int CNT = 200;
int n,m,a[N + 5];
int block,pos[N + 5];
int cnt1[CNT + 5][V + 5],cnt2[CNT + 5][N + 5];
void update(int x,int y)
{
for(register int i = pos[x];i <= pos[n];++i)
--cnt2[i][cnt1[i][a[x]]--],
++cnt2[i][++cnt1[i][y]];
a[x] = y;
}
int query1(int x,int y)
{
int ret = cnt1[pos[x] - 1][y];
for(register int i = (pos[x] - 1) * block + 1;i <= x;++i)
ret += a[i] == y;
return ret;
}
int query2(int x,int y)
{
int ret = cnt2[pos[x] - 1][y];
for(register int i = (pos[x] - 1) * block + 1;i <= x;++i)
if(cnt1[pos[x] - 1][a[i]]++ == y - 1)
++ret;
for(register int i = (pos[x] - 1) * block + 1;i <= x;++i)
--cnt1[pos[x] - 1][a[i]];
return ret;
}
int main()
{
freopen("password.in","r",stdin),freopen("password.out","w",stdout);
scanf("%d",&n),block = BLK;
for(register int i = 1;i <= n;++i)
scanf("%d",a + i),pos[i] = (i - 1) / block + 1,++cnt1[pos[i]][a[i]];
for(register int i = 1;i <= pos[n];++i)
for(register int j = 1;j <= V;++j)
++cnt2[i][cnt1[i][j] += cnt1[i - 1][j]];
for(register int i = 1;i <= pos[n];++i)
for(register int j = n;j;--j)
cnt2[i][j - 1] += cnt2[i][j];
scanf("%d",&m);
for(int op,x,y;m;--m)
{
scanf("%d%d%d",&op,&x,&y);
if(op == 1)
update(y,x);
else if(x == 1)
printf("%d\n",a[y]);
else if(x & 1)
printf("%d\n",query2(y,query1(y,a[y])));
else
printf("%d\n",query1(y,a[y]));
}
}