fork download
  1. #![allow(warnings)]
  2. use std::io;
  3. use std::str::FromStr;
  4.  
  5. enum VecFormate {
  6. Inline,
  7. NotInline,
  8. }
  9.  
  10. fn take_item<T>() -> T
  11. where
  12. T: FromStr /*+ std::fmt::Debug*/,
  13. T::Err: std::fmt::Debug,
  14. {
  15. let mut s = String::new();
  16. io::stdin().read_line(&mut s).unwrap();
  17. s.trim().parse().unwrap()
  18. }
  19.  
  20. fn take_vec<T>(n: u32, array_formate: VecFormate) -> Vec<T>
  21. where
  22. T: FromStr /*+ std::fmt::Debug*/,
  23. T::Err: std::fmt::Debug, // Ensure the type implements FromStr and provides a debug error
  24. {
  25. let mut v: Vec<T> = Vec::new();
  26. match array_formate {
  27. VecFormate::Inline => {
  28. let mut input = String::new();
  29. io::stdin().read_line(&mut input).unwrap();
  30. v = input
  31. .trim()
  32. .split_whitespace()
  33. .map(|x| x.parse::<T>().unwrap()) // Parse input into generic type
  34. .collect();
  35. }
  36. VecFormate::NotInline => {
  37. // We don't need to read an extra line before the loop
  38. for _ in 0..n {
  39. let mut s = String::new();
  40. std::io::stdin().read_line(&mut s).unwrap();
  41. v.push(s.trim().parse::<T>().unwrap()); // Parse input into generic type and push to vector
  42. }
  43. }
  44. }
  45. if v.len() as u32 > n {
  46. panic!("You added additional {:?} items", v.len() as u32 - n);
  47. } else if n > v.len() as u32 {
  48. panic!("You missed remaining {:?} items", n - v.len() as u32);
  49. }
  50. v
  51. }
  52.  
  53. fn main() {
  54. let n: u32 = take_item();
  55. let mut v: Vec<u64> = take_vec(n, VecFormate::Inline);
  56. let m: u32 = take_item();
  57.  
  58. let mut v2 = v.clone();
  59. v2.sort();
  60.  
  61. v2 = v2.iter_mut().scan(0, |sum, i| {
  62. *sum += *i;
  63. *i = *sum;
  64. Some(*sum)
  65. }).collect::<Vec<_>>();
  66.  
  67. v = v.iter_mut().scan(0, |sum, i| {
  68. *sum += *i;
  69. *i = *sum;
  70. Some(*sum)
  71. }).collect::<Vec<_>>();
  72.  
  73. for i in 0..m {
  74. let lrn: Vec<u32> = take_vec(3, VecFormate::Inline);
  75. if lrn.first() == Some(&1) {
  76. if lrn[1] == 1{
  77. print!("{:?}\n", v[lrn[2] as usize - 1]);
  78. continue;
  79. }
  80. print!("{:?}\n", v[lrn[2] as usize - 1] - v[lrn[1] as usize - 2]);
  81. } else {
  82. if lrn[1] == 1{
  83. print!("{:?}\n", v2[lrn[2] as usize - 1]);
  84. continue;
  85. }
  86. print!("{:?}\n", v2[lrn[2] as usize - 1] - v2[lrn[1] as usize - 2]);
  87. }
  88. }
  89.  
  90. }
  91.  
Runtime error #stdin #stdout #stderr 0s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseIntError { kind: Empty }', prog.rs:17:22
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace