求代码的时间复杂度+优化方式
  • 板块学术版
  • 楼主China_U_19641016
  • 当前回复12
  • 已保存回复12
  • 发布时间2025/7/25 18:25
  • 上次更新2025/7/26 07:36:13
查看原帖
求代码的时间复杂度+优化方式
1304502
China_U_19641016楼主2025/7/25 18:25

rt.

Code:

#include<bits/stdc++.h>
using namespace std;
char buf[1<<26],*p1=buf,*p2=buf;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline unsigned long long read() {
	unsigned long long x=0;
	char ch=gc();
	while(!isdigit(ch))ch=gc();
	while(isdigit(ch)) x=x*10+(ch^48),ch=gc();
	return x;
}
const __int128 MOD=pow(2,64);
unsigned long long n,x,y,ans;
unordered_map<unsigned long long,unsigned long long>a;
int main(){
	n=read();
	for(unsigned long long i=1;i<=n;i++){
		x=read();y=read();
		ans=(ans+(a[x]*i))%MOD;
		a[x]=y;
	}
    printf("%llu",ans);
    return 0;
}

理论上是 O(n)O(n) ,而且已经用了快读优化,但还是TLE.

2025/7/25 18:25
加载中...