[NOI2017]蚯蚓排队 hash

时间:2023-03-09 03:47:42
[NOI2017]蚯蚓排队 hash

题面:洛谷

题解:

  我们暴力维护当前所有队伍内的所有子串(长度k = 1 ~ 50)的出现次数。

  把每个子串都用一个hash值来表示,每次改变队伍形态都用双向链表维护,并暴力更新出现次数。

  现在考虑复杂度。

  如果只有连接,没有断开,那么复杂度为不同的子串个数:50n(注意只要O(n)预处理前缀和后缀hash就可以做到O(1)得到一个子串的hash值)

  如果有断开,那么最多就每断开一次就对应要再重连一次。所以复杂度最多增加2500 * 断开次数,而断开次数1e3....

  所以复杂度就是正确的了。

  此题略卡常。

  如果T了一两个点,,,就多交几次吧,,,说不定哪次就过了呢?

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 201000
#define ac 10501000
#define LL long long
#define us unsigned
#define maxn 49//每边最多49个,以为另外一边至少一个
#define base 19260817//2333//直接用10???
#define bu 16777215//49999991//100000081//cket 一个质数,,,
#define mod 998244353
#define h(f) (f & bu) int n, m, top;
int v[AC], last[AC], Next[AC];
us LL hs[AC], ls[ac], p[ac];
int Head[ac * ], Next1[ac], date[ac], tot, id;
int cnt[ac]; us LL power[ac];//存下每个id对应的hash值以及出现次数
int s[AC];
char ss[ac];
//int tot, id; #define Next Next1
//inline int h(int f){
//return (((f & bu) ^ (mod >> 5)) + 1);
//} struct node{ inline void add(int f, us LL x)
{//如果x这个表上没有k这个值,那就要新开id,否则直接用原来的id
// printf("%d %llu\n", f, x);
// printf("%llu\n", x);
for(R i = Head[f]; i; i = Next[i])
if(power[date[i]] == x) {++ cnt[date[i]]; return ;}
int tmp = ++ id;
date[++ tot] = tmp, Next[tot] = Head[f], Head[f] = tot;
power[tmp] = x, cnt[tmp] = ;
} inline void del(int f, us LL x)//找到这个值并删除一个
{
for(R i = Head[f]; i; i = Next[i])
if(power[date[i]] == x) {-- cnt[date[i]]; return ;}
} inline int find(int f, us LL x)
{
for(R i = Head[f]; i; i = Next[i])
if(power[date[i]] == x) return cnt[date[i]];
return ;
}
}T;
#undef Next inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} void pre()
{
n = read(), m = read(), p[] = ;
for(R i = ; i <= n; i ++) v[i] = read(), T.add(h(v[i]), v[i]);//先把单个的加进去
for(R i = ; i <= n; i ++) p[i] = p[i - ] * base;//自然溢出
} us LL cal(int l, int r){//返回[l, r]的hash值
return ls[r] - ls[l - ] * p[r - l + ];
} void get_hs(int mid, bool z)//获取所有长度<= 50的,跨mid的hs值
{
for(R i = ; i <= top; i ++) ls[i] = ls[i - ] * base + s[i];
for(R i = ; i <= mid; i ++)//枚举开头
{
int lim1 = mid - i + , lim2 = top - i + ;//长度要在[lim1, lim2]的范围内
for(R len = lim1; len <= lim2; len ++)//才能保证合法
{
int r = i + len - ;
us LL x = cal(i, r);//获取区间hash值
if(z) T.add(h(x), x);
else T.del(h(x), x);
}
}
} void link1()//每次合并的时候暴力加hash值,每次最多增加1250个
{
int x = read(), y = read(), cnt = , tmp = ;//把j接到i之后
Next[x] = y, last[y] = x, top = ;
for(R i = x; i && cnt < maxn; i = last[i]) ++ cnt, tmp = i;
for(R i = tmp; i != x; i = Next[i]) s[++ top] = v[i];
s[++ top] = v[x], cnt = top;
for(R i = y; i && top - cnt != maxn; i = Next[i]) s[++ top] = v[i];
get_hs(cnt, );
} void link()//每次合并的时候暴力加hash值,每次最多增加1250个
{
int x = read(), y = read(), cnt = , tmp = ;//把j接到i之后
Next[x] = y, last[y] = x, top = ;
for(R i = x; i && cnt < maxn; i = last[i]) ++ cnt, tmp = i;
for(R i = tmp; i != x; i = Next[i]) s[++ top] = v[i];
s[++ top] = v[x], cnt = top;
for(R i = y; i && top - cnt != maxn; i = Next[i]) s[++ top] = v[i];
get_hs(cnt, );
} void cut()//每次合并的时候暴力减hash值,每次最多减少1250个,但总体很小
{
int x = read(), y = Next[x], cnt = , tmp = ;//把i和它之后的一个蚯蚓断开
last[y] = Next[x] = top = ;
for(R i = x; i && cnt < maxn; i = last[i]) ++ cnt, tmp = i;
for(R i = tmp; i != x; i = Next[i]) s[++ top] = v[i];
s[++ top] = v[x], cnt = top;
for(R i = y; i && top - cnt != maxn; i = Next[i]) s[++ top] = v[i];
get_hs(cnt, );
} void find()//处理hash值的时候暴力O(n)处理前缀hash值,然后查询的时候也O(n)遍历查询
{//因为s长度之和最多1e7....
//cin >> ss + 1, top = strlen(ss + 1);
scanf("%s", ss + ), top = strlen(ss + );
int k = read(), b = top - k + ;
for(R i = ; i <= top; i ++)
ls[i] = ls[i - ] * base + ss[i] - '';
LL ans = ;
for(R i = ; i <= b; i ++)
{
us LL x = cal(i, i + k - );
ans *= T.find(h(x), x);
//printf("!!%d ", T.find(x & bu, x));
if(ans > mod) ans %= mod;
}
//printf("\n");
printf("%lld\n", ans);
} void work()
{
for(R i = ; i <= m; i ++)
{
int opt = read();
if(opt == ) link();
else if(opt == ) cut();
else find();
}
} int main()
{
// freopen("in.in", "r", stdin);
pre();
work();
//fclose(stdin);
return ;
}