QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#795456#9809. The Grand Contestucup-team296#AC ✓229ms50748kbRust59.5kb2024-11-30 20:38:562024-11-30 20:38:57

Judging History

你现在查看的是最新测评结果

  • [2024-11-30 20:38:57]
  • 评测
  • 测评结果:AC
  • 用时:229ms
  • 内存:50748kb
  • [2024-11-30 20:38:56]
  • 提交

answer

// https://contest.ucup.ac/contest/1865/problem/9809
pub mod solution {
//{"name":"L. The Grand Contest","group":"Universal Cup - The 3rd Universal Cup. Stage 19: Shenyang","url":"https://contest.ucup.ac/contest/1865/problem/9809","interactive":false,"timeLimit":1000,"tests":[{"input":"2\n6 20\n1 1 60 0\n2 2 60 0\n2 2 120 1\n1 2 180 1\n1 2 180 0\n2 2 300 1\n2 20\n1 1 300 1\n2 2 300 1\n","output":"120 160\n-1\n"}],"testType":"single","input":{"type":"stdin","fileName":null,"pattern":null},"output":{"type":"stdout","fileName":null,"pattern":null},"languages":{"java":{"taskClass":"LTheGrandContest"}}}

use crate::algo_lib::collections::default_map::default_hash_map::DefaultHashMap;
use crate::algo_lib::collections::fx_hash_map::FxHashSet;
use crate::algo_lib::collections::iter_ext::iter_copied::ItersCopied;
use crate::algo_lib::collections::min_max::MinimMaxim;
use crate::algo_lib::collections::segment_tree::SegmentTree;
use crate::algo_lib::collections::segment_tree::SegmentTreeNode;
use crate::algo_lib::collections::slice_ext::backward::Back;
use crate::algo_lib::collections::slice_ext::bounds::Bounds;
use crate::algo_lib::collections::slice_ext::compress::compress;
use crate::algo_lib::collections::slice_ext::compress::Compressed;
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::misc::direction::Direction;
use crate::algo_lib::misc::test_type::TaskType;

use crate::algo_lib::misc::test_type::TestType;
use crate::algo_lib::numbers::num_utils::PartialSums;
use crate::algo_lib::numbers::num_utils::UpperDiv;

type PreCalc = ();

fn solve(input: &mut Input, out: &mut Output, _test_case: usize, _data: &mut PreCalc) {
    let n = input.read_size();
    let p = input.read_long();
    let submissions = input.read_vec::<(usize, usize, i64, usize)>(n);

    let mut score = [0; 2];
    let mut penalty = [0; 2];
    let mut solve_at = vec![Vec::new(); 2];
    let mut solved = vec![FxHashSet::default(); 2];
    let mut submits = vec![DefaultHashMap::<_, i64>::new(); 2];
    for (team, problem, time, verdict) in submissions.copy_iter() {
        let team = team - 1;
        if solved[team].contains(&problem) {
            continue;
        }
        if verdict == 1 {
            penalty[team] += time + submits[team][problem] * p;
            score[team] += 1;
            solved[team].insert(problem);
            solve_at[team].push(time);
        } else {
            submits[team][problem] += 1;
        }
    }
    if score[0] != score[1] || score[0] == 0 {
        out.print_line(-1);
        return;
    }
    let need = if penalty[0] <= penalty[1] {
        penalty[1] - penalty[0] + 1
    } else {
        solve_at.swap(0, 1);
        penalty[0] - penalty[1]
    };

    let s0 = solve_at[0].clone();
    let s1 = solve_at[1].clone();
    let Compressed {
        order: poi,
        arrs: [s0, s1],
    } = compress([&s0, &s1]);
    let mut len = vec![0; poi.len()];
    for i in poi.indices() {
        len[i] = poi[i] - if i > 0 { poi[i - 1] } else { 0 };
    }
    let mut delta = vec![0; poi.len()];
    for (mult, s) in [(-1, s0), (1, s1)] {
        let mut cur = 0;
        let mut x = score[0];
        for i in s {
            for i in cur..=i {
                delta[i] += mult * x;
            }
            x -= 1;
            cur = i + 1;
        }
    }

    let mut ans = None;
    let total = poi[Back(0)];
    for reversed in [false, true] {
        #[derive(Default, Clone)]
        struct Node {
            sum: i64,
            max: i64,
        }
        impl SegmentTreeNode for Node {
            fn new(_left: usize, _right: usize) -> Self {
                Self::default()
            }

            fn join(&mut self, left_val: &Self, right_val: &Self) {
                self.sum = left_val.sum + right_val.sum;
                self.max = left_val.max.max(left_val.sum + right_val.max);
            }

            fn accumulate(&mut self, _value: &Self) {}

            fn reset_delta(&mut self) {}
        }
        let mut st = SegmentTree::<Node>::new(poi.len());
        let s = len.partial_sums();
        for i in poi.indices().rev() {
            st.point_update(
                i,
                Node {
                    sum: delta[i] * len[i],
                    max: (delta[i] * len[i]).max(0),
                },
            );
            if st.query(..).max < need {
                continue;
            }
            let mut need_rem = need;
            let pos = st.binary_search(
                |left, _right| {
                    if left.max >= need_rem {
                        Direction::Left
                    } else {
                        need_rem -= left.sum;
                        Direction::Right
                    }
                },
                |_, pos| pos,
            );
            assert!(delta[pos] > 0);
            let end = s[pos] + need_rem.upper_div(delta[pos]);
            let cur_len = end - s[i];
            let (start, end) = if reversed {
                (total - end, total - s[i])
            } else {
                (s[i], end)
            };
            ans.minim((cur_len, start, end));
        }
        len.reverse();
        delta.reverse();
    }
    if ans.is_none() {
        out.print_line(-1);
        return;
    }
    let (_, mut l, mut r) = ans.unwrap();
    let p0 = poi.lower_bound(&l);
    let p1 = poi.lower_bound(&r);
    if p0 != p1 {
        let p0_prev = if p0 > 0 { poi[p0 - 1] } else { 0 };
        let p1_prev = if p1 > 0 { poi[p1 - 1] } else { 0 };
        let mut total = 0;
        for i in p0 + 1..p1 {
            total += delta[i] * len[i];
        }
        total += (poi[p0] - l) * delta[p0] + (r - p1_prev) * delta[p1];
        let max = (poi[p0] - p0_prev).min(poi[p1] - p1_prev);
        let cur = delta[p1] - delta[p0];
        assert!(cur > 0);
        let shift = ((total - need) / cur).min(max);
        l -= shift;
        r -= shift;
    }
    out.print_line((l, r));
}

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.is_empty(),
        TaskType::Interactive => true,
    }
}

}
pub mod algo_lib {
#![allow(clippy::too_many_arguments)]
#![allow(clippy::type_complexity)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::legacy_numeric_constants)]

pub mod collections {
pub mod bounds {
use std::ops::RangeBounds;

pub fn clamp(range: impl RangeBounds<usize>, n: usize) -> (usize, usize) {
    let start = match range.start_bound() {
        std::ops::Bound::Included(&x) => x,
        std::ops::Bound::Excluded(&x) => x + 1,
        std::ops::Bound::Unbounded => 0,
    };
    let end = match range.end_bound() {
        std::ops::Bound::Included(&x) => x + 1,
        std::ops::Bound::Excluded(&x) => x,
        std::ops::Bound::Unbounded => n,
    };
    (start, end.min(n))
}
}
pub mod default_map {
pub mod default_hash_map {
use crate::algo_lib::collections::fx_hash_map::FxHashMap;
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, Debug)]
pub struct DefaultHashMap<K: Hash + Eq, V>(FxHashMap<K, V>, V);

impl<K: Hash + Eq, V> Deref for DefaultHashMap<K, V> {
    type Target = FxHashMap<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(FxHashMap::default(), 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 fx_hash_map {
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::cell::Cell;
use std::convert::TryInto;
use std::time::SystemTime;
use std::collections::HashMap;
use std::collections::HashSet;
use std::hash::BuildHasherDefault;
use std::hash::Hasher;
use std::mem::size_of;
use std::ops::BitXor;

pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;

pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;

#[derive(Default)]
pub struct FxHasher {
    hash: usize,
}

thread_local! {
    static K: Cell<usize> = Cell::new(
        ((SystemTime::UNIX_EPOCH.elapsed().unwrap().as_nanos().wrapping_mul(2) + 1) & 0xFFFFFFFFFFFFFFFF) as usize
    );
}

impl FxHasher {
    #[inline]
    fn add_to_hash(&mut self, i: usize) {
        self.hash = self
            .hash
            .rotate_left(5)
            .bitxor(i)
            .wrapping_mul(K.with(|k| k.get()));
    }
}

impl Hasher for FxHasher {
    #[inline]
    fn write(&mut self, mut bytes: &[u8]) {
        let read_usize = |bytes: &[u8]| u64::from_ne_bytes(bytes[..8].try_into().unwrap());

        let mut hash = FxHasher { hash: self.hash };
        while bytes.len() >= size_of::<usize>() {
            hash.add_to_hash(read_usize(bytes) as usize);
            bytes = &bytes[size_of::<usize>()..];
        }
        if (size_of::<usize>() > 4) && (bytes.len() >= 4) {
            hash.add_to_hash(u32::from_ne_bytes(bytes[..4].try_into().unwrap()) as usize);
            bytes = &bytes[4..];
        }
        if (size_of::<usize>() > 2) && bytes.len() >= 2 {
            hash.add_to_hash(u16::from_ne_bytes(bytes[..2].try_into().unwrap()) as usize);
            bytes = &bytes[2..];
        }
        if (size_of::<usize>() > 1) && !bytes.is_empty() {
            hash.add_to_hash(bytes[0] as usize);
        }
        self.hash = hash.hash;
    }

    #[inline]
    fn write_u8(&mut self, i: u8) {
        self.add_to_hash(i as usize);
    }

    #[inline]
    fn write_u16(&mut self, i: u16) {
        self.add_to_hash(i as usize);
    }

    #[inline]
    fn write_u32(&mut self, i: u32) {
        self.add_to_hash(i as usize);
    }

    #[inline]
    fn write_u64(&mut self, i: u64) {
        self.add_to_hash(i as usize);
    }

    #[inline]
    fn write_usize(&mut self, i: usize) {
        self.add_to_hash(i);
    }

    #[inline]
    fn finish(&self) -> u64 {
        self.hash as u64
    }
}
}
pub mod iter_ext {
pub mod iter_copied {
use std::iter::Chain;
use std::iter::Copied;
use std::iter::Enumerate;
use std::iter::Filter;
use std::iter::Map;
use std::iter::Rev;
use std::iter::Skip;
use std::iter::StepBy;
use std::iter::Sum;
use std::iter::Take;
use std::iter::Zip;

pub trait ItersCopied<'a, T: 'a + Copy>: Sized + 'a
where
    &'a Self: IntoIterator<Item = &'a T>,
{
    fn copy_iter(&'a self) -> Copied<<&'a Self as IntoIterator>::IntoIter> {
        self.into_iter().copied()
    }
    fn copy_enumerate(&'a self) -> Enumerate<Copied<<&'a Self as IntoIterator>::IntoIter>> {
        self.copy_iter().enumerate()
    }
    fn copy_rev(&'a self) -> Rev<Copied<<&'a Self as IntoIterator>::IntoIter>>
    where
        Copied<<&'a Self as IntoIterator>::IntoIter>: DoubleEndedIterator,
    {
        self.copy_iter().rev()
    }
    fn copy_skip(&'a self, n: usize) -> Skip<Copied<<&'a Self as IntoIterator>::IntoIter>> {
        self.copy_iter().skip(n)
    }
    fn copy_take(&'a self, n: usize) -> Take<Copied<<&'a Self as IntoIterator>::IntoIter>> {
        self.copy_iter().take(n)
    }
    fn copy_chain<V>(
        &'a self,
        chained: &'a V,
    ) -> Chain<
        Copied<<&'a Self as IntoIterator>::IntoIter>,
        Copied<<&'a V as IntoIterator>::IntoIter>,
    >
    where
        &'a V: IntoIterator<Item = &'a T>,
    {
        self.copy_iter().chain(chained.into_iter().copied())
    }
    fn copy_zip<V>(
        &'a self,
        other: &'a V,
    ) -> Zip<Copied<<&'a Self as IntoIterator>::IntoIter>, Copied<<&'a V as IntoIterator>::IntoIter>>
    where
        &'a V: IntoIterator<Item = &'a T>,
    {
        self.copy_iter().zip(other.into_iter().copied())
    }
    fn copy_max(&'a self) -> T
    where
        T: Ord,
    {
        self.copy_iter().max().unwrap()
    }
    fn copy_max_by_key<B, F>(&'a self, f: F) -> T
    where
        F: FnMut(&T) -> B,
        B: Ord,
    {
        self.copy_iter().max_by_key(f).unwrap()
    }
    fn copy_min(&'a self) -> T
    where
        T: Ord,
    {
        self.copy_iter().min().unwrap()
    }
    fn copy_min_by_key<B, F>(&'a self, f: F) -> T
    where
        F: FnMut(&T) -> B,
        B: Ord,
    {
        self.copy_iter().min_by_key(f).unwrap()
    }
    fn copy_sum(&'a self) -> T
    where
        T: Sum<T>,
    {
        self.copy_iter().sum()
    }
    fn copy_map<F, U>(&'a self, f: F) -> Map<Copied<<&'a Self as IntoIterator>::IntoIter>, F>
    where
        F: FnMut(T) -> U,
    {
        self.copy_iter().map(f)
    }
    fn copy_all(&'a self, f: impl FnMut(T) -> bool) -> bool {
        self.copy_iter().all(f)
    }
    fn copy_any(&'a self, f: impl FnMut(T) -> bool) -> bool {
        self.copy_iter().any(f)
    }
    fn copy_step_by(&'a self, step: usize) -> StepBy<Copied<<&'a Self as IntoIterator>::IntoIter>> {
        self.copy_iter().step_by(step)
    }
    fn copy_filter<F: FnMut(&T) -> bool>(
        &'a self,
        f: F,
    ) -> Filter<Copied<<&'a Self as IntoIterator>::IntoIter>, F> {
        self.copy_iter().filter(f)
    }
    fn copy_fold<Acc, F>(&'a self, init: Acc, f: F) -> Acc
    where
        F: FnMut(Acc, T) -> Acc,
    {
        self.copy_iter().fold(init, f)
    }
    fn copy_reduce<F>(&'a self, f: F) -> Option<T>
    where
        F: FnMut(T, T) -> T,
    {
        self.copy_iter().reduce(f)
    }
    fn copy_position<P>(&'a self, predicate: P) -> Option<usize>
    where
        P: FnMut(T) -> bool,
    {
        self.copy_iter().position(predicate)
    }
}

impl<'a, U: 'a, T: 'a + Copy> ItersCopied<'a, T> for U where &'a U: IntoIterator<Item = &'a T> {}
}
}
pub mod min_max {
pub trait MinimMaxim<Rhs = Self>: PartialOrd + Sized {
    fn minim(&mut self, other: Rhs) -> bool;

    fn maxim(&mut self, other: Rhs) -> bool;
}

impl<T: PartialOrd> MinimMaxim for T {
    fn minim(&mut self, other: Self) -> bool {
        if other < *self {
            *self = other;
            true
        } else {
            false
        }
    }

    fn maxim(&mut self, other: Self) -> bool {
        if other > *self {
            *self = other;
            true
        } else {
            false
        }
    }
}

impl<T: PartialOrd> MinimMaxim<T> for Option<T> {
    fn minim(&mut self, other: T) -> bool {
        match self {
            None => {
                *self = Some(other);
                true
            }
            Some(v) => v.minim(other),
        }
    }

    fn maxim(&mut self, other: T) -> bool {
        match self {
            None => {
                *self = Some(other);
                true
            }
            Some(v) => v.maxim(other),
        }
    }
}
}
pub mod segment_tree {
use crate::algo_lib::collections::bounds::clamp;
use crate::algo_lib::misc::direction::Direction;
use crate::algo_lib::numbers::num_traits::algebra::One;
use crate::algo_lib::numbers::num_traits::algebra::Zero;
use crate::when;
use std::marker::PhantomData;
use std::ops::Add;
use std::ops::MulAssign;
use std::ops::RangeBounds;

pub trait SegmentTreeNode {
    fn new(left: usize, right: usize) -> Self;
    fn join(&mut self, left_val: &Self, right_val: &Self);
    fn accumulate(&mut self, value: &Self);
    fn reset_delta(&mut self);
}

pub trait Pushable<T>: SegmentTreeNode {
    fn push(&mut self, delta: T);
}

impl<T: SegmentTreeNode> Pushable<&T> for T {
    fn push(&mut self, delta: &T) {
        self.accumulate(delta);
    }
}

impl<T: SegmentTreeNode> Pushable<T> for T {
    fn push(&mut self, delta: T) {
        *self = delta;
    }
}

pub trait QueryResult<Result, Args>: SegmentTreeNode {
    fn empty_result(args: &Args) -> Result;
    fn result(&self, args: &Args) -> Result;
    fn join_results(
        left_res: Result,
        right_res: Result,
        args: &Args,
        left: usize,
        mid: usize,
        right: usize,
    ) -> Result;
}

impl<T: SegmentTreeNode + Clone> QueryResult<T, ()> for T {
    fn empty_result(_: &()) -> T {
        Self::new(0, 0)
    }

    fn result(&self, _: &()) -> T {
        self.clone()
    }

    fn join_results(left_res: T, right_res: T, _: &(), left: usize, mid: usize, right: usize) -> T {
        when! {
            left == mid => right_res,
            right == mid => left_res,
            else => {
                let mut res = Self::new(left, right);
                res.join(&left_res, &right_res);
                res
            },
        }
    }
}

impl<
        Key: Add<Output = Key> + MulAssign<Delta> + Zero + Copy,
        Delta: MulAssign<Delta> + One + Copy,
    > SegmentTreeNode for (Key, Delta)
{
    fn new(_left: usize, _right: usize) -> Self {
        (Key::zero(), Delta::one())
    }

    fn join(&mut self, left_val: &Self, right_val: &Self) {
        self.0 = left_val.0 + right_val.0;
    }

    fn accumulate(&mut self, value: &Self) {
        self.0 *= value.1;
        self.1 *= value.1;
    }

    fn reset_delta(&mut self) {
        self.1 = Delta::one();
    }
}

#[derive(Clone)]
pub struct SegmentTree<Node> {
    n: usize,
    nodes: Vec<Node>,
}

impl<Node: SegmentTreeNode> SegmentTree<Node> {
    pub fn new(n: usize) -> Self {
        Self::from_generator(n, |left| Node::new(left, left + 1))
    }

    pub fn from_array(arr: Vec<Node>) -> Self {
        let n = arr.len();
        let mut iter = arr.into_iter();
        Self::from_generator(n, |_| iter.next().unwrap())
    }

    pub fn from_generator<F>(n: usize, gen: F) -> Self
    where
        F: FnMut(usize) -> Node,
    {
        if n == 0 {
            return Self {
                n,
                nodes: vec![Node::new(0, 0)],
            };
        }
        let mut res = Self {
            n,
            nodes: Vec::with_capacity(2 * n - 1),
        };
        res.init(gen);
        res
    }

    fn init<F>(&mut self, mut f: F)
    where
        F: FnMut(usize) -> Node,
    {
        self.init_impl(2 * self.n - 2, 0, self.n, &mut f);
    }

    fn init_impl<F>(&mut self, root: usize, left: usize, right: usize, f: &mut F)
    where
        F: FnMut(usize) -> Node,
    {
        if left + 1 == right {
            self.nodes.push(f(left));
        } else {
            let mid = left + ((right - left) >> 1);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            self.init_impl(left_child, left, mid, f);
            self.init_impl(right_child, mid, right, f);
            let mut node = Node::new(left, right);
            node.join(&self.nodes[left_child], &self.nodes[right_child]);
            self.nodes.push(node);
        }
    }

    pub fn point_query(&mut self, at: usize) -> &Node {
        assert!(at < self.n);
        self.do_point_query(self.nodes.len() - 1, 0, self.n, at)
    }

    fn do_point_query(&mut self, root: usize, left: usize, right: usize, at: usize) -> &Node {
        if left + 1 == right {
            &self.nodes[root]
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            if at < mid {
                self.do_point_query(left_child, left, mid, at)
            } else {
                self.do_point_query(right_child, mid, right, at)
            }
        }
    }

    pub fn point_update<T>(&mut self, at: usize, val: T)
    where
        Node: Pushable<T>,
    {
        assert!(at < self.n);
        self.do_point_update(self.nodes.len() - 1, 0, self.n, at, val);
    }

    fn do_point_update<T>(&mut self, root: usize, left: usize, right: usize, at: usize, val: T)
    where
        Node: Pushable<T>,
    {
        if left + 1 == right {
            self.nodes[root].push(val);
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            if at < mid {
                self.do_point_update(left_child, left, mid, at, val);
            } else {
                self.do_point_update(right_child, mid, right, at, val);
            }
            self.join(root, mid, right);
        }
    }

    pub fn point_through_update<'a, T>(&mut self, at: usize, val: &'a T)
    where
        Node: Pushable<&'a T>,
    {
        assert!(at < self.n);
        self.do_point_through_update(self.nodes.len() - 1, 0, self.n, at, val);
    }

    fn do_point_through_update<'a, T>(
        &mut self,
        root: usize,
        left: usize,
        right: usize,
        at: usize,
        val: &'a T,
    ) where
        Node: Pushable<&'a T>,
    {
        self.nodes[root].push(val);
        if left + 1 != right {
            let mid = (left + right) >> 1;
            self.push_down(root, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            if at < mid {
                self.do_point_through_update(left_child, left, mid, at, val);
            } else {
                self.do_point_through_update(right_child, mid, right, at, val);
            }
        }
    }

    pub fn point_operation<Args, Res>(
        &mut self,
        op: &mut dyn PointOperation<Node, Args, Res>,
        args: Args,
    ) -> Res {
        assert_ne!(self.n, 0);
        self.do_point_operation(op, self.nodes.len() - 1, 0, self.n, args)
    }

    fn do_point_operation<Args, Res>(
        &mut self,
        op: &mut dyn PointOperation<Node, Args, Res>,
        root: usize,
        left: usize,
        right: usize,
        args: Args,
    ) -> Res {
        if left + 1 == right {
            op.adjust_leaf(&mut self.nodes[root], left, args)
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            let (l, r) = self.nodes.split_at_mut(root);
            let (l, m) = l.split_at_mut(right_child);
            let direction = op.select_branch(
                &mut r[0],
                &mut l[left_child],
                &mut m[0],
                &args,
                left,
                mid,
                right,
            );
            let res = match direction {
                Direction::Left => self.do_point_operation(op, left_child, left, mid, args),
                Direction::Right => self.do_point_operation(op, right_child, mid, right, args),
            };
            self.join(root, mid, right);
            res
        }
    }

    pub fn update<'a, T>(&mut self, range: impl RangeBounds<usize>, val: &'a T)
    where
        Node: Pushable<&'a T>,
    {
        let (from, to) = clamp(range, self.n);
        self.do_update(self.nodes.len() - 1, 0, self.n, from, to, val)
    }

    pub fn do_update<'a, T>(
        &mut self,
        root: usize,
        left: usize,
        right: usize,
        from: usize,
        to: usize,
        val: &'a T,
    ) where
        Node: Pushable<&'a T>,
    {
        when! {
            left >= to || right <= from => {},
            left >= from && right <= to => self.nodes[root].push(val),
            else => {
                let mid = (left + right) >> 1;
                self.push_down(root, mid, right);
                let left_child = root - 2 * (right - mid);
                let right_child = root - 1;
                self.do_update(left_child, left, mid, from, to, val);
                self.do_update(right_child, mid, right, from, to, val);
                self.join(root, mid, right);
            },
        }
    }

    pub fn operation<Args, Res>(
        &mut self,
        range: impl RangeBounds<usize>,
        op: &mut dyn Operation<Node, Args, Res>,
        args: &Args,
    ) -> Res {
        let (from, to) = clamp(range, self.n);
        self.do_operation(self.nodes.len() - 1, 0, self.n, from, to, op, args)
    }

    pub fn do_operation<Args, Res>(
        &mut self,
        root: usize,
        left: usize,
        right: usize,
        from: usize,
        to: usize,
        op: &mut dyn Operation<Node, Args, Res>,
        args: &Args,
    ) -> Res {
        when! {
            left >= to || right <= from => op.empty_result(left, right, args),
            left >= from && right <= to => op.process_result(&mut self.nodes[root], args),
            else => {
                let mid = (left + right) >> 1;
                self.push_down(root, mid, right);
                let left_child = root - 2 * (right - mid);
                let right_child = root - 1;
                let left_result = self.do_operation(left_child, left, mid, from, to, op, args);
                let right_result = self.do_operation(right_child, mid, right, from, to, op, args);
                self.join(root, mid, right);
                op.join_results(left_result, right_result, args)
            },
        }
    }

    pub fn binary_search<Res>(
        &mut self,
        wh: impl FnMut(&Node, &Node) -> Direction,
        calc: impl FnMut(&Node, usize) -> Res,
    ) -> Res {
        self.do_binary_search(self.nodes.len() - 1, 0, self.n, wh, calc)
    }

    fn do_binary_search<Res>(
        &mut self,
        root: usize,
        left: usize,
        right: usize,
        mut wh: impl FnMut(&Node, &Node) -> Direction,
        mut calc: impl FnMut(&Node, usize) -> Res,
    ) -> Res {
        if left + 1 == right {
            calc(&self.nodes[root], left)
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            let direction = wh(&self.nodes[left_child], &self.nodes[right_child]);
            match direction {
                Direction::Left => self.do_binary_search(left_child, left, mid, wh, calc),
                Direction::Right => self.do_binary_search(right_child, mid, right, wh, calc),
            }
        }
    }

    fn join(&mut self, root: usize, mid: usize, right: usize) {
        let left_child = root - 2 * (right - mid);
        let right_child = root - 1;
        let (left_node, right_node) = self.nodes.split_at_mut(root);
        right_node[0].join(&left_node[left_child], &left_node[right_child]);
    }

    fn do_push_down(&mut self, parent: usize, to: usize) {
        let (left_nodes, right_nodes) = self.nodes.split_at_mut(parent);
        left_nodes[to].accumulate(&right_nodes[0]);
    }

    fn push_down(&mut self, root: usize, mid: usize, right: usize) {
        self.do_push_down(root, root - 2 * (right - mid));
        self.do_push_down(root, root - 1);
        self.nodes[root].reset_delta();
    }

    pub fn query<T>(&mut self, range: impl RangeBounds<usize>) -> T
    where
        Node: QueryResult<T, ()>,
    {
        let (from, to) = clamp(range, self.n);
        if from >= to {
            Node::empty_result(&())
        } else {
            self.do_query(self.nodes.len() - 1, 0, self.n, from, to, &())
        }
    }

    pub fn query_with_args<T, Args>(&mut self, range: impl RangeBounds<usize>, args: &Args) -> T
    where
        Node: QueryResult<T, Args>,
    {
        let (from, to) = clamp(range, self.n);
        if from >= to {
            Node::empty_result(args)
        } else {
            self.do_query(self.nodes.len() - 1, 0, self.n, from, to, args)
        }
    }

    fn do_query<T, Args>(
        &mut self,
        root: usize,
        left: usize,
        right: usize,
        from: usize,
        to: usize,
        args: &Args,
    ) -> T
    where
        Node: QueryResult<T, Args>,
    {
        if left >= from && right <= to {
            self.nodes[root].result(args)
        } else {
            let mid = (left + right) >> 1;
            self.push_down(root, mid, right);
            let left_child = root - 2 * (right - mid);
            let right_child = root - 1;
            when! {
                to <= mid => self.do_query(left_child, left, mid, from, to, args),
                from >= mid => self.do_query(right_child, mid, right, from, to, args),
                else => {
                    let left_result = self.do_query(left_child, left, mid, from, to, args);
                    let right_result = self.do_query(right_child, mid, right, from, to, args);
                    Node::join_results(left_result, right_result, args, left, mid, right)
                },
            }
        }
    }
}

pub trait PointOperation<Node: SegmentTreeNode, Args, Res = Node> {
    fn adjust_leaf(&mut self, leaf: &mut Node, at: usize, args: Args) -> Res;
    fn select_branch(
        &mut self,
        root: &mut Node,
        left_child: &mut Node,
        right_child: &mut Node,
        args: &Args,
        left: usize,
        mid: usize,
        right: usize,
    ) -> Direction;
}

pub struct PointOperationClosure<'s, Node: SegmentTreeNode, Args, Res = Node> {
    adjust_leaf: Box<dyn FnMut(&mut Node, usize, Args) -> Res + 's>,
    select_branch: Box<
        dyn FnMut(&mut Node, &mut Node, &mut Node, &Args, usize, usize, usize) -> Direction + 's,
    >,
    phantom_node: PhantomData<Node>,
    phantom_args: PhantomData<Args>,
    phantom_res: PhantomData<Res>,
}

impl<'s, Node: SegmentTreeNode, Args, Res> PointOperationClosure<'s, Node, Args, Res> {
    pub fn new<F1, F2>(adjust_leaf: F1, select_branch: F2) -> Self
    where
        F1: FnMut(&mut Node, usize, Args) -> Res + 's,
        F2: FnMut(&mut Node, &mut Node, &mut Node, &Args, usize, usize, usize) -> Direction + 's,
    {
        Self {
            adjust_leaf: Box::new(adjust_leaf),
            select_branch: Box::new(select_branch),
            phantom_node: Default::default(),
            phantom_args: Default::default(),
            phantom_res: Default::default(),
        }
    }
}

impl<'s, Node: SegmentTreeNode, Args, Res> PointOperation<Node, Args, Res>
    for PointOperationClosure<'s, Node, Args, Res>
{
    fn adjust_leaf(&mut self, leaf: &mut Node, at: usize, args: Args) -> Res {
        (self.adjust_leaf)(leaf, at, args)
    }

    fn select_branch(
        &mut self,
        root: &mut Node,
        left_child: &mut Node,
        right_child: &mut Node,
        args: &Args,
        left: usize,
        mid: usize,
        right: usize,
    ) -> Direction {
        (self.select_branch)(root, left_child, right_child, args, left, mid, right)
    }
}

pub trait Operation<Node: SegmentTreeNode, Args, Res = Node> {
    fn process_result(&mut self, node: &mut Node, args: &Args) -> Res;
    fn join_results(&mut self, left_res: Res, right_res: Res, args: &Args) -> Res;
    fn empty_result(&mut self, left: usize, right: usize, args: &Args) -> Res;
}

pub struct OperationClosure<'s, Node: SegmentTreeNode, Args, Res = Node> {
    process_result: Box<dyn FnMut(&mut Node, &Args) -> Res + 's>,
    join_results: Box<dyn FnMut(Res, Res, &Args) -> Res + 's>,
    empty_result: Box<dyn FnMut(usize, usize, &Args) -> Res + 's>,
    phantom_node: PhantomData<Node>,
    phantom_args: PhantomData<Args>,
    phantom_res: PhantomData<Res>,
}

impl<'s, Node: SegmentTreeNode, Args, Res> OperationClosure<'s, Node, Args, Res> {
    pub fn new<F1, F2, F3>(process_result: F1, join_results: F2, empty_result: F3) -> Self
    where
        F1: FnMut(&mut Node, &Args) -> Res + 's,
        F2: FnMut(Res, Res, &Args) -> Res + 's,
        F3: FnMut(usize, usize, &Args) -> Res + 's,
    {
        Self {
            process_result: Box::new(process_result),
            join_results: Box::new(join_results),
            empty_result: Box::new(empty_result),
            phantom_node: Default::default(),
            phantom_args: Default::default(),
            phantom_res: Default::default(),
        }
    }
}

impl<'s, Node: SegmentTreeNode, Args, Res> Operation<Node, Args, Res>
    for OperationClosure<'s, Node, Args, Res>
{
    fn process_result(&mut self, node: &mut Node, args: &Args) -> Res {
        (self.process_result)(node, args)
    }

    fn join_results(&mut self, left_res: Res, right_res: Res, args: &Args) -> Res {
        (self.join_results)(left_res, right_res, args)
    }

    fn empty_result(&mut self, left: usize, right: usize, args: &Args) -> Res {
        (self.empty_result)(left, right, args)
    }
}
}
pub mod slice_ext {
pub mod backward {
use std::ops::Index;
use std::ops::IndexMut;

pub struct Back(pub usize);

impl<T> Index<Back> for [T] {
    type Output = T;

    fn index(&self, index: Back) -> &Self::Output {
        &self[self.len() - index.0 - 1]
    }
}

impl<T> IndexMut<Back> for [T] {
    fn index_mut(&mut self, index: Back) -> &mut Self::Output {
        &mut self[self.len() - index.0 - 1]
    }
}

impl<T> Index<Back> for Vec<T> {
    type Output = T;

    fn index(&self, index: Back) -> &Self::Output {
        self.as_slice().index(index)
    }
}

impl<T> IndexMut<Back> for Vec<T> {
    fn index_mut(&mut self, index: Back) -> &mut Self::Output {
        self.as_mut_slice().index_mut(index)
    }
}
}
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 compress {
use crate::algo_lib::collections::slice_ext::bounds::Bounds;
use std::mem::MaybeUninit;

#[derive(Eq, PartialEq, Debug)]
pub struct Compressed<T, const N: usize> {
    pub order: Vec<T>,
    pub arrs: [Vec<usize>; N],
}

pub fn compress<T: Ord + Clone, const N: usize>(vs: [&[T]; N]) -> Compressed<T, N> {
    let mut size = 0;
    for v in &vs {
        size += v.len();
    }
    let mut all = Vec::with_capacity(size);
    for v in &vs {
        all.extend_from_slice(v);
    }
    all.sort();
    all.dedup();
    let arrs = unsafe {
        let mut arr: MaybeUninit<[Vec<usize>; N]> = MaybeUninit::uninit();
        for (i, v) in vs.iter().enumerate() {
            let mut cur = Vec::with_capacity(vs[i].len());
            for vv in *v {
                cur.push(all.bin_search(vv).unwrap());
            }
            arr.as_mut_ptr().cast::<Vec<usize>>().add(i).write(cur);
        }
        arr.assume_init()
    };
    Compressed { order: all, arrs }
}
}
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;
use std::mem::MaybeUninit;

pub struct Input<'s> {
    input: &'s mut (dyn Read + Send),
    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 + Send)) -> 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 + Send), 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) -> u8 {
        self.skip_whitespace();
        self.get().unwrap()
    }

    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 u8 {
    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)
    }
}

impl<T: Readable, const SIZE: usize> Readable for [T; SIZE] {
    fn read(input: &mut Input) -> Self {
        unsafe {
            let mut res = MaybeUninit::<[T; SIZE]>::uninit();
            for i in 0..SIZE {
                let ptr: *mut T = (*res.as_mut_ptr()).as_mut_ptr();
                ptr.add(i).write(input.read::<T>());
            }
            res.assume_init()
        }
    }
}

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 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,
    precision: Option<usize>,
}

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,
            precision: None,
        }
    }

    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,
            precision: None,
        }
    }

    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;
    }
    pub fn set_precision(&mut self, precision: usize) {
        self.precision = Some(precision);
    }
    pub fn reset_precision(&mut self) {
        self.precision = None;
    }
    pub fn get_precision(&self) -> Option<usize> {
        self.precision
    }
}

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 Writable for u8 {
    fn write(&self, output: &mut Output) {
        output.put(*self);
    }
}

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!(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 direction {
#[derive(Copy, Clone)]
pub enum Direction {
    Left,
    Right,
}
}
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 as_index {
pub trait AsIndex {
    fn from_index(idx: usize) -> Self;
    fn to_index(self) -> usize;
}

macro_rules! from_index_impl {
    ($($t: ident)+) => {$(
        impl AsIndex for $t {
            fn from_index(idx: usize) -> Self {
                idx as $t
            }

            fn to_index(self) -> usize {
                self as usize
            }
        }
    )+};
}

from_index_impl!(i128 i64 i32 i16 i8 isize u128 u64 u32 u16 u8 usize);
}
pub mod invertible {
pub trait Invertible {
    type Output;

    fn inv(&self) -> Option<Self::Output>;
}
}
}
pub mod num_utils {
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoid;
use crate::algo_lib::numbers::num_traits::algebra::IntegerRing;
use crate::algo_lib::numbers::num_traits::algebra::MultiplicationMonoid;
use crate::algo_lib::numbers::num_traits::as_index::AsIndex;

pub fn factorials<T: MultiplicationMonoid + Copy + AsIndex>(len: usize) -> Vec<T> {
    let mut res = Vec::new();
    if len > 0 {
        res.push(T::one());
    }
    while res.len() < len {
        res.push((*res.last().unwrap()) * T::from_index(res.len()));
    }
    res
}

pub fn powers<T: MultiplicationMonoid + Copy>(base: T, len: usize) -> Vec<T> {
    let mut res = Vec::new();
    if len > 0 {
        res.push(T::one());
    }
    while res.len() < len {
        res.push((*res.last().unwrap()) * base);
    }
    res
}

pub struct Powers<T: MultiplicationMonoid + Copy> {
    small: Vec<T>,
    big: Vec<T>,
}

impl<T: MultiplicationMonoid + Copy> Powers<T> {
    pub fn new(base: T, len: usize) -> Self {
        let small = powers(base, len);
        let big = powers(small[len - 1] * base, len);
        Self { small, big }
    }

