Ybt 金牌导航 6.3.A. 区间众数 / P4168 [Violet] 蒲公英(分块)

题意简述

多次查询区间 [l,r] 的众数,若有多个取数值最小的。强制在线。

n4×104,m5×104

分析

加强版

考虑分块。

首先预处理出块区间内的众数 majl,r 和每种数在某个块的前缀的出现次数 cnti,ai,时空复杂度都是 O(nn) 的。

对于询问区间的整块,直接取出预处理好的众数以及出现次数。

由于散块中只有 O(n) 种数,故直接提取出这 O(n) 个数在整块的出现次数,然后暴力计算这些数的出现次数,找到最终的众数。

时空复杂度 O(nn)

具体见代码。

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define FlushIn fread(Fread::ibuf,1,1<<21,stdin)
#define FlushOut fwrite(Fwrite::obuf,1,Fwrite::S-Fwrite::obuf,stdout)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define rep(a,b,c) for(int a=(b);a<=(c);a++)
#define per(a,b,c) for(int a=(b);a>=(c);a--)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=d)
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
bool smallingll(long long x,long long y){return x<y;}
namespace Fread {
	const int SIZE=1<<21;
	char ibuf[SIZE],*S,*T;
	inline char getc(){if(S==T){T=(S=ibuf)+fread(ibuf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite{
	const int SIZE=1<<21;
	char obuf[SIZE],*S=obuf,*T=obuf+SIZE;
	inline void flush(){fwrite(obuf,1,S-obuf,stdout);S=obuf;}
	inline void putc(char c){*S++=c;if(S==T)flush();}
	struct NTR{~NTR(){flush();}}ztr;
}
/*#ifdef ONLINE_JUDGE
#define getchar Fread::getc
#define putchar Fwrite::putc
#endif*/
inline int rd(){
	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;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=4e4+5,maxm=205,inf=0x3f3f3f3f,BSIZE=300;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,Q,a[maxn],b[maxn];
int num;
int cnt[maxm][maxn];
int maj[maxm][maxm];
int tnc[maxn],tim[maxn];
int L[maxm],R[maxm],bel[maxn];
void solve_the_problem(){
	n=rd(),Q=rd();
	num=(n+BSIZE-1)/BSIZE;
	rep(i,1,n)b[i]=a[i]=rd();
	sort(b+1,b+n+1);int tot=unique(b+1,b+n+1)-(b+1);
	rep(i,1,n)a[i]=lower_bound(b+1,b+tot+1,a[i])-b;
	rep(i,1,num){
		L[i]=R[i-1]+1,R[i]=min(R[i-1]+BSIZE,n);
		rep(j,L[i],R[i])bel[j]=i;
	}
	rep(i,1,num){
		rep(j,1,tot)cnt[i][j]=cnt[i-1][j];
		rep(j,L[i],R[i])cnt[i][a[j]]++;
	}
	rep(i,1,num){
		int mx=0;
		rep(j,L[i],R[i]){
			tnc[a[j]]++;
			if(tnc[a[j]]>mx||(tnc[a[j]]==mx&&a[j]<maj[i][i]))mx=tnc[a[j]],maj[i][i]=a[j];
		}
		rep(j,i+1,num){
			maj[i][j]=maj[i][j-1];
			rep(k,L[j],R[j]){
				tnc[a[k]]++;
				if(tnc[a[k]]>mx||(tnc[a[k]]==mx&&a[k]<maj[i][j]))mx=tnc[a[k]],maj[i][j]=a[k];
			}
		}
		rep(j,1,tot)tnc[j]=0;
	}
	int lstans=0;
	rep(_,1,Q){
		int l=(rd()+lstans-1)%n+1,r=(rd()+lstans-1)%n+1;
		if(l>r)swap(l,r);
		int bl=bel[l],br=bel[r];
		int res=maj[bl+1][br-1],mx=bl==br?0:cnt[br-1][res]-cnt[bl][res];
		rep(i,max(l,L[bl]),min(r,R[bl])){
			if(tim[a[i]]<_)tim[a[i]]=_,tnc[a[i]]=bl==br?0:cnt[br-1][a[i]]-cnt[bl][a[i]];
			tnc[a[i]]++;
			if(tnc[a[i]]>mx||(tnc[a[i]]==mx&&a[i]<res))mx=tnc[a[i]],res=a[i];
		}
		if(bl^br){
			rep(i,max(l,L[br]),min(r,R[br])){
				if(tim[a[i]]<_)tim[a[i]]=_,tnc[a[i]]=bl==br?0:cnt[br-1][a[i]]-cnt[bl][a[i]];
				tnc[a[i]]++;
				if(tnc[a[i]]>mx||(tnc[a[i]]==mx&&a[i]<res))mx=tnc[a[i]],res=a[i];
			}
		}
		write(lstans=b[res],10);
	}
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=1;while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/17985351

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示