結果
問題 | No.2361 Many String Compare Queries |
ユーザー | dyktr_06 |
提出日時 | 2023-06-24 00:08:52 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 15,151 bytes |
コンパイル時間 | 8,104 ms |
コンパイル使用メモリ | 320,760 KB |
実行使用メモリ | 78,700 KB |
最終ジャッジ日時 | 2024-07-01 03:56:10 |
合計ジャッジ時間 | 14,986 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,812 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 3 ms
6,940 KB |
testcase_04 | WA | - |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | AC | 915 ms
77,868 KB |
testcase_12 | AC | 909 ms
77,996 KB |
testcase_13 | AC | 833 ms
77,748 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
ソースコード
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; #define overload4(_1, _2, _3, _4, name, ...) name #define rep1(n) for(int i = 0; i < (int)(n); ++i) #define rep2(i, n) for(int i = 0; i < (int)(n); ++i) #define rep3(i, a, b) for(int i = (a); i < (int)(b); ++i) #define rep4(i, a, b, c) for(int i = (a); i < (int)(b); i += (c)) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; --i) #define ALL(a) a.begin(), a.end() #define Sort(a) sort(a.begin(), a.end()) #define RSort(a) sort(a.rbegin(), a.rend()) typedef long long int ll; typedef unsigned long long ul; typedef long double ld; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<char> vc; typedef vector<string> vst; typedef vector<double> vd; typedef vector<long double> vld; typedef pair<long long, long long> P; template<class T> long long sum(const T& a){ return accumulate(a.begin(), a.end(), 0LL); } template<class T> auto min(const T& a){ return *min_element(a.begin(), a.end()); } template<class T> auto max(const T& a){ return *max_element(a.begin(), a.end()); } const long long MINF = 0x7fffffffffff; const long long INF = 0x1fffffffffffffff; const long long MOD = 998244353; const long double EPS = 1e-9; const long double PI = acos(-1); template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p){ is >> p.first >> p.second; return is; } template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){ os << "(" << p.first << ", " << p.second << ")"; return os; } template<typename T> istream &operator>>(istream &is, vector<T> &v){ for(T &in : v) is >> in; return is; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &v){ for(int i = 0; i < (int) v.size(); ++i){ os << v[i] << (i + 1 != (int) v.size() ? " " : ""); } return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp){ for(auto &[key, val] : mp){ os << key << ":" << val << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st){ auto itr = st.begin(); for(int i = 0; i < (int)st.size(); ++i){ os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st){ auto itr = st.begin(); for(int i = 0; i < (int)st.size(); ++i){ os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; } template <typename T> ostream &operator<<(ostream &os, queue<T> q){ while(q.size()){ os << q.front() << " "; q.pop(); } return os; } template <typename T> ostream &operator<<(ostream &os, deque<T> q){ while(q.size()){ os << q.front() << " "; q.pop_front(); } return os; } template <typename T> ostream &operator<<(ostream &os, stack<T> st){ while(st.size()){ os << st.top() << " "; st.pop(); } return os; } template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq){ while(pq.size()){ os << pq.top() << " "; pq.pop(); } return os; } template<class T, class U> inline T vin(T& vec, U n) { vec.resize(n); for(int i = 0; i < (int) n; ++i) cin >> vec[i]; return vec; } template<class T> inline void vout(T vec, string s = "\n"){ for(auto x : vec) cout << x << s; } template<class... T> void in(T&... a){ (cin >> ... >> a); } void out(){ cout << '\n'; } template<class T, class... Ts> void out(const T& a, const Ts&... b){ cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; } template<class T, class U> void inGraph(vector<vector<T>>& G, U n, U m, bool directed = false){ G.resize(n); for(int i = 0; i < m; ++i){ int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); if(!directed) G[b].push_back(a); } } template<typename T, T INF> struct SegmentTreeBeats{ int n, n0; vector<T> max_v, smax_v, max_c; vector<T> min_v, smin_v, min_c; vector<T> sum; vector<T> len, ladd, lval; void update_node_max(int k, T x) { sum[k] += (x - max_v[k]) * max_c[k]; if(max_v[k] == min_v[k]){ max_v[k] = min_v[k] = x; }else if(max_v[k] == smin_v[k]){ max_v[k] = smin_v[k] = x; }else{ max_v[k] = x; } if(lval[k] != INF && x < lval[k]){ lval[k] = x; } } void update_node_min(int k, T x) { sum[k] += (x - min_v[k]) * min_c[k]; if(max_v[k] == min_v[k]){ max_v[k] = min_v[k] = x; }else if(smax_v[k] == min_v[k]){ min_v[k] = smax_v[k] = x; }else{ min_v[k] = x; } if(lval[k] != INF && lval[k] < x){ lval[k] = x; } } void push(int k){ if(n0 - 1 <= k) return; if(lval[k] != INF){ updateall(2 * k + 1, lval[k]); updateall(2 * k + 2, lval[k]); lval[k] = INF; return; } if(ladd[k] != 0){ addall(2 * k + 1, ladd[k]); addall(2 * k + 2, ladd[k]); ladd[k] = 0; } if(max_v[k] < max_v[2 * k + 1]){ update_node_max(2 * k + 1, max_v[k]); } if(min_v[2 * k + 1] < min_v[k]){ update_node_min(2 * k + 1, min_v[k]); } if(max_v[k] < max_v[2 * k + 2]){ update_node_max(2 * k + 2, max_v[k]); } if(min_v[2 * k + 2] < min_v[k]){ update_node_min(2 * k + 2, min_v[k]); } } void update(int k){ sum[k] = sum[2 * k + 1] + sum[2 * k + 2]; if(max_v[2 * k + 1] < max_v[2 * k + 2]){ max_v[k] = max_v[2 * k + 2]; max_c[k] = max_c[2 * k + 2]; smax_v[k] = max(max_v[2 * k + 1], smax_v[2 * k + 2]); }else if(max_v[2 * k + 1] > max_v[2 * k + 2]){ max_v[k] = max_v[2 * k + 1]; max_c[k] = max_c[2 * k + 1]; smax_v[k] = max(smax_v[2 * k + 1], max_v[2 * k + 2]); }else{ max_v[k] = max_v[2 * k + 1]; max_c[k] = max_c[2 * k + 1] + max_c[2 * k + 2]; smax_v[k] = max(smax_v[2 * k + 1], smax_v[2 * k + 2]); } if(min_v[2 * k + 1] < min_v[2 * k + 2]){ min_v[k] = min_v[2 * k + 1]; min_c[k] = min_c[2 * k + 1]; smin_v[k] = min(smin_v[2 * k + 1], min_v[2 * k + 2]); }else if(min_v[2 * k + 1] > min_v[2 * k + 2]){ min_v[k] = min_v[2 * k + 2]; min_c[k] = min_c[2 * k + 2]; smin_v[k] = min(min_v[2 * k + 1], smin_v[2 * k + 2]); }else{ min_v[k] = min_v[2 * k + 1]; min_c[k] = min_c[2 * k + 1] + min_c[2 * k + 2]; smin_v[k] = min(smin_v[2 * k + 1], smin_v[2 * k + 2]); } } void _query_chmin(T x, int a, int b, int k, int l, int r) { if(b <= l || r <= a || max_v[k] <= x){ return; } if(a <= l && r <= b && smax_v[k] < x){ update_node_max(k, x); return; } push(k); _query_chmin(x, a, b, 2 * k + 1, l, (l + r) / 2); _query_chmin(x, a, b, 2 * k + 2, (l + r) / 2, r); update(k); } void _query_chmax(T x, int a, int b, int k, int l, int r) { if(b <= l || r <= a || x <= min_v[k]){ return; } if(a <= l && r <= b && x < smin_v[k]){ update_node_min(k, x); return; } push(k); _query_chmax(x, a, b, 2 * k + 1, l, (l + r) / 2); _query_chmax(x, a, b, 2 * k + 2, (l + r) / 2, r); update(k); } void addall(int k, T x) { max_v[k] += x; if(smax_v[k] != -INF) smax_v[k] += x; min_v[k] += x; if(smin_v[k] != INF) smin_v[k] += x; sum[k] += len[k] * x; if(lval[k] != INF){ lval[k] += x; }else{ ladd[k] += x; } } void updateall(int k, T x) { max_v[k] = x; smax_v[k] = -INF; min_v[k] = x; smin_v[k] = INF; max_c[k] = min_c[k] = len[k]; sum[k] = x * len[k]; lval[k] = x; ladd[k] = 0; } void _query_add(T x, int a, int b, int k, int l, int r) { if(b <= l || r <= a){ return; } if(a <= l && r <= b){ addall(k, x); return; } push(k); _query_add(x, a, b, 2 * k + 1, l, (l + r) / 2); _query_add(x, a, b, 2 * k + 2, (l + r) / 2, r); update(k); } void _query_update(T x, int a, int b, int k, int l, int r) { if(b <= l || r <= a){ return; } if(a <= l && r <= b){ updateall(k, x); return; } push(k); _query_update(x, a, b, 2 * k + 1, l, (l + r) / 2); _query_update(x, a, b, 2 * k + 2, (l + r) / 2, r); update(k); } T _query_max(int a, int b, int k, int l, int r) { if(b <= l || r <= a){ return -INF; } if(a <= l && r <= b){ return max_v[k]; } push(k); T lv = _query_max(a, b, 2 * k + 1, l, (l + r) / 2); T rv = _query_max(a, b, 2 * k + 2, (l + r) / 2, r); return max(lv, rv); } T _query_min(int a, int b, int k, int l, int r) { if(b <= l || r <= a){ return INF; } if(a <= l && r <= b){ return min_v[k]; } push(k); T lv = _query_min(a, b, 2 * k + 1, l, (l + r) / 2); T rv = _query_min(a, b, 2 * k + 2, (l + r) / 2, r); return min(lv, rv); } T _query_sum(int a, int b, int k, int l, int r) { if(b <= l || r <= a){ return 0; } if(a <= l && r <= b){ return sum[k]; } push(k); T lv = _query_sum(a, b, 2 * k + 1, l, (l + r) / 2); T rv = _query_sum(a, b, 2 * k + 2, (l + r) / 2, r); return lv + rv; } public: SegmentTreeBeats(int n) : n(n){ vector<T> a; init(n, a); } SegmentTreeBeats(int n, vector<T> &a) : n(n){ init(n, a); } void init(int n, vector<T> &a){ max_v.assign(4 * n, 0), smax_v.assign(4 * n, 0), max_c.assign(4 * n, 0); min_v.assign(4 * n, 0), smin_v.assign(4 * n, 0), min_c.assign(4 * n, 0); sum.assign(4 * n, 0); len.assign(4 * n, 0), ladd.assign(4 * n, 0); lval.assign(4 * n, 0); n0 = 1; while(n0 < n) n0 <<= 1; for(int i = 0; i < 2 * n0; ++i) ladd[i] = 0, lval[i] = INF; len[0] = n0; for(int i = 0; i < n0 - 1; ++i) len[2 * i + 1] = len[2 * i + 2] = (len[i] >> 1); for(int i = 0; i < n; ++i){ max_v[n0 - 1 + i] = min_v[n0 - 1 + i] = sum[n0 - 1 + i] = (!a.empty() ? a[i] : 0); smax_v[n0 - 1 + i] = -INF; smin_v[n0 - 1 + i] = INF; max_c[n0 - 1 + i] = min_c[n0 - 1 + i] = 1; } for(int i = n; i < n0; ++i){ max_v[n0 - 1 + i] = smax_v[n0 - 1 + i] = -INF; min_v[n0 - 1 + i] = smin_v[n0 - 1 + i] = INF; max_c[n0 - 1 + i] = min_c[n0 - 1 + i] = 0; } for(int i = n0 - 2; i >= 0; --i){ update(i); } } // range minimize query void query_chmin(int a, int b, T x){ _query_chmin(x, a, b, 0, 0, n0); } // range maximize query void query_chmax(int a, int b, T x){ _query_chmax(x, a, b, 0, 0, n0); } // range add query void query_add(int a, int b, T x){ _query_add(x, a, b, 0, 0, n0); } // range update query void query_update(int a, int b, T x){ _query_update(x, a, b, 0, 0, n0); } // range minimum query T query_max(int a, int b){ return _query_max(a, b, 0, 0, n0); } // range maximum query T query_min(int a, int b){ return _query_min(a, b, 0, 0, n0); } // range sum query T query_sum(int a, int b){ return _query_sum(a, b, 0, 0, n0); } T get(int x){ return _query_sum(x, x + 1, 0, 0, n0); } }; template <typename X> struct SegTree{ using FX = function<X(X, X)>; // X•X -> X となる関数の型 int n; FX fx; const X ex; vector<X> dat; SegTree(int n_, const FX &fx_, const X &ex_) : n(), fx(fx_), ex(ex_){ int x = 1; while(n_ > x){ x *= 2; } n = x; dat.assign(n * 2, ex); } X get(int i) const { return dat[i + n]; } void set(int i, X x){ dat[i + n] = x; } void build(){ for(int k = n - 1; k >= 1; k--) dat[k] = fx(dat[k * 2], dat[k * 2 + 1]); } void update(int i, X x){ i += n; dat[i] = x; while(i > 0){ i >>= 1; // parent dat[i] = fx(dat[i * 2], dat[i * 2 + 1]); } } X query(int a, int b){ X vl = ex; X vr = ex; int l = a + n; int r = b + n; while(l < r){ if(l & 1) vl = fx(vl, dat[l++]); if(r & 1) vr = fx(dat[--r], vr); l >>= 1; r >>= 1; } return fx(vl, vr); } X operator [](int i) const { return dat[i + n]; } }; ll n, q; string s; void input(){ in(n, q, s); } void solve(){ vi sa = suffix_array(s); vi la = lcp_array(s, sa); vll inv(n); rep(i, n){ inv[sa[i]] = i; } vll s(n + 1), s2(n); SegmentTreeBeats<ll, INF> segb(n); rep(i, n - 1) segb.query_update(i, i + 1, la[i]); vll ld(n); // longest decrease rrep(i, n - 1){ segb.query_chmin(i, n - 1, la[i]); ld[i] = segb.query_sum(i, n - 1); }// out(la); out(ld); rep(i, n) s[i + 1] = s[i] + (n - sa[i]); rep(i, n - 1) s2[i + 1] = s2[i] + la[i]; auto fx = [](ll a, ll b){ return min(a, b); }; SegTree<ll> seg(n - 1, fx, INF); rep(i, n - 1) seg.update(i, la[i]); while(q--){ ll l, r; in(l, r); l--, r--; ll ans = r - l; ll idx = inv[l], len = r - l + 1; ll ok = idx, ng = n; while(abs(ok - ng) > 1){ ll mid = (ok + ng) / 2; if(seg.query(idx, mid + 1) >= len){ ok = mid; }else{ ng = mid; } } idx = ok; if(idx == 0){ out(ans); continue; } ll ok2 = idx, ng2 = -1; while(abs(ok2 - ng2) > 1){ ll mid = (ok2 + ng2) / 2; if(seg.query(mid, idx) >= len){ ok2 = mid; }else{ ng2 = mid; } } ans += s[ok2] + (idx - ok2) * (r - l); // out(ok, ok2); if(len >= 2) ans += ld[ok]; out(ans); } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); input(); solve(); }