    pub fn power(&self, exp: usize) -> T {
        debug_assert!(exp < self.small.len() * self.small.len());
        self.big[exp / self.small.len()] * self.small[exp % self.small.len()]
    }
}

pub fn factorial<T: MultiplicationMonoid + AsIndex>(n: usize) -> T {
    let mut res = T::one();
    for i in 1..=n {
        res *= T::from_index(i);
    }
    res
}

pub trait PartialSums<T> {
    fn partial_sums(&self) -> Vec<T>;
}

impl<T: AdditionMonoid + Copy> PartialSums<T> for [T] {
    fn partial_sums(&self) -> Vec<T> {
        let mut res = Vec::with_capacity(self.len() + 1);
        res.push(T::zero());
        for i in self.iter() {
            res.push(*res.last().unwrap() + *i);
        }
        res
    }
}

pub trait UpperDiv {
    fn upper_div(self, other: Self) -> Self;
}

impl<T: IntegerRing + Copy> UpperDiv for T {
    fn upper_div(self, other: Self) -> Self {
        (self + other - Self::one()) / other
    }
}
}
}
}
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: 2168kb

input:

2
6 20
1 1 60 0
2 2 60 0
2 2 120 1
1 2 180 1
1 2 180 0
2 2 300 1
2 20
1 1 300 1
2 2 300 1

output:

120 160
-1

result:

ok 3 number(s): "120 160 -1"

Test #2:

score: 0
Accepted
time: 106ms
memory: 2044kb

input:

400000
1 1
1 1000000000 1000000000000 1
1 1
2 1000000000 1 0
1 1
2 1 1000000000000 1
1 1
1 1 1000000000000 1
1 1
2 1000000000 1000000000000 0
1 1
2 1 1 0
1 1000000000000
2 1000000000 1 0
1 1000000000000
1 1 1 0
1 1000000000000
1 1 1 1
1 1000000000000
2 1000000000 1000000000000 0
1 1
1 1000000000 1 0...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 400000 numbers

Test #3:

score: 0
Accepted
time: 57ms
memory: 2340kb

input:

10000
4 542575683220
2 101300788 7308006925 1
1 604560531 257884671293 0
1 46911674 422781533607 0
2 10550533 771273976896 1
116 793781361888
1 819065134 15224463201 1
2 552573547 15992997563 1
2 424217 27032314690 0
2 70252887 41541882886 0
2 274093456 46129251985 0
1 458919850 46344406806 1
1 8416...

output:

-1
-1
-1
-1
-1
66660446969 904724933033
-1
-1
-1
-1
-1
-1
37226106549 311799565893
-1
-1
-1
-1
-1
-1
48301734080 375528816957
-1
-1
-1
459021288402 632610827258
-1
-1
-1
-1
-1
-1
-1
688320095661 898231263806
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
21800224424...

