結果
問題 | No.2361 Many String Compare Queries |
ユーザー |
|
提出日時 | 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 |
ソースコード
#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 = 9e18(int は -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"#endifusing 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_VERfriend 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_substringsll 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_Dint 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_gint 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_hif (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/2361id.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/2361Assert(!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_aAssert(!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_VERfriend 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_addx1_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_addx_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";}}