QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#331063 | #8056. Travel 2 | ucup-team296# | AC ✓ | 180ms | 2904kb | Rust | 23.2kb | 2024-02-17 23:08:42 | 2024-02-17 23:08:43 |
Judging History
answer
//
pub mod solution {
//{"name":"uc23_j","group":"Manual","url":"","interactive":true,"timeLimit":2000,"tests":[{"input":"","output":""}],"testType":"single","input":{"type":"stdin","fileName":null,"pattern":null},"output":{"type":"stdout","fileName":null,"pattern":null},"languages":{"java":{"taskClass":"uc23_j"}}}
use crate::algo_lib::collections::default_map::default_hash_map::DefaultHashMap;
use crate::algo_lib::collections::iter_ext::collect::IterCollect;
use crate::algo_lib::collections::slice_ext::indices::Indices;
use crate::algo_lib::io::input::Input;
use crate::algo_lib::io::output::Output;
use crate::algo_lib::string::str::StrReader;
use std::collections::HashSet;
type PreCalc = ();
fn solve(input: &mut Input, out: &mut Output, _test_case: usize, _data: &PreCalc) {
let mut edges = HashSet::new();
let mut next = DefaultHashMap::<_, usize>::new();
let mut last = None;
let mut last_id = None;
let mut tree = DefaultHashMap::<_, Vec<_>>::new();
let mut parent = DefaultHashMap::new();
let mut edge_to_parent = DefaultHashMap::new();
loop {
let x = input.read_size();
let dx = input.read_size();
if next[x] == 0 {
tree[x] = vec![0; dx];
parent[x] = last.unwrap_or(0);
}
if let Some(last) = last {
if parent[last] == x {
edge_to_parent[last] = last_id.unwrap();
}
if next[x] == 0 {
tree[last][last_id.unwrap()] = x;
}
if !edges.contains(&(last, x)) && !edges.contains(&(x, last)) {
edges.insert((last, x));
}
}
last = Some(x);
// eprintln!("{:#?}", tree.iter().collect_vec());
if next[x] == dx {
let mut go = false;
for i in tree[x].indices() {
if tree[x][i] != 0 {
tree[x][i] = 0;
out.print_line(('>', i + 1));
go = true;
break;
}
}
if go {
continue;
}
if x == 1 {
out.print("! ");
out.print_iter(edges.into_iter());
out.print_line(());
if input.read_str() == b"Correct".into() {
return;
}
panic!("Incorrect");
}
out.print_line(('>', edge_to_parent[x] + 1));
continue;
}
last_id = Some(next[x]);
next[x] += 1;
out.print_line(('>', next[x]));
}
}
pub(crate) fn run(mut input: Input, mut output: Output) -> bool {
let pre_calc = ();
#[allow(dead_code)]
enum TestType {
Single,
MultiNumber,
MultiEof,
}
let test_type = TestType::MultiNumber;
match test_type {
TestType::Single => solve(&mut input, &mut output, 1, &pre_calc),
TestType::MultiNumber => {
let t = input.read();
for i in 1..=t {
solve(&mut input, &mut output, i, &pre_calc);
}
}
TestType::MultiEof => {
let mut i = 1;
while input.peek().is_some() {
solve(&mut input, &mut output, i, &pre_calc);
i += 1;
}
}
}
output.flush();
// input.skip_whitespace();
// input.peek().is_none()
true
}
}
pub mod algo_lib {
pub mod collections {
pub mod default_map {
pub mod default_hash_map {
use std::collections::HashMap;
use std::hash::Hash;
use std::iter::FromIterator;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ops::Index;
use std::ops::IndexMut;
#[derive(Default, Clone, Eq, PartialEq)]
pub struct DefaultHashMap<K: Hash + Eq, V>(HashMap<K, V>, V);
impl<K: Hash + Eq, V> Deref for DefaultHashMap<K, V> {
type Target = HashMap<K, V>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<K: Hash + Eq, V> DerefMut for DefaultHashMap<K, V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<K: Hash + Eq, V: Default> DefaultHashMap<K, V> {
pub fn new() -> Self {
Self(HashMap::new(), V::default())
}
pub fn with_capacity(cap: usize) -> Self {
Self(HashMap::with_capacity(cap), V::default())
}
pub fn get(&self, key: &K) -> &V {
self.0.get(key).unwrap_or(&self.1)
}
pub fn get_mut(&mut self, key: K) -> &mut V {
self.0.entry(key).or_insert_with(|| V::default())
}
pub fn into_values(self) -> std::collections::hash_map::IntoValues<K, V> {
self.0.into_values()
}
}
impl<K: Hash + Eq, V: Default> Index<K> for DefaultHashMap<K, V> {
type Output = V;
fn index(&self, index: K) -> &Self::Output {
self.get(&index)
}
}
impl<K: Hash + Eq, V: Default> IndexMut<K> for DefaultHashMap<K, V> {
fn index_mut(&mut self, index: K) -> &mut Self::Output {
self.get_mut(index)
}
}
impl<K: Hash + Eq, V> IntoIterator for DefaultHashMap<K, V> {
type Item = (K, V);
type IntoIter = std::collections::hash_map::IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<K: Hash + Eq, V: Default> FromIterator<(K, V)> for DefaultHashMap<K, V> {
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
Self(iter.into_iter().collect(), V::default())
}
}
}
}
pub mod iter_ext {
pub mod collect {
pub trait IterCollect<T>: Iterator<Item = T> + Sized {
fn collect_vec(self) -> Vec<T> {
self.collect()
}
}
impl<T, I: Iterator<Item = T> + Sized> IterCollect<T> for I {}
}
}
pub mod slice_ext {
pub mod indices {
use std::ops::Range;
pub trait Indices {
fn indices(&self) -> Range<usize>;
}
impl<T> Indices for [T] {
fn indices(&self) -> Range<usize> {
0..self.len()
}
}
}
}
pub mod vec_ext {
pub mod default {
pub fn default_vec<T: Default>(len: usize) -> Vec<T> {
let mut v = Vec::with_capacity(len);
for _ in 0..len {
v.push(T::default());
}
v
}
}
}
}
pub mod io {
pub mod input {
use crate::algo_lib::collections::vec_ext::default::default_vec;
use std::io::Read;
pub struct Input<'s> {
input: &'s mut dyn Read,
buf: Vec<u8>,
at: usize,
buf_read: usize,
}
macro_rules! read_impl {
($t: ty, $read_name: ident, $read_vec_name: ident) => {
pub fn $read_name(&mut self) -> $t {
self.read()
}
pub fn $read_vec_name(&mut self, len: usize) -> Vec<$t> {
self.read_vec(len)
}
};
($t: ty, $read_name: ident, $read_vec_name: ident, $read_pair_vec_name: ident) => {
read_impl!($t, $read_name, $read_vec_name);
pub fn $read_pair_vec_name(&mut self, len: usize) -> Vec<($t, $t)> {
self.read_vec(len)
}
};
}
impl<'s> Input<'s> {
const DEFAULT_BUF_SIZE: usize = 4096;
pub fn new(input: &'s mut dyn Read) -> Self {
Self {
input,
buf: default_vec(Self::DEFAULT_BUF_SIZE),
at: 0,
buf_read: 0,
}
}
pub fn new_with_size(input: &'s mut dyn Read, buf_size: usize) -> Self {
Self {
input,
buf: default_vec(buf_size),
at: 0,
buf_read: 0,
}
}
pub fn get(&mut self) -> Option<u8> {
if self.refill_buffer() {
let res = self.buf[self.at];
self.at += 1;
if res == b'\r' {
if self.refill_buffer() && self.buf[self.at] == b'\n' {
self.at += 1;
}
return Some(b'\n');
}
Some(res)
} else {
None
}
}
pub fn peek(&mut self) -> Option<u8> {
if self.refill_buffer() {
let res = self.buf[self.at];
Some(if res == b'\r' { b'\n' } else { res })
} else {
None
}
}
pub fn skip_whitespace(&mut self) {
while let Some(b) = self.peek() {
if !char::from(b).is_whitespace() {
return;
}
self.get();
}
}
pub fn next_token(&mut self) -> Option<Vec<u8>> {
self.skip_whitespace();
let mut res = Vec::new();
while let Some(c) = self.get() {
if char::from(c).is_whitespace() {
break;
}
res.push(c);
}
if res.is_empty() {
None
} else {
Some(res)
}
}
//noinspection RsSelfConvention
pub fn is_exhausted(&mut self) -> bool {
self.peek().is_none()
}
//noinspection RsSelfConvention
pub fn is_empty(&mut self) -> bool {
self.skip_whitespace();
self.is_exhausted()
}
pub fn read<T: Readable>(&mut self) -> T {
T::read(self)
}
pub fn read_vec<T: Readable>(&mut self, size: usize) -> Vec<T> {
let mut res = Vec::with_capacity(size);
for _ in 0..size {
res.push(self.read());
}
res
}
pub fn read_char(&mut self) -> char {
self.skip_whitespace();
self.get().unwrap().into()
}
read_impl!(u32, read_unsigned, read_unsigned_vec);
read_impl!(u64, read_u64, read_u64_vec);
read_impl!(usize, read_size, read_size_vec, read_size_pair_vec);
read_impl!(i32, read_int, read_int_vec, read_int_pair_vec);
read_impl!(i64, read_long, read_long_vec, read_long_pair_vec);
read_impl!(i128, read_i128, read_i128_vec);
fn refill_buffer(&mut self) -> bool {
if self.at == self.buf_read {
self.at = 0;
self.buf_read = self.input.read(&mut self.buf).unwrap();
self.buf_read != 0
} else {
true
}
}
}
pub trait Readable {
fn read(input: &mut Input) -> Self;
}
impl Readable for char {
fn read(input: &mut Input) -> Self {
input.read_char()
}
}
impl<T: Readable> Readable for Vec<T> {
fn read(input: &mut Input) -> Self {
let size = input.read();
input.read_vec(size)
}
}
macro_rules! read_integer {
($($t:ident)+) => {$(
impl Readable for $t {
fn read(input: &mut Input) -> Self {
input.skip_whitespace();
let mut c = input.get().unwrap();
let sgn = match c {
b'-' => {
c = input.get().unwrap();
true
}
b'+' => {
c = input.get().unwrap();
false
}
_ => false,
};
let mut res = 0;
loop {
assert!(c.is_ascii_digit());
res *= 10;
let d = (c - b'0') as $t;
if sgn {
res -= d;
} else {
res += d;
}
match input.get() {
None => break,
Some(ch) => {
if ch.is_ascii_whitespace() {
break;
} else {
c = ch;
}
}
}
}
res
}
}
)+};
}
read_integer!(i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize);
macro_rules! tuple_readable {
($($name:ident)+) => {
impl<$($name: Readable), +> Readable for ($($name,)+) {
fn read(input: &mut Input) -> Self {
($($name::read(input),)+)
}
}
}
}
tuple_readable! {T}
tuple_readable! {T U}
tuple_readable! {T U V}
tuple_readable! {T U V X}
tuple_readable! {T U V X Y}
tuple_readable! {T U V X Y Z}
tuple_readable! {T U V X Y Z A}
tuple_readable! {T U V X Y Z A B}
tuple_readable! {T U V X Y Z A B C}
tuple_readable! {T U V X Y Z A B C D}
tuple_readable! {T U V X Y Z A B C D E}
tuple_readable! {T U V X Y Z A B C D E F}
impl Read for Input<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if self.at == self.buf_read {
self.input.read(buf)
} else {
let mut i = 0;
while i < buf.len() && self.at < self.buf_read {
buf[i] = self.buf[self.at];
i += 1;
self.at += 1;
}
Ok(i)
}
}
}
}
pub mod output {
use crate::algo_lib::collections::vec_ext::default::default_vec;
use std::io::stderr;
use std::io::Stderr;
use std::io::Write;
#[derive(Copy, Clone)]
pub enum BoolOutput {
YesNo,
YesNoCaps,
PossibleImpossible,
Custom(&'static str, &'static str),
}
impl BoolOutput {
pub fn output(&self, output: &mut Output, val: bool) {
(if val { self.yes() } else { self.no() }).write(output);
}
fn yes(&self) -> &str {
match self {
BoolOutput::YesNo => "Yes",
BoolOutput::YesNoCaps => "YES",
BoolOutput::PossibleImpossible => "Possible",
BoolOutput::Custom(yes, _) => yes,
}
}
fn no(&self) -> &str {
match self {
BoolOutput::YesNo => "No",
BoolOutput::YesNoCaps => "NO",
BoolOutput::PossibleImpossible => "Impossible",
BoolOutput::Custom(_, no) => no,
}
}
}
pub struct Output<'s> {
output: &'s mut dyn Write,
buf: Vec<u8>,
at: usize,
auto_flush: bool,
bool_output: BoolOutput,
}
impl<'s> Output<'s> {
const DEFAULT_BUF_SIZE: usize = 4096;
pub fn new(output: &'s mut dyn Write) -> Self {
Self {
output,
buf: default_vec(Self::DEFAULT_BUF_SIZE),
at: 0,
auto_flush: false,
bool_output: BoolOutput::YesNoCaps,
}
}
pub fn new_with_auto_flush(output: &'s mut dyn Write) -> Self {
Self {
output,
buf: default_vec(Self::DEFAULT_BUF_SIZE),
at: 0,
auto_flush: true,
bool_output: BoolOutput::YesNoCaps,
}
}
pub fn flush(&mut self) {
if self.at != 0 {
self.output.write_all(&self.buf[..self.at]).unwrap();
self.output.flush().unwrap();
self.at = 0;
}
}
pub fn print<T: Writable>(&mut self, s: T) {
s.write(self);
self.maybe_flush();
}
pub fn print_line<T: Writable>(&mut self, s: T) {
self.print(s);
self.put(b'\n');
self.maybe_flush();
}
pub fn put(&mut self, b: u8) {
self.buf[self.at] = b;
self.at += 1;
if self.at == self.buf.len() {
self.flush();
}
}
pub fn maybe_flush(&mut self) {
if self.auto_flush {
self.flush();
}
}
pub fn print_per_line<T: Writable>(&mut self, arg: &[T]) {
for i in arg {
i.write(self);
self.put(b'\n');
}
}
pub fn print_iter<T: Writable, I: Iterator<Item = T>>(&mut self, iter: I) {
let mut first = true;
for e in iter {
if first {
first = false;
} else {
self.put(b' ');
}
e.write(self);
}
}
pub fn print_iter_ref<'a, T: 'a + Writable, I: Iterator<Item = &'a T>>(&mut self, iter: I) {
let mut first = true;
for e in iter {
if first {
first = false;
} else {
self.put(b' ');
}
e.write(self);
}
}
pub fn set_bool_output(&mut self, bool_output: BoolOutput) {
self.bool_output = bool_output;
}
}
impl Write for Output<'_> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let mut start = 0usize;
let mut rem = buf.len();
while rem > 0 {
let len = (self.buf.len() - self.at).min(rem);
self.buf[self.at..self.at + len].copy_from_slice(&buf[start..start + len]);
self.at += len;
if self.at == self.buf.len() {
self.flush();
}
start += len;
rem -= len;
}
self.maybe_flush();
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
self.flush();
Ok(())
}
}
pub trait Writable {
fn write(&self, output: &mut Output);
}
impl Writable for &str {
fn write(&self, output: &mut Output) {
output.write_all(self.as_bytes()).unwrap();
}
}
impl Writable for String {
fn write(&self, output: &mut Output) {
output.write_all(self.as_bytes()).unwrap();
}
}
impl Writable for char {
fn write(&self, output: &mut Output) {
output.put(*self as u8);
}
}
impl<T: Writable> Writable for [T] {
fn write(&self, output: &mut Output) {
output.print_iter_ref(self.iter());
}
}
impl<T: Writable, const N: usize> Writable for [T; N] {
fn write(&self, output: &mut Output) {
output.print_iter_ref(self.iter());
}
}
impl<T: Writable> Writable for &T {
fn write(&self, output: &mut Output) {
T::write(self, output)
}
}
impl<T: Writable> Writable for Vec<T> {
fn write(&self, output: &mut Output) {
self.as_slice().write(output);
}
}
impl Writable for () {
fn write(&self, _output: &mut Output) {}
}
macro_rules! write_to_string {
($($t:ident)+) => {$(
impl Writable for $t {
fn write(&self, output: &mut Output) {
self.to_string().write(output);
}
}
)+};
}
write_to_string!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
macro_rules! tuple_writable {
($name0:ident $($name:ident: $id:tt )*) => {
impl<$name0: Writable, $($name: Writable,)*> Writable for ($name0, $($name,)*) {
fn write(&self, out: &mut Output) {
self.0.write(out);
$(
out.put(b' ');
self.$id.write(out);
)*
}
}
}
}
tuple_writable! {T}
tuple_writable! {T U:1}
tuple_writable! {T U:1 V:2}
tuple_writable! {T U:1 V:2 X:3}
tuple_writable! {T U:1 V:2 X:3 Y:4}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6 B:7}
impl<T: Writable> Writable for Option<T> {
fn write(&self, output: &mut Output) {
match self {
None => (-1).write(output),
Some(t) => t.write(output),
}
}
}
impl Writable for bool {
fn write(&self, output: &mut Output) {
let bool_output = output.bool_output;
bool_output.output(output, *self)
}
}
static mut ERR: Option<Stderr> = None;
pub fn err() -> Output<'static> {
unsafe {
if ERR.is_none() {
ERR = Some(stderr());
}
Output::new_with_auto_flush(ERR.as_mut().unwrap())
}
}
}
}
pub mod string {
pub mod str {
use crate::algo_lib::collections::iter_ext::collect::IterCollect;
use crate::algo_lib::io::input::Input;
use crate::algo_lib::io::input::Readable;
use crate::algo_lib::io::output::Output;
use crate::algo_lib::io::output::Writable;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::iter::Copied;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ops::Index;
use std::ops::IndexMut;
use std::slice::Iter;
use std::slice::IterMut;
use std::slice::SliceIndex;
use std::str::FromStr;
use std::vec::IntoIter;
pub enum Str<'s> {
Extendable(Vec<u8>, PhantomData<&'s [u8]>),
Owned(Box<[u8]>, PhantomData<&'s [u8]>),
Ref(&'s [u8]),
}
impl Default for Str<'static> {
fn default() -> Self {
Self::new()
}
}
impl Str<'static> {
pub fn new() -> Self {
Str::Extendable(Vec::new(), PhantomData)
}
pub fn with_capacity(cap: usize) -> Self {
Str::Extendable(Vec::with_capacity(cap), PhantomData)
}
}
impl<'s> Str<'s> {
pub fn push(&mut self, c: u8) {
self.transform_to_extendable();
self.as_extendable().push(c)
}
pub fn pop(&mut self) -> Option<u8> {
self.transform_to_extendable();
self.as_extendable().pop()
}
pub fn as_slice(&self) -> &[u8] {
match self {
Str::Extendable(s, _) => s.as_ref(),
Str::Owned(s, _) => s.as_ref(),
Str::Ref(s) => s,
}
}
pub fn len(&self) -> usize {
self.as_slice().len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> Copied<Iter<u8>> {
match self {
Str::Extendable(v, _) => v.iter(),
Str::Owned(v, _) => v.iter(),
Str::Ref(v) => v.iter(),
}
.copied()
}
pub fn iter_mut(&mut self) -> IterMut<u8> {
self.transform_to_owned();
self.as_mut_slice().iter_mut()
}
pub fn sort(&mut self) {
self.transform_to_owned();
self.as_mut_slice().sort_unstable();
}
pub fn into_owned(mut self) -> Str<'static> {
self.transform_to_owned();
match self {
Str::Extendable(v, _) => Str::Extendable(v, PhantomData),
Str::Owned(v, _) => Str::Owned(v, PhantomData),
_ => unreachable!(),
}
}
fn transform_to_extendable(&mut self) {
match self {
Str::Extendable(_, _) => {}
Str::Owned(_, _) => {
let mut fake = Str::new();
std::mem::swap(self, &mut fake);
if let Str::Owned(s, _) = fake {
*self = Str::Extendable(s.to_vec(), PhantomData)
}
}
Str::Ref(s) => *self = Str::Extendable(s.to_vec(), PhantomData),
}
}
fn as_extendable(&mut self) -> &mut Vec<u8> {
match self {
Str::Extendable(s, _) => s,
_ => panic!("unreachable"),
}
}
fn transform_to_owned(&mut self) {
if let Str::Ref(s) = self {
*self = Str::Owned(s.to_vec().into_boxed_slice(), PhantomData)
}
}
pub fn as_mut_slice(&mut self) -> &mut [u8] {
self.transform_to_owned();
match self {
Str::Extendable(s, _) => s.as_mut_slice(),
Str::Owned(s, _) => s.as_mut(),
_ => panic!("unreachable"),
}
}
pub fn into_string(self) -> String {
match self {
Str::Extendable(v, _) => unsafe { String::from_utf8_unchecked(v) },
Str::Owned(v, _) => unsafe { String::from_utf8_unchecked(v.into_vec()) },
Str::Ref(v) => String::from_utf8_lossy(v).into_owned(),
}
}
pub fn reverse(&mut self) {
self.as_mut_slice().reverse();
}
pub fn trim(&self) -> Str<'_> {
let mut start = 0;
let mut end = self.len();
while start < end && (self[start] as char).is_whitespace() {
start += 1;
}
while start < end && (self[end - 1] as char).is_whitespace() {
end -= 1;
}
self[start..end].into()
}
pub fn split<'a, 'b>(&'a self, sep: impl Into<Str<'b>>) -> Vec<Str<'a>>
where
's: 'a,
{
let sep = sep.into();
let mut res = Vec::new();
let mut start = 0;
for i in 0..self.len() {
if self[i..].starts_with(sep.as_slice()) {
res.push(self[start..i].into());
start = i + sep.len();
}
}
res.push(self[start..].into());
res
}
pub fn parse<F: FromStr>(self) -> F
where
F::Err: Debug,
{
self.into_string().parse().unwrap()
}
pub fn parse_vec<T: Readable>(&self) -> Vec<T> {
let mut bytes = self.as_slice();
let mut input = Input::new(&mut bytes);
let mut res = Vec::new();
while !input.is_exhausted() {
res.push(input.read());
}
res
}
}
impl<'s> IntoIterator for Str<'s> {
type Item = u8;
type IntoIter = IntoIter<u8>;
#[allow(clippy::unnecessary_to_owned)]
fn into_iter(self) -> Self::IntoIter {
match self {
Str::Extendable(v, _) => v.into_iter(),
Str::Owned(v, _) => v.into_vec().into_iter(),
Str::Ref(v) => v.to_vec().into_iter(),
}
}
}
impl From<String> for Str<'static> {
fn from(s: String) -> Self {
Str::Extendable(s.into(), PhantomData)
}
}
impl<'s> From<&'s str> for Str<'s> {
fn from(s: &'s str) -> Self {
Str::Ref(s.as_bytes())
}
}
impl From<Vec<u8>> for Str<'static> {
fn from(s: Vec<u8>) -> Self {
Str::Extendable(s, PhantomData)
}
}
impl<'s> From<&'s [u8]> for Str<'s> {
fn from(s: &'s [u8]) -> Self {
Str::Ref(s)
}
}
impl<'s, const N: usize> From<&'s [u8; N]> for Str<'s> {
fn from(s: &'s [u8; N]) -> Self {
Str::Ref(s)
}
}
impl<'s> From<&'s String> for Str<'s> {
fn from(s: &'s String) -> Self {
Str::Ref(s.as_bytes())
}
}
impl<'s> From<&'s Vec<u8>> for Str<'s> {
fn from(s: &'s Vec<u8>) -> Self {
Str::Ref(s.as_slice())
}
}
impl From<u8> for Str<'static> {
fn from(c: u8) -> Self {
Str::Owned(Box::new([c]), PhantomData)
}
}
impl From<char> for Str<'static> {
fn from(c: char) -> Self {
Str::from(c as u8)
}
}
impl<'s, 't: 's> From<&'s Str<'t>> for Str<'s> {
fn from(value: &'s Str<'t>) -> Self {
Str::Ref(value.as_slice())
}
}
impl<R: SliceIndex<[u8]>> Index<R> for Str<'_> {
type Output = R::Output;
fn index(&self, index: R) -> &Self::Output {
self.as_slice().index(index)
}
}
impl<R: SliceIndex<[u8]>> IndexMut<R> for Str<'_> {
fn index_mut(&mut self, index: R) -> &mut Self::Output {
self.transform_to_owned();
self.as_mut_slice().index_mut(index)
}
}
impl Clone for Str<'_> {
fn clone(&self) -> Self {
match self {
Str::Extendable(s, _) => s.clone().into(),
Str::Owned(s, _) => s.to_vec().into(),
Str::Ref(s) => Str::Ref(s),
}
}
}
impl<'r, 's, S: Into<Str<'r>>> AddAssign<S> for Str<'s> {
fn add_assign(&mut self, rhs: S) {
self.transform_to_extendable();
self.as_extendable()
.extend_from_slice(rhs.into().as_slice());
}
}
impl<'r, 's, S: Into<Str<'r>>> Add<S> for Str<'s> {
type Output = Str<'s>;
fn add(mut self, rhs: S) -> Self::Output {
self += rhs;
self
}
}
impl Readable for Str<'static> {
fn read(input: &mut Input) -> Self {
input.next_token().unwrap().into()
}
}
impl Writable for Str<'_> {
fn write(&self, output: &mut Output) {
for c in self.as_slice() {
output.put(*c);
}
output.maybe_flush();
}
}
impl Display for Str<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
<String as Display>::fmt(&String::from_utf8(self.as_slice().to_vec()).unwrap(), f)
}
}
impl Hash for Str<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_slice().hash(state);
}
}
impl<'r> PartialEq<Str<'r>> for Str<'_> {
fn eq(&self, other: &Str<'r>) -> bool {
self.as_slice().eq(other.as_slice())
}
}
impl Eq for Str<'_> {}
impl<'r> PartialOrd<Str<'r>> for Str<'_> {
fn partial_cmp(&self, other: &Str<'r>) -> Option<Ordering> {
self.as_slice().partial_cmp(other.as_slice())
}
}
impl Ord for Str<'_> {
fn cmp(&self, other: &Self) -> Ordering {
self.as_slice().cmp(other.as_slice())
}
}
impl FromIterator<u8> for Str<'static> {
fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
Self::Extendable(iter.into_iter().collect_vec(), Default::default())
}
}
impl<'r> FromIterator<&'r u8> for Str<'static> {
fn from_iter<T: IntoIterator<Item = &'r u8>>(iter: T) -> Self {
Self::Extendable(iter.into_iter().cloned().collect_vec(), Default::default())
}
}
impl Deref for Str<'_> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}
impl DerefMut for Str<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
}
}
pub trait StrReader {
fn read_str(&mut self) -> Str<'static>;
fn read_str_vec(&mut self, n: usize) -> Vec<Str<'static>>;
fn read_line(&mut self) -> Str<'static>;
fn read_line_vec(&mut self, n: usize) -> Vec<Str<'static>>;
fn read_lines(&mut self) -> Vec<Str<'static>>;
}
impl StrReader for Input<'_> {
fn read_str(&mut self) -> Str<'static> {
self.read()
}
fn read_str_vec(&mut self, n: usize) -> Vec<Str<'static>> {
self.read_vec(n)
}
fn read_line(&mut self) -> Str<'static> {
let mut res = Str::new();
while let Some(c) = self.get() {
if c == b'\n' {
break;
}
res.push(c);
}
res
}
fn read_line_vec(&mut self, n: usize) -> Vec<Str<'static>> {
let mut res = Vec::with_capacity(n);
for _ in 0..n {
res.push(self.read_line());
}
res
}
fn read_lines(&mut self) -> Vec<Str<'static>> {
let mut res = Vec::new();
while !self.is_exhausted() {
res.push(self.read_line());
}
if let Some(s) = res.last() {
if s.is_empty() {
res.pop();
}
}
res
}
}
}
}
}
fn main() {
let mut sin = std::io::stdin();
let input = if true {
algo_lib::io::input::Input::new_with_size(&mut sin, 1)
} else {
algo_lib::io::input::Input::new(&mut sin)
};
let mut stdout = std::io::stdout();
let output = if true {
algo_lib::io::output::Output::new_with_auto_flush(&mut stdout)
} else {
algo_lib::io::output::Output::new(&mut stdout)
};
solution::run(input, output);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 2156kb
input:
2 1 1 2 1 1 1 2 1 1 1 Correct 1 3 2 2 1 3 3 1 1 3 4 2 1 3 2 2 4 2 2 2 1 3 3 1 1 3 4 2 1 3 Correct
output:
> 1 > 1 > 1 > 1 ! 1 2 > 1 > 1 > 2 > 1 > 3 > 1 > 1 > 2 > 2 > 1 > 2 > 1 > 3 > 1 ! 1 3 1 4 2 4 1 2
result:
ok correct
Test #2:
score: 0
Accepted
time: 116ms
memory: 2176kb
input:
1000 1 9 2 7 1 9 3 9 1 9 4 9 1 9 5 8 1 9 6 9 1 9 7 7 1 9 8 8 1 9 9 8 1 9 10 6 1 9 2 7 3 9 4 9 3 9 9 8 3 9 2 7 10 6 2 7 6 9 2 7 5 8 8 8 5 8 9 8 5 8 2 7 9 8 2 7 4 9 7 7 4 9 10 6 8 8 10 6 4 9 8 8 9 8 8 8 7 7 8 8 3 9 5 8 3 9 10 6 6 9 10 6 3 9 8 8 6 9 5 8 6 9 8 8 4 9 5 8 7 7 5 8 4 9 2 7 1 9 3 9 7 7 3 9 6...
output:
> 1 > 1 > 2 > 1 > 3 > 1 > 4 > 1 > 5 > 1 > 6 > 1 > 7 > 1 > 8 > 1 > 9 > 1 > 1 > 2 > 2 > 2 > 3 > 2 > 4 > 3 > 2 > 4 > 2 > 5 > 2 > 2 > 3 > 3 > 4 > 6 > 4 > 7 > 3 > 2 > 4 > 3 > 3 > 4 > 5 > 4 > 5 > 5 > 3 > 6 > 5 > 5 > 6 > 5 > 3 > 6 > 7 > 7 > 4 > 6 > 5 > 8 > 6 > 7 > 4 > 8 > 7 > 1 > 2 > 8 > 5 > 9 > 6 > 1 > 3 ...
result:
ok correct
Test #3:
score: 0
Accepted
time: 173ms
memory: 2272kb
input:
500 1 19 2 8 1 19 3 7 1 19 4 8 1 19 5 7 1 19 6 7 1 19 7 11 1 19 8 4 1 19 9 7 1 19 10 9 1 19 11 8 1 19 12 7 1 19 13 4 1 19 14 9 1 19 15 8 1 19 16 7 1 19 17 10 1 19 18 7 1 19 19 7 1 19 20 6 1 19 2 8 17 10 2 8 3 7 20 6 3 7 11 8 3 7 4 8 3 7 2 8 14 9 19 7 14 9 17 10 6 7 5 7 7 11 5 7 6 7 17 10 14 9 6 7 11...
output:
> 1 > 1 > 2 > 1 > 3 > 1 > 4 > 1 > 5 > 1 > 6 > 1 > 7 > 1 > 8 > 1 > 9 > 1 > 10 > 1 > 11 > 1 > 12 > 1 > 13 > 1 > 14 > 1 > 15 > 1 > 16 > 1 > 17 > 1 > 18 > 1 > 19 > 1 > 1 > 2 > 2 > 3 > 2 > 2 > 3 > 2 > 4 > 2 > 5 > 4 > 2 > 2 > 3 > 3 > 2 > 2 > 2 > 3 > 3 > 4 > 4 > 4 > 3 > 2 > 4 > 2 > 2 > 3 > 5 > 3 > 2 > 4 > ...
result:
ok correct
Test #4:
score: 0
Accepted
time: 145ms
memory: 2080kb
input:
100 1 99 2 5 1 99 3 5 1 99 4 10 1 99 5 3 1 99 6 9 1 99 7 6 1 99 8 8 1 99 9 9 1 99 10 5 1 99 11 4 1 99 12 7 1 99 13 6 1 99 14 8 1 99 15 7 1 99 16 7 1 99 17 7 1 99 18 6 1 99 19 10 1 99 20 4 1 99 21 4 1 99 22 5 1 99 23 6 1 99 24 10 1 99 25 4 1 99 26 10 1 99 27 7 1 99 28 6 1 99 29 11 1 99 30 5 1 99 31 7...
output:
> 1 > 1 > 2 > 1 > 3 > 1 > 4 > 1 > 5 > 1 > 6 > 1 > 7 > 1 > 8 > 1 > 9 > 1 > 10 > 1 > 11 > 1 > 12 > 1 > 13 > 1 > 14 > 1 > 15 > 1 > 16 > 1 > 17 > 1 > 18 > 1 > 19 > 1 > 20 > 1 > 21 > 1 > 22 > 1 > 23 > 1 > 24 > 1 > 25 > 1 > 26 > 1 > 27 > 1 > 28 > 1 > 29 > 1 > 30 > 1 > 31 > 1 > 32 > 1 > 33 > 1 > 34 > 1 > 3...
result:
ok correct
Test #5:
score: 0
Accepted
time: 172ms
memory: 2848kb
input:
10 1 999 2 8 1 999 3 9 1 999 4 8 1 999 5 7 1 999 6 7 1 999 7 4 1 999 8 4 1 999 9 13 1 999 10 11 1 999 11 7 1 999 12 7 1 999 13 5 1 999 14 5 1 999 15 8 1 999 16 9 1 999 17 5 1 999 18 5 1 999 19 8 1 999 20 4 1 999 21 8 1 999 22 8 1 999 23 6 1 999 24 8 1 999 25 2 1 999 26 9 1 999 27 5 1 999 28 6 1 999 ...
output:
> 1 > 1 > 2 > 1 > 3 > 1 > 4 > 1 > 5 > 1 > 6 > 1 > 7 > 1 > 8 > 1 > 9 > 1 > 10 > 1 > 11 > 1 > 12 > 1 > 13 > 1 > 14 > 1 > 15 > 1 > 16 > 1 > 17 > 1 > 18 > 1 > 19 > 1 > 20 > 1 > 21 > 1 > 22 > 1 > 23 > 1 > 24 > 1 > 25 > 1 > 26 > 1 > 27 > 1 > 28 > 1 > 29 > 1 > 30 > 1 > 31 > 1 > 32 > 1 > 33 > 1 > 34 > 1 > 3...
result:
ok correct
Test #6:
score: 0
Accepted
time: 156ms
memory: 2904kb
input:
4 1 999 2 24 1 999 3 20 1 999 4 17 1 999 5 29 1 999 6 21 1 999 7 21 1 999 8 19 1 999 9 21 1 999 10 14 1 999 11 12 1 999 12 17 1 999 13 23 1 999 14 23 1 999 15 16 1 999 16 14 1 999 17 16 1 999 18 15 1 999 19 16 1 999 20 19 1 999 21 16 1 999 22 16 1 999 23 25 1 999 24 23 1 999 25 22 1 999 26 16 1 999 ...
output:
> 1 > 1 > 2 > 1 > 3 > 1 > 4 > 1 > 5 > 1 > 6 > 1 > 7 > 1 > 8 > 1 > 9 > 1 > 10 > 1 > 11 > 1 > 12 > 1 > 13 > 1 > 14 > 1 > 15 > 1 > 16 > 1 > 17 > 1 > 18 > 1 > 19 > 1 > 20 > 1 > 21 > 1 > 22 > 1 > 23 > 1 > 24 > 1 > 25 > 1 > 26 > 1 > 27 > 1 > 28 > 1 > 29 > 1 > 30 > 1 > 31 > 1 > 32 > 1 > 33 > 1 > 34 > 1 > 3...
result:
ok correct
Test #7:
score: 0
Accepted
time: 165ms
memory: 2808kb
input:
4 1 199 2 106 1 199 3 95 1 199 4 102 1 199 5 103 1 199 6 103 1 199 7 110 1 199 8 109 1 199 9 104 1 199 10 98 1 199 11 85 1 199 12 94 1 199 13 86 1 199 14 105 1 199 15 102 1 199 16 96 1 199 17 97 1 199 18 94 1 199 19 112 1 199 20 108 1 199 21 116 1 199 22 109 1 199 23 104 1 199 24 96 1 199 25 92 1 19...
output:
> 1 > 1 > 2 > 1 > 3 > 1 > 4 > 1 > 5 > 1 > 6 > 1 > 7 > 1 > 8 > 1 > 9 > 1 > 10 > 1 > 11 > 1 > 12 > 1 > 13 > 1 > 14 > 1 > 15 > 1 > 16 > 1 > 17 > 1 > 18 > 1 > 19 > 1 > 20 > 1 > 21 > 1 > 22 > 1 > 23 > 1 > 24 > 1 > 25 > 1 > 26 > 1 > 27 > 1 > 28 > 1 > 29 > 1 > 30 > 1 > 31 > 1 > 32 > 1 > 33 > 1 > 34 > 1 > 3...
result:
ok correct
Test #8:
score: 0
Accepted
time: 134ms
memory: 2780kb
input:
4 1 140 2 140 1 140 3 140 1 140 4 140 1 140 5 140 1 140 6 140 1 140 7 140 1 140 8 140 1 140 9 140 1 140 10 140 1 140 11 140 1 140 12 140 1 140 13 140 1 140 14 140 1 140 15 140 1 140 16 140 1 140 17 140 1 140 18 140 1 140 19 140 1 140 20 140 1 140 21 140 1 140 22 140 1 140 23 140 1 140 24 140 1 140 2...
output:
> 1 > 1 > 2 > 1 > 3 > 1 > 4 > 1 > 5 > 1 > 6 > 1 > 7 > 1 > 8 > 1 > 9 > 1 > 10 > 1 > 11 > 1 > 12 > 1 > 13 > 1 > 14 > 1 > 15 > 1 > 16 > 1 > 17 > 1 > 18 > 1 > 19 > 1 > 20 > 1 > 21 > 1 > 22 > 1 > 23 > 1 > 24 > 1 > 25 > 1 > 26 > 1 > 27 > 1 > 28 > 1 > 29 > 1 > 30 > 1 > 31 > 1 > 32 > 1 > 33 > 1 > 34 > 1 > 3...
result:
ok correct
Test #9:
score: 0
Accepted
time: 180ms
memory: 2856kb
input:
4 1 2498 2 2 1 2498 3 2 1 2498 4 2 1 2498 5 2 1 2498 6 2 1 2498 7 2 1 2498 8 2 1 2498 9 2 1 2498 10 2 1 2498 11 2 1 2498 12 2 1 2498 13 2 1 2498 14 2 1 2498 15 2 1 2498 16 2 1 2498 17 2 1 2498 18 2 1 2498 19 2 1 2498 20 2 1 2498 21 2 1 2498 22 2 1 2498 23 2 1 2498 24 2 1 2498 25 2 1 2498 26 2 1 2498...
output:
> 1 > 1 > 2 > 1 > 3 > 1 > 4 > 1 > 5 > 1 > 6 > 1 > 7 > 1 > 8 > 1 > 9 > 1 > 10 > 1 > 11 > 1 > 12 > 1 > 13 > 1 > 14 > 1 > 15 > 1 > 16 > 1 > 17 > 1 > 18 > 1 > 19 > 1 > 20 > 1 > 21 > 1 > 22 > 1 > 23 > 1 > 24 > 1 > 25 > 1 > 26 > 1 > 27 > 1 > 28 > 1 > 29 > 1 > 30 > 1 > 31 > 1 > 32 > 1 > 33 > 1 > 34 > 1 > 3...
result:
ok correct
Extra Test:
score: 0
Extra Test Passed