result:

ok 10846 numbers

Test #4:

score: 0
Accepted
time: 39ms
memory: 2416kb

input:

1000
41 699991536758
1 846433454 45030190307 1
2 882516075 48235731920 1
1 488580715 68600854082 1
2 467682948 92731902940 1
1 218024396 138543808852 1
2 124969525 150196554430 0
2 989301314 181283691649 1
2 752581868 202920989593 0
2 164838619 269703109427 0
1 696316428 295229433897 0
2 711333918 3...

output:

329739379675 908226682656
-1
-1
-1
-1
-1
424620801981 831021050071
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
6963797897 888755778656
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
568768655870 677350535270
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 1042 numbers

Test #5:

score: 0
Accepted
time: 48ms
memory: 3552kb

input:

100
5622 365182448552
2 639763453 293138584 0
1 150269480 461335412 1
1 215320018 935778069 1
2 455090474 986867198 1
2 137209887 1025838937 1
1 639542200 1323284104 0
2 975624632 1331236944 1
1 419729668 1535875032 0
1 754484749 1638561677 1
2 718600604 2047704086 0
2 793817561 2082808091 1
2 89416...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

ok 100 numbers

Test #6:

score: 0
Accepted
time: 49ms
memory: 7900kb

input:

10
17647 497735816936
2 674642608 86555331 1
1 362561577 201254993 1
2 311798376 317505931 0
1 997152835 354905086 0
1 501042015 406191428 1
2 346791377 498440233 0
2 883536093 569248570 0
1 242082992 714310537 1
1 149897006 726750432 1
2 951017299 800159980 0
2 258554143 816615965 0
1 681430878 825...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

