結果

問題 No.1226 I hate Robot Arms
ユーザー くれちーくれちー
提出日時 2020-09-12 00:30:17
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 12,362 bytes
コンパイル時間 5,500 ms
コンパイル使用メモリ 172,744 KB
実行使用メモリ 16,556 KB
最終ジャッジ日時 2023-08-30 11:01:13
合計ジャッジ時間 11,214 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// The main code is at the very bottom.

#[allow(unused_imports)]
use {
  lib::byte::ByteChar,
  std::cell::{Cell, RefCell},
  std::cmp::{
    self,
    Ordering::{self, *},
    Reverse,
  },
  std::collections::*,
  std::convert::identity,
  std::fmt::{self, Debug, Display, Formatter},
  std::io::prelude::*,
  std::iter::{self, FromIterator},
  std::marker::PhantomData,
  std::mem,
  std::num::Wrapping,
  std::ops::{Range, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive},
  std::process,
  std::rc::Rc,
  std::thread,
  std::time::{Duration, Instant},
  std::{char, f32, f64, i128, i16, i32, i64, i8, isize, str, u128, u16, u32, u64, u8, usize},
};

#[allow(unused_imports)]
#[macro_use]
pub mod lib {
  pub mod byte {
    pub use self::byte_char::*;

    mod byte_char {
      use std::error::Error;
      use std::fmt::{self, Debug, Display, Formatter};
      use std::str::FromStr;

      #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
      #[repr(transparent)]
      pub struct ByteChar(pub u8);

      impl Debug for ByteChar {
        fn fmt(&self, f: &mut Formatter) -> fmt::Result {
          write!(f, "b'{}'", self.0 as char)
        }
      }

      impl Display for ByteChar {
        fn fmt(&self, f: &mut Formatter) -> fmt::Result {
          write!(f, "{}", self.0 as char)
        }
      }

      impl FromStr for ByteChar {
        type Err = ParseByteCharError;

        fn from_str(s: &str) -> Result<ByteChar, ParseByteCharError> {
          match s.as_bytes().len() {
            1 => Ok(ByteChar(s.as_bytes()[0])),
            0 => Err(ParseByteCharErrorKind::EmptyStr.into()),
            _ => Err(ParseByteCharErrorKind::TooManyBytes.into()),
          }
        }
      }

      #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
      pub struct ParseByteCharError {
        kind: ParseByteCharErrorKind,
      }

      impl Display for ParseByteCharError {
        fn fmt(&self, f: &mut Formatter) -> fmt::Result {
          f.write_str(match self.kind {
            ParseByteCharErrorKind::EmptyStr => "empty string",
            ParseByteCharErrorKind::TooManyBytes => "too many bytes",
          })
        }
      }

      impl Error for ParseByteCharError {}

      #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
      enum ParseByteCharErrorKind {
        EmptyStr,
        TooManyBytes,
      }

      impl From<ParseByteCharErrorKind> for ParseByteCharError {
        fn from(kind: ParseByteCharErrorKind) -> ParseByteCharError {
          ParseByteCharError { kind }
        }
      }
    }
  }

  pub mod io {
    pub use self::scanner::*;

    mod scanner {
      use std::io::{self, BufRead};
      use std::iter;
      use std::str::FromStr;

      #[derive(Debug)]
      pub struct Scanner<R> {
        reader: R,
        buf: String,
        pos: usize,
      }

      impl<R: BufRead> Scanner<R> {
        pub fn new(reader: R) -> Self {
          Scanner {
            reader,
            buf: String::new(),
            pos: 0,
          }
        }

        pub fn next(&mut self) -> io::Result<&str> {
          let start = loop {
            match self.rest().find(|c| c != ' ') {
              Some(i) => break i,
              None => self.fill_buf()?,
            }
          };
          self.pos += start;
          let len = self.rest().find(' ').unwrap_or(self.rest().len());
          let s = &self.buf[self.pos..][..len]; // self.rest()[..len]
          self.pos += len;
          Ok(s)
        }

        pub fn parse_next<T>(&mut self) -> io::Result<Result<T, T::Err>>
        where
          T: FromStr,
        {
          Ok(self.next()?.parse())
        }

        pub fn parse_next_n<T>(&mut self, n: usize) -> io::Result<Result<Vec<T>, T::Err>>
        where
          T: FromStr,
        {
          iter::repeat_with(|| self.parse_next()).take(n).collect()
        }

        pub fn map_next_bytes<T, F>(&mut self, mut f: F) -> io::Result<Vec<T>>
        where
          F: FnMut(u8) -> T,
        {
          Ok(self.next()?.bytes().map(&mut f).collect())
        }

        pub fn map_next_bytes_n<T, F>(&mut self, n: usize, mut f: F) -> io::Result<Vec<Vec<T>>>
        where
          F: FnMut(u8) -> T,
        {
          iter::repeat_with(|| self.map_next_bytes(&mut f))
            .take(n)
            .collect()
        }

        fn rest(&self) -> &str {
          &self.buf[self.pos..]
        }

