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)

// https://atcoder.jp/contests/practice2/tasks/practice2_i
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

/// Suffix Array. Build: O(N(logN)^2), where N = s.len()
/// sa[i]  := i-th lexicographically smaller suffix of original string.
///           We can obtain actual string by s[sa[i]..].
///           In this implementation, we'll consider empty string
///           one of the suffix of the given string. Therefore,
///           sa[0] is always N and |sa| = N+1.
/// lcp[i] := length of largest common prefix of s[sa[i]..] and s[sa[i+1]..].
///           |lcp| = N.
struct SuffixArray {
    s: Vec<char>,
    sa: Vec<usize>,
    lcp: Vec<usize>,
}

impl SuffixArray {
    fn compare(i: usize, j: usize, len: usize, rank: &Vec<i32>) -> Ordering {
        if rank[i] != rank[j] {
            rank[i].cmp(&rank[j])
        } else {
            let ri = *rank.get(i+len).unwrap_or(&-1);
            let rj = *rank.get(j+len).unwrap_or(&-1);
            ri.cmp(&rj)
        }
    }

    fn new(s: Vec<char>) -> Self {
        let n = s.len();
        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 i32 } else { -1 };
        }

        let mut tmp = vec![0; n+1];
        let mut step = 1;
        while step <= n {
            sa.sort_by(|&a, &b| SuffixArray::compare(a, b, step, &rank));
            tmp[sa[0]] = 0;
            for i in 1..n+1 {
                let a = sa[i-1];
                let b = sa[i];
                tmp[b] = tmp[a] + if SuffixArray::compare(a, b, step, &rank) == Ordering::Less { 1 } else { 0 };
            }
            for i in 0..n+1 {
                rank[i] = tmp[i];
            }
            step *= 2;
        }

        let mut lcp = vec![0; n];
        for i in 0..n+1 {
            rank[sa[i]] = i as i32;
        }
        let mut h = 0;
        for i in 0..n {
            let idx = rank[i] as usize-1;
            let j = sa[idx];
            if h > 0 {
                h -= 1;
            }
            while j + h < n && i + h < n {
                if s[j+h] != s[i+h] {
                    break;
                }
                h += 1;
            }
            lcp[idx] = h;
        }
        Self { s: s, sa: sa, lcp: lcp }
    }
}

//===

fn main() {
    input! {
        s: chars
    };
    let n = s.len();
    let sa = SuffixArray::new(s);
    let mut same = 0;
    for i in 0..n {
        same += sa.lcp[i];
    }
    println!("{}", n*(n+1)/2-same);
}