ok 10 numbers

Test #7:

score: 0
Accepted
time: 46ms
memory: 23980kb

input:

1
400000 263629556026
2 954826535 1576313 1
1 970048058 1644473 1
1 100070583 1779363 0
1 862973854 2602197 0
1 544583759 6163089 1
1 292939527 13244435 1
1 818324382 16877678 1
1 255969879 32429661 0
1 578761398 33091132 1
2 337038014 34601245 0
1 46604309 39135309 0
2 501363911 40833345 1
1 210491...

output:

-1

result:

ok 1 number(s): "-1"

Test #8:

score: 0
Accepted
time: 222ms
memory: 50448kb

input:

1
399863 925298013757
2 100628989 1 0
2 879908544 1 0
2 835150000 1 0
2 965097317 1 0
2 806475166 1 0
2 883153545 1 0
2 537871549 1 0
2 666240466 1 0
2 772690810 1 0
2 468000116 1 0
2 57197741 1 0
2 752910244 1 0
2 34579873 1 0
2 764798890 1 0
2 385606621 1 0
2 359331506 1 0
2 280339733 1 0
2 485810...

output:

93600917303 93601131398

result:

ok 2 number(s): "93600917303 93601131398"

Test #9:

score: 0
Accepted
time: 226ms
memory: 50452kb

