跳到正文

题解

题解:关注 small_lemon_qwq 谢谢喵

small_lemon_qwq约 9 分钟
原文
文章信息与版本

文章信息

收录
2026年1月2日
更新
2026年1月10日
篇幅
约 9 分钟
题目
P3567

版本

  1. v1当前

这是一个快速且简短的代码,最优解就是这个做法,不卡常也可以轻松跑进最优解第一页。

update:加了快读快写,我成最优解了,时间是次优解的 291416\frac{291}{416}(约为 70%70\%)。

先假设绝对众数存在,将绝对众数二进制拆分,可以发现,绝对众数的每一位也都是对应位的绝对众数。

形式化地讲,设绝对众数在二进制下第 kk 位是 cc,那么 ll 到 rr 所有数在二进制下第 kk 位是 cc 的数的个数超过一半,证明略。

显然要求 ll 到 rr 所有数在二进制下第 kk 位是 11(或求 00)的数的个数,用前缀和即可。

现在的问题是,如果绝对众数不存在怎么办?只要看求出的答案在 ll 到 rr 中的出现次数是否超过一半即可,在值域上开 vector(开 vector 数组)存储每个数出现的下标,用二分求出个数,再判断即可。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,a[500005],c[500005][19],d[500005];
vector<int>vec[500005];
inline int query(int i,int x){
	return upper_bound(vec[x].begin(),vec[x].end(),i)-vec[x].begin();
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		vec[a[i]].push_back(i);
		for(int j=0;j<=18;j++)c[i][j]=c[i-1][j]+((a[i]>>j)&1);
	}
	while(m--){
		int l,r,ans=0;
		cin>>l>>r;
		for(int i=0;i<=18;i++){
			int t=c[r][i]-c[l-1][i];
			if(t>(r-l+1>>1))ans|=(1<<i);
		}
		if(query(r,ans)-query(l-1,ans)<=(r-l+1>>1))ans=0;
		cout<<ans<<"\n";
	}
	return 0;
}
加了快读快写的版本(谨慎观看)
C++
#ifndef _reader_H
#define _reader_H
#include<stdio.h>
#ifndef _type_H
#define _type_H
typedef short     int16;
typedef int       int32;
typedef long long int64;
typedef __int128  int128;
typedef unsigned short     uint16;
typedef unsigned int       uint32;
typedef unsigned long long uint64;
typedef unsigned __int128  uint128;

typedef float       float32;
typedef double      float64;
typedef long double float80;
typedef __float128  float128;

typedef unsigned char uchar;
#endif
#ifndef check_EOF
#define check_EOF 0
#endif
struct reader{
	#if check_EOF
	#define gc (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin)),(p1!=p2)?(*p1++):-1)
	#else
	#define gc (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin)),*p1++)
	#endif
	template<class T>T c(){return gc;}
	template<class T>T i(){
		T k(0);bool f(0);char c(gc);
		while(c<48)c==45&&(f=1),c=gc;
		if(!f)while(c>=48)k=(k<<1)+(k<<3)+(c&15),c=gc;
		else while(c>=48)k=k*10-(c&15),c=gc;
		return k;}
	template<class T>T ui(){
		T k(0);char c(gc);
		while(c<48)c=gc;
		while(c>=48)k=(k<<1)+(k<<3)+(c&15),c=gc;
		return k;}
	#undef gc
	#define g(T,t,tt) inline T g_##t(){return tt<T>();}
	g(   char,  c, c)
	g(  int16,  s, i)
	g(  int32,  i, i)
	g(  int64,  l, i)
	g( int128, ll, i)
	g(  uchar, uc, c)
	g( uint16, us,ui)
	g( uint32, ui,ui)
	g( uint64, ul,ui)
	g(uint128,ull,ui)
	#undef g
	private:
		unsigned char buf[1<<20],*p1=buf,*p2=buf;
}fin;
#endif
#ifndef _printer_H
#define _printer_H
#include<stdio.h>
#include<string.h>
struct printer{
	#define pc(x) (p1>=(buf+(1<<20))&&(fwrite(buf,1,p1-buf,stdout),p1=buf),*p1++=(x))
	template<class T>void c(const T&x){pc(x);}
	template<class T>void i(T x){
		static unsigned char stk[50];
		unsigned char tp(50);
		if(x<0)for(pc('-');x;x/=10)stk[--tp]=(-(x%10))|48;
		else do stk[--tp]=(x%10)|48,x/=10;while(x);
		memcpy(p1,stk+tp,50-tp);p1+=50-tp;
		p1>=(buf+(1<<20))&&(fwrite(buf,1,p1-buf,stdout),p1=buf);}
	template<class T>void ui(T x){
		static unsigned char stk[50];
		unsigned char tp(50);
		do stk[--tp]=(x%10)|48,x/=10;while(x);
		memcpy(p1,stk+tp,50-tp);p1+=50-tp;
		p1>=(buf+(1<<20))&&(fwrite(buf,1,p1-buf,stdout),p1=buf);}
	#undef pc
	#define p(T,t,tt) inline void p_##t(const T&x){tt<T>(x);}
	p(   char,  c, c)
	p(  int16,  s, i)
	p(  int32,  i, i)
	p(  int64,  l, i)
	p( int128, ll, i)
	p(  uchar, uc, c)
	p( uint16, us,ui)
	p( uint32, ui,ui)
	p( uint64, ul,ui)
	p(uint128,ull,ui)
	#undef p
	void flush(){fwrite(buf,1,p1-buf,stdout),p1=buf;}
	~printer(){flush();}
	printer(){}
	private:
		unsigned char buf[(1<<20)+100],*p1=buf;
}fout;
#endif
using namespace std;
#include<bits/stdc++.h>
int32 n,m,a[500005],c[500005][19],d[500005];
vector<int>vec[500005];
inline int query(int i,int x){
	return upper_bound(vec[x].begin(),vec[x].end(),i)-vec[x].begin();
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	n=fin.g_ui();
	m=fin.g_ui();
	for(int i=1;i<=n;i++){
		a[i]=fin.g_ui();
		vec[a[i]].push_back(i);
		for(int j=0;j<=18;j++)c[i][j]=c[i-1][j]+((a[i]>>j)&1);
	}
	while(m--){
		int l,r,ans=0;
		l=fin.g_ui();
		r=fin.g_ui();
		for(int i=0;i<=18;i++){
			int t=c[r][i]-c[l-1][i];
			if(t>(r-l+1>>1))ans|=(1<<i);
		}
		if(query(r,ans)-query(l-1,ans)<=(r-l+1>>1))ans=0;
		fout.p_ui(ans);
		fout.p_c('\n');
	}
	return 0;
}
原文