c++IO哪家强?这篇文章帮你忙!
__CrossBow_EXE__
·
·
算法·理论
前言
本次,我们邀请到了如下选手:
- 普通
cin,cout
- 普通
scanf,printf
- 解绑
cin,cout
cin.tie(0),cout.tie(0)
- 解绑+
tie(0)
- 普通快读快写
- 优化的快读快写
fread,fwrite
- 同学做的IO
buf 读 string
mmap(洛谷上可用),快写为优化版
unlocked()(洛谷上可用),快写为优化版
- 同学声称超快的快读
为了测试公平,我们使用了自创的题目与数据。洛谷给出的快速读入模板只有 4 个测试点,我造的题目有 10 个。
题目链接
数据点分配如下:
| 子任务编号 |
测试点数量 |
数据范围 |
| 1 |
6 |
n \le 10^3,a_i \le 10^5 |
| 2 |
6 |
n \le 10^5,a_i \le 10^7 |
| 3 |
3 |
n \le 10^6,a_i \le 10^6 |
保证每个子任务最后一个测试点为极限数据。
时限 5s,空间 512MB。每名选手都会测试 3 次,时间、空间取平均数。满分 150 分。
数据生成器如下:
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
string num_to_id(string problem,int num){
string str=problem;
if(num<=9) return str+"00"+char(num+'0');
else if(10<=num&&num<=99) return str+"0"+char(num/10+'0')+char(num%10+'0');
else if(100<=num&&num<=999) return str+char(num/100+'0')+char(num/10%10+'0')+char(num%10+'0');
return "Out of limit";
}
void write_in_file(string problem,int num){
string str=num_to_id(problem,num);
char a[15];
int l=str.size();
for(int i=0;i<l;i++){
a[i]=str[i];
}
a[l]='.';
a[l+1]='i';
a[l+2]='n';
a[l+3]='\0';
freopen(a,"w",stdout);
}
void close_in_file(){fclose(stdout);}
void init(){srand(time(0));}
int randint(int l,int r){return l+int(1.0*rand()/RAND_MAX*(r-l)+0.5);}
int N,ai,n;
void make1(){
N=1e3,ai=1e5;
for(int cs=1;cs<=5;cs++){
write_in_file("t",cs);
n=randint(1,N);
cout<<n<<endl;
for(int i=1;i<=n;i++){
cout<<randint(1,ai)<<' ';
}
}
for(int cs=6;cs<=6;cs++){
write_in_file("t",cs);
n=N;
cout<<n<<endl;
for(int i=1;i<=n;i++){
cout<<ai<<' ';
}
}
}
void make2(){
N=1e5,ai=1e7;
for(int cs=7;cs<=11;cs++){
write_in_file("t",cs);
n=randint(1,N);
cout<<n<<endl;
for(int i=1;i<=n;i++){
cout<<randint(1,ai)<<' ';
}
}
for(int cs=12;cs<=12;cs++){
write_in_file("t",cs);
n=N;
cout<<n<<endl;
for(int i=1;i<=n;i++){
cout<<ai<<' ';
}
}
}
void make3(){
N=1e6,ai=1e6;
for(int cs=13;cs<=14;cs++){
write_in_file("t",cs);
n=randint(1,N);
cout<<n<<endl;
for(int i=1;i<=n;i++){
cout<<randint(1,ai)<<' ';
}
}
for(int cs=15;cs<=15;cs++){
write_in_file("t",cs);
n=N;
cout<<n<<endl;
for(int i=1;i<=n;i++){
cout<<ai<<' ';
}
}
}
int main(){
init();
make1();
make2();
make3();
return 0;
}
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
string num_to_id(string problem,int num){
string str=problem;
if(num<=9) return str+"00"+char(num+'0');
else if(10<=num&&num<=99) return str+"0"+char(num/10+'0')+char(num%10+'0');
else if(100<=num&&num<=999) return str+char(num/100+'0')+char(num/10%10+'0')+char(num%10+'0');
return "Out of limit";
}
void read_in_file(string problem,int num){
string str=num_to_id(problem,num);
char a[15];
int l=str.size();
for(int i=0;i<l;i++){
a[i]=str[i];
}
a[l]='.';
a[l+1]='i';
a[l+2]='n';
a[l+3]='\0';
freopen(a,"r",stdin);
}
void write_out_file(string problem,int num){
string str=num_to_id(problem,num);
char a[15];
int l=str.size();
for(int i=0;i<l;i++){
a[i]=str[i];
}
a[l]='.';
a[l+1]='o';
a[l+2]='u';
a[l+3]='t';
a[l+4]='\0';
freopen(a,"w",stdout);
}
void close_file(){
fclose(stdin);
fclose(stdout);
}
ll n,ans=0;
int main(){
for(int cs=1;cs<=15;cs++){
read_in_file("t",cs);
write_out_file("t",cs);
ans=0;
cin>>n;
for(register int i=1,x;i<=n;++i){
cin>>x;
cout<<x<<' ';
ans+=x;
}
cout<<endl<<ans<<endl;
close_file();
}
return 0;
}
下面给出每名参赛选手的代码。
代码
普通 cin,cout
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int n;
ll ans=0;
signed main(int argc,char *argv[]){
cin>>n;
for(int i=1,x;i<=n;i++){
cin>>x;ans+=x;
cout<<x<<' ';
}
cout<<endl<<ans<<endl;
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 15:10
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
普通 scanf,printf
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int n;
ll ans=0;
signed main(int argc,char *argv[]){
scanf("%d",&n);
for(int i=1,x;i<=n;i++){
scanf("%d",&x);
ans+=x;
printf("%d ",x);
}
printf("\n%lld\n",ans);
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 15:50
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
解绑 cin,cout
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int n;
ll ans=0;
signed main(int argc,char *argv[]){
ios::sync_with_stdio(0);
cin>>n;
for(int i=1,x;i<=n;i++){
cin>>x;
ans+=x;
cout<<x<<' ';
}
cout<<endl<<ans<<endl;
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 15:51
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
cin.tie(0),cout.tie(0)
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int n;
ll ans=0;
signed main(int argc,char *argv[]){
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1,x;i<=n;i++){
cin>>x;
ans+=x;
cout<<x<<' ';
}
cout<<endl<<ans<<endl;
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 15:52
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
解绑+tie(0)
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int n;
ll ans=0;
signed main(int argc,char *argv[]){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1,x;i<=n;i++){
cin>>x;
ans+=x;
cout<<x<<' ';
}
cout<<endl<<ans<<endl;
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 15:52
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
普通快读快写
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int n;
ll ans=0;
int read(){
int k=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') k=k*10+c-'0',c=getchar();
return k*f;
}
void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x<10) putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
signed main(int argc,char *argv[]){
n=read();
for(int i=1,x;i<=n;i++){
x=read();
ans+=x;
write(x);putchar(' ');
}
putchar('\n');
write(ans);
putchar('\n');
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 15:55
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
优化的快读快写
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int n;
ll ans=0;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
inline void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
signed main(int argc,char *argv[]){
n=read();
for(int i=1,x;i<=n;i++){
x=read();
ans+=x;
write(x);putchar(' ');
}
putchar('\n');
write(ans);
putchar('\n');
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 15:57
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
fread,fwrite
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int n;
ll ans=0;
const int BUFSIZE=1<<20;
char ibuf[BUFSIZE],*is=ibuf,*it=ibuf;
inline char getch(){if(is==it)it=(is=ibuf)+fread(ibuf,1,BUFSIZE,stdin);return is==it?EOF:*is++;}
char tmp[BUFSIZE];int cnt=0;
void flush(){fwrite(tmp,1,cnt,stdout);cnt=0;}
void putch(char c){tmp[cnt++]=c;if(cnt==BUFSIZE)flush();}
inline int read(){
int x=0,f=1;
char ch=getch();
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getch();
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);
ch=getch();
}
return x*f;
}
inline void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
signed main(int argc,char *argv[]){
n=read();
for(int i=1,x;i<=n;i++){
x=read();
ans+=x;
write(x);putchar(' ');
}
putchar('\n');
write(ans);
putchar('\n');
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 16:00
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
同学做的IO
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
ll ans=0;
#define MAXSIZE (1<<20)
#define isdigit(x) ( x >= '0' && x <= '9' )
#define blank(x) ( x == ' ' || x == '\n' || x == '\r' || x == '\t' )
#define Setprecision 6
namespace Fread {
char buf[MAXSIZE] ,*p1,*p2 ;//输入缓冲区优化(Fread命名空间)
inline char getchar () { if (p1 == p2) p2 = (p1 = buf) + fread(buf , 1 , MAXSIZE , stdin) ; return p1 == p2 ? '\n' : *p1++ ; }//定义getchar()
}
namespace Fwrite {//输出缓冲区优化(Fwrite命名空间)
char buf[MAXSIZE],*p1 = buf , *p2 = buf + MAXSIZE ;
inline void flush () { fwrite(buf, 1, p1 - buf, stdout); p1 = buf; }
inline void putchar (char c) { *p1++ = c; if (p1 == p2) flush(); }//定义putchar()
struct NTR { ~NTR() { flush(); } } ztr;// 程序结束时自动刷新
}
using namespace Fread ;
using namespace Fwrite ;
#define getchar Fread::getchar
#define putchar Fwrite::putchar
namespace IO
{
struct Reader //Reader类(重载>>运算符)
{
template <typename T> Reader& operator >> (T &x) //int ,long long ,long ,__int128
{
x = 0 ; short f = 1 ;char c = getchar() ;
while ( !isdigit(c) ) {if (c == '-') { f = -1 ; } c = getchar() ; }
while ( isdigit(c) ) x = (x << 3) + (x << 1) + (c ^ 48) , c = getchar() ;
x *= f ;
return *this ;
}
Reader& operator >> (double &x)//double
{
x = 0 ; double t = 0 ; short f = 1 , s = 0 ; char c = getchar() ;
while ( (!isdigit(c)) && c != '.' ) {if (c == '-') { f = -1 ; } c = getchar() ; }
while ( (isdigit(c)) && c != '.' ) x = (x * 10) + (c ^ 48) , c = getchar() ;
if (c == '.') c = getchar() ;
else { x *= f ; return *this ; }
while (isdigit(c)) t = t * 10 + (c ^ 48) , s++ , c = getchar() ;
while (s--) t /= 10.0 ;
x = (x + t) * f ;
return *this ;
}
Reader& operator >> (long double &x)//long double
{
x = 0 ; long double t = 0 ; short f = 1 , s = 0 ; char c = getchar() ;
while ( (!isdigit(c)) && c != '.') { if (c == '-') { f = -1 ; } c = getchar() ; }
while ( (isdigit(c)) && c != '.' ) x = (x * 10) + (c ^ 48) , c = getchar() ;
if (c == '.') c = getchar() ;
else { x *= f ; return *this ; }
while (isdigit(c)) t = t * 10 + (c ^ 48) , s++ , c = getchar() ;
while (s--) t /= 10.0 ;
x = (x + t) * f ;
return *this ;
}
Reader& operator >> (__float128 &x)//__float128
{
x = 0 ; __float128 t = 0 ; short f = 1 , s = 0 ; char c = getchar() ;
while ( (!isdigit(c)) && c != '.') { if (c == '-') { f = -1 ; } c = getchar() ; }
while ( (isdigit(c)) && c != '.' ) x = (x * 10) + (c ^ 48) , c = getchar() ;
if (c == '.') c = getchar() ;
else { x *= f ; return *this ; }
while (isdigit(c)) t = t * 10 + (c ^ 48) , s++ , c = getchar() ;
while (s--) t /= 10.0 ;
x = (x + t) * f ;
return *this ;
}
Reader& operator >> (char &c)//char
{
c = getchar() ;
while ( blank(c) ) c = getchar() ;
return *this ;
}
Reader& operator >> (char *str)//char[]
{
int len = 0;
char c = getchar() ;
while ( blank(c) ) c = getchar() ;
while ( !blank(c) ) str[len++] = c , c = getchar() ;
str[len] = '\0' ;
return *this ;
}
Reader& operator >> (string &str)//string
{
str.clear() ;
char c = getchar() ;
while ( blank(c) ) c = getchar() ;
while ( !blank(c) ) str.push_back(c) , c = getchar() ;
return *this ;
}
Reader() {}
}cin;
const char endl = '\n' ;//endl -> '\n'
struct Writer//Writer类(重载<<运算符)
{
typedef int mxdouble ;
template <typename T> Writer& operator << (T x)//int ,long long ,long ,__int128
{
if (x == 0) { putchar('0') ; return *this; }
if (x < 0) putchar('-') , x = -x ;
static short sta[40] ;
short top = 0 ;
while (x > 0) sta[++top] = x % 10 , x /= 10 ;
while (top > 0) putchar(sta[top] + '0') , top-- ;
return *this ;
}
Writer& operator << (double x)//double
{
if (x < 0) putchar('-') , x = -x ;
mxdouble _ = x;
x -= (double)_;
static short sta[40] ;
short top = 0 ;
while (_ > 0) sta[++top] = _ % 10 , _ /= 10 ;
if (top == 0) putchar('0') ;
while (top > 0) putchar(sta[top] + '0'), top-- ;
putchar('.') ;
for (int i = 0 ; i < Setprecision; i++) x *= 10 ;
_ = x;
while (_ > 0) sta[++top] = _ % 10 , _ /= 10 ;
for (int i = 0 ; i < Setprecision - top ; i++) putchar('0') ;
while (top > 0) putchar(sta[top] + '0') , top-- ;
return *this ;
}
Writer& operator << (long double x)//long double
{
if (x < 0) putchar('-') , x = -x ;
mxdouble _ = x ;
x -= (long double)_ ;
static short sta[40] ;
short top = 0 ;
while (_ > 0) sta[++top] = _ % 10 , _ /= 10 ;
if (top == 0) putchar('0') ;
while (top > 0) putchar(sta[top] + '0') , top-- ;
putchar('.');
for (int i = 0 ; i < Setprecision ; i++) x *= 10 ;
_ = x ;
while (_ > 0) sta[++top] = _ % 10 , _ /= 10 ;
for (int i = 0 ; i < Setprecision - top ; i++) putchar('0') ;
while (top > 0) putchar(sta[top] + '0') , top-- ;
return *this ;
}
Writer& operator << (__float128 x)//__float128
{
if (x < 0) putchar('-') , x = -x ;
mxdouble _ = x ;
x -= (__float128)_ ;
static short sta[40] ;
short top = 0;
while (_ > 0) sta[++top] = _ % 10 , _ /= 10 ;
if (top == 0) putchar('0') ;
while (top > 0) putchar(sta[top] + '0'), top-- ;
putchar('.') ;
for (int i = 0 ; i < Setprecision ; i++) x *= 10 ;
_ = x;
while (_ > 0) sta[++top] = _ % 10 , _ /= 10 ;
for (int i = 0 ; i < Setprecision - top; i++) putchar('0') ;
while (top > 0) putchar(sta[top] + '0') , top-- ;
return *this ;
}
Writer& operator << (char c) { putchar(c) ; return *this ; }//char
Writer& operator << (char *str)//char[]
{
int cur = 0 ;
while (str[cur]) putchar(str[cur++]) ;
return *this ;
}
Writer& operator << (const char *str)
{
int cur = 0 ;
while (str[cur]) putchar(str[cur++]) ;
return *this ;
}
Writer& operator << (string str)//string
{
int st = 0, ed = str.size() ;
while (st < ed) putchar(str[st++]) ;
return *this ;
}
Writer() {}
}cout;
}
using namespace IO;
#define cin IO::cin
#define cout IO::cout
#define endl IO::endl
signed main(int argc,char *argv[]){
cin>>n;
for(int i=1,x;i<=n;i++){
cin>>x;
ans+=x;
cout<<x<<' ';
}
cout<<endl<<ans<<endl;
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 16:03
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
buf 读 string
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
ll ans=0;
namespace Fast_I{
char *_Buf,*_Start_ptr,*_End_ptr;
streambuf *inbuf;
unsigned int Size;
bool _Ok;
struct Fast_Istream{
operator bool(){return _Ok;}
Fast_Istream(streambuf *in,unsigned int Sz){
_Ok=1;
Fast_I::Size=Sz;
inbuf=in;
_Start_ptr=_End_ptr=_Buf=new char[Sz];
}
Fast_Istream(const char *in,unsigned int Sz){
_Ok=1;
Fast_I::Size=Sz;
rdbuf(in);
_Start_ptr=_End_ptr=_Buf=new char[Sz];
}
Fast_Istream(unsigned int Sz){
_Ok=1;
Fast_I::Size=Sz;
_Start_ptr=_End_ptr=_Buf=new char[Sz];
}
void rdbuf(const char *File){
static ifstream __In__(File);
rdbuf(__In__.rdbuf());
}
void Get_Char(char &_Val){
if(_Start_ptr==_End_ptr){
_Start_ptr=_Buf;
_End_ptr=_Buf+inbuf->sgetn(_Buf,Size);
}
if(_Start_ptr==_End_ptr){
_Val=-1;
_Ok=0;
}else _Val=*_Start_ptr++;
}
Fast_Istream &operator>>(char &_Val){
if(_Ok){
Get_Char(_Val);
while(_Val==32||_Val==10||_Val==13||_Val==8||_Val==9||_Val==7||_Val==12||_Val==11) Get_Char(_Val);
}
return *this;
}
Fast_Istream &operator>>(char *_Val){
if(_Ok){
Get_Char(*_Val);
while(*_Val==32||*_Val==10||*_Val==13||*_Val==8||*_Val==9||*_Val==7||*_Val==12||*_Val==11) Get_Char(*_Val);
while(*_Val!=32&&*_Val!=10&&*_Val&&*_Val!=-1&&*_Val!=9 &&*_Val!=11&&*_Val!=12&&~*_Val) Get_Char(*++_Val);
*_Val=0;
--_Start_ptr;
}
return *this;
}
Fast_Istream &operator>>(string &_Val){
if(_Ok){
char c;
Get_Char(c);
while(c==32||c==10||c==13||c==8||c==9||c==7||c==12||c==11) Get_Char(c);
for(_Val.clear();c!=32&&c!=10&&c&&c!=-1&&c!=9&&c!=11&&c!=12&&~c;Get_Char(c)) _Val.push_back(c);
--_Start_ptr;
}
return *this;
}
template <typename Typex>
void Get_Int(Typex &_Val){
if(_Ok){
char ch;
bool _F=0;
for(Get_Char(ch);(ch<48||ch>57)&&~ch;Get_Char(ch)) _F=ch==45;
for(_Val=0;ch>47&&ch<58&&~ch;Get_Char(ch)) _Val=_Val*10+(ch^48);
if(_F) _Val=~_Val+1;
--_Start_ptr;
}
}
template <typename Typex>
void Get_Unsigned(Typex &_Val){
if(_Ok){
char ch;
Get_Char(ch);
while((ch<48||ch>57)&&~ch) Get_Char(ch);
for(_Val=0;ch>47&&ch<58&&~ch;Get_Char(ch)) _Val=_Val*10+(ch^48);
--_Start_ptr;
}
}
template <typename Typex>
void Get_Double(Typex &_Val){
if(_Ok){
char ch;
bool _F=0;
for(Get_Char(ch);(ch<48||ch>57)&&~ch;Get_Char(ch)) _F=ch==45;
for(_Val=0;ch>47&&ch<58&&~ch;Get_Char(ch)) _Val=_Val*10+(ch^48);
if(ch==46){
unsigned long long _Pow=1;
for(Get_Char(ch);ch>47&&ch<58&&~ch;Get_Char(ch)) _Val+=Typex((ch^48)*1.0/(_Pow*=10));
}
if(_F) _Val=-_Val;
--_Start_ptr;
}
}
Fast_Istream &operator>>(bool &_Val){
if(_Ok){
char ch;
Get_Char(ch);
while(ch==32||ch==10||ch==13||ch==8||ch==9||ch==7||ch==12||ch==11) Get_Char(ch);
while(ch!=32&&ch!=10&&ch&&~ch&&ch!=9&&ch!=11&&ch!=12&&~ch){
_Val|=ch!=48;
Get_Char(ch);
}
--_Start_ptr;
}
return *this;
}
Fast_Istream &operator>>(short &_Val){
Get_Int(_Val);
return *this;
}
Fast_Istream &operator>>(int &_Val){
Get_Int(_Val);
return *this;
}
Fast_Istream &operator>>(long &_Val){
Get_Int(_Val);
return *this;
}
Fast_Istream &operator>>(long long &_Val){
Get_Int(_Val);
return *this;
}
Fast_Istream &operator>>(unsigned short &_Val){
Get_Unsigned(_Val);
return *this;
}
Fast_Istream &operator>>(unsigned int &_Val){
Get_Unsigned(_Val);
return *this;
}
Fast_Istream &operator>>(unsigned long &_Val){
Get_Unsigned(_Val);
return *this;
}
Fast_Istream &operator>>(unsigned long long &_Val){
Get_Unsigned(_Val);
return *this;
}
Fast_Istream &operator>>(float &_Val){
Get_Double(_Val);
return *this;
}
Fast_Istream &operator>>(double &_Val){
Get_Double(_Val);
return *this;
}
Fast_Istream &operator>>(long double &_Val){
Get_Double(_Val);
return *this;
}
template <typename Typex,typename... More>
void operator()(Typex &_Val,More &... _More){
*this>>_Val;
operator()(_More...);
}
void pop(){
char ch;
Get_Char(ch);
}
char peek(){
if(_Start_ptr==_End_ptr){
_Start_ptr=_Buf;
_End_ptr=_Buf+inbuf->sgetn(_Buf,Size);
}
if(_Start_ptr==_End_ptr){
_Ok=0;
return -1;
}else return *_Start_ptr;
}
template <typename Typex>
void operator()(Typex &_Val){*this>>_Val;}
template <typename Typex,typename...More>
streambuf *rdbuf(){return inbuf;}
void rdbuf(streambuf *_inbuf){inbuf=_inbuf;}
Fast_Istream getline(string &s,char _End='\n'){
if(_Ok){
char c;
Get_Char(c);
while((c==32||c==10||c==13||c==8||c==9||c==7||c==12||c==11||c==-1)&&c!=_End) Get_Char(c);
for(s.clear();c!=_End&&~c;Get_Char(c)) s.push_back(c);
--_Start_ptr;
}
return *this;
}
};
}
//快写
namespace Fast_O{
string buf;
streambuf *outbuf;
int _M_precision=6;
struct Fast_Ostream{
Fast_Ostream(streambuf *out,unsigned int Size){
buf.reserve(Size);
outbuf=out;
}
Fast_Ostream(std::streambuf* out){outbuf=out;}
Fast_Ostream(const char *File,unsigned int Size){
buf.reserve(Size);
rdbuf(File);
}
void rdbuf(const char *File){
static ofstream __Out__(File);
rdbuf(__Out__.rdbuf());
}
Fast_Ostream(unsigned int Size){buf.reserve(Size);}
void flush(){
outbuf->sputn(buf.data(),buf.size());
buf.clear();
}
~Fast_Ostream(){flush();}
void endl(){buf.push_back('\n');}
Fast_Ostream &operator<<(char _Val){
buf.push_back(_Val);
return *this;
}
Fast_Ostream &operator<<(const char *_Val){
while(*_Val) buf.push_back(*_Val++);
return *this;
}
Fast_Ostream &operator<<(const string &_Val){
buf+=_Val;
return *this;
}
template <typename Typex>
void Put_Unsigned(Typex _Val){
char *_Stack=(char *)malloc(sizeof(Typex)*3);
unsigned S_top=0;
while(_Val){
_Stack[++S_top]=(_Val%10)^48;
_Val/=10;
}
if(!S_top) buf.push_back('0');
while(S_top) buf.push_back(_Stack[S_top--]);
free(_Stack);
}
template <typename Typex>
void Put_Int(Typex _Val){
if(_Val<0){
buf.push_back('-');
Put_Unsigned(~_Val+1);
}else Put_Unsigned(_Val);
}
Fast_Ostream &operator<<(bool _Val){
buf.push_back(_Val?'1':'0');
return *this;
}
Fast_Ostream &operator<<(short _Val){
Put_Int(_Val);
return *this;
}
Fast_Ostream &operator<<(int _Val){
Put_Int(_Val);
return *this;
}
Fast_Ostream &operator<<(long _Val){
Put_Int(_Val);
return *this;
}
Fast_Ostream &operator<<(long long _Val){
Put_Int(_Val);
return *this;
}
Fast_Ostream &operator<<(unsigned short _Val){
Put_Unsigned(_Val);
return *this;
}
Fast_Ostream &operator<<(unsigned int _Val){
Put_Unsigned(_Val);
return *this;
}
Fast_Ostream &operator<<(unsigned long _Val){
Put_Unsigned(_Val);
return *this;
}
Fast_Ostream &operator<<(unsigned long long _Val){
Put_Unsigned(_Val);
return *this;
}
template <typename Typex>
void endl(const Typex &_Val){*this<<_Val<<'\n';}
template <typename Typex,typename... More>
void endl(const Typex &_Val,const More &... _More){
*this<<_Val;
endl(_More...);
}
template <typename Typex>
void operator()(const Typex &_Val){*this<<_Val;}
template <typename Typex,typename... More>
void operator()(const Typex &_Val,const More &... _More){
*this<<_Val;
operator()(_More...);
}
std::streambuf *rdbuf(){return outbuf;}
void rdbuf(std::streambuf *_outbuf){outbuf=_outbuf;}
};
}
namespace Fast_IO{
Fast_I::Fast_Istream fin(cin.rdbuf(),16777216);
Fast_O::Fast_Ostream fout(cout.rdbuf());
}
using namespace Fast_IO;
signed main(int argc,char *argv[]){
fin>>n;
for(int i=1,x;i<=n;i++){
fin>>x;
ans+=x;
fout<<x<<' ';
}
fout<<'\n'<<ans<<'\n';
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 16:07
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
mmap(洛谷上可用),快写为优化版
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
char *pc;
int rd(){
int x=0,f=1;
char c=*pc++;
while(!isdigit(c)){
if(c=='-') f=-1;
c=*pc++;
}
while(isdigit(c)) x=x*10+(c^48),c=*pc++;
return x*f;
}
inline void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
int n;
ll ans=0;
signed main(int argc,char *argv[]){
int fd=0;
struct stat state;
fstat(fd,&state);
pc=(char*)mmap(NULL,state.st_size,PROT_READ,MAP_PRIVATE,fd,0);
close(fd);
n=rd();
for(int i=1,x;i<=n;i++){
x=rd();
ans+=x;
write(x);
putchar(' ');
}
putchar('\n');
write(ans);
putchar('\n');
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 16:14
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
unlocked()(洛谷上可用),快写为优化版
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
ll ans=0;
inline int read(){
int num=0,f=1;char c=getchar_unlocked();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar_unlocked();}
while(c>='0'&&c<='9'){num=num*10+(c-'0');c=getchar_unlocked();}
return num*f;
}
inline void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
signed main(int argc,char *argv[]){
n=read();
for(int i=1,x;i<=n;i++){
x=read();
ans+=x;
write(x);
putchar(' ');
}
putchar('\n');
write(ans);
putchar('\n');
return 0;
}
/*
---INFORMATIONS---
TIME:25/04/25 16:17
PROBLEM:U557278
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
同学声称超快的快读
#include<bits/stdc++.h>
#define ll long long
#define uint unsigned
#define ull uint ll
#define __Pa(__Type) pair<__Type,__Type>
#define __Greater_Pri_Q(__Type) priority_queue<__Type,vector<__Type>,greater<__Type>>
#define __Stucmp_Pri_Q(__Type,__Cmp) priority_queue<__Type,vector<__Type>,__Cmp>
#define __Clscmp_Pri_Q __Stucmp_Pri_Q
#define __Func_Pri_Q(__Type,__Cmp) priority_queue<__Type,vector<__Type>,decltype(&__Cmp)>
#define __Pri_Q(__Type) priority_queue<__Type>
#define __Mu_M(__Key,__Type) multimap<__Key,__Type>
#define __Mu_S(__Type) multiset<__Type>
#define __Unord_M(__Key,__Type) unordered_map<__Key,__Type>
#define __Unord_S(__Type) unordered_set<__Type>
#define __Unord_Mu_M(__Key,__Type) unordered_multimap<__Key,__Type>
#define __Unord_Mu_S(__Type) unordered_multiset<__Type>
//#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define __put(__X,__F1,__F2) ((__F1)?(cout<<(#__X)<<':'<<(__X)<<" \n"[__F2]):(cout<<(__X)<<" \n"[__F2]))
#define __put0 cout<<"0 ";
#define __putenter cout<<'\n'
#else
#define __put(__X,__F1,__F2)
#define __put0
#define __putenter
#endif
//#define __INT_TO_LL
#ifdef __INT_TO_LL
#define int long long
#endif
//#define __USE_FREOPEN
#define __CLOSE_SYNC
#ifdef __USE_FREOPEN
#undef __CLOSE_SYNC
#endif
#define y1 __Y1_By_MySelf__
#define ass(__Num,__Arr) memset((__Arr),(__Num),sizeof(__Arr))
#define clr(__Arr) memset((__Arr),0,sizeof(__Arr))
#define assneg(__Arr) memset((__Arr),-1,sizeof(__Arr))
#define assmax(__Arr) memset((__Arr),0x7f,sizeof(__Arr))
#define assmax_s(__Arr) memset((__Arr),0x3f,sizeof(__Arr))
#define assmin(__Arr) memset((__Arr),0x80,sizeof(__Arr))
#define assmin_s(__Arr) memset((__Arr),0xc0,sizeof(__Arr))
using namespace std;
//光速IO(类似于文件读入,要用Ctrl+Z或F6结束读入,再一起输出)(方便调试)
//快读
namespace Fast_I{
char *_Buf,*_Start_ptr,*_End_ptr,ch;
streambuf *inbuf;
constexpr unsigned int Size=1<<18;
bool _F;
struct Fast_Istream{
Fast_Istream(streambuf *in){
inbuf=in;
_Start_ptr=_End_ptr=_Buf=new char[Size];
}
void Get_Char(char &_Val){
if(__builtin_expect(_Start_ptr==_End_ptr,0)){
_Start_ptr=_Buf;
_End_ptr=_Buf+inbuf->sgetn(_Buf,Size);
if(__builtin_expect(_Start_ptr==_End_ptr,0)){
_Val=-1;
return;
}
}
_Val=*_Start_ptr++;
}
void Get_Unsigned(ull &_Val){
Get_Char(ch);
for(_Val=0;ch>47&&ch<58;Get_Char(ch)){
_Val=_Val*10+(ch^48);
}
}
Fast_Istream &operator>>(ull &_Val){
Get_Unsigned(_Val);
return *this;
}
void GetN(ull &_Val){
Get_Unsigned(_Val);
_Start_ptr++;
}
};
}// namespace Fast_I
//快写
namespace Fast_O{
string buf;
streambuf *outbuf;
char _Stack[13];
unsigned S_top;
struct Fast_Ostream{
Fast_Ostream(std::streambuf* out){
outbuf=out;
}
void flush(){
outbuf->sputn(buf.data(),buf.size());
buf.clear();
}
~Fast_Ostream(){
flush();
}
void Put_Unsigned(ull _Val){
S_top=0;
while(_Val){
_Stack[++S_top]=(_Val%10)^48;
_Val/=10;
}
if(!S_top){
buf+='0';
}
while(S_top){
buf+=_Stack[S_top--];
}
}
Fast_Ostream &operator<<(ull _Val){
Put_Unsigned(_Val);
return *this;
}
Fast_Ostream &operator<<(char _Val){
buf+=_Val;
return *this;
}
};
}// namespace Fast_O
namespace Fast_IO{
Fast_I::Fast_Istream fin(cin.rdbuf());
Fast_O::Fast_Ostream fout(cout.rdbuf());
}// namespace Fast_IO
#define fin Fast_IO::fin
#define fout Fast_IO::fout
#undef __CLOSE_SYNC
ull s,n,a;
signed main(){
#ifdef __CLOSE_SYNC
ios_base::sync_with_stdio(0);
#endif
#ifdef __USE_FREOPEN
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
fin.GetN(n);
while(n--){
fin>>a;
fout<<a<<' ';
s+=a;
}
fout<<'\n'<<s;
return 0;
}
测试
普通 cin,cout
| 测试编号 |
总用时 |
| 1 |
2.34s |
| 2 |
2.32s |
| 3 |
2.32s |
普通 scanf,printf
| 测试编号 |
总用时 |
| 1 |
446ms |
| 2 |
444ms |
| 3 |
447ms |
解绑 cin,cout
| 测试编号 |
总用时 |
| 1 |
1.69s |
| 2 |
1.67s |
| 3 |
1.69s |
cin.tie(0),cout.tie(0)
| 测试编号 |
总用时 |
| 1 |
754ms |
| 2 |
756ms |
| 3 |
750ms |
解绑+tie(0)
| 测试编号 |
总用时 |
| 1 |
371ms |
| 2 |
324ms |
| 3 |
326ms |
普通快读快写
| 测试编号 |
总用时 |
| 1 |
180ms |
| 2 |
183ms |
| 3 |
180ms |
优化的快读快写
| 测试编号 |
总用时 |
| 1 |
342ms |
| 2 |
343ms |
| 3 |
345ms |
fread,fwrite
| 测试编号 |
总用时 |
| 1 |
316ms |
| 2 |
315ms |
| 3 |
314ms |
同学做的IO
| 测试编号 |
总用时 |
| 1 |
130ms |
| 2 |
130ms |
| 3 |
128ms |
buf 读 string
| 测试编号 |
总用时 |
| 1 |
181ms |
| 2 |
180ms |
| 3 |
181ms |
mmap(洛谷上可用),快写为优化版
| 测试编号 |
总用时 |
| 1 |
309ms |
| 2 |
310ms |
| 3 |
310ms |
unlocked()(洛谷上可用),快写为优化版
| 测试编号 |
总用时 |
| 1 |
329ms |
| 2 |
333ms |
| 3 |
331ms |
同学声称超快的快读
| 测试编号 |
总用时 |
| 1 |
152ms |
| 2 |
156ms |
| 3 |
155ms |
结算
| Rank |
Name |
Time(ms) |
| 1 |
同学做的IO |
130 |
| 2 |
同学声称超快的快读 |
154 |
| 3 |
buf 读 string |
180.67 |
| 4 |
普通快读快写 |
181 |
| 5 |
mmap |
310 |
| 6 |
fread,fwrite |
315 |
| 7 |
unlocked() |
331 |
| 8 |
解绑+tie(0) |
340 |
| 9 |
优化的快读快写 |
343 |
| 10 |
普通 scanf,printf |
446 |
| 11 |
tie(0) |
753 |
| 12 |
解绑 cin,cout |
1680 |
| 13 |
普通 cin,cout |
2330 |
- 俩同学都nb,包揽前三名
- 解绑+
tie(0) 我首推,性价比高
- 快读快写优化之后慢了?
mmap 不稳定发挥,作为信仰必须重测
如果前 3 名是 A 档,再 5 名是 B 档,最后是 C 档:
| 档次 |
平均用时(ms) |
| A |
155 |
| B |
295 |
| C |
1112 |
这次测评就到这里了。(下面有隐藏字!)
\color{white}哼,我的信仰 mmap 居然这么靠后,必须重测!
\color{white}看来不得不出第2集了
\color{white}数据得造强一点,直接上 2\times 10^7
\color{white}哈哈哈,下次再见