input:

1
399494 961261170252
1 500382911 1 0
1 343020544 1 0
1 754347645 1 0
1 333064084 1 0
1 478385269 1 0
1 622968098 1 0
1 980169601 1 0
1 352209103 1 0
1 402748538 1 0
1 218430474 1 0
1 288936411 1 0
1 5773706 1 0
1 698161932 1 0
1 366209929 1 0
1 73511871 1 0
1 725720796 1 0
1 888663191 1 0
1 3011404...

output:

93601000787 93601218921

result:

ok 2 number(s): "93601000787 93601218921"

Test #10:

score: 0
Accepted
time: 27ms
memory: 14564kb

input:

1
400000 663010
1 680238422 483214 0
1 680238422 521442 0
1 680238422 609593 0
1 680238422 1476058 0
1 680238422 2424603 0
1 680238422 2483379 0
1 680238422 2777054 0
1 680238422 3992033 0
1 680238422 4038346 0
1 680238422 4500144 0
1 680238422 4613302 0
1 680238422 4698260 0
1 680238422 4860707 0
1...

output:

-1

result:

ok 1 number(s): "-1"

Test #11:

score: 0
Accepted
time: 39ms
memory: 14512kb

input:

1
400000 79784
1 110051200 202567778970 0
1 110051200 202567778970 0
1 110051200 202567778970 0
1 110051200 202567778970 0
1 110051200 202567778970 0
1 110051200 202567778970 0
1 110051200 202567778970 0
1 110051200 202567778970 0
1 110051200 202567778970 0
1 110051200 202567778970 0
1 110051200 202...

output:

202567778970 879492417995

result:

ok 2 number(s): "202567778970 879492417995"

Test #12:

score: 0
Accepted
time: 36ms
memory: 14500kb

input:

1
400000 48790
1 554756625 480690 0
1 554756625 3230620 0
1 554756625 3409127 0
1 514075804 3753536 0
1 554756625 4724772 0
2 188054527 5311431 0
1 100903989 5807895 0
1 933163205 7291004 0
2 382472190 8139517 0
1 239315307 8310712 0
1 554756625 8348744 0
1 14438382 8939856 0
1 554756625 11989642 0
...

output:

13251717318 599133935039

result:

ok 2 number(s): "13251717318 599133935039"

Test #13:

score: 0
Accepted
time: 34ms
memory: 14688kb

input:

1
400000 255041
2 181766528 25787236213 0
2 181766528 25787236213 0
2 181766528 25787236213 0
2 181766528 25787236213 0
2 181766528 25787236213 0
2 181766528 25787236213 0
2 181766528 25787236213 0
2 181766528 25787236213 0
2 181766528 25787236213 0
2 181766528 25787236213 0
2 181766528 25787236213 ...

output:

306238410801 624516496355

