Problem:
Score : 100 points

Problem Statement

You are given a string of length N. Calculate the number of distinct substrings of S.

Constraints

1 \leq N \leq 500,000

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.

Sample Input 1

abcbcba

Sample Output 1

21

Sample Input 2

mississippi

Sample Output 2

53

Sample Input 3

ababacaca

Sample Output 3

33

Sample Input 4

aaaaa

Sample Output 4

5

Solution in Rust (1.42.0) (judge verdict: Accepted)

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let s = read::<String>();
    let sa = construct_sa(&s);
    let lcp = construct_lcp(&s, &sa);
    let ans =
        s.len() as i64 * (s.len() as i64 + 1) / 2 - lcp.iter().map(|&x| x as i64).sum::<i64>();
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn compare_sa(i: usize, j: usize, k: usize, n: usize, rank: &Vec<i32>) -> bool {
    if rank[i] != rank[j] {
        return rank[i] < rank[j];
    }
    let ri = if i + k <= n { rank[i + k] } else { -1 };
    let rj = if i + k <= n { rank[j + k] } else { -1 };
    ri < rj
}

fn construct_sa(s: &str) -> Vec<usize> {
    let n = s.len();
    let s = s.chars().collect::<Vec<_>>();
    let mut sa = vec![0; n + 1];

    let mut rank = vec![0; n + 1];
    for i in 0..n + 1 {
        sa[i] = i;
        rank[i] = if i < n { s[i] as u64 as i32 } else { -1 };
    }

    let mut k: usize = 1;
    while k <= n {
        let mut tmp = vec![0; n + 1];
        sa.sort_by_key(|&i| (rank[i], *rank.get(i + k).unwrap_or(&-1)));
        tmp[sa[0]] = 0;
        for i in 1..n + 1 {
            tmp[sa[i]] = tmp[sa[i - 1]];
            if compare_sa(sa[i - 1], sa[i], k, n, &rank) {
                tmp[sa[i]] += 1;
            }
        }
        rank = tmp;
        k *= 2;
    }
    sa
}

fn construct_lcp(s: &str, sa: &[usize]) -> Vec<usize> {
    let s = s.chars().collect::<Vec<_>>();
    let mut rank = vec![0; s.len() + 1];
    for i in 0..s.len() + 1 {
        rank[sa[i]] = i;
    }

    let mut h = 0;
    let mut lcp = vec![0; s.len() + 1];
    for i in 0..s.len() {
        let mut j = sa[rank[i] - 1];
        if h > 0 {
            h -= 1;
        }
        while j + h < s.len() && i + h < s.len() {
            if s[i + h] != s[j + h] {
                break;
            }
            h += 1;
        }

        lcp[rank[i] - 1] = h;
    }
    lcp
}