【学习笔记】莫队算法&板子&几个模板题

参考

原理看参考吧…

关键就是要能离线,然后L和R指针变化时对答案的更新要O(1),最后的复杂度就是O(Nsqrt(N))

要改的就是udpate函数,其他都是板子

莫队板子:

#include<bits/stdc++.h>
#define MAXN 1000005
using namespace std;
typedef long long LL;

int N,M,B;
int a[MAXN], pos[MAXN];
int ANS[MAXN];

struct Req{
    int L,R,id;

    bool operator < (const Req& q1) const{
        if(pos[L]!=pos[q1.L]) return pos[L] < pos[q1.L];
        else return R < q1.R;
    }
} q[MAXN];

int ans = 0, cnt[MAXN];
void update(int id, int f){//就修改这里就行
    cnt[a[id]] += f;
    if(f==1 && cnt[a[id]]==1) ++ans;
    if(f==-1 && cnt[a[id]]==0) --ans;
}

int main(){
    scanf("%d", &N);
    B = sqrt(N);
    for(int i=1;i<=N;i++){
        scanf("%d", &a[i]);
        pos[i] = i/B;
    }
    scanf("%d", &M);
    for(int i=0;i<M;i++){
        scanf("%d%d", &q[i].L, &q[i].R);
        q[i].id = i;
    }   

    sort(q, q+M);
    for(int i=0,L=1,R=0;i<M;i++){
        while(R<q[i].R) update(++R, +1);
        while(R>q[i].R) update(R--, -1);
        while(L<q[i].L) update(L++, -1);
        while(L>q[i].L) update(--L, +1);

        ANS[q[i].id] = ans;
    }

    for(int i=0;i<M;i++){
        printf("%d\n", ANS[i]);
    }
    return 0;
}

洛谷P1494:模板题,直接莫队搞就完事,分母是len(len-1),分子是区间内∑cnt(cnt-1),然后这个区间和是可以O(1)维护的,因为只有加1减1的操作。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=50005;
int N,M,B;
int a[MAXN], pos[MAXN];
LL ANS[MAXN][2];

struct Req{
    int L,R,id;

    bool operator < (const Req& q1) const{
        if(pos[L]!=pos[q1.L]) return pos[L] < pos[q1.L];
        else return R < q1.R;
    }
} q[MAXN];

LL ans = 0, cnt[MAXN];
void update(int id, int f){
    ans-=cnt[a[id]]*(cnt[a[id]]-1);
	cnt[a[id]] += f;
    ans+=cnt[a[id]]*(cnt[a[id]]-1);
}
LL gcd(LL x,LL y)
{
	if(x%y==0) return y;
	else return gcd(y,x%y);
}
int main(){
    scanf("%d%d", &N, &M);
    B = sqrt(N);
    for(int i=1;i<=N;i++){
        scanf("%d", &a[i]);
        pos[i] = i/B;
    }
    for(int i=0;i<M;i++){
        scanf("%d%d", &q[i].L, &q[i].R);
        q[i].id = i;
    }   

    sort(q, q+M);
    for(int i=0,L=1,R=0;i<M;i++){
        while(R<q[i].R) update(++R, +1);
        while(R>q[i].R) update(R--, -1);
        while(L<q[i].L) update(L++, -1);
        while(L>q[i].L) update(--L, +1);
        if(q[i].L==q[i].R){
        	ANS[q[i].id][0]=0;
        	ANS[q[i].id][1]=1;
        	continue;
		}
		LL len=q[i].R-q[i].L+1; 
		LL d=gcd(ans,len*(len-1));
        ANS[q[i].id][0] = ans/d;
        ANS[q[i].id][1]=len*(len-1)/d;
    }
    for(int i=0;i<M;i++){ 
        printf("%d/%d\n", ANS[i][0],ANS[i][1]);
    }
    return 0;
}

