洛谷 P4715 「英语」Z 语言

SOL:

  我们对字符串哈希即可。

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define sight(x) ('0'<=x&&x<='9')
using namespace std;
inline void read(int &x){
    static char c;
    for (c=getchar();!sight(c);c=getchar());
    for (x=0;sight(c);c=getchar())x=x*10+c-48;
}
void write(int x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);}
inline void writeln(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); }
inline void writel(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); }
#define ull unsigned long long
struct Ha{
    #define HaN 1000007
    #define HaM 1000007
    int head[HaN],net[HaM],fall[HaM],tag[HaN],tot,tim,
    c[HaM],g[HaM];
    ull h[HaM];
    void _new() {++tim; tot=0;}
    int push(int x) {
        return (tag[x]^tim)?(tag[x]=tim,head[x]=0):head[x];
    }
    int &get(ull _h) {
        static int hs; hs=(_h%HaN+HaN)%HaN;
        for (int i=push(hs);i;i=net[i]) 
         if (h[i]==_h) return g[i];
        h[++tot]=_h; g[tot]=0; net[tot]=head[hs]; head[hs]=tot;
        return g[tot];
    }
    int Get(ull _h) {
        static int hs; hs=(_h%HaN+HaN)%HaN;
        for (int i=push(hs);i;i=net[i]) 
         if (h[i]==_h) return g[i];
        h[++tot]=_h; g[tot]=0; net[tot]=head[hs]; head[hs]=tot;
        return g[tot];
    }
}f;
#define N 200007
ull hs[N],p[N],S;
int siz[N],w[N],c[N][2],fa[N],rt,v[N],m,n,q,a[N],b[N];
const ull pr=11117;
inline void up(int x){
    int l=c[x][0],r=c[x][1];
    siz[x]=siz[l]+siz[r]+1;
    hs[x]=hs[l]+p[siz[l]]*w[x]+p[siz[l]+1]*(hs[r]);
}
inline void rotate(int x,int &tar){
    int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
    if (y==tar) tar=x; else c[z][c[z][1]==y]=x;
    fa[c[x][r]]=y; fa[y]=x; fa[x]=z;
    c[y][l]=c[x][r]; c[x][r]=y; up(y); up(x);
}
inline void splay(int x,int &goal){
    while (x^goal) {
        int y=fa[x],z=fa[y];
        if (y^goal) {
            if (c[y][0]==x^c[z][0]==y) rotate(x,goal); else rotate(y,goal);
        } rotate(x,goal);
    }
}
inline void del(int x){
    splay(x,rt);
    if (!(c[x][0]*c[x][1])) {rt=c[x][0]+c[x][1];fa[rt]=0;c[x][0]=c[x][1]=0;return;}
    int su=c[x][1],pr=c[x][0];
    while (c[pr][1]) pr=c[pr][1];
    while (c[su][0]) su=c[su][0];
    splay(pr,rt); splay(su,c[rt][1]); c[su][0]=0; fa[x]=0; up(su); up(pr);
    c[x][0]=c[x][1]=0;
}
inline void insert(int &x,int id,int f){
    if (!x) {x=id;fa[x]=f; up(x); splay(x,rt);return;}
    if (v[id]<v[x]) insert(c[x][0],id,x);
    else insert(c[x][1],id,x);
}
void dfs(int x){
    if (c[x][0]) dfs(c[x][0]);
    printf("%d ",x);
    if (c[x][1]) dfs(c[x][1]);
}
signed main() {
    read(n); read(m); read(q); p[0]=1; 
    for (int i=1;i<=n;i++) read(v[i]),w[i]=i,p[i]=p[i-1]*pr;
    for (int i=1;i<=m;i++) read(b[i]),
    insert(rt,i,0),S+=p[i-1];
//    dfs(rt);cout<<hs[rt];puts("");
    for (int i=m;i<=n;i++) {
        f.get((hs[rt]-(i-m)*S))++; 
//        cout<<(hs[rt]-(i-m)*S)<<endl;
        if (i==n) break;
        del(i-m+1); 
        insert(rt,i+1,0);
//        dfs(rt);cout<<hs[rt];puts("");
    } 
    rt=0;memset(c,0,sizeof(c));memset(fa,0,sizeof(fa));
    for (int i=1;i<=m;++i) v[i]=b[i],insert(rt,i,0);
    int x,cc;
    while(q--){
        read(x),read(cc);del(x);
        v[x]=cc;insert(rt,x,0);
//         dfs(rt);cout<<hs[rt];puts("");
        writeln(f.Get(hs[rt]));
    } return 0;
}

猜你喜欢

转载自www.cnblogs.com/rrsb/p/9318198.html