基础莫队——对询问分块

询问若干个区间内的情况:将这些区间重新排序,尽量让前一个区间跳到后一个区间时变化较小。

如何排序?
先把所有询问[l,r]读入,把这些询问按照左端点递增排序,然后分成 N \sqrt{N} N 块。每块内部再按照右端点排序。

bool cmp(section x,section y)
{
	if(x.pos==y.pos) return x.r<y.r;
	else return x.pos<y.pos;
}
	int t=sqrt(n);
	for(int i=1;i<=n;i++)
		a[i]=read();
	for(int i=1;i<=m;i++)
	{
		sec[i].l=read();
		sec[i].r=read();
		sec[i].pos=(sec[i].l-1)/t+1;
		sec[i].id=i;
	}
	sort(sec+1,sec+m+1,cmp);

这样一来,相邻两个询问的左端点变化在 N \sqrt{N} N 以内,右端点变化是单调的。以上一次询问[L,R]为基础,,求[L’,R’],只需要一步步转移,花费 O ( N ) O(\sqrt{N}) O(N )处理左端点,花费 O ( N ) O(N) O(N)处理右端点,时间复杂度 O ( N N ) O(N\sqrt{N}) O(NN )。区间已经被重新排序过,所以要先把答案储存下来,在区间原位置编号处输出。

	int pl=1,pr=0;
	long long temp=0;
	for(int i=1;i<=m;i++)
	{
		while(pl>sec[i].l) pl--,...;
		while(pr<sec[i].r) pr++,...;
		while(pl<sec[i].l) ...,pl++;
		while(pr>sec[i].r) ...,pr--;
		ans[sec[i].id]=temp;
	}

莫队题的特点:

  1. 不强制在线
  2. O ( n 3 / 2 ) O(n^{3/2}) O(n3/2)可过

P2709 小B的询问

x 2 − ( x − 1 ) 2 = 2 ∗ x − 1 x^{2} -(x-1)^{2}=2*x-1 x2(x1)2=2x1,边移动边维护。

#include<bits/stdc++.h>
using namespace std;
int n,m,k,a[50010],cnt[50010];
long long ans[50010];
struct section
{
	int l,r,pos,id;
}sec[50010];

bool cmp(section x,section y)
{
	if(x.pos==y.pos) return x.r<y.r;
	else return x.pos<y.pos;
}

int main()
{
	cin>>n>>m>>k;
	int t=sqrt(n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&sec[i].l,&sec[i].r);
		sec[i].pos=(sec[i].l-1)/t+1;
		sec[i].id=i;
	}
	sort(sec+1,sec+m+1,cmp);
	int pl=1,pr=0;
	long long temp=0;
	for(int i=1;i<=m;i++)
	{
		while(pl>sec[i].l) pl--,cnt[a[pl]]++,temp+=2*cnt[a[pl]]-1;
		while(pr<sec[i].r) pr++,cnt[a[pr]]++,temp+=2*cnt[a[pr]]-1;
		while(pl<sec[i].l) cnt[a[pl]]--,temp-=2*cnt[a[pl]]+1,pl++;
		while(pr>sec[i].r) cnt[a[pr]]--,temp-=2*cnt[a[pr]]+1,pr--;
		ans[sec[i].id]=temp;
	}
	for(int i=1;i<=m;i++)
		printf("%lld\n",ans[i]);
	return 0;
}

P1494 [国家集训队] 小 Z 的袜子

加了一点组合数,每个区间长度是len,分母就是 C l e n 2 = l e n ∗ ( l e n − 1 ) / 2 C_{len}^{2}=len*(len-1)/2 Clen2=len(len1)/2,每种颜色出现的次数分别是 c n t 1 , c n t 2 , . . . c n t n cnt_1,cnt_2,...cnt_n cnt1,cnt2,...cntn,分子就是 C c n t 1 2 + C c n t 2 2 + . . . C c n t n 2 C_{cnt_1}^{2}+C_{cnt_2}^{2}+...C_{cnt_n}^{2} Ccnt12+Ccnt22+...Ccntn2,分子分母约分后记录下来。

#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
int n,m;
int a[N];
long long ansx[N],ansy[N],cnt[N];

struct section
{
	int l,r,id,pos;
}sec[N];

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

bool cmp(section x,section y)
{
	if(x.pos==y.pos) return x.r<y.r;
	else return x.pos<y.pos;
}

int main()
{
	cin>>n>>m;
	int t=sqrt(n);
	for(int i=1;i<=n;i++)
		a[i]=read();
	for(int i=1;i<=m;i++)
	{
		sec[i].l=read();
		sec[i].r=read();
		sec[i].pos=(sec[i].l-1)/t+1;
		sec[i].id=i;
	}
	sort(sec+1,sec+m+1,cmp);
	int pl=1,pr=0;
	long long temp=0;
	for(int i=1;i<=m;i++)
	{
		while(pl>sec[i].l)
		{
			pl--;
			temp-=(cnt[a[pl]]*(cnt[a[pl]]-1))/2;
			cnt[a[pl]]++;
			temp+=(cnt[a[pl]]*(cnt[a[pl]]-1))/2;
		}
		while(pr<sec[i].r)
		{
			pr++;
			temp-=(cnt[a[pr]]*(cnt[a[pr]]-1))/2;
			cnt[a[pr]]++;
			temp+=(cnt[a[pr]]*(cnt[a[pr]]-1))/2;
		}
		while(pl<sec[i].l)
		{
			temp-=(cnt[a[pl]]*(cnt[a[pl]]-1))/2;
			cnt[a[pl]]--;
			temp+=(cnt[a[pl]]*(cnt[a[pl]]-1))/2;
			pl++;
		}
		while(pr>sec[i].r)
		{
			temp-=(cnt[a[pr]]*(cnt[a[pr]]-1))/2;
			cnt[a[pr]]--;
			temp+=(cnt[a[pr]]*(cnt[a[pr]]-1))/2;
			pr--;
		}
		if(sec[i].l==sec[i].r||temp<1)
		{
			ansx[sec[i].id]=0,ansy[sec[i].id]=1;
			continue;
		}
		ansx[sec[i].id]=temp;
		long long len=sec[i].r-sec[i].l+1;
		ansy[sec[i].id]=len*(len-1)/2;
		long long g=__gcd(ansx[sec[i].id],ansy[sec[i].id]);
		ansx[sec[i].id]/=g;
		ansy[sec[i].id]/=g;
	}
	for(int i=1;i<=m;i++)
		printf("%lld/%lld\n",ansx[i],ansy[i]);
    return 0;
}

P3901 数列找不同

开桶记录每个数据的个数,
删除:从1->0即少了一个种类;
添加:从0->1即多了一个种类。

#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,m,a[N],cnt[N];
long long ans[N];
long long temp=0;

struct section
{
	int l,r,pos,id;
}sec[N];

bool cmp(section x,section y)
{
	if(x.pos==y.pos) return x.r<y.r;
	else return x.pos<y.pos;
}

void add(int pos)
{
	if((++cnt[a[pos]]) == 1) ++temp;
}

void del(int pos)
{
	if((--cnt[a[pos]]) == 0) --temp;
}
int main()
{
	cin>>n>>m;
	int t=sqrt(n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&sec[i].l,&sec[i].r);
		sec[i].pos=(sec[i].l-1)/t+1;
		sec[i].id=i;
	}
	sort(sec+1,sec+m+1,cmp);
	int pl=1,pr=0;
	for(int i=1;i<=m;i++)
	{
		while(pl>sec[i].l) pl--,add(pl);
		while(pr<sec[i].r) pr++,add(pr);
		while(pl<sec[i].l) del(pl),pl++;
		while(pr>sec[i].r) del(pr),pr--;
		//cout<<"sec[i].l:"<<sec[i].l<<" sec[i].r"<<sec[i].r<<' '<<temp<<endl;
		if(temp==sec[i].r-sec[i].l+1)
			ans[sec[i].id]=1;
		else ans[sec[i].id]=0;
	}
	for(int i=1;i<=m;i++)
	{
		if(ans[i]==1) cout<<"Yes\n";
		else cout<<"No\n";
	}
	return 0;
}

还看到一种 O ( n ) O(n) O(n)的做法:

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5,LogN=20;
int st[MAXN],last[MAXN];
int n,m;
int main()
{
	cin>>n>>m;
	for (int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		st[i]=max(st[i-1],last[x]+1);
		last[x]=i;
	}
	for (int i=1;i<=m;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		if (st[r]<=l)
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春弦_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值