正文
增加 162 行删除 120 行
66 行未改动6768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941952 行未改动19819920020120266 行未改动#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));}```66 行未改动6768697071727374752 行未改动7879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324466 行未改动#pragma GCC optimize("-fdelete-null-pointer-checks")#pragma GCC optimize(2)7.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 1000000char 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 1000000char 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;```