result:

ok 2 number(s): "306238410801 624516496355"

Test #14:

score: 0
Accepted
time: 36ms
memory: 14584kb

input:

1
400000 952892
1 669079782 5249 0
1 669079782 1051034 0
1 669079782 1478798 0
1 643031526 2194715 0
1 669079782 2318113 0
2 195012273 3054972 0
2 608030266 3674227 0
1 927916867 3904831 0
1 669079782 4349111 0
2 214184786 6595409 0
1 669079782 6663358 0
2 403805672 6933063 0
1 669079782 7146955 0
2...

output:

13932447919 987373706861

result:

ok 2 number(s): "13932447919 987373706861"

Test #15:

score: 0
Accepted
time: 31ms
memory: 14552kb

input:

1
400000 445351
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 43725609 629636445 0
2 437256...

output:

-1

result:

ok 1 number(s): "-1"

Test #16:

score: 0
Accepted
time: 39ms
memory: 15412kb

input:

1
400000 737917
2 104548428 355151 0
2 798447330 563764 0
2 807313778 726293 0
1 225967449 777825 0
1 76979820 830949 0
2 798447330 1204358 0
1 152843063 1242227 0
1 147816717 1604061 0
2 884732354 2045708 0
1 899307173 2280899 0
1 923713510 2447044 0
2 807313778 2651301 0
2 148086652 2704087 0
2 79...

output:

294972586721 744194775850

result:

ok 2 number(s): "294972586721 744194775850"

Test #17:

score: 0
Accepted
time: 32ms
memory: 15244kb

input:

1
400000 797979
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025121 76252326 0
1 698025...

output:

297321818946 857889105465

result:

ok 2 number(s): "297321818946 857889105465"

Test #18:

score: 0
Accepted
time: 50ms
memory: 19560kb

input:

1
400000 559759
1 334035078 470519 0
1 768140446 2967944 0
1 77040897 3088991 0
2 493261355 4283608 0
2 914055376 4561618 0
1 249929265 5223548 0
1 471764388 5364482 0
1 438202587 6820774 0
1 548072976 7173586 0
1 152064110 7904320 0
2 187798068 9277616 0
2 554380228 11571775 0
2 999624048 11848525 ...

output:

368607061812 459867556579

result:

ok 2 number(s): "368607061812 459867556579"

Test #19:

score: 0
Accepted
time: 39ms
memory: 19364kb

input:

1
400000 923129
1 830682125 6777784 0
1 830682125 6777784 0
1 830682125 6777784 0
1 830682125 6777784 1
1 830682125 6777784 0
1 830682125 6777784 0
1 830682125 6777784 0
2 502376403 98306211 0
2 502376403 98306211 0
2 502376403 98306211 0
2 502376403 98306211 0
2 502376403 98306211 0
2 502376403 983...

output:

603264713406 904051411073

result:

ok 2 number(s): "603264713406 904051411073"

Test #20:

score: 0
Accepted
time: 124ms
memory: 2492kb

input:

10000
34 713414155711
1 353899840 4470478880 1
1 101300788 14617874162 1
2 224463201 46129251985 1
2 997067155 53850132914 1
1 493411629 67237771644 1
2 60685412 153612550178 1
2 932182989 159471048139 1
2 821881991 174028196456 1
2 887385900 188331357182 1
2 819065134 200111294061 1
2 348545253 247...

output:

483871970490 840598269617
196200037892 970964066001
283567194848 914830957817
-1
-1
224079150450 800412473197
100330184885 227585151128
50506210564 783882723066
406628103960 924434625801
634770465011 738356832148
16323343084 723566375795
28913275304 938587397050
-1
461193028837 931629993981
36470471...

result:

ok 18515 numbers

Test #21:

score: 0
Accepted
time: 137ms
memory: 2708kb

input:

1000
60 171285665612
1 202084572 21122606424 1
2 658066585 35408059059 1
1 23311972 51059588296 1
1 555006790 53867325184 1
2 668895851 61404100899 1
2 439334198 66467682948 1
2 814813731 75505419882 1
2 563694648 84052137524 1
1 863531125 84160720592 1
2 666879230 92731902940 1
1 310726910 12688787...

output:

200468370512 983989301314
105910501928 853497904904
928492782007 939126373321
342619682920 456502006066
282459092744 713342359655
99203627100 110822178667
496813497898 828241040007
5149945202 998323717204
20989066921 948454205300
353986139415 989439227450
134191973984 722450971884
64821218433 932293...

result:

ok 1942 numbers

Test #22:

score: 0
Accepted
time: 144ms
memory: 4748kb

input:

100
302 495480207413
1 157557196 5191700656 1
2 688131848 7822346081 1
2 302959585 11342249961 1
2 667487485 12602857386 1
1 981368818 14235797064 1
2 862638974 15778895657 1
2 938801239 16033062185 1
2 141565804 18783237506 1
2 600109241 21882439145 1
1 866713826 23983150467 1
2 17855262 2555711801...

output:

12878839367 465356894994
98670299607 984006430918
659460612429 921298289091
261717617158 758562225078
16656906317 946242259897
604865303455 742610447485
65635714807 979156099520
414255260549 900331635889
86571338038 510051964019
302298934987 885584590000
15514653060 928519135170
11536475812 99954734...

result:

ok 197 numbers

Test #23:

score: 0
Accepted
time: 160ms
memory: 18760kb

input:

10
69720 312974804124
1 375110834 3284756 1
2 958035607 5372989 1
1 96816917 5896324 1
1 659349114 23601583 1
1 123026525 45334114 1
2 810136368 59250875 1
2 382704738 59429789 1
1 397855561 69769833 1
2 194473720 85533928 1
1 41349414 87337444 1
1 854195287 89285949 1
2 208588652 102226384 1
2 8322...

output:

192359142508 690359228247
4301019212 770771226671
59321270708 972659014596
34895107242 342872704017
43369717825 828295749790
281202583338 607302423053
105982895679 805855161263
12072175115 993484291384
114227155230 666678426833
136839246084 981181564986

result:

ok 20 numbers

Test #24:

score: 0
Accepted
time: 229ms
memory: 50728kb

input:

1
400000 862246722332
1 879284128 1644473 1
1 566310427 2602197 1
1 305759998 3635844 1
2 306032716 11995694 1
2 389513871 14509476 1
2 761692038 14593427 1
2 924522024 17673793 1
1 604735028 18750462 1
2 312343123 19169330 1
1 37783135 19583722 1
2 765576144 19935364 1
1 508498315 23251498 1
2 3123...

output:

511340089351 555367600772

result:

ok 2 number(s): "511340089351 555367600772"

Test #25:

score: 0
Accepted
time: 203ms
memory: 50488kb

input:

1
400000 601546500618
1 193617644 437488 1
1 687635851 1161220 1
1 867854270 3644275 1
2 75321946 7668994 1
2 19512874 8264014 1
2 2373384 11375302 1
2 167691473 17921764 1
1 837478720 23661825 1
1 319362132 27535622 1
2 9492126 28282384 1
2 21937131 28442718 1
1 284421469 28539626 1
1 313762592 307...

output:

212927310768 944532983806

result:

ok 2 number(s): "212927310768 944532983806"

Test #26:

score: 0
Accepted
time: 116ms
memory: 2528kb

input:

10000
59 10000000000
2 38224116 294735 0
1 791184194 294735 1
2 38224116 1963991 1
1 46743924 2052699 1
1 555377382 3945603 1
2 686459062 3952976 1
2 905596132 7512134 1
2 420873888 8587106 1
1 586803310 8596700 1
1 755231195 12584511 1
1 403524468 17231015 1
1 460174100 23414884 1
2 115239866 25206...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 13652 numbers

Test #27:

score: 0
Accepted
time: 130ms
memory: 2748kb

input:

