Skip to content

Instantly share code, notes, and snippets.

@jdabtieu
Last active August 8, 2021 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdabtieu/3069b7b971e247642bf34d109aa7825d to your computer and use it in GitHub Desktop.
Save jdabtieu/3069b7b971e247642bf34d109aa7825d to your computer and use it in GitHub Desktop.
C++ Fast IO (read & print signed & unsigned ints)
// IMPORTANT: Call Write(); at the end of your main function to flush the output buffer
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC optimize ("Ofast")
int oPtr = 0;
#ifdef WIN64
char sc() {return getchar();}
void pc(char c) {putchar(c);}
void Write() {return;}
#else
#include <sys/syscall.h>
#define BUF_SIZE 61846
int iPtr = 0;
int maxPtr = 0;
char ibuf[BUF_SIZE];
char obuf[BUF_SIZE];
void Read() {maxPtr = syscall(SYS_read, 0, &ibuf, BUF_SIZE);iPtr = 0;}
void Write() {syscall(SYS_write, 1, &obuf, oPtr);oPtr = 0;}
char sc() {if (iPtr >= maxPtr) Read();return ibuf[iPtr++];}
void pc(char c) {if (oPtr >= BUF_SIZE) Write();obuf[oPtr++] = c;}
#endif // WIN64
void _pi(int n) {if (n == 0) return;_pi(n / 10);pc((char) (n % 10 + '0'));}
void pi(int num) {if (num < 0) {pc('-');_pi(-num);} else if (num == 0) {pc('0');} else {_pi(num);}}
int si() {int x = 0;char c;while ((c = sc()) <= ' ');int neg = 0;if (c == '-') {neg = 1;c = sc();}do {x = x * 10 + (c - '0');} while ((c = sc()) > ' ');return neg ? -x : x;}
unsigned int su() {int x = 0;char c;while ((c = sc()) <= ' ');do {x = x * 10 + (c - '0');} while ((c = sc()) > ' ');return x;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment