P10815 【模板】快速读入 题解

· · 题解

温馨提示:在专栏食用效果更佳。

若用 scanf 读入,会超时。可以写一个快读。

#include <cstdio>
#include <cctype>
inline long long read() {
    register long long ret=0,w=1;
    register char c=getchar_unlocked();
    while(!isdigit(c)) { // 跳过非数字字符,并检查是否有负号。
        if(c=='-') w=-1; // 如果遇到负号,则将权重 w 设为 -1。
        c=getchar_unlocked();
    } while(isdigit(c)) { // 处理数字字符。
        ret=(ret<<1)+(ret<<3)+(c^48); // 等价于 ret=ret*2+ret*8+c-48,用位运算能快些。
        c=getchar_unlocked();
    } return ret*w;
}