ACM:读入优化
两个简单的读入优化
int getin(){
int ans=;char tmp;bool sign=;
while(!isdigit(tmp=getchar()) && tmp!='-');
if(tmp=='-')sign=,tmp=getchar();
do ans=(ans<<)+(ans<<)+tmp-'';
while(isdigit(tmp=getchar()));
return sign?-ans:ans;
}
inline int read(){
int p,data=;
char ch=;
while ((ch!='-') && ch<'' || ch>'') ch=getchar();
if (ch=='-')
{
p=-;
ch=getchar();
} else p=;
while (ch>='' && ch<='') data=data*+ch-'',ch=getchar();
return data*p;
}
大佬的读入优化
from:https://blog.csdn.net/x_iya/article/details/9003416
namespace fastIO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
#define ll long long
//fread->read
bool IOerror=;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if (p1==pend){
p1=buf; pend=buf+fread(buf,,BUF_SIZE,stdin);
if (pend==p1){IOerror=;return -;}
//{printf("IO error!\n");system("pause");for (;;);exit(0);}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){
bool sign=; char ch=nc(); x=;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=,ch=nc();
for (;ch>=''&&ch<='';ch=nc())x=x*+ch-'';
if (sign)x=-x;
}
inline void read(ll &x){
bool sign=; char ch=nc(); x=;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=,ch=nc();
for (;ch>=''&&ch<='';ch=nc())x=x*+ch-'';
if (sign)x=-x;
}
inline void read(double &x){
bool sign=; char ch=nc(); x=;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=,ch=nc();
for (;ch>=''&&ch<='';ch=nc())x=x*+ch-'';
if (ch=='.'){
double tmp=; ch=nc();
for (;ch>=''&&ch<='';ch=nc())tmp/=10.0,x+=tmp*(ch-'');
}
if (sign)x=-x;
}
inline void read(char *s){
char ch=nc();
for (;blank(ch);ch=nc());
if (IOerror)return;
for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
*s=;
}
inline void read(char &c){
for (c=nc();blank(c);c=nc());
if (IOerror){c=-;return;}
}
//getchar->read
inline void read1(int &x){
char ch;int bo=;x=;
for (ch=getchar();ch<''||ch>'';ch=getchar())if (ch=='-')bo=;
for (;ch>=''&&ch<='';x=x*+ch-'',ch=getchar());
if (bo)x=-x;
}
inline void read1(ll &x){
char ch;int bo=;x=;
for (ch=getchar();ch<''||ch>'';ch=getchar())if (ch=='-')bo=;
for (;ch>=''&&ch<='';x=x*+ch-'',ch=getchar());
if (bo)x=-x;
}
inline void read1(double &x){
char ch;int bo=;x=;
for (ch=getchar();ch<''||ch>'';ch=getchar())if (ch=='-')bo=;
for (;ch>=''&&ch<='';x=x*+ch-'',ch=getchar());
if (ch=='.'){
double tmp=;
for (ch=getchar();ch>=''&&ch<='';tmp/=10.0,x+=tmp*(ch-''),ch=getchar());
}
if (bo)x=-x;
}
inline void read1(char *s){
char ch=getchar();
for (;blank(ch);ch=getchar());
for (;!blank(ch);ch=getchar())*s++=ch;
*s=;
}
inline void read1(char &c){for (c=getchar();blank(c);c=getchar());}
//scanf->read
inline void read2(int &x){scanf("%d",&x);}
inline void read2(ll &x){
#ifdef _WIN32
scanf("%I64d",&x);
#else
#ifdef __linux
scanf("%lld",&x);
#else
puts("error:can't recognize the system!");
#endif
#endif
}
inline void read2(double &x){scanf("%lf",&x);}
inline void read2(char *s){scanf("%s",s);}
inline void read2(char &c){scanf(" %c",&c);}
inline void readln2(char *s){gets(s);}
//fwrite->write
struct Ostream_fwrite{
char *buf,*p1,*pend;
Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}
void out(char ch){
if (p1==pend){
fwrite(buf,,BUF_SIZE,stdout);p1=buf;
}
*p1++=ch;
}
void print(int x){
static char s[],*s1;s1=s;
if (!x)*s1++='';if (x<)out('-'),x=-x;
while(x)*s1++=x%+'',x/=;
while(s1--!=s)out(*s1);
}
void println(int x){
static char s[],*s1;s1=s;
if (!x)*s1++='';if (x<)out('-'),x=-x;
while(x)*s1++=x%+'',x/=;
while(s1--!=s)out(*s1); out('\n');
}
void print(ll x){
static char s[],*s1;s1=s;
if (!x)*s1++='';if (x<)out('-'),x=-x;
while(x)*s1++=x%+'',x/=;
while(s1--!=s)out(*s1);
}
void println(ll x){
static char s[],*s1;s1=s;
if (!x)*s1++='';if (x<)out('-'),x=-x;
while(x)*s1++=x%+'',x/=;
while(s1--!=s)out(*s1); out('\n');
}
void print(double x,int y){
static ll mul[]={,,,,,,,,,
,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL};
if (x<-1e-)out('-'),x=-x;x*=mul[y];
ll x1=(ll)floor(x); if (x-floor(x)>=0.5)++x1;
ll x2=x1/mul[y],x3=x1-x2*mul[y]; print(x2);
if (y>){out('.'); for (size_t i=;i<y&&x3*mul[i]<mul[y];out(''),++i); print(x3);}
}
void println(double x,int y){print(x,y);out('\n');}
void print(char *s){while (*s)out(*s++);}
void println(char *s){while (*s)out(*s++);out('\n');}
void flush(){if (p1!=buf){fwrite(buf,,p1-buf,stdout);p1=buf;}}
~Ostream_fwrite(){flush();}
}Ostream;
inline void print(int x){Ostream.print(x);}
inline void println(int x){Ostream.println(x);}
inline void print(char x){Ostream.out(x);}
inline void println(char x){Ostream.out(x);Ostream.out('\n');}
inline void print(ll x){Ostream.print(x);}
inline void println(ll x){Ostream.println(x);}
inline void print(double x,int y){Ostream.print(x,y);}
inline void println(double x,int y){Ostream.println(x,y);}
inline void print(char *s){Ostream.print(s);}
inline void println(char *s){Ostream.println(s);}
inline void println(){Ostream.out('\n');}
inline void flush(){Ostream.flush();}
//puts->write
char Out[OUT_SIZE],*o=Out;
inline void print1(int x){
static char buf[];
char *p1=buf;if (!x)*p1++='';if (x<)*o++='-',x=-x;
while(x)*p1++=x%+'',x/=;
while(p1--!=buf)*o++=*p1;
}
inline void println1(int x){print1(x);*o++='\n';}
inline void print1(ll x){
static char buf[];
char *p1=buf;if (!x)*p1++='';if (x<)*o++='-',x=-x;
while(x)*p1++=x%+'',x/=;
while(p1--!=buf)*o++=*p1;
}
inline void println1(ll x){print1(x);*o++='\n';}
inline void print1(char c){*o++=c;}
inline void println1(char c){*o++=c;*o++='\n';}
inline void print1(char *s){while (*s)*o++=*s++;}
inline void println1(char *s){print1(s);*o++='\n';}
inline void println1(){*o++='\n';}
inline void flush1(){if (o!=Out){if (*(o-)=='\n')*--o=;puts(Out);}}
struct puts_write{
~puts_write(){flush1();}
}_puts;
inline void print2(int x){printf("%d",x);}
inline void println2(int x){printf("%d\n",x);}
inline void print2(char x){printf("%c",x);}
inline void println2(char x){printf("%c\n",x);}
inline void print2(ll x){
#ifdef _WIN32
printf("%I64d",x);
#else
#ifdef __linux
printf("%lld",x);
#else
puts("error:can't recognize the system!");
#endif
#endif
}
inline void println2(ll x){print2(x);printf("\n");}
inline void println2(){printf("\n");}
#undef ll
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace fastIO;
ACM:读入优化的更多相关文章
- 【ACM非算法部分】读入优化
今天做了ACdream的比赛才知道原来还有读入优化这一说.Orz 读入一个整数的时候这么写: int a; scanf("%d",&a); 和 int a; char c; ...
- fread读入优化,寻找速度极限
序: 在之前的测试中,我们比较了四种读入方式,发现使用读入优化是最快的选择,但是我们知道fread()是比它更快的方法.这一次,我们对比四种读入优化,探寻C++读取速度的极限. 分别是getchar( ...
- OI黑科技:读入优化
利用getchar()函数加速读入. Q:读入优化是什么? A :更加快速地读入一些较大的数字. Q:scanf不是已经够快了吗? A:Naive,scanf还是不!够!快! Q:那怎么办呢? A:我 ...
- c++读入优化
对于输入数据非常大的一些可(变)爱(态)题目,scanf就会大大拖慢程序的运行速度,cin就更不用说了,所以我们要用一种高大上的东西——读入优化. 读入优化的原理其实就是一个一个字符的读入,再组成数字 ...
- c++ 读入优化、输出优化模板
0. 在有些输入数据很多的变态题中,scanf会大大拖慢程序的时间,cin就更慢了,所以就出现了读入优化.其原理就是一个一个字符的读入,输出优化同理,主要使用getchar,putchar函数. 1. ...
- 读入优化&输出优化
读入优化 int read() { ; ') ; '; ') num=num*+c-'; return ff*num; } 输出优化 void write(int x) { ) { putchar(' ...
- C++ 读入优化&输出优化
读入优化:读入优化只是针对整数,由于getchar()读字符非常的快,所以采用getchar()来进行读入,下设输入的数为x 负数处理:用一个标志变量f,开始时为1,当读入了'-'时,f变为-1,最后 ...
- HDU 5972 Regular Number(ShiftAnd+读入优化)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5972 [题目大意] 给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串 ...
- 【CSP模拟赛】天才绅士少女助手克里斯蒂娜(线段树&读入优化&输出优化)
题面描述 红莉栖想要弄清楚楼下天王寺大叔的显像管电视对“电话微波炉(暂定)”的影响.选取显像管的任意一个平面,一开始平面内有个n电子,初始速度分别为vi,定义飘升系数为 $$\sum_{1\leqsl ...
随机推荐
- Eclipse中Maven Install时发生错误
问题描述 要把一个本地包保存进本地maven库中, 所以对该project执行了run as => Maven Install, 结果报下面的错误. 解决办法 1. 通过命令窗口手动创建这两个文 ...
- Python 可视化工具 Matplotlib
英文出处:Chris Moffitt. Matplotlib是Python中最常用的可视化工具之一,可以非常方便地创建海量类型的2D图表和一些基本的3D图表.本文主要介绍了在学习Matplotlib时 ...
- java基础小项目练习之1----3天做出飞机大战
Shoot射击游戏第一天一. 关键问题(理论):1. 简述FlyingObject.Enemy.Award.Airplane.Bee.Bullet.Hero之间的继承与实现关系2. 简述Hero类构造 ...
- 使用Freemarker 实现JSP页面的静态化
使用Freemarker 静态化网页 一.原理 Freemarker 生成静态页面,首先需要使用自己定义的模板页面,这个模板页面可以是最最普通的html,也可以是嵌套freemarker中的 取值表达 ...
- (5)STM32使用HAL库实现串口通讯——实战操作
功能需求: (1)对接收的字符串原样返回(每10个字符一次). (2)发送一个字符串完成后改变LED的状态. 1.创建工程 使用的是F407Discovery,4个LED对应PD12-PD14. (1 ...
- redis与CPU、内存
任何一个后端应用,包括代码都要考虑对于CPU和内存的影响.redis本质上类似于nodejs,单进程.单线程,事件驱动,但不同的是redis是CPU密集型的.这里列出了redis与内存CPU的相关考虑 ...
- 浅析JavaScript解析赋值、浅拷贝和深拷贝的区别
文章首发于sau交流学习社区 一.赋值(Copy) 赋值是将某一数值或对象赋给某个变量的过程,分为: 1.基本数据类型:赋值,赋值之后两个变量互不影响 2.引用数据类型:赋**址**,两个变量具有相同 ...
- 【原】js数组对象去重最简单的方法
简单的数组去重是比较简单的,方法也特别多,如给下面的数组去重: let arr = [1,2,2,4,9,6,7,5,2,3,5,6,5] 最常用的可以用for循环套for循环,再用splice删除重 ...
- .NET Core IdentityServer4实战-开篇介绍与规划
一.开篇寄语 由于假期的无聊,我决定了一个非常有挑战性的活动,也就是在年假给大家带来一个基于OAuth 2.0的身份授权框架,它就是 IdentityServer4 ,如果没有意外的话,一定可以顺利的 ...
- 简述private,protected,public,internal修饰符的访问权限
private:私有成员,在类的内部才可以访问 protected:保护成员,在类的内部和继承类中可以访问 public:公共成员,完全公开,没有访问限制 internal:当前程序集内可以访问