跳到正文

题解

题解:P10815 【模板】快速读入

zhoujiefu约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P10815

版本

仅有当前版本

这道题一眼看到,不禁问道,你谷怎么简单的题都评橙吗? 我是小丑

言归正传,这道题有 10810^8 的输入量,我们肯定需要使用快读。

C++
#include<bits/stdc++.h>
using namespace std;
template<class T>
inline void read(T &x){
	bool f=1;x=0;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=!f;c=getchar();}
	while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
	x=f?x:-x;
}
template<class T>
inline void write(T x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
int main(){
    int n,ans=0;read(n);
    for(int i=1;i<=n;++i){
        int x;read(x);
        ans+=x;
    }
    write(ans);
    return 0;
}

普通快读交上去只有 75 分,超时了。我们只能使用优化了 getchar() 的快读了。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline char gc(){// 优化后的getchar
    static char buf[1048576],*p1,*p2;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1048576,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){//使用优化后的getchar
	long long s=0,w=1;char c=gc();
	while(c<'0'||c>'9'){if(c=='-')w=-1;c=gc();}
	while(c>='0'&&c<='9'){s=(s<<1LL)+(s<<3LL)+(c^48LL);c=gc();}
	return s*w;
}
void write(int x){
    if(x<0){
    	putchar('-');
    	x=-x;
	}
    if(x>9) write(x/10);
	char a=(x%10+(1<<5)+(1<<4));
	if(a>='0'&&a<='9') putchar(a);
}
signed main(){
	int n,sum=0;
	n=read();
	for(int i=1;i<=n;i++){
		int a=read();
        sum+=a;
	}
    write(sum);
	return 0;
}

注意要开 long long 。

原文