Skip to content

Instantly share code, notes, and snippets.

@kiwiyou
Last active October 15, 2023 07:34
Show Gist options
  • Save kiwiyou/3035fd463c5779b7a12a714333657584 to your computer and use it in GitHub Desktop.
Save kiwiyou/3035fd463c5779b7a12a714333657584 to your computer and use it in GitHub Desktop.
Simple IO
use std::io::*;
fn solve() {
let mut reader = Reader::new();
let t = reader.read();
for _ in 0..t {
let a: i32 = reader.read();
let b: i32 = reader.read();
println!("{}", a + b);
}
}
fn main() {
solve();
STDOUT.with(|cell| cell.borrow_mut().flush().ok());
}
struct Reader<'a> {
buf: Vec<u8>,
off: usize,
len: usize,
stdin: StdinLock<'a>,
}
const BUFFER_SIZE: usize = 1 << 17;
impl Reader<'_> {
fn new() -> Self {
Self {
buf: vec![0; BUFFER_SIZE],
off: BUFFER_SIZE,
len: BUFFER_SIZE,
stdin: stdin().lock(),
}
}
fn skip_whitespace(&mut self) {
while self.len > 0 {
if let Some(i) = self.buf[self.off..self.len].iter().position(|&b| b > b' ') {
self.off += i;
break;
}
self.fill();
}
}
fn remain(&self) -> &[u8] {
&self.buf[self.off..self.len]
}
#[cold]
fn fill(&mut self) {
let len = self.stdin.read(&mut self.buf).unwrap();
self.len = len;
self.off = 0;
}
fn read<T: Readable>(&mut self) -> T {
self.skip_whitespace();
T::from_reader(self)
}
}
trait Readable {
fn from_reader(r: &mut Reader) -> Self;
}
impl Readable for String {
fn from_reader(r: &mut Reader) -> Self {
let mut s = Self::new();
while r.len > 0 {
let remain = r.remain();
if let Some(i) = remain.iter().position(|&b| b <= b' ') {
unsafe { s.as_mut_vec() }.extend_from_slice(&remain[..i]);
r.off += i;
break;
} else {
unsafe { s.as_mut_vec() }.extend_from_slice(remain);
}
r.fill();
}
s
}
}
struct Line(String);
impl Readable for Line {
fn from_reader(r: &mut Reader) -> Self {
let mut s = String::new();
while r.len > 0 {
let remain = r.remain();
if let Some(i) = remain.iter().position(|&b| b < b' ') {
unsafe { s.as_mut_vec() }.extend_from_slice(&remain[..i]);
r.off += i;
break;
} else {
unsafe { s.as_mut_vec() }.extend_from_slice(remain);
}
r.fill();
}
Line(s)
}
}
macro_rules! readable_signed {
($($t:ty)+) => {
$(
impl Readable for $t {
fn from_reader(r: &mut Reader) -> Self {
let mut neg = false;
let mut x = 0u64;
'next_char: while r.len > 0 {
for (i, &b) in r.remain().iter().enumerate() {
if b == b'-' {
neg = true;
} else if b >= b'0' && b <= b'9' {
x = x * 10 + b as u64 - 48;
} else {
r.off += i;
break 'next_char;
}
}
r.fill();
}
if neg {
x.wrapping_neg() as $t
} else {
x as $t
}
}
}
)+
};
}
macro_rules! readable_unsigned {
($($t:ty)+) => {
$(
impl Readable for $t {
fn from_reader(r: &mut Reader) -> Self {
let mut x = 0;
'next_char: while r.len > 0 {
for (i, &b) in r.remain().iter().enumerate() {
if b >= b'0' && b <= b'9' {
x = x * 10 + b as $t - 48;
} else {
r.off += i;
break 'next_char;
}
}
r.fill();
}
x
}
}
)+
};
}
readable_signed!(i8 i16 i32 i64 isize);
readable_unsigned!(u8 u16 u32 u64 usize);
thread_local! {
static STDOUT: std::cell::RefCell<BufWriter<StdoutLock<'static>>> = std::cell::RefCell::new(std::io::BufWriter::with_capacity(1 << 17, stdout().lock()));
}
#[macro_export]
macro_rules! println {
($($t:tt)*) => {
STDOUT.with(|cell| writeln!(cell.borrow_mut(), $($t)*).unwrap());
};
}
#[macro_export]
macro_rules! print {
($($t:tt)*) => {
STDOUT.with(|cell| write!(cell.borrow_mut(), $($t)*).unwrap());
};
}
#[macro_export]
macro_rules! flush {
() => {
STDOUT.with(|cell| cell.borrow_mut().flush().unwrap());
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment