#![allow(unused_macros)] #![allow(dead_code)] #![allow(unused_imports)] // # ファイル構成 // - use 宣言 // - lib モジュール // - main 関数 // - basic モジュール // // 常に使うテンプレートライブラリは basic モジュール内にあります。 // 問題に応じて使うライブラリ lib モジュール内にコピペしています。 // ライブラリのコードはこちら → https://github.com/RheoTommy/at_coder // Twitter はこちら → https://twitter.com/RheoTommy use std::collections::*; use std::io::{stdout, BufWriter, Write}; use crate::basic::*; use crate::lib::*; pub mod lib { pub trait LazyMonoid { type M: Clone; type L: Clone; fn e() -> Self::M; fn id() -> Self::L; } pub struct LazySegTree { n: usize, height: usize, len: usize, data: Vec<(M::M, M::L)>, fold: Box M::M>, eval: Box M::M>, merge: Box M::L>, } impl std::fmt::Debug for LazySegTree where ::M: std::fmt::Debug, ::L: std::fmt::Debug, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for i in 0..=self.height { for j in 0..1 << i { write!(f, "{:?} ", &self.data[(1 << i) + j])?; } writeln!(f)?; } write!(f, "") } } impl LazySegTree { pub fn new( n: usize, fold: Box M::M>, eval: Box M::M>, merge: Box M::L>, ) -> Self { let len = n; let n = n.next_power_of_two(); let height = n.trailing_zeros() as usize; Self { n, height, len, fold, eval, merge, data: vec![(M::e(), M::id()); 2 * n], } } pub fn from_slice( a: &[M::M], mut fold: Box M::M>, eval: Box M::M>, merge: Box M::L>, ) -> Self { let n = a.len().next_power_of_two(); let height = n.trailing_zeros() as usize; let mut data = vec![(M::e(), M::id()); 2 * n]; for i in 0..a.len() { data[i + n].0 = a[i].clone(); } for i in (1..n).rev() { data[i].0 = fold(&data[i * 2].0, &data[i * 2 + 1].0); } Self { n, height, len: a.len(), data, fold, eval, merge, } } fn apply(&mut self, x: usize, op: &M::L) { let node = &mut self.data[x]; node.0 = (self.eval)(&node.0, op); node.1 = (self.merge)(&node.1, op); } fn propagate_at(&mut self, x: usize) { let op = std::mem::replace(&mut self.data[x].1, M::id()); self.apply(2 * x, &op); self.apply(2 * x + 1, &op); } fn save_at(&mut self, x: usize) { self.data[x].0 = (self.fold)(&self.data[x * 2].0, &self.data[x * 2 + 1].0); } fn propagate(&mut self, l: usize, r: usize) { let l = l + self.n; let r = r + self.n; for i in (1..=self.height).rev() { if (l >> i) << i != l { self.propagate_at(l >> i); } if (r >> i) << i != r { self.propagate_at((r - 1) >> i); } } } fn save(&mut self, l: usize, r: usize) { let l = l + self.n; let r = r + self.n; for i in 1..=self.height { if (l >> i) << i != l { self.save_at(l >> i); } if (r >> i) << i != r { self.save_at((r - 1) >> i); } } } pub fn set(&mut self, l: usize, r: usize, op: M::L) { assert!(l <= r && r <= self.n); if l == r { return; } self.propagate(l, r); let mut x = l + self.n; let mut y = r + self.n; while x < y { if x & 1 == 1 { self.apply(x, &op); x += 1; } if y & 1 == 1 { y -= 1; self.apply(y, &op); } x >>= 1; y >>= 1; } self.save(l, r) } pub fn fold(&mut self, l: usize, r: usize) -> M::M { assert!(l <= r && r <= self.n); if l == r { return M::e(); } self.propagate(l, r); let mut x = l + self.n; let mut y = r + self.n; let mut p = M::e(); let mut q = M::e(); while x < y { if x & 1 == 1 { p = (self.fold)(&p, &self.data[x].0); x += 1; } if y & 1 == 1 { y -= 1; q = (self.fold)(&self.data[y].0, &q); } x >>= 1; y >>= 1; } (self.fold)(&p, &q) } pub fn find_rightest(&mut self, l: usize, mut p: impl FnMut(&M::M) -> bool) -> usize { assert!(l < self.n); self.propagate(l, self.n); if p(&self.fold(l, self.n)) { return self.len; } let mut now = M::e(); let mut res = l + self.n; while res + 1 != res.next_power_of_two() { if res & 1 == 1 { let tmp = (self.fold)(&now, &self.data[res].0); if !p(&tmp) { break; } now = tmp; res += 1; } res >>= 1; } while res < self.n { res <<= 1; let tmp = (self.fold)(&now, &self.data[res].0); if p(&tmp) { now = tmp; res += 1; } } (res - self.n).min(self.len) } pub fn find_leftest(&mut self, r: usize, mut p: impl FnMut(&M::M) -> bool) -> usize { assert!(r <= self.n); self.propagate(0, r); if p(&self.fold(0, r)) { return 0; } let mut now = M::e(); let mut res = r + self.n; while res > 1 { if res & 1 == 1 { res -= 1; let tmp = (self.fold)(&self.data[res].0, &now); if !p(&tmp) { break; } now = tmp; } res >>= 1; } while res < self.n { res <<= 1; res += 1; let tmp = (self.fold)(&self.data[res].0, &now); if p(&tmp) { res -= 1; now = tmp; } } (res - self.n + 1).min(self.len) } } pub trait Monoid { type Item: Clone + std::fmt::Debug; fn op(a: &Self::Item, b: &Self::Item) -> Self::Item; fn id() -> Self::Item; } #[derive(Clone)] pub struct SegTree { n: usize, len: usize, data: Vec, } impl std::fmt::Debug for SegTree { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", &self.data[self.n - 1..]) } } impl SegTree { pub fn new(n: usize) -> Self { Self { len: n, n: n.next_power_of_two(), data: vec![M::id(); 2 * n.next_power_of_two() - 1], } } /// O(N) で a をもととする配列を構築 pub fn from_slice(a: &[M::Item]) -> Self { let n = a.len().next_power_of_two(); let mut data = vec![M::id(); 2 * n - 1]; for i in 0..a.len() { data[i + n - 1] = a[i].clone(); } for i in (0..n - 1).rev() { let left = &data[i * 2 + 1]; let right = &data[i * 2 + 2]; data[i] = M::op(left, right); } Self { n, data, len: a.len(), } } /// O(1) で data[i] を取得 pub fn get(&self, i: usize) -> &M::Item { assert!(i < self.n); &self.data[i + self.n - 1] } /// O(logN) で data[i] <- x pub fn set(&mut self, i: usize, x: M::Item) { assert!(i < self.n); self.data[i + self.n - 1] = x; let mut j = i + self.n - 1; if j == 0 { return; } while { j = (j - 1) / 2; let left = &self.data[2 * j + 1]; let right = &self.data[2 * j + 2]; self.data[j] = M::op(left, right); j != 0 } {} } /// O(logN) で [l,r) の区間和を返す pub fn fold(&self, mut l: usize, mut r: usize) -> M::Item { assert!(l <= r); assert!(r <= self.n); l += self.n; r += self.n; let mut left = M::id(); let mut right = M::id(); while l < r { if l & 1 == 1 { left = M::op(&left, &self.data[l - 1]); l += 1; } if r & 1 == 1 { r -= 1; right = M::op(&self.data[r - 1], &right); } l >>= 1; r >>= 1; } M::op(&left, &right) } /// O(logN) で、p(fold(l, res)) が真となるような最右端 res を返す pub fn find_rightest(&self, l: usize, mut p: impl FnMut(&M::Item) -> bool) -> usize { assert!(l < self.n); if p(&self.fold(l, self.n)) { return self.len; } let mut now = M::id(); let mut res = l + self.n; while res + 1 != res.next_power_of_two() { if res & 1 == 1 { let tmp = M::op(&now, &self.data[res - 1]); if !p(&tmp) { break; } now = tmp; res += 1; } res >>= 1; } while res < self.n { res <<= 1; let tmp = M::op(&now, &self.data[res - 1]); if p(&tmp) { now = tmp; res += 1; } } (res - self.n).min(self.len) } /// O(logN) で、p(fold(res, r)) が真となるような最左端 res を返す pub fn find_leftest(&self, r: usize, mut p: impl FnMut(&M::Item) -> bool) -> usize { assert!(r <= self.n); if p(&self.fold(0, r)) { return 0; } let mut now = M::id(); let mut res = r + self.n; while res > 1 { if res & 1 == 1 { res -= 1; let tmp = M::op(&self.data[res - 1], &now); if !p(&tmp) { break; } now = tmp; } res >>= 1; } while res < self.n { res <<= 1; res += 1; let tmp = M::op(&self.data[res - 1], &now); if p(&tmp) { res -= 1; now = tmp; } } (res - self.n + 1).min(self.len) } } pub struct Max; pub struct Min; pub struct Add; pub struct Mul; pub struct Xor; impl Monoid for Max { type Item = usize; fn id() -> Self::Item { 0 } fn op(a: &Self::Item, b: &Self::Item) -> Self::Item { *a.max(b) } } impl Monoid for Min { type Item = i64; fn id() -> Self::Item { (1 << 60) + (1 << 30) } fn op(a: &Self::Item, b: &Self::Item) -> Self::Item { *a.min(b) } } impl Monoid for Add { type Item = i64; fn id() -> Self::Item { 0 } fn op(a: &Self::Item, b: &Self::Item) -> Self::Item { a + b } } impl Monoid for Mul { type Item = i64; fn id() -> Self::Item { 1 } fn op(a: &Self::Item, b: &Self::Item) -> Self::Item { a * b } } impl Monoid for Xor { type Item = i64; fn id() -> Self::Item { 0 } fn op(a: &Self::Item, b: &Self::Item) -> Self::Item { a ^ b } } pub type MaxSegTree = SegTree; pub type MinSegTree = SegTree; pub type AddSegTree = SegTree; pub type MulSegTree = SegTree; pub type XorSegTree = SegTree; } struct M; impl LazyMonoid for M { // sum, len type M = (usize, usize); type L = Option; fn e() -> Self::M { (0, 0) } fn id() -> Self::L { None } } fn main() { let mut io = IO::new(); let n = io.next_usize(); let a = io.next_vec::(n); let mut ind = vec![vec![n]; n + 1]; ind[0].push(0); for i in 0..n { ind[a[i]].push(i); } for i in ind.iter_mut() { i.sort(); i.reverse(); } let mut b = vec![0; n + 1]; for i in 1..=n { b[i] = b[i - 1].max(*ind[i].last().unwrap()); } let mut lst = LazySegTree::::from_slice( &(0..=n).map(|i| (b[i], 1)).collect::>(), Box::new(|&(a, b), &(d, e)| (a + d, b + e)), Box::new(|&(a, b), &d| if let Some(d) = d { (d * b, b) } else { (a, b) }), Box::new(|&a, &b| match (a, b) { (_, Some(b)) => Some(b), _ => a, }), ); let mut st = MaxSegTree::from_slice(&b); let mut sum = 0; for l in 0..n { sum += n * (n + 1) - lst.fold(0, n + 1).0; let al = a[l]; ind[al].pop(); let next_ind = *ind[al].last().unwrap(); let right = st.find_rightest(0, |c| *c <= next_ind); st.set(al, next_ind); if al < right { lst.set(al, right, Some(next_ind)); } lst.set(0, 1, Some(l + 1)); st.set(0, l + 1); } io.println(sum); } pub mod basic { pub const U_INF: u64 = (1 << 60) + (1 << 30); pub const I_INF: i64 = (1 << 60) + (1 << 30); pub struct IO { iter: std::str::SplitAsciiWhitespace<'static>, buf: std::io::BufWriter>, } impl Default for IO { fn default() -> Self { Self::new() } } impl IO { pub fn new() -> Self { use std::io::*; let mut input = String::new(); std::io::stdin().read_to_string(&mut input).unwrap(); let input = Box::leak(input.into_boxed_str()); let out = Box::new(stdout()); IO { iter: input.split_ascii_whitespace(), buf: BufWriter::new(Box::leak(out).lock()), } } pub fn next_str(&mut self) -> &str { self.iter.next().unwrap() } pub fn read(&mut self) -> T where ::Err: std::fmt::Debug, { self.iter.next().unwrap().parse().unwrap() } pub fn next_usize(&mut self) -> usize { self.read() } pub fn next_uint(&mut self) -> u64 { self.read() } pub fn next_int(&mut self) -> i64 { self.read() } pub fn next_float(&mut self) -> f64 { self.read() } pub fn next_chars(&mut self) -> std::str::Chars { self.next_str().chars() } pub fn next_vec(&mut self, n: usize) -> Vec where ::Err: std::fmt::Debug, { (0..n).map(|_| self.read()).collect::>() } pub fn print(&mut self, t: T) { use std::io::Write; write!(self.buf, "{}", t).unwrap(); } pub fn println(&mut self, t: T) { self.print(t); self.print("\n"); } pub fn print_iter>( &mut self, mut iter: I, sep: &str, ) { if let Some(v) = iter.next() { self.print(v); for vi in iter { self.print(sep); self.print(vi); } } self.print("\n"); } pub fn flush(&mut self) { use std::io::Write; self.buf.flush().unwrap(); } } }