前言
本来是想卡卡常,看快速幂 能否通过的,被卡成三秒 ,遂考虑奇怪优化。
然而发现可以证到单根号?题解区全是光速幂?快速幂比光速幂还要快?
更新:发现出现了一处不等号反向的问题,已修正。
简要解法
详细思路建议去考察其它题解,这里简单过一下。下面认为 同阶。
离线莫队,用链表维护桶的桶,这里由于性质特殊,维护起来比较容易。
查询时,利用链表遍历有效的位置,位置 的贡献为 ,其中 表示当前查询的区间长度, 是出现次数为 (针对原序列的数组)的数值的和。
由于桶的桶只有 个,所以暴力快速幂可以做到 ,没卡过去。
于是用光速幂优化到 。
比光速幂还快的快速幂?
注
基于本题数据测试。
实现
只需要考虑算 即可。我们知道,桶的桶只有根号个是因为:第一层桶的总和 ,由于 ,最多占据 个位置。下文忽略 项。
考虑利用这个总和的性质,设第二层桶有 个有效位置,为 ,则 。设差分数组为 ,则 。
原本的快速幂实现中,每次指数都从 开始,太浪费了。考虑倒着做,这样 就是升序的(本题不保证有逆元),此时每次快速幂复杂度为 。
这样,单次查询总复杂度为 ,其中 代表的是从 到 的快速幂。
证明
研究 ,条件是 。下面有一些是定义在实数上的。
不妨令 ,再令 ,则 。
,由多元基本不等式,对于一个固定的 ,有 ,取等条件为 。
则 。
。
则 ,令 。
令导函数为零:,则 ,可以取到,是极大值点。
则 。
于是,我们证明了上述方法的复杂度仍为 。
实现
从测试结果来看,上述方法常数并不大,可能原因:很多等号根本取不到,因为取值范围是整数而不是证明时的实数;数据并没有卡满这种做法。
快速幂洛谷最优解,光速幂洛谷次优解,快速幂 QOJ 最优解。
光速幂实现
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define lll __uint128_t
namespace fastIO{
#define gc() (rp1==rp2&&(rp2=(rp1=buf)+fread(buf,1,IO,stdin))==rp1?EOF:*rp1++)
#define pc(a) ((wrp==obuf+IO&&(fwrite(obuf,1,IO,stdout),wrp=obuf)),(*wrp++)=a)
const int IO=1<<18;
char buf[IO+1],obuf[IO+1],*wrp=obuf,*rp1,*rp2;
template<typename qwq>inline void read(qwq &a){
a=0;int c=gc();
while(!isdigit(c)) c=gc();
while(isdigit(c)) a=10*a+c-'0',c=gc();
}
template<typename qwq>inline void write(qwq x,char c=' '){
int sk[20],top=0;
do{
sk[++top]=x%10,x/=10;
}while(x);
while(top) pc(sk[top--]+'0');
pc(c);
}
#undef pc
#undef gc
};using namespace fastIO;
const int N=100005,B=512;
int n,m,up,mod,ret,pre[N],nxt[N];
int ans[N],cnt[N],A[N],Acnt[N];
ull val[N],F[B+1],G[B+1],sum;
struct FastMod{
ull b,m;
void init(ull b){this->b=b,m=(1ull<<63)/b;}
inline ull operator()(ull a){
ull q=(ull)((lll(m)*a)>>63),r=a-q*b;
return r>=b?r-b:r;
}
}M;
struct node{
int l,r,p,id;
inline bool operator <(const node &x)const{
if((l>>9)!=(x.l>>9)) return l<x.l;
return ((l>>9)&1)?r>x.r:r<x.r;
}
}Q[N];
inline void add(int p){
int x=Acnt[p],y=++Acnt[p];
cnt[x]--,cnt[y]++,val[x]-=p,val[y]+=p;//注意带权
if(cnt[x]==0&&cnt[y]==1){
pre[nxt[y]=nxt[x]]=y,nxt[pre[y]=pre[x]]=y;
}
else if(cnt[x]==0) nxt[pre[y]=pre[x]]=y;
else if(cnt[y]==1) pre[nxt[y]=nxt[x]]=y,nxt[x]=y,pre[y]=x;
}
inline void del(int p){
int y=Acnt[p],x=--Acnt[p];
cnt[x]++,cnt[y]--,val[x]+=p,val[y]-=p;
if(cnt[y]==0&&cnt[x]==1){
pre[nxt[x]=nxt[y]]=x,nxt[pre[x]=pre[y]]=x;
}
else if(cnt[y]==0) pre[nxt[x]=nxt[y]]=x;
else if(cnt[x]==1) nxt[pre[x]=pre[y]]=x,pre[y]=x,nxt[x]=y;
}
template<typename qwq>inline void inc(qwq &a,qwq b){a+=b,(a>=mod)&&(a-=mod);}
inline ull qpow(int x){return M(F[x&511]*G[x>>9]);}
inline int query(int len){
M.init(mod),ret=0,sum=0;
for(int i=1;i<=B;i++) F[i]=F[i-1],inc(F[i],F[i]);
for(int i=1;i<=up;i++) G[i]=M(F[B]*G[i-1]);
for(int p=nxt[0];p<=n;p=nxt[p]){
ret=M(ret+qpow(len-p)*val[p]),sum+=val[p];
}//val[p] 虽然大于 mod,但是不超过 5e9
ret=M(qpow(len)*sum+mod-ret);
return ret;
}
int main(){
read(n),read(m),up=n/B+1,F[0]=1,G[0]=1;
for(int i=1;i<=n;i++) read(A[i]);
for(int i=1;i<=m;i++){
read(Q[i].l),read(Q[i].r);
Q[i].id=i,read(Q[i].p);
}
sort(Q+1,Q+1+m),cnt[0]=n+1,nxt[0]=n+1,cnt[n+1]=1;
for(int i=1,l=1,r=0;i<=m;i++){
while(r<Q[i].r) add(A[++r]);
while(Q[i].l<l) add(A[--l]);
while(Q[i].r<r) del(A[r--]);
while(l<Q[i].l) del(A[l++]);
if((mod=Q[i].p)==1) continue;//答案一定为 0
ans[Q[i].id]=query(Q[i].r-Q[i].l+1);
}
for(int i=1;i<=m;i++) write(ans[i],'\n');
return fwrite(obuf,1,wrp-obuf,stdout),0;
}
/*
经典结论:桶的桶只有根号个
*/优化后的快速幂实现
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define lll __uint128_t
namespace fastIO{
#define gc() (rp1==rp2&&(rp2=(rp1=buf)+fread(buf,1,IO,stdin))==rp1?EOF:*rp1++)
#define pc(a) ((wrp==obuf+IO&&(fwrite(obuf,1,IO,stdout),wrp=obuf)),(*wrp++)=a)
const int IO=1<<18;
char buf[IO+1],obuf[IO+1],*wrp=obuf,*rp1,*rp2;
template<typename qwq>inline void read(qwq &a){
a=0;int c=gc();
while(!isdigit(c)) c=gc();
while(isdigit(c)) a=10*a+c-'0',c=gc();
}
template<typename qwq>inline void write(qwq x,char c=' '){
int sk[20],top=0;
do{
sk[++top]=x%10,x/=10;
}while(x);
while(top) pc(sk[top--]+'0');
pc(c);
}
#undef pc
#undef gc
};using namespace fastIO;
const int N=100005,B=512;
int n,m,up,mod,ret,pre[N],nxt[N];
int ans[N],cnt[N],A[N],Acnt[N];
ull val[N],sum,tmp;
struct FastMod{
ull b,m;
void init(ull b){this->b=b,m=(1ull<<63)/b;}
inline ull operator()(ull a){
ull q=(ull)((lll(m)*a)>>63),r=a-q*b;
return r>=b?r-b:r;
}
}M;
struct node{
int l,r,p,id;
inline bool operator <(const node &x)const{
if((l>>9)!=(x.l>>9)) return l<x.l;
return ((l>>9)&1)?r>x.r:r<x.r;
}
}Q[N];
inline void add(int p){
int x=Acnt[p],y=++Acnt[p];
cnt[x]--,cnt[y]++,val[x]-=p,val[y]+=p;//注意带权
if(cnt[x]==0&&cnt[y]==1){
pre[nxt[y]=nxt[x]]=y,nxt[pre[y]=pre[x]]=y;
}
else if(cnt[x]==0) nxt[pre[y]=pre[x]]=y;
else if(cnt[y]==1) pre[nxt[y]=nxt[x]]=y,nxt[x]=y,pre[y]=x;
}
inline void del(int p){
int y=Acnt[p],x=--Acnt[p];
cnt[x]++,cnt[y]--,val[x]+=p,val[y]-=p;
if(cnt[y]==0&&cnt[x]==1){
pre[nxt[x]=nxt[y]]=x,nxt[pre[x]=pre[y]]=x;
}
else if(cnt[y]==0) pre[nxt[x]=nxt[y]]=x;
else if(cnt[x]==1) nxt[pre[x]=pre[y]]=x,pre[y]=x,nxt[x]=y;
}
template<typename qwq>inline void inc(qwq &a,qwq b){a+=b,(a>=mod)&&(a-=mod);}
inline void qpow(int x){
for(ull a=2;x;a=M(a*a),x>>=1){
if(x&1) tmp=M(tmp*a);
}
}
inline int query(int len){
M.init(mod),ret=0,sum=0,tmp=1;int lst=len;
for(int p=pre[n+1];p>0;p=pre[p]){
qpow(lst-p),lst=p;
ret=M(ret+tmp*val[p]),sum+=val[p];
}//val[p] 虽然大于 mod,但是不超过 5e9
qpow(lst),ret=M(tmp*sum+mod-ret);
return ret;
}
int main(){
read(n),read(m),up=n/B+1;
for(int i=1;i<=n;i++) read(A[i]);
for(int i=1;i<=m;i++){
read(Q[i].l),read(Q[i].r);
Q[i].id=i,read(Q[i].p);
}
sort(Q+1,Q+1+m),cnt[0]=n+1,nxt[0]=n+1,cnt[n+1]=1;
for(int i=1,l=1,r=0;i<=m;i++){
while(r<Q[i].r) add(A[++r]);
while(Q[i].l<l) add(A[--l]);
while(Q[i].r<r) del(A[r--]);
while(l<Q[i].l) del(A[l++]);
if((mod=Q[i].p)==1) continue;//答案一定为 0
ans[Q[i].id]=query(Q[i].r-Q[i].l+1);
}
for(int i=1;i<=m;i++) write(ans[i],'\n');
return fwrite(obuf,1,wrp-obuf,stdout),0;
}
/*
经典结论:桶的桶只有根号个
*/