版本比较
ka chang
正文
增加 162 行删除 120 行
66 行未改动676869707172717475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191727374752 行未改动7879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324466 行未改动#pragma GCC optimize("-fdelete-null-pointer-checks")#pragma GCC optimize(2)
7.#include <cstdio>#include <cctype>
// 快读 long longinline long long readll() { static const int BUFFER_SIZE = 1 << 16; // 64KB缓冲区 static char buffer[BUFFER_SIZE]; static int pos = 0, len = 0; // 循环保护计数器 int loop_cnt = 0; const int MAX_LOOP = 1000000; // 最大循环次数,防止无限循环 // 确保缓冲区有数据 while (pos >= len) { if (++loop_cnt > MAX_LOOP) return 0; // 循环超限,强制退出 len = fread(buffer, 1, BUFFER_SIZE, stdin); pos = 0; if (len == 0) return EOF; // 输入结束 } // 跳过空白字符 char ch = buffer[pos++]; loop_cnt = 0; while (isspace(ch)) { if (++loop_cnt > MAX_LOOP) return 0; if (pos >= len) { len = fread(buffer, 1, BUFFER_SIZE, stdin); pos = 0; if (len == 0) return EOF; } ch = buffer[pos++]; } // 处理符号 bool negative = false; if (ch == '-') { negative = true; if (pos >= len) { len = fread(buffer, 1, BUFFER_SIZE, stdin); pos = 0; if (len == 0) return 0; // 只有负号,返回0 } ch = buffer[pos++]; } // 读取数字(long long最多19位) unsigned long long result = 0; // 用unsigned避免溢出判断 loop_cnt = 0; while (isdigit(ch)) { if (++loop_cnt > MAX_LOOP) return 0; result = result * 10 + (ch - '0'); if (pos >= len) { len = fread(buffer, 1, BUFFER_SIZE, stdin); pos = 0; if (len == 0) break; // 输入结束,退出循环 } ch = buffer[pos++]; } // 处理负数 if (negative) { return -((long long)result); } return (long long)result;}
// 快写 long longinline void writell(long long x) { if (x == 0) { putchar('0'); return; } // 处理-9223372036854775808特殊情况(直接输出,避免取负溢出) if (x == -9223372036854775808LL) { fputs("-9223372036854775808", stdout); return; } // 处理负数 if (x < 0) { putchar('-'); x = -x; } // 用数组存储数字(long long最多19位) char digits[20]; int top = 0; // 提取数字(逆序) while (x > 0) { digits[top++] = x % 10 + '0'; x /= 10; } // 逆序输出 while (top > 0) { putchar(digits[--top]); }}
// 使用示例int main() { // 读取n个long long,计算总和并输出 long long n = readll(); if (n == EOF) return 0; // 处理空输入 long long sum = 0; for (long long i = 0; i < n; i++) { long long num = readll(); if (num == EOF) break; // 输入不足时退出 sum += num; } writell(sum); putchar('\n'); return 0;}
char buf[1 << 23], *p1, *p2, c;#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 22, stdin), p1 == p2) ? EOF : *p1++)
inline void read(int &x) {2 行未改动 do { x = (x << 3) + (x << 1) + (c ^ '0'); } while (isdigit(c = gc));}
8.namespace fast_IO {#define IOSIZE 1000000 char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)#define isdigit(ch) (ch>47&&ch<58)#define isspace(ch) (ch<33) template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; } template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; } template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); } inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; } inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; } inline void print(char x) { putchar(x); } inline void print(char *x) { while (*x) putchar(*x++); } inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); } inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; } inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); } inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; } inline void print(bool b) { putchar(b+48); } template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); } template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); } struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io; template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; } template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }#define cout io#define cin io#define endl '\n'} using namespace fast_IO;
8.
namespace fast_IO {#define IOSIZE 1000000 char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)#define isdigit(ch) (ch>47&&ch<58)#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; } template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; } // 核心优化:递归改迭代,消除栈开销和调用开销 template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); } // 针对int的特化:用do-while+数组反转,比递归快30-50% template<> inline void print(int x) { if (x < 0) putchar('-'), x = -x; static char buf[16]; int n = 0; do { buf[n++] = x % 10 + 48; x /= 10; } while (x); while (n--) putchar(buf[n]); } // 针对long long的特化 template<> inline void print(long long x) { if (x < 0) putchar('-'), x = -x; static char buf[24]; int n = 0; do { buf[n++] = x % 10 + 48; x /= 10; } while (x); while (n--) putchar(buf[n]); } inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; } inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; } inline void print(char x) { putchar(x); } inline void print(char *x) { while (*x) putchar(*x++); } inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); } inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; } inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); } inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; } inline void print(bool b) { putchar(b+48); } template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); } template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); } struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io; template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; } template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; } #define cout io#define cin io#define endl '\n'} using namespace fast_IO;```