結果

問題 No.2361 Many String Compare Queries
ユーザー ecottea
提出日時 2024-10-09 19:57:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 569 ms / 2,500 ms
コード長 16,445 bytes
コンパイル時間 5,330 ms
コンパイル使用メモリ 284,404 KB
最終ジャッジ日時 2025-02-24 17:12:47
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#ifndef HIDDEN_IN_VS //
//
#define _CRT_SECURE_NO_WARNINGS
//
#include <bits/stdc++.h>
using namespace std;
//
using ll = long long; using ull = unsigned long long; // -2^63 2^63 = 9e18int -2^31 2^31 = 2e9
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
//
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
//
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
//
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 n-1
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s t
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s t
#define repe(v, a) for(const auto& v : (a)) // a
#define repea(v, a) for(auto& v : (a)) // a
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} //
#define EXIT(a) {cout << (a) << endl; exit(0);} //
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) //
//
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // true
    
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // true
    
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // mod
//
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif //
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
using mint = modint998244353;
//using mint = static_modint<1000000007>;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // Visual Studio
#include "local.hpp"
#else // gcc
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
template <size_t N> inline int lsb(const bitset<N>& b) { return b._Find_first(); }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(...)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE MLE
#endif
//suffix tree
/*
* Suffix_tree<STR>(STR s) : O(n)
* s[0..n)
*
* ll count_unique() : O(n)
* s
*
* int search(STR p) : O(m log σ) ?
* s[i..i+m) = p[0..m) i -1
*
* int count(STR p) : O(m log σ) ?
* s p
*
* dfs_all(FUNC f) : O(n β)
* [x1..x2)×[y1..y2) f(x1, x2, y1, y2)
* [x1..x2)×[y1..y2) sa[x1..x2) [y1..y2)
*
* build_dictionary() : O(n)
*
*
* pii get(ll k) : O(log n)
* k s[l..r) {l, r}
* {-1, -1} build_dictionary()
*
* pii get_unique(ll k) : O(log n)
* k s[l..r) {l, r}
* {-1, -1} build_dictionary()
*/
template <class STR>
class Suffix_tree {
struct Node {
int x1, x2; // sa[x1..x2)
int y1, y2; // [y1..y2)
vi ch; //
Node(int x1, int x2, int y1, int y2) : x1(x1), x2(x2), y1(y1), y2(y2) {}
Node() : x1(-1), x2(-1), y1(-1), y2(-1) {};
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Node& v) {
os << "x:[" << v.x1 << "," << v.x2 << ") "
<< "y:[" << v.y1 << "," << v.y2 << ") "
<< "ch:" << v.ch;
return os;
}
#endif
};
// n :
int n;
// s :
STR s;
// g : s 0
vector<Node> g;
// id[i] : i
vi id;
// acc[i] : id[0..i)
vl acc;
// acc_uniq[i] : id[0..i)
vl acc_uniq;
// i
void build_dictionary_dfs(int i) {
id.emplace_back(i);
ll dx = g[i].x2 - g[i].x1;
ll dy = g[i].y2 - g[i].y1;
acc.emplace_back(acc.back() + dx * dy);
acc_uniq.emplace_back(acc_uniq.back() + dy);
repe(ni, g[i].ch) build_dictionary_dfs(ni);
}
public:
// sa[x] : s x
vi sa;
// s[0..n)
Suffix_tree(const STR& s) : n(sz(s)), s(s) {
// verify : https://atcoder.jp/contests/abc362/tasks/abc362_g
// ACL SA LCP
sa = suffix_array(s);
auto lcp = lcp_array(s, sa);
lcp.insert(lcp.begin(), 0);
lcp.push_back(0);
//
g.emplace_back(0, n, 0, 0);
// stk :
stack<int> stk;
stk.push(0);
// suffix tree
repi(x, 0, n) {
int i = -1;
while (!stk.empty()) {
i = stk.top();
if (g[i].y2 <= lcp[x]) break;
if (g[i].y1 < lcp[x]) {
g.emplace_back(g[i].x1, x, lcp[x], g[i].y2);
g.back().ch = move(g[i].ch);
g[i].ch.push_back(sz(g) - 1);
g[i].y2 = lcp[x];
}
else {
stk.pop();
g[i].x2 = x;
}
}
if (x < n) {
g[i].ch.push_back(sz(g));
stk.push(sz(g));
g.emplace_back(x, -1, lcp[x], n - sa[x]);
}
}
}
// s
ll count_unique() const {
// verify : https://judge.yosupo.jp/problem/number_of_substrings
ll res = 0;
repe(v, g) res += v.y2 - v.y1;
return res;
}
// s[i..i+m) = p[0..m) i -1
int search(const STR& p) {
// verify : https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_14_D
int m = sz(p);
Assert(m > 0);
int i = 0; int j = 0;
//
while (true) {
int ok = 0, ng = sz(g[i].ch);
if (ng == 0) return -1;
while (ng - ok > 1) {
int mid = (ok + ng) / 2;
int ic = g[i].ch[mid];
if (p[j] >= s[sa[g[ic].x1] + g[ic].y1]) ok = mid;
else ng = mid;
}
i = g[i].ch[ok];
int ws = g[i].y2 - g[i].y1;
int wp = m - j;
rep(d, min(ws, wp)) if (s[sa[g[i].x1] + g[i].y1 + d] != p[j + d]) return -1;
if (wp <= ws) return sa[g[i].x1];
j += ws;
}
}
// s p
int count(const STR& p) const {
// verify : https://atcoder.jp/contests/abc362/tasks/abc362_g
int m = sz(p);
Assert(m > 0);
int i = 0; int j = 0;
//
while (true) {
int ok = 0, ng = sz(g[i].ch);
if (ng == 0) return 0;
while (ng - ok > 1) {
int mid = (ok + ng) / 2;
int ic = g[i].ch[mid];
if (p[j] >= s[sa[g[ic].x1] + g[ic].y1]) ok = mid;
else ng = mid;
}
i = g[i].ch[ok];
int ws = g[i].y2 - g[i].y1;
int wp = m - j;
rep(d, min(ws, wp)) if (s[sa[g[i].x1] + g[i].y1 + d] != p[j + d]) return 0;
if (wp <= ws) return g[i].x2 - g[i].x1;
j += ws;
}
}
// [x1..x2)×[y1..y2) f(x1, x2, y1, y2)
template <class FUNC>
void dfs_all(const FUNC& f, int i = 0) const {
// verify : https://atcoder.jp/contests/abc280/tasks/abc280_h
if (i != 0) f(g[i].x1, g[i].x2, g[i].y1, g[i].y2);
repe(ni, g[i].ch) dfs_all(f, ni);
}
//
void build_dictionary() {
// verify : https://yukicoder.me/problems/no/2361
id.reserve(sz(g));
acc.reserve(sz(g) + 1);
acc.emplace_back(0);
acc_uniq.reserve(sz(g) + 1);
acc_uniq.emplace_back(0);
build_dictionary_dfs(0);
}
// k s[l..r) {l, r} {-1, -1}
pii get(ll k) const {
// verify : https://yukicoder.me/problems/no/2361
Assert(!acc.empty());
if (k < 0 || k >= acc.back()) return { -1, -1 };
int i = ubpos(acc, k) - 1;
int v = id[i];
k -= acc[i];
int dx = g[v].x2 - g[v].x1;
return { sa[g[v].x1], sa[g[v].x1] + g[v].y1 + (int)k / dx + 1};
}
// k s[l..r) {l, r} {-1, -1}
pii get_unique(ll k) const {
// verify : https://atcoder.jp/contests/arc097/tasks/arc097_a
Assert(!acc_uniq.empty());
if (k < 0 || k >= acc_uniq.back()) return { -1, -1 };
int i = ubpos(acc_uniq, k) - 1;
int v = id[i];
k -= acc_uniq[i];
int dx = g[v].x2 - g[v].x1;
return { sa[g[v].x1], sa[g[v].x1] + g[v].y1 + (int)k / dx + 1 };
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Suffix_tree& S) {
os << "[suffixes]:" << endl;
rep(i, S.n) {
repi(j, S.sa[i], sz(S.s) - 1) os << S.s[j];
os << endl;
}
os << "[blocks]:" << endl;
auto f = [&](int x1, int x2, int y1, int y2) {
os << "x:[" << x1 << "," << x2 << ") ";
os << "y:[" << y1 << "," << y2 << ") ";
repi(j, S.sa[x1] + y1, S.sa[x1] + y2 - 1) os << S.s[j];
os << endl;
};
S.dfs_all(f);
return os;
}
#endif
};
// → 1
/*
* Offline_rectangle_add<T>() : O(1)
* v[0..h)[0..w) = 0 h, w 調
*
* void rectangle_add(ll x1, ll x2, ll y1, ll y2, T val) : O(1)
* v[x1..x2)[y1..y2) += val
*
* void get(ll x, ll y) : O(1)
* v[x][y]
*
* vT solve() : O((n + q) log n)
* v[0..h)[0..w)
*
*
*/
template <class T>
class Offline_rectangle_add {
vl x1_add, x2_add, y1_add, y2_add; vector<T> w_add;
vl x_get, y_get;
public:
// v[0..h)[0..w) = 0 h, w 調
Offline_rectangle_add() {
// verify : https://mojacoder.app/users/Tonegawac/problems/plane_add
}
// v[x1..x2)[y1..y2) += val
void rectangle_add(ll x1, ll x2, ll y1, ll y2, T val) {
// verify : https://mojacoder.app/users/Tonegawac/problems/plane_add
x1_add.emplace_back(x1);
x2_add.emplace_back(x2);
y1_add.emplace_back(y1);
y2_add.emplace_back(y2);
w_add.emplace_back(val);
}
// v[x][y]
void get(ll x, ll y) {
// verify : https://mojacoder.app/users/Tonegawac/problems/plane_add
x_get.emplace_back(x);
y_get.emplace_back(y);
}
//
vector<T> solve() {
// verify : https://mojacoder.app/users/Tonegawac/problems/plane_add
// ys : y
vl ys;
ys.reserve(sz(y1_add) + sz(y2_add));
repe(y, y1_add) ys.emplace_back(y);
repe(y, y2_add) ys.emplace_back(y);
uniq(ys);
// (x , , y , )
vector<tuple<ll, int, int, int>> ev;
rep(i, sz(x1_add)) {
ev.emplace_back(x1_add[i], -i - 1, lbpos(ys, y1_add[i]), 1);
ev.emplace_back(x1_add[i], -i - 1, lbpos(ys, y2_add[i]), -1);
ev.emplace_back(x2_add[i], -i - 1, lbpos(ys, y1_add[i]), -1);
ev.emplace_back(x2_add[i], -i - 1, lbpos(ys, y2_add[i]), 1);
}
int q = sz(x_get);
rep(t, q) {
ev.emplace_back(x_get[t], t, ubpos(ys, y_get[t]), 1);
}
//
sort(all(ev));
fenwick_tree<T> fen(sz(ys));
vector<T> res(q);
//
for (auto& [x, id, y, sgn] : ev) {
if (id < 0) {
//
fen.add(y, sgn * w_add[-id - 1]);
}
else {
//
res[id] = fen.sum(0, y);
}
}
return res;
}
};
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
int n, q;
cin >> n >> q;
string s;
cin >> s;
Suffix_tree S(s);
auto& sa = S.sa;
dump(S);
vi sa_inv(n);
rep(i, n) sa_inv[sa[i]] = i;
Offline_rectangle_add<int> O;
vector<tuple<int, int, int, int>> rects; vl acc{ 0 }; int pt = 0;
auto f = [&](int x1, int x2, int y1, int y2) {
rects.emplace_back(x1, x2, y1, y2);
acc.emplace_back(acc.back() + (ll)(x2 - x1) * (y2 - y1));
O.rectangle_add(x1, x2, y1, y2, pt++);
};
S.dfs_all(f);
dumpel(rects); dump(acc);
vi ys;
rep(j, q) {
int l, r;
cin >> l >> r;
l--;
int x = sa_inv[l];
int y = r - l - 1;
ys.push_back(y);
O.get(x, y);
}
dump(ys);
vi id = O.solve();
dump(id);
rep(j, q) {
auto [x1, x2, y1, y2] = rects[id[j]];
ll res = acc[id[j]];
res += (ll)(x2 - x1) * (ys[j] - y1);
cout << res << "\n";
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0