[Ynoi Easy Round 2015] 盼君勿忘 题解

[Ynoi Easy Round 2015] 盼君勿忘

题意为,对于每个询问求其中所有子序列内部去重后的和的总和模 \(p\)。

我们对一个数 \(x\) 考虑其贡献,当它在区间里出现了 \(k\) 次,它就可以给每个含它的子序列贡献一个 \(x\),对答案贡献了 \(2^l-2^{l-k}\) 个 \(x\),其中 \(l\) 为区间长度。

我们并不太能一一枚举每个数,考虑从出现次数的角度看,设出现次数为 \(c\) 的数之和为 \(s_c\),则贡献为 \(s_c\times (2^l-2^{l-c})\),而一个区间内的所有出现次数是 \(O(\sqrt n)\) 量级的,所以我们考虑用一个东西支持添加和删除次数,可以用双向链表解决,配合莫队可以做到单次 \(O(1)\) 移动指针。

还有一个问题,每次询问的模数不同,如果用快速幂的话复杂度就变成 \(O(n\sqrt n\log n)\) 了,所以我们考虑光速幂。光速幂是 \(O(\sqrt n)\) 预处理,\(O(1)\) 查询的单一底数幂求法,预处理出 \(2^0,2^1,\dots2^{\sqrt n}\) 和 \(2^{\sqrt n},2^{2\sqrt n},2^{3\sqrt n}\dots 2^{n}\),则我们回答 \(2^t\) 只要变为 \(2^{\frac{t}{\sqrt n}} \times 2^{t \bmod \sqrt n}\) 即可。

时间复杂度 \(O(n\sqrt n)\)。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define _int __int128
#define ull unsigned long long
#define pii pair<int,int>
#define fst first
#define scd second
#define pq priority_queue
#define mkp make_pair
#define popcount(x) __builtin_popcountll(x)
#define fo(i,l,r) for(int i=l;i<=r;i++)
#define rof(i,r,l) for(int i=r;i>=l;i--)
#define endl '\n'
inline int read(){
    int k=0,f=1,c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())k=k*10+(c^48);
    return k*f;
}
inline void write(LL x){
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
int n,m,t;
const int N = 1e5+10;
struct lis{
    int pre[N],nxt[N],tot;
    void ins(int x){
        pre[x]=tot;
        nxt[tot]=x;
        tot=x;
    }
    void erase(int x){
        if(x!=tot){
            nxt[pre[x]]=nxt[x];
            pre[nxt[x]]=pre[x];
        }else{
            nxt[pre[x]]=0;
            tot=pre[tot];
        }
        pre[x]=nxt[x]=0;
    }
}b;
namespace fstpow{
    LL pw1[N],pw2[N],mod;
    void init(LL x,int len){
        mod=x;
        pw1[0]=1;pw2[0]=1;
        fo(i,1,t){
            pw1[i]=(pw1[i-1]*2);
            if(pw1[i]>=mod)pw1[i]-=mod;
        }
        fo(i,1,len/t)pw2[i]=(pw2[i-1]*pw1[t])%mod;
    }
    LL qpow(LL x){
        return (pw2[x/t]*pw1[x%t])%mod;
    }
}
int a[N],cnt[N];
LL s[N],ans[N];
struct qy{
    int l,r,id;
    LL p;
}q[N];
bool cmp(qy a,qy b){
    if(a.l/t==b.l/t){
        if((a.l/t)&1)return a.r<b.r;
        else return a.r>b.r;
    }
    return a.l/t<b.l/t;
}
void upd(int x,int v){
    if(!(s[cnt[a[x]]]-=a[x]))b.erase(cnt[a[x]]);
    if(!s[cnt[a[x]]+=v])b.ins(cnt[a[x]]);
    s[cnt[a[x]]]+=a[x];
}
using namespace fstpow;
signed main(){
    n=read(),m=read();
    fo(i,1,n){
        a[i]=read();
    }
    t=max(1.0,n/sqrt(m));
    fo(i,1,m){
        q[i].l=read(),q[i].r=read(),q[i].p=read();
        q[i].id=i;  
    }
    sort(q+1,q+1+m,cmp);
    int ll=1,rr=0;
    fo(i,1,m){
        if(q[i].p==1){
            continue;
        }
        while(rr<q[i].r)upd(++rr,1);
        while(ll>q[i].l)upd(--ll,1);
        while(rr>q[i].r)upd(rr--,-1);
        while(ll<q[i].l)upd(ll++,-1);
        init(q[i].p,rr-ll+1);
        for(int j=b.nxt[0];j;j=b.nxt[j]){
            LL c=(qpow(rr-ll+1)-qpow(rr-ll+1-j)+mod)%mod;
            (ans[q[i].id]+=c*s[j]%mod)%=mod;
        }
    }
    fo(i,1,m)write(ans[i]),putchar(endl);
    return 0;
}
posted @ 2026-11-16 14:04  farenara_sayowell  阅读(2)  评论(0)    收藏  举报