萌新求助时间复杂度
  • 板块灌水区
  • 楼主zjrdmd
  • 当前回复12
  • 已保存回复12
  • 发布时间2020/10/21 22:02
  • 上次更新2023/11/5 10:12:33
查看原帖
萌新求助时间复杂度
174897
zjrdmd楼主2020/10/21 22:02

听说unordered_map是O(1)O(1)的,那么下面代码的复杂度不应该是O(900×n)O(900\times n)的嘛?但是n=1e5n=1e5的数据本地要跑46s/kk

#include<bits/stdc++.h>
#define ri register int
#define int long long

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<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}

const int N=1e5+5;
int a[N],b[N],n,m,ans=0;
std::unordered_map<int,int>mp;

int qwq(int x,int k){
	int p=(1<<k);
	if((x&p)!=0)x-=p;
	else x+=p;
	return x;
} 

int tot(int x,int xx){
	int sum=0;
	for(ri i=1;i<=30;i++){
		for(ri j=i+1;j<=30;j++){
			if(i==j)continue;
			xx=qwq(x,i-1),xx=qwq(xx,j-1);
			sum+=mp[xx];
		} 
	}
	return sum;
}

signed main(void){
	freopen("date.in","r",stdin);
	freopen("date.out","w",stdout);
	n=read(),m=read();
	for(ri i=1;i<=n;i++)a[i]=read();
	for(ri i=1;i<=m;i++)b[i]=read(),mp[b[i]]++;
	for(ri i=1;i<=n;i++){
		ans+=tot(a[i],0);
	}
	printf("%lld",ans);
	return 0;
}

2020/10/21 22:02
加载中...