求助qwq双哈希wa2 调一年了
查看原帖
求助qwq双哈希wa2 调一年了
1133079
Maxduan楼主2024/8/15 22:01
#include <bits/stdc++.h>
#define maxn 500005
using namespace std;
int a[maxn],tr1[4*maxn],tr2[4*maxn];
int pre1[maxn],pre2[maxn];
const int p1=982469809,p2=904651609;
const int sd1=1145141,sd2=1333331;
pair<int,int> h[maxn];
vector<int>g[maxn];
map<pair<int,int>,int>mp;
int dep[maxn];

void pushup(int u,int len)
{
    int len1=len-len/2;
    tr1[u]=1LL*tr1[u<<1]*pre1[len1]%p1+tr1[u<<1|1];
    tr2[u]=1LL*tr2[u<<1]*pre2[len1]%p2+tr2[u<<1|1];
    if(tr1[u]>=p1)tr1[u]-=p1;
    if(tr2[u]>=p2)tr2[u]-=p2;
}

void init(int u,int l,int r)
{
    if(l==r)tr1[u]=tr2[u]=a[l];
    else
    {
        int mid=l+r>>1;
        init(u<<1,l,mid);
        init(u<<1|1,mid+1,r);
        pushup(u,r-l+1);
    }
}

pair<int,int> qry(int u,int l,int r,int l2,int r2)
{
    if(l2<=l&&r2>=r){
        return {1LL*tr1[u]*pre1[r2-r]%p1,1LL*tr2[u]*pre2[r2-r]%p2};
    }
    int mid=l+r>>1;
    pair<int,int> ans={0,0};
    if(mid>=l2)ans=qry(u<<1,l,mid,l2,r2);
    if(mid<r2){
        auto tmp=qry(u<<1|1,mid+1,r,l2,r2);
        ans.first+=tmp.first;
        ans.second+=tmp.second;
        if(ans.first>=p1)ans.first-=p1;
        if(ans.second>=p2)ans.second-=p2;
    }
    return ans;
}

void upd(int u, int l,int r,int l2,int r2,int k)
{
    if(l2>r||r2<l)return;
    if(l2<=l&&r2>=r)
    {
        tr1[u]=tr2[u]=k;
        return;
    }
    int mid=l+r>>1;
    if(mid>=l2)upd(u<<1,l,mid,l2,r2,k);
    if(mid<r2)upd(u<<1|1,mid+1,r,l2,r2,k);
    pushup(u,r-l+1);
}

void dfs(int u){
    sort(g[u].begin(),g[u].end());
    for(int i=1;i<=g[u].size();i++){
        int v=g[u][i-1];
        dep[v]=dep[u]+1;
        h[v]={1LL*h[u].first*sd1%p1+i,1LL*h[u].second*sd2%p2+i};
        if(h[v].first>=p1)h[v].first-=p1;
        if(h[v].second>=p2)h[v].second-=p2;
        mp[h[v]]=v;
        dfs(v);
    }
}

void solve()
{
    int n,m,q;cin>>n>>m>>q;
    pre1[0]=1;
    pre2[0]=1;
    for(int i=1;i<=500000;i++)pre1[i]=1LL*pre1[i-1]*sd1%p1;
    for(int i=1;i<=500000;i++)pre2[i]=1LL*pre2[i-1]*sd2%p2;
    for(int i=1;i<=n;i++){
        int x;cin>>x;
        g[x].push_back(i);
    }
    for(int i=1;i<=m;i++)cin>>a[i];
    init(1,1,m);
    dfs(1);
    while(q--){
        int op;cin>>op;
        if(op==1){
            int x,l,r;cin>>x>>l>>r;
            int ans=x;
            int lo=l,hi=r;
            while(lo<=hi){
                int mid=lo+hi>>1;
                pair<int,int> tmp={1LL*h[x].first*pre1[mid-l+1]%p1,1LL*h[x].second*pre2[mid-l+1]%p2};
                auto tp=qry(1,1,m,l,mid);
                tmp.first+=tp.first;
                tmp.second+=tp.second;
                if(tmp.first>=p1)tmp.first-=p1;
                if(tmp.second>=p2)tmp.second-=p2;
                if(mp.count(tmp)){
                    ans=mp[tmp];
                    lo=mid+1;
                }
                else hi=mid-1;
            }
            cout<<ans<<'\n';
        }
        else{
            int x,y;cin>>x>>y;
            upd(1,1,m,x,x,y);
        }
    }

}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;//cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}
佬们浇浇qwq
2024/8/15 22:01
加载中...