QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#597839 | #9424. Stop the Castle 2 | ucup-team296# | AC ✓ | 255ms | 30820kb | Rust | 57.5kb | 2024-09-28 19:05:17 | 2024-09-28 19:05:17 |
Judging History
answer
//
pub mod solution {
//{"name":"ucup_10_g","group":"Manual","url":"","interactive":false,"timeLimit":2000,"tests":[{"input":"","output":""}],"testType":"multiNumber","input":{"type":"stdin","fileName":null,"pattern":null},"output":{"type":"stdout","fileName":null,"pattern":null},"languages":{"java":{"taskClass":"ucup_10_g"}}}
use crate::algo_lib::collections::bit_set::BitSet;
use crate::algo_lib::collections::iter_ext::collect::IterCollect;
use crate::algo_lib::collections::vec_ext::inc_dec::IncDec;
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
use crate::algo_lib::graph::edges::flow_edge::FlowEdge;
use crate::algo_lib::graph::edges::flow_edge_trait::FlowEdgeTrait;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::graph::max_flow::MaxFlow;
use crate::algo_lib::io::input::Input;
use crate::algo_lib::io::output::Output;
use crate::algo_lib::misc::test_type::TaskType;
use crate::algo_lib::misc::test_type::TestType;
type PreCalc = ();
fn solve(input: &mut Input, out: &mut Output, _test_case: usize, _data: &mut PreCalc) {
let n = input.read_size();
let m = input.read_size();
let k = input.read_size();
let castles = input.read_size_pair_vec(n);
let obstacles = input.read_size_pair_vec(m);
let mut base = 0;
#[derive(Copy, Clone)]
enum ObjectType {
Castle,
Obstacle,
}
#[derive(Copy, Clone)]
struct Object {
id: usize,
x: usize,
y: usize,
t: ObjectType,
}
let mut objects = castles
.iter()
.enumerate()
.map(|(i, &(x, y))| Object {
id: i,
x,
y,
t: ObjectType::Castle,
})
.chain(obstacles.iter().enumerate().map(|(i, &(x, y))| Object {
id: i,
x,
y,
t: ObjectType::Obstacle,
}))
.collect::<Vec<_>>();
objects.sort_by_key(|o| (o.x, o.y));
let mut last_x = 0;
let mut obstacles_in_gap = Vec::new();
let mut castle_started = false;
let mut hor_gap_id = 0;
let mut obst_hor_gap = vec![None; m];
for obj in &objects {
if obj.x != last_x {
obstacles_in_gap.clear();
castle_started = false;
}
match obj.t {
ObjectType::Castle => {
if castle_started {
if obstacles_in_gap.is_empty() {
base += 1;
} else {
for &obstacle in &obstacles_in_gap {
obst_hor_gap[obstacle] = Some(hor_gap_id);
}
obstacles_in_gap.clear();
hor_gap_id += 1;
}
}
castle_started = true;
}
ObjectType::Obstacle => {
if castle_started {
obstacles_in_gap.push(obj.id);
}
}
}
last_x = obj.x;
}
objects.sort_by_key(|o| (o.y, o.x));
let mut last_y = 0;
let mut obstacles_in_gap = Vec::new();
let mut castle_started = false;
let mut ver_gap_id = 0;
let mut obst_ver_gap = vec![None; m];
for obj in &objects {
if obj.y != last_y {
obstacles_in_gap.clear();
castle_started = false;
}
match obj.t {
ObjectType::Castle => {
if castle_started {
if obstacles_in_gap.is_empty() {
base += 1;
} else {
for &obstacle in &obstacles_in_gap {
obst_ver_gap[obstacle] = Some(ver_gap_id);
}
obstacles_in_gap.clear();
ver_gap_id += 1;
}
}
castle_started = true;
}
ObjectType::Obstacle => {
if castle_started {
obstacles_in_gap.push(obj.id);
}
}
}
last_y = obj.y;
}
let mut graph = Graph::new(hor_gap_id + ver_gap_id + 3);
let source = hor_gap_id + ver_gap_id;
let sink = source + 1;
let real_source = sink + 1;
for i in 0..hor_gap_id {
graph.add_edge(FlowEdge::with_payload(source, i, 1, 0));
}
for i in 0..ver_gap_id {
graph.add_edge(FlowEdge::with_payload(hor_gap_id + i, sink, 1, 0));
}
for i in 0..m {
if let Some(hor_gap) = obst_hor_gap[i] {
if let Some(ver_gap) = obst_ver_gap[i] {
graph.add_edge(FlowEdge::with_payload(hor_gap, hor_gap_id + ver_gap, 1, i));
}
}
}
graph.add_edge(FlowEdge::with_payload(real_source, source, m - k, 0));
let mut ans = base + hor_gap_id + ver_gap_id;
let max_flow = graph.max_flow(real_source, sink);
ans -= 2 * max_flow;
let mut to_remove = BitSet::new(m);
to_remove.fill(true);
let mut left_done = BitSet::new(hor_gap_id);
let mut right_done = BitSet::new(ver_gap_id);
for i in 0..hor_gap_id {
for e in &graph[i] {
if e.to() != source && e.flow(&graph) == 1 {
to_remove.unset(*e.payload());
left_done.set(i);
right_done.set(e.to() - hor_gap_id);
}
}
}
let mut can_do = m - k - max_flow;
for i in 0..m {
if let Some(hor_gap) = obst_hor_gap[i] {
if !left_done[hor_gap] && can_do > 0 {
assert!(to_remove[i]);
can_do -= 1;
to_remove.unset(i);
left_done.set(hor_gap);
ans -= 1;
}
}
if let Some(ver_gap) = obst_ver_gap[i] {
if !right_done[ver_gap] && can_do > 0 {
assert!(to_remove[i]);
can_do -= 1;
to_remove.unset(i);
right_done.set(ver_gap);
ans -= 1;
}
}
}
for i in 0..m {
if to_remove[i] && can_do > 0 {
can_do -= 1;
to_remove.unset(i);
}
}
out.print_line(ans);
out.print_line(to_remove.iter().collect_vec().inc());
}
pub static TEST_TYPE: TestType = TestType::MultiNumber;
pub static TASK_TYPE: TaskType = TaskType::Classic;
pub(crate) fn run(mut input: Input, mut output: Output) -> bool {
let mut pre_calc = ();
match TEST_TYPE {
TestType::Single => solve(&mut input, &mut output, 1, &mut pre_calc),
TestType::MultiNumber => {
let t = input.read();
for i in 1..=t {
solve(&mut input, &mut output, i, &mut pre_calc);
}
}
TestType::MultiEof => {
let mut i = 1;
while input.peek().is_some() {
solve(&mut input, &mut output, i, &mut pre_calc);
i += 1;
}
}
}
output.flush();
match TASK_TYPE {
TaskType::Classic => {
input.skip_whitespace();
input.peek().is_none()
}
TaskType::Interactive => true,
}
}
}
pub mod algo_lib {
pub mod collections {
pub mod bit_set {
use crate::algo_lib::collections::slice_ext::legacy_fill::LegacyFill;
use crate::algo_lib::numbers::num_traits::bit_ops::BitOps;
use std::ops::BitAndAssign;
use std::ops::BitOrAssign;
use std::ops::Index;
use std::ops::ShlAssign;
use std::ops::ShrAssign;
const TRUE: bool = true;
const FALSE: bool = false;
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct BitSet {
data: Vec<u64>,
len: usize,
}
impl BitSet {
pub fn new(len: usize) -> Self {
let data_len = if len == 0 {
0
} else {
Self::index(len - 1) + 1
};
Self {
data: vec![0; data_len],
len,
}
}
pub fn from_slice(len: usize, set: &[usize]) -> Self {
let mut res = Self::new(len);
for &i in set {
res.set(i);
}
res
}
pub fn set(&mut self, at: usize) {
assert!(at < self.len);
self.data[Self::index(at)].set_bit(at & 63);
}
pub fn unset(&mut self, at: usize) {
assert!(at < self.len);
self.data[Self::index(at)].unset_bit(at & 63);
}
pub fn change(&mut self, at: usize, value: bool) {
if value {
self.set(at);
} else {
self.unset(at);
}
}
pub fn flip(&mut self, at: usize) {
self.change(at, !self[at]);
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.len
}
pub fn fill(&mut self, value: bool) {
// 1.43
self.data.legacy_fill(if value { std::u64::MAX } else { 0 });
if value {
self.fix_last();
}
}
pub fn is_superset(&self, other: &Self) -> bool {
assert_eq!(self.len, other.len);
for i in 0..self.data.len() {
if self.data[i] & other.data[i] != other.data[i] {
return false;
}
}
true
}
pub fn is_subset(&self, other: &Self) -> bool {
other.is_superset(self)
}
pub fn iter(&self) -> impl Iterator<Item = usize> + '_ {
self.into_iter()
}
fn index(at: usize) -> usize {
at >> 6
}
pub fn count_ones(&self) -> usize {
self.data.iter().map(|x| x.count_ones() as usize).sum()
}
fn fix_last(&mut self) {
if self.len & 63 != 0 {
let mask = (1 << (self.len & 63)) - 1;
*self.data.last_mut().unwrap() &= mask;
}
}
}
pub struct BitSetIter<'s> {
at: usize,
inside: usize,
set: &'s BitSet,
}
impl<'s> Iterator for BitSetIter<'s> {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
while self.at < self.set.data.len()
&& (self.inside == 64 || (self.set.data[self.at] >> self.inside) == 0)
{
self.at += 1;
self.inside = 0;
}
if self.at == self.set.data.len() {
None
} else {
while !self.set.data[self.at].is_set(self.inside) {
self.inside += 1;
}
let res = self.at * 64 + self.inside;
if res < self.set.len {
self.inside += 1;
Some(res)
} else {
None
}
}
}
}
impl<'a> IntoIterator for &'a BitSet {
type Item = usize;
type IntoIter = BitSetIter<'a>;
fn into_iter(self) -> Self::IntoIter {
BitSetIter {
at: 0,
inside: 0,
set: self,
}
}
}
impl BitOrAssign<&BitSet> for BitSet {
fn bitor_assign(&mut self, rhs: &BitSet) {
assert_eq!(self.len, rhs.len);
for (i, &j) in self.data.iter_mut().zip(rhs.data.iter()) {
*i |= j;
}
}
}
impl BitAndAssign<&BitSet> for BitSet {
fn bitand_assign(&mut self, rhs: &BitSet) {
assert_eq!(self.len, rhs.len);
for (i, &j) in self.data.iter_mut().zip(rhs.data.iter()) {
*i &= j;
}
}
}
impl ShlAssign<usize> for BitSet {
fn shl_assign(&mut self, rhs: usize) {
if rhs == 0 {
return;
}
let small_shift = rhs & 63;
if small_shift != 0 {
let mut carry = 0;
for i in 0..self.data.len() {
let new_carry = self.data[i] >> (64 - small_shift);
self.data[i] <<= small_shift;
self.data[i] |= carry;
carry = new_carry;
}
}
let big_shift = rhs >> 6;
if big_shift != 0 {
self.data.rotate_right(big_shift);
self.data[..big_shift].fill(0);
}
self.fix_last();
}
}
impl ShrAssign<usize> for BitSet {
fn shr_assign(&mut self, rhs: usize) {
if rhs == 0 {
return;
}
let small_shift = rhs & 63;
if small_shift != 0 {
let mut carry = 0;
for i in (0..self.data.len()).rev() {
let new_carry = self.data[i] << (64 - small_shift);
self.data[i] >>= small_shift;
self.data[i] |= carry;
carry = new_carry;
}
}
let big_shift = rhs >> 6;
if big_shift != 0 {
self.data.rotate_left(big_shift);
let from = self.data.len() - big_shift;
self.data[from..].fill(0);
}
}
}
impl Index<usize> for BitSet {
type Output = bool;
fn index(&self, at: usize) -> &Self::Output {
assert!(at < self.len);
if self.data[Self::index(at)].is_set(at & 63) {
&TRUE
} else {
&FALSE
}
}
}
impl From<Vec<bool>> for BitSet {
fn from(data: Vec<bool>) -> Self {
let mut res = Self::new(data.len());
for (i, &value) in data.iter().enumerate() {
res.change(i, value);
}
res
}
}
}
pub mod dsu {
use crate::algo_lib::collections::iter_ext::collect::IterCollect;
use crate::algo_lib::collections::slice_ext::bounds::Bounds;
use crate::algo_lib::collections::slice_ext::legacy_fill::LegacyFill;
use std::cell::Cell;
#[derive(Clone)]
pub struct DSU {
id: Vec<Cell<u32>>,
size: Vec<u32>,
count: usize,
}
impl DSU {
pub fn new(n: usize) -> Self {
Self {
id: (0..n).map(|i| Cell::new(i as u32)).collect_vec(),
size: vec![1; n],
count: n,
}
}
pub fn size(&self, i: usize) -> usize {
self.size[self.get(i)] as usize
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.id.len()
}
pub fn iter(&self) -> impl Iterator<Item = usize> + '_ {
self.id.iter().enumerate().filter_map(|(i, id)| {
if (i as u32) == id.get() {
Some(i)
} else {
None
}
})
}
pub fn set_count(&self) -> usize {
self.count
}
pub fn join(&mut self, mut a: usize, mut b: usize) -> bool {
a = self.get(a);
b = self.get(b);
if a == b {
false
} else {
self.size[a] += self.size[b];
self.id[b].replace(a as u32);
self.count -= 1;
true
}
}
pub fn get(&self, i: usize) -> usize {
if self.id[i].get() != i as u32 {
let res = self.get(self.id[i].get() as usize);
self.id[i].replace(res as u32);
}
self.id[i].get() as usize
}
pub fn clear(&mut self) {
self.count = self.id.len();
self.size.legacy_fill(1);
self.id.iter().enumerate().for_each(|(i, id)| {
id.replace(i as u32);
});
}
pub fn parts(&self) -> Vec<Vec<usize>> {
let roots = self.iter().collect_vec();
let mut res = vec![Vec::new(); roots.len()];
for i in 0..self.id.len() {
res[roots.as_slice().bin_search(&self.get(i)).unwrap()].push(i);
}
res
}
}
}
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 bounds {
pub trait Bounds<T: PartialOrd> {
fn lower_bound(&self, el: &T) -> usize;
fn upper_bound(&self, el: &T) -> usize;
fn bin_search(&self, el: &T) -> Option<usize>;
fn more(&self, el: &T) -> usize;
fn more_or_eq(&self, el: &T) -> usize;
fn less(&self, el: &T) -> usize;
fn less_or_eq(&self, el: &T) -> usize;
}
impl<T: PartialOrd> Bounds<T> for [T] {
fn lower_bound(&self, el: &T) -> usize {
let mut left = 0;
let mut right = self.len();
while left < right {
let mid = left + ((right - left) >> 1);
if &self[mid] < el {
left = mid + 1;
} else {
right = mid;
}
}
left
}
fn upper_bound(&self, el: &T) -> usize {
let mut left = 0;
let mut right = self.len();
while left < right {
let mid = left + ((right - left) >> 1);
if &self[mid] <= el {
left = mid + 1;
} else {
right = mid;
}
}
left
}
fn bin_search(&self, el: &T) -> Option<usize> {
let at = self.lower_bound(el);
if at == self.len() || &self[at] != el {
None
} else {
Some(at)
}
}
fn more(&self, el: &T) -> usize {
self.len() - self.upper_bound(el)
}
fn more_or_eq(&self, el: &T) -> usize {
self.len() - self.lower_bound(el)
}
fn less(&self, el: &T) -> usize {
self.lower_bound(el)
}
fn less_or_eq(&self, el: &T) -> usize {
self.upper_bound(el)
}
}
}
pub mod legacy_fill {
// 1.50
pub trait LegacyFill<T> {
fn legacy_fill(&mut self, val: T);
}
impl<T: Clone> LegacyFill<T> for [T] {
fn legacy_fill(&mut self, val: T) {
for el in self.iter_mut() {
*el = val.clone();
}
}
}
}
}
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 inc_dec {
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;
use crate::algo_lib::numbers::num_traits::algebra::One;
pub trait IncDec {
#[must_use]
fn inc(self) -> Self;
#[must_use]
fn dec(self) -> Self;
}
impl<T: AdditionMonoidWithSub + One> IncDec for T {
fn inc(self) -> Self {
self + T::one()
}
fn dec(self) -> Self {
self - T::one()
}
}
impl<T: AdditionMonoidWithSub + One> IncDec for Vec<T> {
fn inc(mut self) -> Self {
self.iter_mut().for_each(|i| *i += T::one());
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|i| *i -= T::one());
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One> IncDec for Vec<(T, U)> {
fn inc(mut self) -> Self {
self.iter_mut().for_each(|(i, j)| {
*i += T::one();
*j += U::one();
});
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|(i, j)| {
*i -= T::one();
*j -= U::one();
});
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One, V> IncDec for Vec<(T, U, V)> {
fn inc(mut self) -> Self {
self.iter_mut().for_each(|(i, j, _)| {
*i += T::one();
*j += U::one();
});
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|(i, j, _)| {
*i -= T::one();
*j -= U::one();
});
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One, V, W> IncDec
for Vec<(T, U, V, W)>
{
fn inc(mut self) -> Self {
self.iter_mut().for_each(|(i, j, ..)| {
*i += T::one();
*j += U::one();
});
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|(i, j, ..)| {
*i -= T::one();
*j -= U::one();
});
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One, V, W, X> IncDec
for Vec<(T, U, V, W, X)>
{
fn inc(mut self) -> Self {
self.iter_mut().for_each(|(i, j, ..)| {
*i += T::one();
*j += U::one();
});
self
}
fn dec(mut self) -> Self {
self.iter_mut().for_each(|(i, j, ..)| {
*i -= T::one();
*j -= U::one();
});
self
}
}
impl<T: AdditionMonoidWithSub + One, U: AdditionMonoidWithSub + One> IncDec for (T, U) {
fn inc(mut self) -> Self {
self.0 += T::one();
self.1 += U::one();
self
}
fn dec(mut self) -> Self {
self.0 -= T::one();
self.1 -= U::one();
self
}
}
}
}
}
pub mod graph {
pub mod edges {
pub mod bi_edge {
use crate::algo_lib::graph::edges::bi_edge_trait::BiEdgeTrait;
use crate::algo_lib::graph::edges::edge_id::EdgeId;
use crate::algo_lib::graph::edges::edge_id::NoId;
use crate::algo_lib::graph::edges::edge_id::WithId;
use crate::algo_lib::graph::edges::edge_trait::BidirectionalEdgeTrait;
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
#[derive(Clone)]
pub struct BiEdgeRaw<Id: EdgeId, P> {
to: u32,
id: Id,
payload: P,
}
impl<Id: EdgeId> BiEdgeRaw<Id, ()> {
pub fn new(from: usize, to: usize) -> (usize, Self) {
(
from,
Self {
to: to as u32,
id: Id::new(),
payload: (),
},
)
}
}
impl<Id: EdgeId, P> BiEdgeRaw<Id, P> {
pub fn with_payload(from: usize, to: usize, payload: P) -> (usize, Self) {
(from, Self::with_payload_impl(to, payload))
}
fn with_payload_impl(to: usize, payload: P) -> BiEdgeRaw<Id, P> {
Self {
to: to as u32,
id: Id::new(),
payload,
}
}
}
impl<Id: EdgeId, P: Clone> BidirectionalEdgeTrait for BiEdgeRaw<Id, P> {}
impl<Id: EdgeId, P: Clone> EdgeTrait for BiEdgeRaw<Id, P> {
type Payload = P;
const REVERSABLE: bool = true;
fn to(&self) -> usize {
self.to as usize
}
fn id(&self) -> usize {
self.id.id()
}
fn set_id(&mut self, id: usize) {
self.id.set_id(id);
}
fn reverse_id(&self) -> usize {
panic!("no reverse id")
}
fn set_reverse_id(&mut self, _: usize) {}
fn reverse_edge(&self, from: usize) -> Self {
Self::with_payload_impl(from, self.payload.clone())
}
fn payload(&self) -> &P {
&self.payload
}
}
impl<Id: EdgeId, P: Clone> BiEdgeTrait for BiEdgeRaw<Id, P> {}
pub type BiEdge<P> = BiEdgeRaw<NoId, P>;
pub type BiEdgeWithId<P> = BiEdgeRaw<WithId, P>;
}
pub mod bi_edge_trait {
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
pub trait BiEdgeTrait: EdgeTrait {}
}
pub mod edge {
use crate::algo_lib::graph::edges::edge_id::EdgeId;
use crate::algo_lib::graph::edges::edge_id::NoId;
use crate::algo_lib::graph::edges::edge_id::WithId;
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
#[derive(Clone)]
pub struct EdgeRaw<Id: EdgeId, P> {
to: u32,
id: Id,
payload: P,
}
impl<Id: EdgeId> EdgeRaw<Id, ()> {
pub fn new(from: usize, to: usize) -> (usize, Self) {
(
from,
Self {
to: to as u32,
id: Id::new(),
payload: (),
},
)
}
}
impl<Id: EdgeId, P> EdgeRaw<Id, P> {
pub fn with_payload(from: usize, to: usize, payload: P) -> (usize, Self) {
(from, Self::with_payload_impl(to, payload))
}
fn with_payload_impl(to: usize, payload: P) -> Self {
Self {
to: to as u32,
id: Id::new(),
payload,
}
}
}
impl<Id: EdgeId, P: Clone> EdgeTrait for EdgeRaw<Id, P> {
type Payload = P;
const REVERSABLE: bool = false;
fn to(&self) -> usize {
self.to as usize
}
fn id(&self) -> usize {
self.id.id()
}
fn set_id(&mut self, id: usize) {
self.id.set_id(id);
}
fn reverse_id(&self) -> usize {
panic!("no reverse")
}
fn set_reverse_id(&mut self, _: usize) {
panic!("no reverse")
}
fn reverse_edge(&self, _: usize) -> Self {
panic!("no reverse")
}
fn payload(&self) -> &P {
&self.payload
}
}
pub type Edge<P> = EdgeRaw<NoId, P>;
pub type EdgeWithId<P> = EdgeRaw<WithId, P>;
}
pub mod edge_id {
pub trait EdgeId: Clone {
fn new() -> Self;
fn id(&self) -> usize;
fn set_id(&mut self, id: usize);
}
#[derive(Clone)]
pub struct WithId {
id: u32,
}
impl EdgeId for WithId {
fn new() -> Self {
Self { id: 0 }
}
fn id(&self) -> usize {
self.id as usize
}
fn set_id(&mut self, id: usize) {
self.id = id as u32;
}
}
#[derive(Clone)]
pub struct NoId {}
impl EdgeId for NoId {
fn new() -> Self {
Self {}
}
fn id(&self) -> usize {
panic!("Id called on no id")
}
fn set_id(&mut self, _: usize) {}
}
}
pub mod edge_trait {
pub trait EdgeTrait: Clone {
type Payload;
const REVERSABLE: bool;
fn to(&self) -> usize;
fn id(&self) -> usize;
fn set_id(&mut self, id: usize);
fn reverse_id(&self) -> usize;
fn set_reverse_id(&mut self, reverse_id: usize);
#[must_use]
fn reverse_edge(&self, from: usize) -> Self;
fn payload(&self) -> &Self::Payload;
}
pub trait BidirectionalEdgeTrait: EdgeTrait {}
}
pub mod flow_edge {
use crate::algo_lib::graph::edges::edge_id::EdgeId;
use crate::algo_lib::graph::edges::edge_id::NoId;
use crate::algo_lib::graph::edges::edge_id::WithId;
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
use crate::algo_lib::graph::edges::flow_edge_trait::FlowEdgeTrait;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;
#[derive(Clone)]
pub struct FlowEdgeRaw<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId, P> {
to: u32,
capacity: C,
reverse_id: u32,
id: Id,
payload: P,
}
impl<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId> FlowEdgeRaw<C, Id, ()> {
pub fn new(from: usize, to: usize, c: C) -> (usize, Self) {
(
from,
Self {
to: to as u32,
capacity: c,
reverse_id: 0,
id: Id::new(),
payload: (),
},
)
}
}
impl<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId, P> FlowEdgeRaw<C, Id, P> {
pub fn with_payload(from: usize, to: usize, c: C, payload: P) -> (usize, Self) {
(from, Self::with_payload_impl(to, c, payload))
}
fn with_payload_impl(to: usize, c: C, payload: P) -> Self {
Self {
to: to as u32,
capacity: c,
reverse_id: 0,
id: Id::new(),
payload,
}
}
}
impl<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId, P: Clone> EdgeTrait
for FlowEdgeRaw<C, Id, P>
{
type Payload = P;
const REVERSABLE: bool = true;
fn to(&self) -> usize {
self.to as usize
}
fn id(&self) -> usize {
self.id.id()
}
fn set_id(&mut self, id: usize) {
self.id.set_id(id);
}
fn reverse_id(&self) -> usize {
self.reverse_id as usize
}
fn set_reverse_id(&mut self, reverse_id: usize) {
self.reverse_id = reverse_id as u32;
}
fn reverse_edge(&self, from: usize) -> Self {
Self::with_payload_impl(from, C::zero(), self.payload.clone())
}
fn payload(&self) -> &P {
&self.payload
}
}
impl<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId, P: Clone> FlowEdgeTrait<C>
for FlowEdgeRaw<C, Id, P>
{
fn capacity(&self) -> C {
self.capacity
}
fn capacity_mut(&mut self) -> &mut C {
&mut self.capacity
}
fn flow(&self, graph: &Graph<Self>) -> C {
graph[self.to as usize][self.reverse_id as usize].capacity
}
}
pub type FlowEdge<C, P> = FlowEdgeRaw<C, NoId, P>;
pub type FlowEdgeWithId<C, P> = FlowEdgeRaw<C, WithId, P>;
}
pub mod flow_edge_trait {
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;
pub trait FlowEdgeTrait<C: AdditionMonoidWithSub + PartialOrd + Copy>: EdgeTrait {
fn capacity(&self) -> C;
fn capacity_mut(&mut self) -> &mut C;
fn flow(&self, graph: &Graph<Self>) -> C;
fn push_flow(&self, flow: C) -> (usize, usize, C) {
(self.to(), self.reverse_id(), flow)
}
}
}
}
pub mod flow_graph {
use crate::algo_lib::graph::edges::flow_edge_trait::FlowEdgeTrait;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;
pub trait FlowGraph<C: AdditionMonoidWithSub + PartialOrd + Copy, E: FlowEdgeTrait<C>> {
fn push_flow(&mut self, push_data: (usize, usize, C));
}
impl<C: AdditionMonoidWithSub + PartialOrd + Copy, E: FlowEdgeTrait<C>> FlowGraph<C, E>
for Graph<E>
{
fn push_flow(&mut self, (to, reverse_id, flow): (usize, usize, C)) {
*self.edges[to][reverse_id].capacity_mut() += flow;
let from = self.edges[to][reverse_id].to();
let direct_id = self.edges[to][reverse_id].reverse_id();
let direct = &mut self.edges[from][direct_id];
assert!(flow >= C::zero() && flow <= direct.capacity());
*direct.capacity_mut() -= flow;
}
}
}
pub mod graph {
use crate::algo_lib::collections::dsu::DSU;
use crate::algo_lib::graph::edges::bi_edge::BiEdge;
use crate::algo_lib::graph::edges::edge::Edge;
use crate::algo_lib::graph::edges::edge_trait::BidirectionalEdgeTrait;
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
use std::ops::Index;
use std::ops::IndexMut;
#[derive(Clone)]
pub struct Graph<E: EdgeTrait> {
pub(super) edges: Vec<Vec<E>>,
edge_count: usize,
}
impl<E: EdgeTrait> Graph<E> {
pub fn new(vertex_count: usize) -> Self {
Self {
edges: vec![Vec::new(); vertex_count],
edge_count: 0,
}
}
pub fn add_edge(&mut self, (from, mut edge): (usize, E)) -> usize {
let to = edge.to();
assert!(to < self.edges.len());
let direct_id = self.edges[from].len();
edge.set_id(self.edge_count);
self.edges[from].push(edge);
if E::REVERSABLE {
let rev_id = self.edges[to].len();
self.edges[from][direct_id].set_reverse_id(rev_id);
let mut rev_edge = self.edges[from][direct_id].reverse_edge(from);
rev_edge.set_id(self.edge_count);
rev_edge.set_reverse_id(direct_id);
self.edges[to].push(rev_edge);
}
self.edge_count += 1;
direct_id
}
pub fn add_vertices(&mut self, cnt: usize) {
self.edges.resize(self.edges.len() + cnt, Vec::new());
}
pub fn clear(&mut self) {
self.edge_count = 0;
for ve in self.edges.iter_mut() {
ve.clear();
}
}
pub fn vertex_count(&self) -> usize {
self.edges.len()
}
pub fn edge_count(&self) -> usize {
self.edge_count
}
pub fn degrees(&self) -> Vec<usize> {
self.edges.iter().map(|v| v.len()).collect()
}
}
impl<E: BidirectionalEdgeTrait> Graph<E> {
pub fn is_tree(&self) -> bool {
if self.edge_count + 1 != self.vertex_count() {
false
} else {
self.is_connected()
}
}
pub fn is_forest(&self) -> bool {
let mut dsu = DSU::new(self.vertex_count());
for i in 0..self.vertex_count() {
for e in self[i].iter() {
if i <= e.to() && !dsu.join(i, e.to()) {
return false;
}
}
}
true
}
pub fn is_connected(&self) -> bool {
let mut dsu = DSU::new(self.vertex_count());
for i in 0..self.vertex_count() {
for e in self[i].iter() {
dsu.join(i, e.to());
}
}
dsu.set_count() == 1
}
}
impl<E: EdgeTrait> Index<usize> for Graph<E> {
type Output = [E];
fn index(&self, index: usize) -> &Self::Output {
&self.edges[index]
}
}
impl<E: EdgeTrait> IndexMut<usize> for Graph<E> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.edges[index]
}
}
impl Graph<Edge<()>> {
pub fn from_edges(n: usize, edges: &[(usize, usize)]) -> Self {
let mut graph = Self::new(n);
for &(from, to) in edges {
graph.add_edge(Edge::new(from, to));
}
graph
}
}
impl<P: Clone> Graph<Edge<P>> {
pub fn from_edges_with_payload(n: usize, edges: &[(usize, usize, P)]) -> Self {
let mut graph = Self::new(n);
for (from, to, p) in edges.iter() {
graph.add_edge(Edge::with_payload(*from, *to, p.clone()));
}
graph
}
}
impl Graph<BiEdge<()>> {
pub fn from_biedges(n: usize, edges: &[(usize, usize)]) -> Self {
let mut graph = Self::new(n);
for &(from, to) in edges {
graph.add_edge(BiEdge::new(from, to));
}
graph
}
}
impl<P: Clone> Graph<BiEdge<P>> {
pub fn from_biedges_with_payload(n: usize, edges: &[(usize, usize, P)]) -> Self {
let mut graph = Self::new(n);
for (from, to, p) in edges.iter() {
graph.add_edge(BiEdge::with_payload(*from, *to, p.clone()));
}
graph
}
}
}
pub mod max_flow {
use crate::algo_lib::collections::slice_ext::legacy_fill::LegacyFill;
use crate::algo_lib::graph::edges::flow_edge_trait::FlowEdgeTrait;
use crate::algo_lib::graph::flow_graph::FlowGraph;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::misc::recursive_function::Callable2;
use crate::algo_lib::misc::recursive_function::RecursiveFunction2;
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;
use crate::algo_lib::numbers::num_traits::ord::MinMax;
use crate::when;
use std::collections::VecDeque;
pub trait MaxFlow<C: AdditionMonoidWithSub + Ord + Copy + MinMax> {
fn max_flow(&mut self, source: usize, destination: usize) -> C;
}
impl<C: AdditionMonoidWithSub + Ord + Copy + MinMax, E: FlowEdgeTrait<C>> MaxFlow<C> for Graph<E> {
fn max_flow(&mut self, source: usize, destination: usize) -> C {
let n = self.vertex_count();
let mut dist = vec![0u32; n];
let mut next_edge = vec![0u32; n];
let inf = C::max_val();
let mut total_flow = C::zero();
let mut q = VecDeque::new();
loop {
// 1.43
dist.legacy_fill(std::u32::MAX);
dist[source] = 0;
q.push_back(source);
while !q.is_empty() {
let cur = q.pop_front().unwrap();
for e in self[cur].iter() {
if e.capacity() != C::zero() {
let next = e.to();
// 1.43
if dist[next] == std::u32::MAX {
dist[next] = dist[cur] + 1;
q.push_back(next);
}
}
}
}
// 1.43
if dist[destination] == std::u32::MAX {
break;
}
next_edge.legacy_fill(0);
let mut dinic_impl = RecursiveFunction2::new(|f, source, mut flow| -> C {
when! {
source == destination => flow,
flow == C::zero() || dist[source] == dist[destination] => C::zero(),
else => {
let mut total_pushed = C::zero();
while (next_edge[source] as usize) < self[source].len() {
let edge = &self[source][next_edge[source] as usize];
if edge.capacity() != C::zero() && dist[edge.to()] == dist[source] + 1 {
let pushed = f.call(edge.to(), flow.min(edge.capacity()));
if pushed != C::zero() {
let push_data = edge.push_flow(pushed);
self.push_flow(push_data);
flow -= pushed;
total_pushed += pushed;
if flow == C::zero() {
return total_pushed;
}
}
}
next_edge[source] += 1;
}
total_pushed
},
}
});
total_flow += dinic_impl.call(source, inf);
}
total_flow
}
}
}
}
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 !b.is_ascii_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 c.is_ascii_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::cmp::Reverse;
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]) {
self.print_per_line_iter(arg.iter());
}
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_line_iter<T: Writable, I: Iterator<Item = T>>(&mut self, iter: I) {
self.print_iter(iter);
self.put(b'\n');
}
pub fn print_per_line_iter<T: Writable, I: Iterator<Item = T>>(&mut self, iter: I) {
for e in iter {
e.write(self);
self.put(b'\n');
}
}
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(self.iter());
}
}
impl<T: Writable, const N: usize> Writable for [T; N] {
fn write(&self, output: &mut Output) {
output.print_iter(self.iter());
}
}
impl<T: Writable + ?Sized> 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}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6 B:7 C:8}
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)
}
}
impl<T: Writable> Writable for Reverse<T> {
fn write(&self, output: &mut Output) {
self.0.write(output);
}
}
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 misc {
pub mod recursive_function {
use std::marker::PhantomData;
macro_rules! recursive_function {
($name: ident, $trait: ident, ($($type: ident $arg: ident,)*)) => {
pub trait $trait<$($type, )*Output> {
fn call(&mut self, $($arg: $type,)*) -> Output;
}
pub struct $name<F, $($type, )*Output>
where
F: FnMut(&mut dyn $trait<$($type, )*Output>, $($type, )*) -> Output,
{
f: std::cell::UnsafeCell<F>,
$($arg: PhantomData<$type>,
)*
phantom_output: PhantomData<Output>,
}
impl<F, $($type, )*Output> $name<F, $($type, )*Output>
where
F: FnMut(&mut dyn $trait<$($type, )*Output>, $($type, )*) -> Output,
{
pub fn new(f: F) -> Self {
Self {
f: std::cell::UnsafeCell::new(f),
$($arg: Default::default(),
)*
phantom_output: Default::default(),
}
}
}
impl<F, $($type, )*Output> $trait<$($type, )*Output> for $name<F, $($type, )*Output>
where
F: FnMut(&mut dyn $trait<$($type, )*Output>, $($type, )*) -> Output,
{
fn call(&mut self, $($arg: $type,)*) -> Output {
unsafe { (*self.f.get())(self, $($arg, )*) }
}
}
}
}
recursive_function!(RecursiveFunction0, Callable0, ());
recursive_function!(RecursiveFunction, Callable, (Arg arg,));
recursive_function!(RecursiveFunction2, Callable2, (Arg1 arg1, Arg2 arg2,));
recursive_function!(RecursiveFunction3, Callable3, (Arg1 arg1, Arg2 arg2, Arg3 arg3,));
recursive_function!(RecursiveFunction4, Callable4, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4,));
recursive_function!(RecursiveFunction5, Callable5, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5,));
recursive_function!(RecursiveFunction6, Callable6, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6,));
recursive_function!(RecursiveFunction7, Callable7, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7,));
recursive_function!(RecursiveFunction8, Callable8, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7, Arg8 arg8,));
recursive_function!(RecursiveFunction9, Callable9, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7, Arg8 arg8, Arg9 arg9,));
}
pub mod test_type {
pub enum TestType {
Single,
MultiNumber,
MultiEof,
}
pub enum TaskType {
Classic,
Interactive,
}
}
pub mod when {
#[macro_export]
macro_rules! when {
{$($cond: expr => $then: expr,)*} => {
match () {
$(_ if $cond => $then,)*
_ => unreachable!(),
}
};
{$($cond: expr => $then: expr,)* else $(=>)? $else: expr,} => {
match () {
$(_ if $cond => $then,)*
_ => $else,
}
};
}
}
}
pub mod numbers {
pub mod num_traits {
pub mod algebra {
use crate::algo_lib::numbers::num_traits::invertible::Invertible;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Div;
use std::ops::DivAssign;
use std::ops::Mul;
use std::ops::MulAssign;
use std::ops::Neg;
use std::ops::Rem;
use std::ops::RemAssign;
use std::ops::Sub;
use std::ops::SubAssign;
pub trait Zero {
fn zero() -> Self;
}
pub trait One {
fn one() -> Self;
}
pub trait AdditionMonoid: Add<Output = Self> + AddAssign + Zero + Eq + Sized {}
impl<T: Add<Output = Self> + AddAssign + Zero + Eq> AdditionMonoid for T {}
pub trait AdditionMonoidWithSub: AdditionMonoid + Sub<Output = Self> + SubAssign {}
impl<T: AdditionMonoid + Sub<Output = Self> + SubAssign> AdditionMonoidWithSub for T {}
pub trait AdditionGroup: AdditionMonoidWithSub + Neg<Output = Self> {}
impl<T: AdditionMonoidWithSub + Neg<Output = Self>> AdditionGroup for T {}
pub trait MultiplicationMonoid: Mul<Output = Self> + MulAssign + One + Eq + Sized {}
impl<T: Mul<Output = Self> + MulAssign + One + Eq> MultiplicationMonoid for T {}
pub trait IntegerMultiplicationMonoid:
MultiplicationMonoid + Div<Output = Self> + Rem<Output = Self> + DivAssign + RemAssign
{
}
impl<T: MultiplicationMonoid + Div<Output = Self> + Rem<Output = Self> + DivAssign + RemAssign>
IntegerMultiplicationMonoid for T
{
}
pub trait MultiplicationGroup:
MultiplicationMonoid + Div<Output = Self> + DivAssign + Invertible<Output = Self>
{
}
impl<T: MultiplicationMonoid + Div<Output = Self> + DivAssign + Invertible<Output = Self>>
MultiplicationGroup for T
{
}
pub trait SemiRing: AdditionMonoid + MultiplicationMonoid {}
impl<T: AdditionMonoid + MultiplicationMonoid> SemiRing for T {}
pub trait SemiRingWithSub: AdditionMonoidWithSub + SemiRing {}
impl<T: AdditionMonoidWithSub + SemiRing> SemiRingWithSub for T {}
pub trait Ring: SemiRing + AdditionGroup {}
impl<T: SemiRing + AdditionGroup> Ring for T {}
pub trait IntegerSemiRing: SemiRing + IntegerMultiplicationMonoid {}
impl<T: SemiRing + IntegerMultiplicationMonoid> IntegerSemiRing for T {}
pub trait IntegerSemiRingWithSub: SemiRingWithSub + IntegerSemiRing {}
impl<T: SemiRingWithSub + IntegerSemiRing> IntegerSemiRingWithSub for T {}
pub trait IntegerRing: IntegerSemiRing + Ring {}
impl<T: IntegerSemiRing + Ring> IntegerRing for T {}
pub trait Field: Ring + MultiplicationGroup {}
impl<T: Ring + MultiplicationGroup> Field for T {}
macro_rules! zero_one_integer_impl {
($($t: ident)+) => {$(
impl Zero for $t {
fn zero() -> Self {
0
}
}
impl One for $t {
fn one() -> Self {
1
}
}
)+};
}
zero_one_integer_impl!(i128 i64 i32 i16 i8 isize u128 u64 u32 u16 u8 usize);
}
pub mod bit_ops {
use crate::algo_lib::numbers::num_traits::algebra::One;
use crate::algo_lib::numbers::num_traits::algebra::Zero;
use std::ops::BitAnd;
use std::ops::BitAndAssign;
use std::ops::BitOr;
use std::ops::BitOrAssign;
use std::ops::BitXor;
use std::ops::BitXorAssign;
use std::ops::Not;
use std::ops::RangeInclusive;
use std::ops::Shl;
use std::ops::ShlAssign;
use std::ops::Shr;
use std::ops::ShrAssign;
pub trait BitOps:
Copy
+ BitAnd<Output = Self>
+ BitAndAssign
+ BitOr<Output = Self>
+ BitOrAssign
+ BitXor<Output = Self>
+ BitXorAssign
+ Not<Output = Self>
+ Shl<usize, Output = Self>
+ ShlAssign<usize>
+ Shr<usize, Output = Self>
+ ShrAssign<usize>
+ Zero
+ One
+ PartialEq
{
fn bit(at: usize) -> Self {
Self::one() << at
}
fn is_set(&self, at: usize) -> bool {
(*self >> at & Self::one()) == Self::one()
}
fn set_bit(&mut self, at: usize) {
*self |= Self::bit(at)
}
fn unset_bit(&mut self, at: usize) {
*self &= !Self::bit(at)
}
#[must_use]
fn with_bit(mut self, at: usize) -> Self {
self.set_bit(at);
self
}
#[must_use]
fn without_bit(mut self, at: usize) -> Self {
self.unset_bit(at);
self
}
fn flip_bit(&mut self, at: usize) {
*self ^= Self::bit(at)
}
fn all_bits(n: usize) -> Self {
let mut res = Self::zero();
for i in 0..n {
res.set_bit(i);
}
res
}
fn iter_all(n: usize) -> RangeInclusive<Self> {
Self::zero()..=Self::all_bits(n)
}
}
impl<
T: Copy
+ BitAnd<Output = Self>
+ BitAndAssign
+ BitOr<Output = Self>
+ BitOrAssign
+ BitXor<Output = Self>
+ BitXorAssign
+ Not<Output = Self>
+ Shl<usize, Output = Self>
+ ShlAssign<usize>
+ Shr<usize, Output = Self>
+ ShrAssign<usize>
+ One
+ Zero
+ PartialEq,
> BitOps for T
{
}
pub trait Bits: BitOps {
fn bits() -> u32;
}
macro_rules! bits_integer_impl {
($($t: ident $bits: expr),+) => {$(
impl Bits for $t {
fn bits() -> u32 {
$bits
}
}
)+};
}
bits_integer_impl!(i128 128, i64 64, i32 32, i16 16, i8 8, isize 64, u128 128, u64 64, u32 32, u16 16, u8 8, usize 64);
}
pub mod invertible {
pub trait Invertible {
type Output;
fn inv(&self) -> Option<Self::Output>;
}
}
pub mod ord {
pub trait MinMax: PartialOrd {
fn min_val() -> Self;
fn max_val() -> Self;
}
macro_rules! min_max_integer_impl {
($($t: ident)+) => {$(
impl MinMax for $t {
fn min_val() -> Self {
// 1.43
std::$t::MIN
}
fn max_val() -> Self {
// 1.43
std::$t::MAX
}
}
)+};
}
min_max_integer_impl!(i128 i64 i32 i16 i8 isize u128 u64 u32 u16 u8 usize);
}
}
}
}
fn main() {
let mut sin = std::io::stdin();
let input = algo_lib::io::input::Input::new(&mut sin);
let mut stdout = std::io::stdout();
let output = algo_lib::io::output::Output::new(&mut stdout);
solution::run(input, output);
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 2072kb
input:
3 8 6 4 1 3 2 1 2 6 4 1 4 7 6 1 6 3 6 6 2 3 3 1 4 3 4 6 5 2 6 4 3 2 1 10 12 10 10 10 11 1 4 1 5 1 3 2 1 1 2 1 2 2 2 3
output:
4 2 3 5 6 2 2 0 2 3
result:
ok ok 3 cases (3 test cases)
Test #2:
score: 0
Accepted
time: 45ms
memory: 4848kb
input:
1224 11 17 14 7 3 4 2 8 13 3 15 3 4 5 11 10 2 3 3 8 6 7 11 2 3 10 4 1 3 12 1 2 5 11 9 11 6 11 10 8 15 1 5 9 14 4 11 1 6 10 7 7 6 11 4 8 4 1 11 18 3 2 14 8 2 14 13 13 9 12 14 12 5 6 8 1 10 5 8 6 8 9 6 6 7 5 12 11 6 11 13 5 1 10 7 6 14 5 6 15 2 4 11 1 1 6 4 14 14 13 9 9 3 10 12 7 5 8 13 9 14 1 9 8 4 9...
output:
7 3 4 5 6 7 8 9 10 11 12 13 15 16 17 15 2 3 0 3 4 5 6 0 2 3 4 5 6 7 8 9 11 1 3 8 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11 12 1 5 6 7 9 10 11 12 8 17 18 19 1 1 2 3 4 5 6 7 8 7 6 8 10 13 14 15 1 10 11 12 13 14 15 16 17 18 19 20 0 1 1 2 3 0 5 6 7 7 8 12 13 14 15 2 10 11 12 13 14 4 3 4 5 6 7 8 1 18 1 4 5 6 7 8 9...
result:
ok ok 1224 cases (1224 test cases)
Test #3:
score: 0
Accepted
time: 98ms
memory: 30820kb
input:
1 86289 95092 40401 911 152 1 270 135 731 347 451 283 224 338 348 166 346 12 385 590 763 939 176 232 405 122 946 397 576 795 823 546 392 33 718 444 598 954 852 185 662 732 539 172 681 386 148 76 495 163 323 711 201 278 363 531 275 66 122 823 983 234 792 102 188 985 423 804 712 419 636 318 331 693 68...
output:
81531 5956 5961 5963 5964 5968 5974 5975 5977 5979 5980 5981 5983 5984 5985 5987 5989 5992 5993 5997 6002 6003 6006 6007 6008 6013 6017 6024 6028 6031 6037 6038 6039 6042 6044 6047 6049 6054 6056 6057 6061 6066 6068 6069 6070 6072 6074 6078 6082 6086 6095 6100 6101 6102 6103 6107 6111 6113 6116 6118...
result:
ok ok 1 cases (1 test case)
Test #4:
score: 0
Accepted
time: 74ms
memory: 22812kb
input:
1 99057 99722 73893 190482185 274379837 466851670 641324039 993028302 128875937 102891466 286559847 526771097 794238060 565736409 328262657 190329865 598878250 790626887 595298790 308031819 470646878 341575785 374318107 257299536 280924175 64420619 591124604 323023069 811512407 428956686 719615923 2...
output:
82045 1 2 6 9 10 11 13 15 16 18 19 20 21 22 24 25 28 29 30 33 34 35 36 37 39 43 45 49 50 51 52 54 55 59 60 61 62 65 66 67 69 70 71 79 81 82 83 87 90 91 93 94 95 96 99 100 101 102 104 105 107 109 110 111 112 113 114 118 120 128 129 131 133 136 137 138 142 143 147 148 149 151 152 153 154 155 156 159 1...
result:
ok ok 1 cases (1 test case)
Test #5:
score: 0
Accepted
time: 142ms
memory: 27256kb
input:
1 100000 99990 27662 913840909 999962982 690565691 31053 780601566 31053 54745498 31053 5383 859704869 538124857 999962982 5383 66851413 1444277 31053 119603839 999962982 999833258 543197820 999833258 349576387 999833258 759855830 999833258 124692224 266093388 999962982 5383 100041707 999833258 2843...
output:
100891 65688 65689 65690 65692 65693 65694 65695 65696 65697 65698 65699 65700 65701 65703 65705 65706 65707 65709 65711 65712 65713 65714 65715 65716 65717 65718 65720 65721 65722 65723 65724 65725 65726 65728 65730 65731 65732 65733 65735 65736 65738 65740 65741 65743 65744 65745 65746 65748 65749...
result:
ok ok 1 cases (1 test case)
Test #6:
score: 0
Accepted
time: 30ms
memory: 19056kb
input:
1 100000 49997 21428 9380 4333 9380 999999628 49202 4333 49202 999999628 50841 4333 50841 999999628 77418 4333 77418 999999628 95722 4333 95722 999999628 144002 4333 144002 999999628 234359 4333 234359 999999628 268942 4333 268942 999999628 288956 4333 288956 999999628 415094 4333 415094 999999628 4...
output:
100000 7099 7100 7102 7103 7104 7105 7106 7108 7110 7113 7114 7117 7119 7120 7122 7123 7126 7130 7131 7134 7135 7136 7140 7145 7149 7151 7154 7157 7158 7160 7162 7163 7167 7170 7172 7173 7174 7176 7178 7182 7183 7184 7188 7190 7197 7199 7201 7204 7205 7206 7208 7209 7211 7212 7213 7215 7216 7221 722...
result:
ok ok 1 cases (1 test case)
Test #7:
score: 0
Accepted
time: 41ms
memory: 21668kb
input:
1 100000 100000 76259 931427170 7 367311884 7 646435086 7 925372747 7 371054451 7 284185575 7 695090232 7 889183241 7 615617158 7 44230096 7 293281406 7 758261641 7 685549291 7 679471071 7 723138327 7 901136691 7 49281635 7 256352978 7 320188290 7 78730802 7 788131872 7 234735044 7 664906524 7 79430...
output:
76258 463 577 797 819 881 890 900 923 993 1008 1061 1208 1267 1273 1283 1330 1357 1370 1381 1402 1438 1488 1493 1550 1556 1566 1614 1619 1655 1673 1721 1727 1758 1767 1804 1813 1829 1831 1844 1882 1906 1908 1914 1941 2020 2100 2193 2201 2209 2245 2284 2289 2303 2456 2466 2476 2484 2504 2537 2557 256...
result:
ok ok 1 cases (1 test case)
Test #8:
score: 0
Accepted
time: 22ms
memory: 19096kb
input:
1 100000 49999 24999 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 9 1 9 1000000000 10 1 10 1000000000 11 1 11 1000000000 12 1 12 1000000000 13 1 13 1000000000 14 1 14 1000000000 15 1 15 1000000000 16 1 16 1000000000 17 1 17 10...
output:
99996 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 10...
result:
ok ok 1 cases (1 test case)
Test #9:
score: 0
Accepted
time: 80ms
memory: 9448kb
input:
556 16 6 3 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 1 2 1000000000 2 1 3 1000000000 3 1 4 1000000000 4 1 5 1000000000 5 1 6 1000000000 6 2 3 3 3 3 2 4 2 2 4 4 4 32 12 6 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 9 ...
output:
14 2 4 5 32 2 4 5 10 11 12 31 1 2 4 9 10 12 13 15 8 1 13 1 3 19 4 5 7 8 9 11 1 5 6 20 3 5 6 15 3 4 6 7 33 4 6 8 9 10 12 14 31 4 6 7 9 11 12 13 16 19 2 3 4 7 8 31 2 5 6 9 10 15 1 2 6 7 28 1 2 3 5 9 11 1 19 1 4 6 8 9 23 1 5 6 9 10 12 34 1 7 10 11 12 13 14 16 31 3 5 7 8 9 12 13 14 29 2 4 5 6 7 12 17 3 ...
result:
ok ok 556 cases (556 test cases)
Test #10:
score: 0
Accepted
time: 255ms
memory: 20920kb
input:
1 100000 50000 25000 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 9 1 9 1000000000 10 1 10 1000000000 11 1 11 1000000000 12 1 12 1000000000 13 1 13 1000000000 14 1 14 1000000000 15 1 15 1000000000 16 1 16 1000000000 17 1 17 10...
output:
99996 2 3 6 9 12 13 16 17 21 23 24 26 27 29 31 32 35 37 40 42 44 49 50 52 53 58 59 62 66 67 69 70 71 72 73 74 75 76 77 79 80 81 83 84 86 87 92 93 95 96 98 99 100 101 105 106 107 108 110 112 116 120 123 125 126 127 128 131 133 134 136 139 140 142 144 150 154 161 163 166 167 168 169 171 172 175 181 18...
result:
ok ok 1 cases (1 test case)
Test #11:
score: 0
Accepted
time: 18ms
memory: 9052kb
input:
556 32 15 7 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 9 1 9 1000000000 1 2 1000000000 2 1 3 1000000000 3 1 4 1000000000 4 1 5 1000000000 5 1 6 1000000000 6 1 7 1000000000 7 1 8 1000000000 8 1 9 1000000000 9 7 6 4 3 5 4 2 2 ...
output:
28 1 2 3 7 8 10 15 11 1 4 20 3 4 23 4 7 8 9 10 26 1 2 6 7 8 17 1 10 2 31 2 3 6 8 14 1 31 2 3 4 5 7 11 14 34 2 3 4 5 7 8 15 16 3 32 1 2 6 7 8 29 3 5 28 1 6 7 8 10 12 15 31 1 2 4 5 6 8 14 25 3 5 8 9 15 2 4 5 29 1 5 6 9 11 31 1 4 7 8 15 1 2 7 29 1 3 27 1 3 6 19 5 6 7 9 25 1 6 7 9 2 1 16 2 32 2 3 9 11 1...
result:
ok ok 556 cases (556 test cases)
Test #12:
score: 0
Accepted
time: 32ms
memory: 19152kb
input:
1 100000 49999 24999 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 9 1 9 1000000000 10 1 10 1000000000 11 1 11 1000000000 12 1 12 1000000000 13 1 13 1000000000 14 1 14 1000000000 15 1 15 1000000000 16 1 16 1000000000 17 1 17 10...
output:
99996 1 3 4 6 8 10 12 17 20 22 25 26 27 29 30 32 33 34 35 37 39 42 44 47 48 49 51 54 58 59 60 61 68 69 70 72 74 75 77 78 81 82 84 85 88 89 90 91 93 94 96 97 98 100 103 110 111 112 114 115 116 120 123 124 127 129 130 133 135 136 139 140 143 145 146 149 150 152 153 160 163 169 171 174 175 176 178 179 ...
result:
ok ok 1 cases (1 test case)
Test #13:
score: 0
Accepted
time: 80ms
memory: 9372kb
input:
556 22 1 1 2 1 2 1000000000 1 2 1000000000 2 1 3 1000000000 3 1 4 1000000000 4 1 5 1000000000 5 1 6 1000000000 6 1 7 1000000000 7 1 8 1000000000 8 1 9 1000000000 9 1 10 1000000000 10 1 11 1000000000 11 2 2 18 3 1 2 1 2 1000000000 3 1 3 1000000000 1 2 1000000000 2 1 3 1000000000 3 1 4 1000000000 4 1 ...
output:
29 1 19 1 20 1 5 14 2 5 25 2 28 1 2 3 4 6 8 9 23 1 29 3 5 8 10 11 28 2 3 5 6 5 1 23 6 7 8 9 11 31 2 3 5 10 13 14 15 29 2 3 7 1 26 1 27 2 3 6 9 12 13 24 1 5 7 14 3 5 32 3 4 5 6 10 11 13 14 24 1 2 5 27 1 2 3 6 7 10 32 1 2 3 4 5 9 14 15 30 1 3 5 24 2 3 7 15 2 3 6 26 1 18 1 2 6 22 2 34 5 6 7 8 11 14 15 ...
result:
ok ok 556 cases (556 test cases)
Test #14:
score: 0
Accepted
time: 235ms
memory: 20688kb
input:
1 100000 49999 24999 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 9 1 9 1000000000 10 1 10 1000000000 11 1 11 1000000000 12 1 12 1000000000 13 1 13 1000000000 14 1 14 1000000000 15 1 15 1000000000 16 1 16 1000000000 17 1 17 10...
output:
99996 1 2 8 11 13 14 15 16 17 19 20 21 26 29 31 36 39 42 45 46 49 51 53 54 55 57 61 62 64 67 68 69 71 73 74 76 78 79 80 81 82 84 85 88 89 91 94 100 101 103 104 109 110 112 113 115 116 120 123 127 130 131 132 133 136 141 147 149 150 151 153 154 155 158 159 161 163 167 168 170 171 173 174 175 177 178 ...
result:
ok ok 1 cases (1 test case)
Test #15:
score: 0
Accepted
time: 88ms
memory: 18228kb
input:
1 100000 49998 34141 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 9 1 9 1000000000 10 1 10 1000000000 11 1 11 1000000000 12 1 12 1000000000 13 1 13 1000000000 14 1 14 1000000000 15 1 15 1000000000 16 1 16 1000000000 17 1 17 10...
output:
118282 1 3 4 5 6 7 8 10 11 12 14 15 16 22 23 25 26 27 28 29 30 31 32 33 34 35 37 38 39 42 43 44 45 46 47 48 49 50 51 53 55 56 57 58 59 60 63 65 66 67 68 69 71 72 73 74 75 76 77 79 80 81 82 83 84 85 86 87 88 89 91 92 93 94 95 98 101 102 103 104 109 113 114 115 116 117 118 119 120 122 124 127 128 129 ...
result:
ok ok 1 cases (1 test case)
Test #16:
score: 0
Accepted
time: 66ms
memory: 23752kb
input:
1 100000 82275 67072 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 9 1 9 1000000000 10 1 10 1000000000 11 1 11 1000000000 12 1 12 1000000000 13 1 13 1000000000 14 1 14 1000000000 15 1 15 1000000000 16 1 16 1000000000 17 1 17 10...
output:
119590 1 2 3 5 7 8 9 13 19 21 22 23 25 31 32 33 35 44 45 48 50 54 58 61 67 69 71 72 73 77 80 81 87 90 93 97 99 105 106 108 112 114 115 117 119 120 127 131 137 141 142 144 145 146 147 148 152 156 158 161 162 165 169 170 180 181 184 185 186 193 195 196 203 206 217 221 222 224 225 226 229 231 233 234 2...
result:
ok ok 1 cases (1 test case)
Test #17:
score: 0
Accepted
time: 59ms
memory: 12112kb
input:
556 30 12 6 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 1 2 1000000000 2 1 3 1000000000 3 1 4 1000000000 4 1 5 1000000000 5 1 6 1000000000 6 1 7 1000000000 7 1 8 1000000000 8 1 9 1000000000 9 2 6 2 8 3 4 4 4 4 8 5 3 5 7 5 8 6...
output:
29 2 4 7 8 10 11 19 2 3 5 6 7 8 9 10 11 25 2 3 4 5 6 8 9 10 11 12 13 3 4 5 31 2 3 5 6 8 9 11 12 13 14 16 17 18 19 20 21 22 23 24 25 26 27 28 36 1 4 5 8 9 10 13 18 2 3 4 5 20 3 4 6 7 8 20 2 3 4 5 6 8 9 10 11 12 13 14 16 17 18 12 2 3 4 5 8 2 3 4 6 7 8 15 2 3 4 5 6 22 2 3 5 6 7 8 9 11 12 13 15 16 17 25...
result:
ok ok 556 cases (556 test cases)
Test #18:
score: 0
Accepted
time: 147ms
memory: 27312kb
input:
1 100000 99991 75553 2 1 2 1000000000 3 1 3 1000000000 4 1 4 1000000000 5 1 5 1000000000 6 1 6 1000000000 7 1 7 1000000000 8 1 8 1000000000 9 1 9 1000000000 10 1 10 1000000000 11 1 11 1000000000 12 1 12 1000000000 13 1 13 1000000000 14 1 14 1000000000 15 1 15 1000000000 16 1 16 1000000000 17 1 17 10...
output:
101120 2 3 5 7 8 9 11 12 13 15 16 17 18 20 21 22 23 24 25 26 27 30 32 33 34 35 37 39 41 42 43 44 47 48 50 52 54 55 57 58 59 60 61 62 64 65 66 67 68 71 72 73 75 76 77 78 79 80 81 82 85 86 87 88 89 90 91 92 93 95 97 98 99 101 102 103 104 105 107 108 109 110 111 112 114 115 116 117 119 120 121 122 124 ...
result:
ok ok 1 cases (1 test case)
Extra Test:
score: 0
Extra Test Passed