#include #include #include using namespace std; using ll = long long; #include #include #line 1 "structure/others/sparse-table.hpp" /** * @brief Sparse-Table(スパーステーブル) * */ template struct SparseTable { F f; vector > st; vector lookup; SparseTable() = default; explicit SparseTable(const vector &v, const F &f) : f(f) { const int n = (int)v.size(); const int b = 32 - __builtin_clz(n); st.assign(b, vector(n)); for (int i = 0; i < v.size(); i++) { st[0][i] = v[i]; } for (int i = 1; i < b; i++) { for (int j = 0; j + (1 << i) <= n; j++) { st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); } } lookup.resize(v.size() + 1); for (int i = 2; i < lookup.size(); i++) { lookup[i] = lookup[i >> 1] + 1; } } inline T fold(int l, int r) const { int b = lookup[r - l]; return f(st[b][l], st[b][r - (1 << b)]); } }; template SparseTable get_sparse_table(const vector &v, const F &f) { return SparseTable(v, f); } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll n; cin>>n; vector s(n); for(int i = 0;i>s[i]; string S = ""; vector idx(n,0); for(int i = 0;i ll { return min(a,b); }; SparseTable sp(lcp,f); int M = sa.size(); vector rev(M); for(int i = 0;i>m>>x>>d; ll ans = 0; for(ll k = 1;k<=m;k++){ ll i = (x/(n-1)) + 1; ll j = (x%(n-1)) + 1; if(i>j) swap(i,j); else j = j + 1; x = (x+d) % (n*(n-1)); i--;j--; int ni = idx[i]; int nj = idx[j]; ni = rev[ni]; nj = rev[nj]; if(ni>nj) swap(ni,nj); ll can = min(s[i].size(),s[j].size()); can = min(can,sp.fold(ni,nj)); ans += can; } cout<