1000
99 10000000000
2 307832277 78148 0
2 307832277 78148 1
1 275518058 2292869 1
2 318752188 3421425 1
1 538132005 4313972 1
2 559436916 4729882 1
2 97509167 4815124 1
2 154178279 4851744 1
2 567062651 6312029 1
2 665106524 7115329 1
2 715927148 8744720 1
1 502131755 9331956 1
2 809734156 9420053 1...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 1468 numbers

Test #28:

score: 0
Accepted
time: 131ms
memory: 4736kb

input:

100
10051 10000000000
2 139050006 1151 0
2 139050006 1151 1
1 500313852 6700 1
2 681995605 9186 1
1 270810606 22090 1
1 995827512 24954 1
1 53631098 30759 1
1 399843074 46446 1
2 126028509 48532 1
1 981492639 53805 1
2 702955098 123192 1
1 795346040 129998 1
2 556795597 151390 1
1 989719151 160084 1...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
182168532 694021729
-1
-1
-1
-1
577259203 783733321
-1
-1
-1
671704640 925928203
-1
-1
-1
-1
-1
138312166 436590105
-1
130000444 166930180
1796852385 5828587855
3700769434 7831971589
5341012067 6770347121
-1
924292593 5310349577
11307...

result:

ok 154 numbers

Test #29:

score: 0
Accepted
time: 167ms
memory: 20596kb

input:

10
128435 10000000000
2 650210546 297 0
2 650210546 297 1
1 614257067 1396 1
2 303092138 3047 1
1 571581225 3700 1
2 385229129 4100 1
1 445435100 4835 1
2 193655129 6197 1
2 153784571 7701 1
1 104200814 10218 1
1 60272333 10546 1
1 633189238 11303 1
1 100743505 11632 1
1 402614614 11726 1
2 6587875 ...

output:

-1
-1
-1
-1
5027602745 7828469284
809575451 2243952975
50008876934 92680594103
68404371711 98198887855
599544816032 676768101584
32564552152 866063397702

result:

ok 16 numbers

Test #30:

score: 0
Accepted
time: 198ms
memory: 50580kb

input:

1
399999 10000000000
2 233850926 90 0
1 748456332 90 1
1 213487336 626 1
1 157311336 716 1
1 28947387 765 1
1 424478786 1436 1
1 548164753 1638 1
1 431903000 1949 1
2 233850926 2028 1
2 716019297 2086 1
2 275417128 2176 1
2 289866313 2231 1
2 275427198 2822 1
2 749995741 2921 1
1 874030515 3547 1
2 ...

output:

-1

result:

ok 1 number(s): "-1"

Test #31:

score: 0
Accepted
time: 204ms
memory: 50592kb

input:

1
399999 10000000000
1 326385488 311 0
1 326385488 311 1
1 203877106 1099 1
2 644432124 1199 1
2 127852006 1290 1
2 407011871 1404 1
1 167030010 1854 1
1 704974376 2001 1
2 176461067 2510 1
2 528064718 2688 1
2 558130105 2947 1
2 472257639 3212 1
1 17497002 3607 1
1 887225470 3609 1
1 885238663 3997...

output:

47241340 63768254

result:

ok 2 number(s): "47241340 63768254"

Test #32:

score: 0
Accepted
time: 197ms
memory: 50428kb

input:

1
399999 10000000000
2 720746168 2538 0
1 389889266 2538 1
1 846649225 3346 1
2 720746168 6867 1
2 220578380 8319 1
2 478006698 14785 1
1 543403305 15269 1
1 994117268 17197 1
1 244609566 17573 1
1 186393243 18023 1
1 429534436 19900 1
1 831875453 21942 1
2 708904325 22934 1
2 674347985 25188 1
1 89...

output:

63929246 848919318

result:

ok 2 number(s): "63929246 848919318"

Test #33:

score: 0
Accepted
time: 199ms
memory: 50652kb

input:

1
399999 10000000000
1 708135129 3777 0
1 708135129 3777 1
2 378842907 12099 1
2 491334572 15760 1
2 57000817 22432 1
2 107356454 24206 1
1 355006013 25808 1
1 215578462 26957 1
1 915491561 27307 1
2 955971633 39399 1
2 609588029 44169 1
2 842497351 48865 1
1 617427596 53607 1
2 398484603 54969 1
2 ...

output:

27037798 925795917

result:

ok 2 number(s): "27037798 925795917"

Test #34:

score: 0
Accepted
time: 194ms
memory: 50528kb

input:

1
399999 10000000000
2 85635799 55054 0
2 85635799 55054 1
1 280335848 174979 1
2 939318300 209001 1
2 894465429 284645 1
2 618497444 297291 1
2 965916461 360005 1
1 974758958 400989 1
1 764253644 422154 1
2 15313245 481707 1
2 663242137 493111 1
1 798251629 520714 1
1 578532763 538498 1
2 468635252...

output:

996300115 8421496745

result:

ok 2 number(s): "996300115 8421496745"

Test #35:

score: 0
Accepted
time: 198ms
memory: 50516kb

input:

1
399999 10000000000
1 419779110 42873 0
2 809879504 42873 1
2 969885028 63945 1
1 419779110 78559 1
2 833465970 114507 1
1 575954552 137275 1
2 845350982 144299 1
1 829415866 177492 1
2 377975311 191331 1
2 543827092 388125 1
2 954840061 429840 1
1 234193563 430657 1
2 814992278 519061 1
2 13492113...

output:

3348662196 5187741015

result:

ok 2 number(s): "3348662196 5187741015"

Test #36:

score: 0
Accepted
time: 219ms
memory: 50484kb

input:

1
399999 10000000000
2 53438133 376125 0
2 53438133 376125 1
2 987743109 395850 1
1 166707936 441017 1
2 740702438 626572 1
2 254475979 650308 1
2 327881470 975326 1
1 519757004 1146648 1
1 745882219 1449930 1
1 704933732 1504462 1
1 567410879 1660559 1
1 501447393 2155145 1
1 217253545 2205303 1
2 ...

output:

75248596220 96080897908

result:

ok 2 number(s): "75248596220 96080897908"

Test #37:

score: 0
Accepted
time: 190ms
memory: 50496kb

input:

1
399999 10000000000
1 490359113 409073 0
2 635603234 409073 1
1 490359113 582604 1
1 466100352 1806496 1
1 851462909 2072668 1
2 835324462 2096985 1
1 306968106 2569687 1
2 843905600 2624494 1
1 421262349 2841982 1
2 566061630 3227185 1
1 840367098 3398146 1
1 755106699 3440275 1
1 893965367 398535...

output:

-1

result:

ok 1 number(s): "-1"

Test #38:

score: 0
Accepted
time: 214ms
memory: 50748kb

input:

1
399999 10000000000
2 108713408 355367 0
1 495440048 355367 1
2 108713408 5246476 1
1 829460048 18527254 1
2 643329162 19258328 1
2 889222716 22102287 1
2 408057726 26361813 1
2 793626956 26469500 1
2 138076520 29441704 1
1 91238764 31551010 1
2 871893174 31820730 1
1 668770819 34779933 1
1 8155239...

output:

326517728008 374248053587

result:

ok 2 number(s): "326517728008 374248053587"

Test #39:

score: 0
Accepted
time: 205ms
memory: 50444kb

input:

1
399999 10000000000
1 107735064 1466866 0
2 435376868 1466866 1
1 107735064 4168250 1
1 514024463 6140405 1
2 743983631 11245466 1
1 806314530 15082970 1
1 785572414 15407736 1
1 210419929 17442417 1
2 614548459 17450482 1
1 82717421 17736378 1
2 747895352 19709129 1
2 512995307 20474608 1
1 917777...

output:

24033093497 889551871011

result:

ok 2 number(s): "24033093497 889551871011"

Extra Test:

score: 0
Extra Test Passed