        fn fill_buf(&mut self) -> io::Result<()> {
          self.buf.clear();
          self.pos = 0;
          let read = self.reader.read_line(&mut self.buf)?;
          if read == 0 {
            return Err(io::ErrorKind::UnexpectedEof.into());
          }
          if *self.buf.as_bytes().last().unwrap() == b'\n' {
            self.buf.pop();
          }
          Ok(())
        }
      }
    }
  }
}

// port of https://ei1333.github.io/library/library/structure/segment-tree/segment-tree.cpp.html
#[allow(unused_imports)]
#[allow(non_snake_case)]
pub mod segment_tree {
  pub struct SegmentTree<Monoid, F> {
    sz: usize,
    seg: Vec<Monoid>,
    f: F,
    M1: Monoid,
  }

  impl<Monoid, F> SegmentTree<Monoid, F>
  where
    Monoid: Clone + PartialEq,
    F: FnMut(&Monoid, &Monoid) -> Monoid,
  {
    pub fn new(n: usize, f: F, M1: Monoid) -> Self {
      let mut sz = 1;
      while sz < n {
        sz <<= 1;
      }
      Self {
        sz,
        seg: vec![M1.clone(); 2 * sz],
        f,
        M1,
      }
    }

    pub fn update(&mut self, mut k: usize, x: Monoid) {
      k += self.sz;
      self.seg[k] = x;
      while {
        k >>= 1;
        k != 0
      } {
        self.seg[k] = (self.f)(&self.seg[2 * k + 0], &self.seg[2 * k + 1]);
      }
    }

    pub fn query(&mut self, mut a: usize, mut b: usize) -> Monoid {
      let mut L = self.M1.clone();
      let mut R = self.M1.clone();
      a += self.sz;
      b += self.sz;
      while a < b {
        if a & 1 != 0 {
          L = (self.f)(&L, &self.seg[a]);
          a += 1;
        }
        if b & 1 != 0 {
          b -= 1;
          R = (self.f)(&self.seg[b], &R);
        }
        a >>= 1;
        b >>= 1;
      }
      (self.f)(&L, &R)
    }

    pub fn get(&self, k: usize) -> &Monoid {
      &self.seg[k + self.sz]
    }
  }
}

// port of https://ei1333.github.io/library/library/structure/segment-tree/dual-segment-tree.cpp.html
#[allow(unused_imports)]
#[allow(non_snake_case)]
pub mod dual_segment_tree {
  pub struct DualSegmentTree<OperatorMonoid, H> {
    sz: usize,
    height: usize,
    lazy: Vec<OperatorMonoid>,
    h: H,
    OM0: OperatorMonoid,
  }

  impl<OperatorMonoid, H> DualSegmentTree<OperatorMonoid, H>
  where
    OperatorMonoid: Clone + PartialEq,
    H: FnMut(&OperatorMonoid, &OperatorMonoid) -> OperatorMonoid,
  {
    pub fn new(n: usize, h: H, OM0: OperatorMonoid) -> Self {
      let mut sz = 1;
      let mut height = 0;
      while sz < n {
        sz <<= 1;
        height += 1;
      }
      Self {
        sz,
        height,
        lazy: vec![OM0.clone(); 2 * sz],
        h,
        OM0,
      }
    }

    fn propagate(&mut self, k: usize) {
      if self.lazy[k] != self.OM0 {
        self.lazy[2 * k + 0] = (self.h)(&self.lazy[2 * k + 0], &self.lazy[k]);
        self.lazy[2 * k + 1] = (self.h)(&self.lazy[2 * k + 1], &self.lazy[k]);
        self.lazy[k] = self.OM0.clone();
      }
    }

    fn thrust(&mut self, k: usize) {
      let mut i = self.height;
      while i > 0 {
        self.propagate(k >> i);
        i -= 1;
      }
    }

    pub fn update(&mut self, mut a: usize, mut b: usize, x: &OperatorMonoid) {
      a += self.sz;
      self.thrust(a);
      b += self.sz - 1;
      self.thrust(b);
      let mut l = a;
      let mut r = b + 1;
      while l < r {
        if l & 1 != 0 {
          self.lazy[l] = (self.h)(&self.lazy[l], &x);
          l += 1;
        }
        if r & 1 != 0 {
          r -= 1;
          self.lazy[r] = (self.h)(&self.lazy[r], &x);
        }
        l >>= 1;
        r >>= 1;
      }
    }

    pub fn get(&mut self, mut k: usize) -> OperatorMonoid {
      k += self.sz;
      self.thrust(k);
      self.lazy[k].clone()
    }
  }
}

#[allow(unused_macros)]
macro_rules! eprint {
  ($($arg:tt)*) => {
    if cfg!(debug_assertions) {
      std::eprint!($($arg)*)
    }
  };
}
#[allow(unused_macros)]
macro_rules! eprintln {
  ($($arg:tt)*) => {
    if cfg!(debug_assertions) {
      std::eprintln!($($arg)*)
    }
  };
}
#[allow(unused_macros)]
macro_rules! dbg {
  ($($arg:tt)*) => {
    if cfg!(debug_assertions) {
      std::dbg!($($arg)*)
    } else {
      ($($arg)*)
    }
  };
}

const CUSTOM_STACK_SIZE_MIB: Option<usize> = Some(1024);
const INTERACTIVE: bool = false;

fn main() -> std::io::Result<()> {
  match CUSTOM_STACK_SIZE_MIB {
    Some(stack_size_mib) => std::thread::Builder::new()
      .name("run_solver".to_owned())
      .stack_size(stack_size_mib * 1024 * 1024)
      .spawn(run_solver)?
      .join()
      .unwrap(),
    None => run_solver(),
  }
}

fn run_solver() -> std::io::Result<()> {
  let stdin = std::io::stdin();
  let reader = stdin.lock();
  let stdout = std::io::stdout();
  let writer = stdout.lock();
  macro_rules! with_wrapper {
    ($($wrapper:expr)?) => {{
      let mut writer = $($wrapper)?(writer);
      solve(reader, &mut writer)?;
      writer.flush()
    }};
  }
  if cfg!(debug_assertions) || INTERACTIVE {
    with_wrapper!()
  } else {
    with_wrapper!(std::io::BufWriter::new)
  }
}

fn solve<R, W>(reader: R, mut writer: W) -> std::io::Result<()>
where
  R: BufRead,
  W: Write,
{
  let mut _scanner = lib::io::Scanner::new(reader);
  #[allow(unused_macros)]
  macro_rules! scan {
    ($T:ty) => {
      _scanner.parse_next::<$T>()?.unwrap()
    };
    ($($T:ty),+) => {
      ($(scan!($T)),+)
    };
    ($T:ty; $n:expr) => {
      _scanner.parse_next_n::<$T>($n)?.unwrap()
    };
    ($($T:ty),+; $n:expr) => {
      iter::repeat_with(|| -> std::io::Result<_> { Ok(($(scan!($T)),+)) })
        .take($n)
        .collect::<std::io::Result<Vec<_>>>()?
    };
  }
  #[allow(unused_macros)]
  macro_rules! scan_bytes_map {
    ($f:expr) => {
      _scanner.map_next_bytes($f)?
    };
    ($f:expr; $n:expr) => {
      _scanner.map_next_bytes_n($n, $f)?
    };
  }
  #[allow(unused_macros)]
  macro_rules! print {
    ($($arg:tt)*) => {
      write!(writer, $($arg)*)?
    };
  }
  #[allow(unused_macros)]
  macro_rules! println {
    ($($arg:tt)*) => {
      writeln!(writer, $($arg)*)?
    };
  }
  #[allow(unused_macros)]
  macro_rules! answer {
    ($($arg:tt)*) => {{
      println!($($arg)*);
      return Ok(());
    }};
  }
  {
    use dual_segment_tree::*;
    use segment_tree::*;

    let (n, q) = scan!(usize, usize);

    const E: [[f64; 2]; 2] = [[1.0, 0.0], [0.0, 1.0]];

    let apply = |[[a, b], [c, d]]: &[[f64; 2]; 2], r: &[f64; 2], l: &[f64; 2]| {
      [a * r[0] + b * r[1] + l[0], c * r[0] + d * r[1] + l[1]]
    };

    let mut s_affine = DualSegmentTree::new(
      n + 1,
      |&([[e, f], [g, h]], r), &([[a, b], [c, d]], l)| {
        (
          [
            [a * e + b * g, a * f + b * h],
            [c * e + d * g, c * f + d * h],
          ],
          apply(&[[a, b], [c, d]], &r, &l),
        )
      },
      (E, [0.0, 0.0]),
    );
    for i in 1..=n {
      s_affine.update(i, n + 1, &(E, [1.0, 0.0]));
    }

    let mut s_theta_sum = SegmentTree::new(n + 1, |l, r| l + r, 0.0);
    let mut len = vec![1.0; n + 1];

    for _ in 0..q {
      match scan!(u8) {
        0 => {
          let (i, x) = scan!(usize, f64);
          let (sin, cos) = x.to_radians().sin_cos();
          let (a, b) = s_affine.get(i - 1);
          let [p, q] = apply(&a, &[0.0, 0.0], &b);
          s_affine.update(i, n + 1, &(E, [-p, -q]));
          s_affine.update(i, n + 1, &([[cos, -sin], [sin, cos]], [p, q]));
          s_theta_sum.update(i, x.to_radians());
        }
        1 => {
          let (i, x) = scan!(usize, f64);
          let (sin, cos) = s_theta_sum.query(1, i + 1).sin_cos();
          let d = x - len[i];
          s_affine.update(i, n + 1, &(E, [d * cos, d * sin]));
          len[i] = x;
        }
        2 => {
          let i = scan!(usize);
          let (a, b) = s_affine.get(i);
          let [p, q] = apply(&a, &[0.0, 0.0], &b);
          println!("{} {}", p, q);
        }
        _ => unreachable!(),
      }
    }
  }
  #[allow(unreachable_code)]
  Ok(())
}
0