洛谷P3709:题面不用看,直接求区间众数出现次数,update那里,用一个cnt数组来维护每个数出现的次数,用一个num数组来维护有多少个数出现了i次,然后如果是这个数的个数多了,就直接跟当前次数最大值判断大小,如果这个数的个数少了,就看ans是不是等于减之前的个数,如果是的话因为有可能有其他众数,所以再用num数组来判断下就好了。然后因为a[i]太大,所以离散化下。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=2e5+10;
int N,M,B;
int a[MAXN], pos[MAXN];
int ANS[MAXN];
int b[MAXN];
struct Req{
    int L,R,id;

    bool operator < (const Req& q1) const{
        if(pos[L]!=pos[q1.L]) return pos[L] < pos[q1.L];
        else return R < q1.R;
    }
} q[MAXN];

int ans = 0, cnt[MAXN];
int num[MAXN];
void update(int id, int f){
	int x=a[id];
	if(f==1){
		--num[cnt[x]];
		++cnt[x];
		++num[cnt[x]];
		if(cnt[x]>ans) ans=cnt[x];
	}
	else{
		--num[cnt[x]];
		if(ans==cnt[x]&&num[cnt[x]]==0) --ans;
		--cnt[x];
		++num[cnt[x]];
	}
}
int main(){
    scanf("%d%d", &N, &M);
    B = sqrt(N);
    for(int i=1;i<=N;i++){
        scanf("%d", &a[i]);
        b[i]=a[i];
        pos[i] = i/B;
    }
    sort(b+1,b+N+1);
    int tot=unique(b+1,b+N+1)-b-1;
    for(int i=1;i<=N;i++){
    	int tmp=lower_bound(b+1,b+tot+1,a[i])-b;
    	a[i]=tmp;
	}
    for(int i=0;i<M;i++){
        scanf("%d%d", &q[i].L, &q[i].R);
        q[i].id = i;
    }   
    sort(q, q+M);
    for(int i=0,L=1,R=0;i<M;i++){
        while(R<q[i].R) update(++R, +1);
        while(R>q[i].R) update(R--, -1);
        while(L<q[i].L) update(L++, -1);
        while(L>q[i].L) update(--L, +1);
        ANS[q[i].id]=ans;
    }
    for(int i=0;i<M;i++){ 
        printf("%d\n", -ANS[i]);
    }
    return 0;
}

洛谷P4462:对于区间[L,R],求有多少子序列满足异或和等于k。这里直接求前缀和,然后就变成了在[L-1,R]区间内有多少对数满足异或等于k。这个就可以直接莫队了。要注意的是左边界是L-1,同时cnt[0]要初始化为1,因为如果前缀和刚好和k相等的时候这也是一种情况,ans+=1

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN=2e5+10;
int N,M,B,K;
int a[MAXN], pos[MAXN];
int ANS[MAXN];
struct Req{
    int L,R,id;

    bool operator < (const Req& q1) const{
        if(pos[L]!=pos[q1.L]) return pos[L] < pos[q1.L];
        else return R < q1.R;
    }
} q[MAXN];

int ans = 0, cnt[MAXN];
void update(int id, int f){
	int x=a[id];
	if(f==1){
		ans+=cnt[K^x];
		cnt[x]++;
	}
	else{
		cnt[x]--;
		ans-=cnt[K^x];
	}
}
int main(){
    scanf("%d%d%d", &N, &M, &K);
    B = sqrt(N);
    for(int i=1;i<=N;i++){
        scanf("%d", &a[i]);
        a[i]^=a[i-1];
        pos[i] = i/B;
    }
    for(int i=0;i<M;i++){
        scanf("%d%d", &q[i].L, &q[i].R);
        q[i].id = i;
    }   
    sort(q, q+M);cnt[0]=1;
    for(int i=0,L=1,R=0;i<M;i++){
        while(R<q[i].R) update(++R, +1);
        while(R>q[i].R) update(R--, -1);
        while(L<q[i].L) update(L-1, -1),L++;
        while(L>q[i].L) L--,update(L-1, +1);
        ANS[q[i].id]=ans;
    }
    for(int i=0;i<M;i++){ 
        printf("%d\n", ANS[i]);
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值