結果

問題 No.962 LCPs
ユーザー ChanyuhChanyuh
提出日時 2020-02-12 18:49:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 3,551 bytes
コンパイル時間 1,210 ms
コンパイル使用メモリ 107,676 KB
実行使用メモリ 11,924 KB
最終ジャッジ日時 2024-04-14 22:54:50
合計ジャッジ時間 4,078 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,996 KB
testcase_01 AC 4 ms
11,924 KB
testcase_02 AC 5 ms
10,876 KB
testcase_03 AC 4 ms
10,364 KB
testcase_04 AC 4 ms
10,672 KB
testcase_05 AC 5 ms
10,960 KB
testcase_06 AC 4 ms
10,052 KB
testcase_07 AC 5 ms
10,172 KB
testcase_08 AC 4 ms
10,892 KB
testcase_09 AC 5 ms
10,836 KB
testcase_10 AC 5 ms
11,084 KB
testcase_11 AC 5 ms
11,192 KB
testcase_12 AC 4 ms
10,752 KB
testcase_13 AC 4 ms
10,648 KB
testcase_14 AC 4 ms
9,908 KB
testcase_15 AC 7 ms
10,920 KB
testcase_16 AC 5 ms
9,976 KB
testcase_17 AC 19 ms
11,912 KB
testcase_18 AC 14 ms
10,824 KB
testcase_19 AC 13 ms
11,256 KB
testcase_20 AC 10 ms
11,028 KB
testcase_21 AC 11 ms
11,388 KB
testcase_22 AC 9 ms
10,756 KB
testcase_23 AC 9 ms
11,288 KB
testcase_24 AC 8 ms
10,512 KB
testcase_25 AC 8 ms
10,388 KB
testcase_26 AC 7 ms
10,584 KB
testcase_27 AC 11 ms
11,444 KB
testcase_28 AC 8 ms
10,708 KB
testcase_29 AC 8 ms
10,580 KB
testcase_30 AC 10 ms
10,924 KB
testcase_31 AC 11 ms
11,244 KB
testcase_32 AC 8 ms
10,640 KB
testcase_33 AC 14 ms
10,860 KB
testcase_34 AC 8 ms
10,448 KB
testcase_35 AC 9 ms
11,080 KB
testcase_36 AC 9 ms
10,696 KB
testcase_37 AC 9 ms
10,924 KB
testcase_38 AC 8 ms
10,732 KB
testcase_39 AC 9 ms
11,512 KB
testcase_40 AC 9 ms
11,296 KB
testcase_41 AC 8 ms
10,632 KB
testcase_42 AC 8 ms
11,716 KB
testcase_43 AC 9 ms
10,856 KB
testcase_44 AC 8 ms
10,624 KB
testcase_45 AC 9 ms
10,968 KB
testcase_46 AC 9 ms
11,556 KB
testcase_47 AC 5 ms
11,372 KB
testcase_48 AC 4 ms
10,644 KB
testcase_49 AC 5 ms
10,204 KB
testcase_50 AC 5 ms
11,136 KB
testcase_51 AC 5 ms
10,464 KB
testcase_52 AC 4 ms
11,532 KB
testcase_53 AC 4 ms
10,384 KB
testcase_54 AC 5 ms
11,004 KB
testcase_55 AC 4 ms
10,068 KB
testcase_56 AC 5 ms
11,292 KB
testcase_57 AC 5 ms
10,220 KB
testcase_58 AC 5 ms
10,452 KB
testcase_59 AC 5 ms
10,812 KB
testcase_60 AC 5 ms
10,984 KB
testcase_61 AC 5 ms
11,856 KB
testcase_62 AC 6 ms
10,888 KB
testcase_63 AC 12 ms
11,660 KB
testcase_64 AC 5 ms
10,008 KB
testcase_65 AC 13 ms
11,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;//二項演算
  T ti;//単位元
  vector<T> dat;

  SegmentTree(){}
  SegmentTree(F f,T ti):f(f),ti(ti){}

  void init(int n_){//sizeがn_のsegtreeを作る
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }

  void build(const vector<T> &v){//vによってsegtreeをbuildする
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }

  void set_val(int k,T x){//k番目をxにする
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }

  T query(int a,int b){//区間[a,b)に対しFを適応した値を返す
    if(a>=b) return ti;
    T vl=ti,vr=ti;
    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }

  template<typename C>
  int find(int st,C &check,T &acc,int k,int l,int r){//
    if(l+1==r){
      acc=f(acc,dat[k]);
      return check(acc)?k-n:-1;
    }
    int m=(l+r)>>1;
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);
    if(st<=l&&!check(f(acc,dat[k]))){
      acc=f(acc,dat[k]);
      return -1;
    }
    int vl=find(st,check,acc,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(st,check,acc,(k<<1)|1,m,r);
  }

  template<typename C>
  int find(int st,C &check){
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }
};

int n,a[200010],r[200010],l[200010];
string s[200010];

void solve(){
  cin >> n;
  rep(i,n){
    cin >> s[i];
  }
  int M=0;
  rep(i,n-1){
    a[i]=0;
    //cout << s[i] << endl;
    for(int k=0;k<min(s[i].length(),s[i+1].length());k++){
      if (s[i][k]!=s[i+1][k]) break;
      a[i]++;
    }
    //cout << i << " " << a[i] << endl;
    M=max(M,a[i]);
  }
  auto f_min=[](int a,int b){return min(a,b);};
  auto f_max=[](int a,int b){return max(a,b);};
  SegmentTree<int> seg_l(f_max,0),seg_r(f_min,n);
  seg_l.init(M+1);seg_r.init(M+1);
  rep(i,n-1){
    l[i]=seg_l.query(0,a[i]);
    seg_l.set_val(a[i],i+1);
  }
  per(i,n-1){
    r[i]=seg_r.query(0,a[i]+1);
    seg_r.set_val(a[i],i+1);
  }
  ll ans=0;
  rep(i,n-1){
    //cout << i << " " << l[i] << " " << r[i] << endl;
    ans+=(ll)a[i]*((ll)(i-l[i]+1)*(ll)(r[i]-2+i)*(ll)(r[i]-1-i)/2-(ll)(r[i]-i-1)*(ll)(i+l[i])*(ll)(i-l[i]+1)/2+2*(ll)(r[i]-i-1)*(ll)(i-l[i]+1));
    //cout << ans << endl;
  }
  rep(i,n){
    ans+=s[i].length();
  }
  cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0