QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#740210#6844. Suffix AutomatonmaspyAC ✓1223ms205048kbC++2324.7kb2024-11-13 03:47:242024-11-13 03:47:24

Judging History

你现在查看的是最新测评结果

  • [2024-11-13 03:47:24]
  • 评测
  • 测评结果:AC
  • 用时:1223ms
  • 内存:205048kb
  • [2024-11-13 03:47:24]
  • 提交

answer

#line 1 "library/my_template.hpp"
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;

template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define FOR4_R(i, a, b, c) for (ll i = (b)-1; i >= ll(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) \
  overload4(__VA_ARGS__, FOR4_R, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) for (ll t = s; t >= 0; t = (t == 0 ? -1 : (t - 1) & s))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

template <typename T>
T SUM(vector<T> &A) {
  T sum = T(0);
  for (auto &&a: A) sum += a;
  return sum;
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T, typename U>
T ceil(T x, U y) {
  return (x > 0 ? (x + y - 1) / y : x / y);
}

template <typename T, typename U>
T floor(T x, U y) {
  return (x > 0 ? x / y : (x - y + 1) / y);
}

template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

ll binary_search(function<bool(ll)> check, ll ok, ll ng) {
  assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    if (check(x))
      ok = x;
    else
      ng = x;
  }
  return ok;
}

template <class T, class S>
inline bool chmax(T &a, const S &b) {
  return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  return (a > b ? a = b, 1 : 0);
}

vi s_to_vi(const string &S, char first_char) {
  vi A(S.size());
  FOR(i, S.size()) { A[i] = S[i] - first_char; }
  return A;
}

template <typename T>
vector<T> cumsum(vector<T> &A, int off = 1) {
  int N = A.size();
  vector<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

template <typename CNT, typename T>
vc<CNT> bincount(const vc<T> &A, int size) {
  vc<CNT> C(size);
  for (auto &&x: A) { ++C[x]; }
  return C;
}

template <typename T>
vector<int> argsort(const vector<T> &A) {
  // stable
  vector<int> ids(A.size());
  iota(all(ids), 0);
  sort(all(ids),
       [&](int i, int j) { return A[i] < A[j] || (A[i] == A[j] && i < j); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  int n = len(A);
  assert(len(I) == n);
  vc<T> B(n);
  FOR(i, n) B[i] = A[I[i]];
  return B;
}
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>

namespace detail {
template <typename T, decltype(&T::is_modint) = &T::is_modint>
std::true_type check_value(int);
template <typename T>
std::false_type check_value(long);
} // namespace detail

template <typename T>
struct is_modint : decltype(detail::check_value<T>(0)) {};
template <typename T>
using is_modint_t = enable_if_t<is_modint<T>::value>;
template <typename T>
using is_not_modint_t = enable_if_t<!is_modint<T>::value>;

struct Scanner {
  FILE *fp;
  char line[(1 << 15) + 1];
  size_t st = 0, ed = 0;
  void reread() {
    memmove(line, line + st, ed - st);
    ed -= st;
    st = 0;
    ed += fread(line + ed, 1, (1 << 15) - ed, fp);
    line[ed] = '\0';
  }
  bool succ() {
    while (true) {
      if (st == ed) {
        reread();
        if (st == ed) return false;
      }
      while (st != ed && isspace(line[st])) st++;
      if (st != ed) break;
    }
    if (ed - st <= 50) {
      bool sep = false;
      for (size_t i = st; i < ed; i++) {
        if (isspace(line[i])) {
          sep = true;
          break;
        }
      }
      if (!sep) reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    while (true) {
      size_t sz = 0;
      while (st + sz < ed && !isspace(line[st + sz])) sz++;
      ref.append(line + st, sz);
      st += sz;
      if (!sz || st != ed) break;
      reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    bool neg = false;
    if (line[st] == '-') {
      neg = true;
      st++;
    }
    ref = T(0);
    while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
    if (neg) ref = -ref;
    return true;
  }
  template <class T, is_modint_t<T> * = nullptr>
  bool read_single(T &ref) {
    long long val = 0;
    bool f = read_single(val);
    ref = T(val);
    return f;
  }
  bool read_single(double &ref) {
    string s;
    if (!read_single(s)) return false;
    ref = std::stod(s);
    return true;
  }
  bool read_single(char &ref) {
    string s;
    if (!read_single(s) || s.size() != 1) return false;
    ref = s[0];
    return true;
  }
  template <class T>
  bool read_single(vector<T> &ref) {
    for (auto &d: ref) {
      if (!read_single(d)) return false;
    }
    return true;
  }
  template <class T, class U>
  bool read_single(pair<T, U> &p) {
    return (read_single(p.first) && read_single(p.second));
  }
  template <class A, class B, class C>
  bool read_single(tuple<A, B, C> &p) {
    return (read_single(get<0>(p)) && read_single(get<1>(p))
            && read_single(get<2>(p)));
  }
  template <class A, class B, class C, class D>
  bool read_single(tuple<A, B, C, D> &p) {
    return (read_single(get<0>(p)) && read_single(get<1>(p))
            && read_single(get<2>(p)) && read_single(get<3>(p)));
  }
  void read() {}
  template <class H, class... T>
  void read(H &h, T &... t) {
    bool f = read_single(h);
    assert(f);
    read(t...);
  }
  Scanner(FILE *fp) : fp(fp) {}
};

struct Printer {
  Printer(FILE *_fp) : fp(_fp) {}
  ~Printer() { flush(); }

  static constexpr size_t SIZE = 1 << 15;
  FILE *fp;
  char line[SIZE], small[50];
  size_t pos = 0;
  void flush() {
    fwrite(line, 1, pos, fp);
    pos = 0;
  }
  void write(const char &val) {
    if (pos == SIZE) flush();
    line[pos++] = val;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  void write(T val) {
    if (pos > (1 << 15) - 50) flush();
    if (val == 0) {
      write('0');
      return;
    }
    if (val < 0) {
      write('-');
      val = -val; // todo min
    }
    size_t len = 0;
    while (val) {
      small[len++] = char(0x30 | (val % 10));
      val /= 10;
    }
    for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
    pos += len;
  }
  void write(const string &s) {
    for (char c: s) write(c);
  }
  void write(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++) write(s[i]);
  }
  void write(const double &x) {
    ostringstream oss;
    oss << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  void write(const long double &x) {
    ostringstream oss;
    oss << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  template <class T, is_modint_t<T> * = nullptr>
  void write(T &ref) {
    write(ref.val);
  }
  template <class T>
  void write(const vector<T> &val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  template <class T, class U>
  void write(const pair<T, U> &val) {
    write(val.first);
    write(' ');
    write(val.second);
  }
  template <class A, class B, class C>
  void write(const tuple<A, B, C> &val) {
    auto &[a, b, c] = val;
    write(a), write(' '), write(b), write(' '), write(c);
  }
  template <class A, class B, class C, class D>
  void write(const tuple<A, B, C, D> &val) {
    auto &[a, b, c, d] = val;
    write(a), write(' '), write(b), write(' '), write(c), write(' '), write(d);
  }
  template <class A, class B, class C, class D, class E>
  void write(const tuple<A, B, C, D, E> &val) {
    auto &[a, b, c, d, e] = val;
    write(a), write(' '), write(b), write(' '), write(c), write(' '), write(d), write(' '), write(e);
  }
  template <class A, class B, class C, class D, class E, class F>
  void write(const tuple<A, B, C, D, E, F> &val) {
    auto &[a, b, c, d, e, f] = val;
    write(a), write(' '), write(b), write(' '), write(c), write(' '), write(d), write(' '), write(e), write(' '), write(f);
  }
  template <class T, size_t S>
  void write(const array<T, S> &val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  void write(i128 val) {
    string s;
    bool negative = 0;
    if(val < 0){
      negative = 1;
      val = -val;
    }
    while (val) {
      s += '0' + int(val % 10);
      val /= 10;
    }
    if(negative) s += "-";
    reverse(all(s));
    if (len(s) == 0) s = "0";
    write(s);
  }
};

Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);

void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  printer.write(head);
  if (sizeof...(Tail)) printer.write(' ');
  print(forward<Tail>(tail)...);
}

void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
  scanner.read(head);
  read(tail...);
}

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __VA_ARGS__; \
  read(__VA_ARGS__)
#define STR(...)      \
  string __VA_ARGS__; \
  read(__VA_ARGS__)
#define CHAR(...)      \
  char __VA_ARGS__; \
  read(__VA_ARGS__)
#define DBL(...)      \
  double __VA_ARGS__; \
  read(__VA_ARGS__)

#define VEC(type, name, size) \
  vector<type> name(size);    \
  read(name)
#define VV(type, name, h, w)                     \
  vector<vector<type>> name(h, vector<type>(w)); \
  read(name)

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"

#line 2 "library/string/suffixarray.hpp"
using namespace std;

#define int long long
struct SuffixArray {
  vector<int> SA;
  vector<int> ISA;
  vector<int> LCP;

  SuffixArray(string& s) {
    char first = 127, last = 0;
    for(auto&& c : s){
      chmin(first, c);
      chmax(last, c);
    }
    SA = calc_suffix_array(s, first, last);
    calc_LCP(s);
  }

  SuffixArray(vector<int>& s) {
    SA = calc_suffix_array(s);
    calc_LCP(s);
  }

  void induced_sort(const std::vector<int>& vect, int val_range,
                    std::vector<int>& SA, const std::vector<bool>& sl,
                    const std::vector<int>& lms_idx) {
    std::vector<int> l(val_range, 0), r(val_range, 0);
    for (int c: vect) {
      if (c + 1 < val_range) ++l[c + 1];
      ++r[c];
    }
    std::partial_sum(l.begin(), l.end(), l.begin());
    std::partial_sum(r.begin(), r.end(), r.begin());
    std::fill(SA.begin(), SA.end(), -1);
    for (int i = (int)lms_idx.size() - 1; i >= 0; --i)
      SA[--r[vect[lms_idx[i]]]] = lms_idx[i];
    for (int i: SA)
      if (i >= 1 && sl[i - 1]) SA[l[vect[i - 1]]++] = i - 1;
    std::fill(r.begin(), r.end(), 0);
    for (int c: vect) ++r[c];
    std::partial_sum(r.begin(), r.end(), r.begin());
    for (int k = (int)SA.size() - 1, i = SA[k]; k >= 1; --k, i = SA[k])
      if (i >= 1 && !sl[i - 1]) { SA[--r[vect[i - 1]]] = i - 1; }
  }

  std::vector<int> SA_IS(const std::vector<int>& vect, int val_range) {
    const int n = vect.size();
    std::vector<int> SA(n), lms_idx;
    std::vector<bool> sl(n);
    sl[n - 1] = false;
    for (int i = n - 2; i >= 0; --i) {
      sl[i] = (vect[i] > vect[i + 1] || (vect[i] == vect[i + 1] && sl[i + 1]));
      if (sl[i] && !sl[i + 1]) lms_idx.push_back(i + 1);
    }
    std::reverse(lms_idx.begin(), lms_idx.end());
    induced_sort(vect, val_range, SA, sl, lms_idx);
    std::vector<int> new_lms_idx(lms_idx.size()), lms_vec(lms_idx.size());
    for (int i = 0, k = 0; i < n; ++i)
      if (!sl[SA[i]] && SA[i] >= 1 && sl[SA[i] - 1]) {
        new_lms_idx[k++] = SA[i];
      }
    int cur = 0;
    SA[n - 1] = cur;
    for (size_t k = 1; k < new_lms_idx.size(); ++k) {
      int i = new_lms_idx[k - 1], j = new_lms_idx[k];
      if (vect[i] != vect[j]) {
        SA[j] = ++cur;
        continue;
      }
      bool flag = false;
      for (int a = i + 1, b = j + 1;; ++a, ++b) {
        if (vect[a] != vect[b]) {
          flag = true;
          break;
        }
        if ((!sl[a] && sl[a - 1]) || (!sl[b] && sl[b - 1])) {
          flag = !((!sl[a] && sl[a - 1]) && (!sl[b] && sl[b - 1]));
          break;
        }
      }
      SA[j] = (flag ? ++cur : cur);
    }
    for (size_t i = 0; i < lms_idx.size(); ++i) lms_vec[i] = SA[lms_idx[i]];
    if (cur + 1 < (int)lms_idx.size()) {
      auto lms_SA = SA_IS(lms_vec, cur + 1);
      for (size_t i = 0; i < lms_idx.size(); ++i) {
        new_lms_idx[i] = lms_idx[lms_SA[i]];
      }
    }
    induced_sort(vect, val_range, SA, sl, new_lms_idx);
    return SA;
  }

  std::vector<int> calc_suffix_array(const std::string& s,
                                     const char first = 'a',
                                     const char last = 'z') {
    std::vector<int> vect(s.size() + 1);
    std::copy(std::begin(s), std::end(s), std::begin(vect));
    for (auto& x: vect) x -= (int)first - 1;
    vect.back() = 0;
    auto ret = SA_IS(vect, (int)last - (int)first + 2);
    ret.erase(ret.begin());
    return ret;
  }

  std::vector<int> calc_suffix_array(const vector<int>& s) {
    vector<int> ss = s;
    sort(ss.begin(), ss.end());
    ss.erase(unique(ss.begin(), ss.end()), ss.end());

    std::vector<int> vect(s.size() + 1);
    std::copy(std::begin(s), std::end(s), std::begin(vect));
    for (auto& x: vect)
      x = lower_bound(ss.begin(), ss.end(), x) - ss.begin() + 1;
    vect.back() = 0;
    auto ret = SA_IS(vect, *max_element(vect.begin(), vect.end()) + 2);
    ret.erase(ret.begin());
    return ret;
  }

  void calc_LCP(const std::string& s) {
    int n = s.size(), k = 0;
    ISA.resize(n);
    LCP.resize(n);
    for (int i = 0; i < n; i++) ISA[SA[i]] = i;
    for (int i = 0; i < n; i++, k ? k-- : 0) {
      if (ISA[i] == n - 1) {
        k = 0;
        continue;
      }
      int j = SA[ISA[i] + 1];
      while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
      LCP[ISA[i]] = k;
    }
    LCP.resize(n - 1);
  }

  void calc_LCP(const vector<int>& s) {
    int n = s.size(), k = 0;
    ISA.resize(n);
    LCP.resize(n);
    for (int i = 0; i < n; i++) ISA[SA[i]] = i;
    for (int i = 0; i < n; i++, k ? k-- : 0) {
      if (ISA[i] == n - 1) {
        k = 0;
        continue;
      }
      int j = SA[ISA[i] + 1];
      while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
      LCP[ISA[i]] = k;
    }
    LCP.resize(n - 1);
  }
};
#line 2 "library/alg/group_add.hpp"
template <class X>
struct Group_Add {
  using value_type = X;
  static constexpr X op(const X &x, const X &y) noexcept { return x + y; }
  static constexpr X inverse(const X &x) noexcept { return -x; }
  static constexpr X power(const X &x, ll n) noexcept { return n * x; }
  static constexpr X unit() { return X(0); }
  static constexpr bool commute = true;
};
#line 3 "library/ds/fenwick.hpp"

template <typename AbelGroup>
struct FenwickTree {
  using E = typename AbelGroup::value_type;
  int n;
  vector<E> dat;
  E total;

  FenwickTree() : FenwickTree(0) {}
  FenwickTree(int n) : n(n), total(AbelGroup::unit()) {
    assert(AbelGroup::commute);
    dat.assign(n, AbelGroup::unit());
  }
  FenwickTree(vc<E> v) : n(len(v)), total(AbelGroup::unit()) {
    assert(AbelGroup::commute);
    FOR(i, n) total = AbelGroup::op(total, v[i]);
    dat = v;
    FOR3(i, 1, n + 1) {
      int j = i + (i & -i);
      if (j <= n) dat[j - 1] = AbelGroup::op(dat[i - 1], dat[j - 1]);
    }
  }

  void reset(){
    total = AbelGroup::unit();
    dat.assign(n, AbelGroup::unit());
  }

  E sum(int k) {
    E ret = AbelGroup::unit();
    for (; k > 0; k -= k & -k) ret = AbelGroup::op(ret, dat[k - 1]);
    return ret;
  }

  E sum(int L, int R) {
    E pos = AbelGroup::unit();
    while (L < R) {
      pos = AbelGroup::op(pos, dat[R - 1]);
      R -= R & -R;
    }
    E neg = AbelGroup::unit();
    while (R < L) {
      neg = AbelGroup::op(neg, dat[L - 1]);
      L -= L & -L;
    }
    return AbelGroup::op(pos, AbelGroup::inverse(neg));
  }

  E sum_all() { return total; }

  void add(int k, E x) {
    total = AbelGroup::op(total, x);
    for (++k; k <= n; k += k & -k) dat[k - 1] = AbelGroup::op(dat[k - 1], x);
  }

  template <class F>
  int max_right(F& check) {
    assert(check(E(0)));
    ll i = 0;
    E s = AbelGroup::unit();
    int k = 1;
    int N = len(dat) + 1;
    while (2 * k < N) k *= 2;
    while (k) {
      if (i + k < N && check(AbelGroup::op(s, dat[i + k - 1]))) {
        i += k;
        s = AbelGroup::op(s, dat[i - 1]);
      }
      k >>= 1;
    }
    return i;
  }

  int find_kth(E k) {
    auto check = [&](E x) -> bool { return x <= k; };
    return max_right(check);
  }

  void debug() { print("fenwick", dat); }
};
#line 2 "library/ds/segtree.hpp"

template <class Monoid>
struct SegTree {
  using X = typename Monoid::value_type;
  using value_type = X;
  vc<X> dat;
  int n, log, size;

  SegTree() : SegTree(0) {}
  SegTree(int n) : SegTree(vc<X>(n, Monoid::unit())) {}
  SegTree(vc<X> v) : n(len(v)) {
    log = 1;
    while ((1 << log) < n) ++log;
    size = 1 << log;
    dat.assign(size << 1, Monoid::unit());
    FOR(i, n) dat[size + i] = v[i];
    FOR3_R(i, 1, size) update(i);
  }

  void reset() { fill(all(dat), Monoid::unit()); }

  void set_all(const vc<X>& v){
    dat.assign(size << 1, Monoid::unit());
    FOR(i, n) dat[size + i] = v[i];
    FOR3_R(i, 1, size) update(i);
  }

  X operator[](int i) { return dat[size + i]; }

  void update(int i) { dat[i] = Monoid::op(dat[2 * i], dat[2 * i + 1]); }

  void set(int i, const X& x) {
    assert(i < n);
    dat[i += size] = x;
    while (i >>= 1) update(i);
  }

  void multiply(int i, const X& x){
    assert(i < n);
    i += size;
    dat[i] = Monoid::op(dat[i], x);
    while (i >>= 1) update(i);
  }

  X prod(int L, int R) {
    assert(L <= R);
    assert(R <= n);
    X vl = Monoid::unit(), vr = Monoid::unit();
    L += size, R += size;
    while (L < R) {
      if (L & 1) vl = Monoid::op(vl, dat[L++]);
      if (R & 1) vr = Monoid::op(dat[--R], vr);
      L >>= 1, R >>= 1;
    }
    return Monoid::op(vl, vr);
  }

  X prod_all() { return dat[1]; }

  template <class F>
  int max_right(F &check, int L) {
    assert(0 <= L && L <= n && check(Monoid::unit()));
    if (L == n) return n;
    L += size;
    X sm = Monoid::unit();
    do {
      while (L % 2 == 0) L >>= 1;
      if (!check(Monoid::op(sm, dat[L]))) {
        while (L < size) {
          L = 2 * L;
          if (check(Monoid::op(sm, dat[L]))) {
            sm = Monoid::op(sm, dat[L]);
            L++;
          }
        }
        return L - size;
      }
      sm = Monoid::op(sm, dat[L]);
      L++;
    } while ((L & -L) != L);
    return n;
  }

  template <class F>
  int min_left(F &check, int R) {
    assert(0 <= R && R <= n && check(Monoid::unit()));
    if (R == 0) return 0;
    R += size;
    X sm = Monoid::unit();
    do {
      --R;
      while (R > 1 && (R % 2)) R >>= 1;
      if (!check(Monoid::op(dat[R], sm))) {
        while (R < size) {
          R = 2 * R + 1;
          if (check(Monoid::op(dat[R], sm))) {
            sm = Monoid::op(dat[R], sm);
            R--;
          }
        }
        return R + 1 - size;
      }
      sm = Monoid::op(dat[R], sm);
    } while ((R & -R) != R);
    return 0;
  }

  // モノイドが可換なら、prod_{l<=i<r}A[i^x] が計算可能
  // https://codeforces.com/contest/1401/problem/F
  X Xor_prod(int l, int r, int xor_val) {
    assert(Monoid::commute);
    X x = Monoid::unit();
    FOR(k, log + 1) {
      if (l >= r) break;
      if (l & 1) { x = Monoid::op(x, dat[(size >> k) + ((l++) ^ xor_val)]); }
      if (r & 1) { x = Monoid::op(x, dat[(size >> k) + ((--r) ^ xor_val)]); }
      l /= 2, r /= 2, xor_val /= 2;
    }
    return x;
  }

  void debug() { print("segtree", dat); }
};
#line 2 "library/alg/monoid_min.hpp"
template <class X>
struct Monoid_Min {
  using value_type = X;
  static constexpr X op(const X &x, const X &y) noexcept { return min(x, y); }
  static constexpr X unit() { return numeric_limits<X>::max(); }
  static constexpr bool commute = true;
};
#line 8 "main.cpp"

void solve() {
  STR(S);
  ll N = len(S);
  LL(Q);
  VEC(ll, query, Q);
  for (auto&& k: query) --k;

  SuffixArray sa(S);
  auto& SA = sa.SA;
  auto& LCP = sa.LCP;

  /*
  長さ -> 追加
  長さ -> 消去
  */
  vvc<int> ADD(N + 2);
  vvc<int> RM(N + 2);
  ADD[1].eb(0);
  FOR(i, len(LCP)) ADD[LCP[i] + 1].eb(i + 1);
  FOR(i, len(SA)) {
    int idx = SA[i];
    RM[N - idx + 1].eb(i);
  }

  /*
  First Occurrence をとらないといけない。
  LCP >= n となる範囲を見る → index の最小値をとる
  どちらも min segtree
  */

  SegTree<Monoid_Min<int>> seg_LCP(LCP);
  SegTree<Monoid_Min<int>> seg_SA(SA);

  FenwickTree<Group_Add<int>> bit(N);

  vc<pi> ANS(Q, {-1, -1});
  auto I = argsort(query);
  reverse(all(I));
  ll off = 0;
  FOR(n, 1, N + 1) {
    for (auto&& i: ADD[n]) bit.add(i, 1);
    for (auto&& i: RM[n]) bit.add(i, -1);
    ll cnt = bit.sum_all();
    while (len(I)) {
      int qid = I.back();
      ll k = query[qid];
      if (off + cnt <= k) break;
      I.pop_back();
      k -= off;
      int l = bit.find_kth(k);
      auto check = [&](auto e) -> bool { return e >= n; };
      int r = seg_LCP.max_right(check, l);
      int idx = seg_SA.prod(l, r + 1);
      ANS[qid] = {idx + 1, idx + n};
    }
    off += cnt;
  }
  for (auto&& [l, r]: ANS) print(l, r);
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(15);

  // LL(T);
  ll T = 1;
  FOR(T) solve();

  return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3592kb

input:

ccpcguilin
5
1
10
4
8
26

output:

1 1
2 3
8 8
1 2
4 7

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3684kb

input:

banana
3
5
10
16

output:

1 2
2 5
-1 -1

result:

ok 3 lines

Test #3:

score: 0
Accepted
time: 1ms
memory: 3580kb

input:

rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
1000
752
397
968
637
706
758
780
574
1032
599
431
1038
700
868
252
1084
813
277
565
112
69
997
222
897
931
75
200
360
698
196
31
971
1064
591
485
179
528
71
45
422
272
925
8
197
796
116
187
983
1057
939
...

output:

-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
1 69
-1 -1
-1 -1
-1 -1
-1 -1
1 75
-1 -1
-1 -1
-1 -1
-1 -1
1 31
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
1 71
1 45
-1 -1
-1 -1
-1 -1
1 8
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-...

result:

ok 1000 lines

Test #4:

score: 0
Accepted
time: 1ms
memory: 3600kb

input:

ywyywywwywwywwwwwywwyyyywywywyyywywwywwwwwyyyyywwwwwwwywwwwwwwywyywwywwyyywwyyyyyyywyyyyyyywwy
1000
1545
1728
2068
3537
4585
3752
2495
3165
4342
4441
4109
4219
1709
3238
2946
1807
2717
4494
888
1956
1317
1508
3494
3945
3261
3892
1299
3903
2293
4055
218
3263
2265
793
4043
3851
2551
1478
1371
4015
267...

output:

26 50
17 44
32 64
30 94
-1 -1
8 81
51 91
19 73
-1 -1
-1 -1
-1 -1
-1 -1
52 79
10 66
10 59
9 37
8 52
-1 -1
44 59
44 74
47 68
35 59
20 83
1 87
4 60
9 90
41 62
12 94
27 63
-1 -1
4 11
26 82
56 92
24 38
-1 -1
6 84
39 80
28 51
19 41
-1 -1
17 60
59 89
40 56
22 56
-1 -1
62 78
57 84
27 49
12 17
11 32
56 82
34...

result:

ok 1000 lines

Test #5:

score: 0
Accepted
time: 1ms
memory: 3928kb

input:

pcfppcppppppffpppfffppfpfpffpfcffccffcfpcpfcfcfcppppfcppcccfffpffpfppffffcffffpfcpcpffppppff
1000
4871
1081
4356
3107
4567
4525
1585
3772
3802
3762
4827
3359
1004
511
4521
2924
4765
3146
2038
2652
3847
1381
1652
1000
4241
2288
4668
4396
4830
437
879
524
2346
4535
425
3738
2249
652
4865
1523
4014
303...

output:

-1 -1
30 46
-1 -1
20 70
-1 -1
-1 -1
54 77
11 82
19 92
13 84
-1 -1
34 91
30 45
59 68
-1 -1
18 64
-1 -1
25 76
58 88
51 91
11 86
47 67
2 26
6 21
-1 -1
54 88
-1 -1
-1 -1
-1 -1
43 51
1 14
73 82
48 83
-1 -1
35 43
18 88
23 56
22 32
-1 -1
53 75
-1 -1
26 74
27 88
35 84
72 84
9 83
1 83
-1 -1
1 27
60 77
31 47
...

result:

ok 1000 lines

Test #6:

score: 0
Accepted
time: 1ms
memory: 3672kb

input:

wejweujeuuweuweweuwwuwjwuuweeejwwujeeuujjeujjwwejejejwuewewuwjeejeejwuuwwuujuuueujeeueejuuu
1000
2184
2550
3992
3956
328
588
4911
3085
4725
974
4880
4674
2769
4546
662
4159
3161
3465
1310
2431
2018
4349
1057
919
767
3513
1844
3671
4385
3792
2031
180
1921
3576
3386
419
2162
3607
2108
308
1613
1681
20...

output:

11 42
15 53
-1 -1
4 90
42 48
82 91
-1 -1
39 88
-1 -1
67 81
-1 -1
-1 -1
38 80
-1 -1
8 18
-1 -1
3 54
14 73
70 88
50 86
50 79
-1 -1
12 27
23 36
34 45
23 84
49 75
17 84
-1 -1
11 83
62 91
23 27
6 33
13 76
6 63
56 63
53 84
1 65
39 69
46 51
33 55
54 77
24 52
55 62
45 73
44 57
9 84
43 75
23 58
18 79
5 63
5 ...

result:

ok 1000 lines

Test #7:

score: 0
Accepted
time: 1ms
memory: 3628kb

input:

wwhceznchkxaapvubwijfxcgyvqywqdoaizsoxlisttlszxwdhcrsmeznsfaldsnbwzxushrmcchhozkrasgnzpsvquthrrh
1000
3471
1192
980
2224
2431
3259
1439
2079
4858
931
3569
4572
1808
1651
1485
1346
3786
3803
2499
2748
5241
4136
1871
2465
1979
3027
264
4165
2885
4512
3061
4578
3724
5154
5258
289
3620
2197
505
4355
238...

output:

32 81
55 69
36 47
47 74
35 65
51 96
50 67
1 26
-1 -1
4 15
36 87
3 96
65 87
46 65
22 39
23 39
15 71
23 80
33 65
12 48
-1 -1
29 95
28 50
57 88
44 68
1 41
70 73
29 96
40 78
4 89
44 85
-1 -1
31 86
-1 -1
-1 -1
68 71
15 67
37 64
3 9
9 84
24 54
-1 -1
4 71
39 84
53 60
10 68
8 23
42 51
14 56
-1 -1
62 95
53 5...

result:

ok 1000 lines

Test #8:

score: 0
Accepted
time: 1ms
memory: 3856kb

input:

fylqobffxudwaqylowoinlqxzvrkswrmdjjnxymspxfxqkjccxzlrunqenyuvflyqfedfwyxrymuvmgmrazceolhdknvxldmk
1000
824
5124
1712
3832
570
3344
4775
3672
3208
4320
589
4098
1436
4177
4364
4664
3037
4456
5145
1425
2422
2973
2474
1175
2689
4978
463
4299
3826
2843
3948
2859
5378
2303
4565
1672
4641
397
4638
4514
11...

output:

61 70
-1 -1
65 85
36 92
24 30
24 69
-1 -1
10 62
3 46
9 79
89 96
17 80
13 30
15 80
17 89
4 96
35 75
19 95
-1 -1
50 66
63 93
43 82
62 93
72 85
5 39
-1 -1
60 65
6 76
16 72
59 95
22 81
34 71
-1 -1
14 42
14 96
25 44
7 96
25 29
9 97
17 96
37 50
36 94
52 81
9 35
53 58
18 50
-1 -1
32 86
40 55
26 33
-1 -1
35...

result:

ok 1000 lines

Test #9:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

pnbahlmruoyvhchfbmcpqfrypdbtbagydeldohigazreigkdauhhcvroajpdhfhiclppvtdcheqvuynvyfzujqklahtlftiklbo
1000
4143
5633
2928
1976
4140
5759
5507
3696
4895
2805
5641
3468
4547
4731
5391
802
548
1456
409
228
1221
2962
3931
4272
4060
1173
3633
3297
941
5302
2555
806
3891
5507
5118
2727
4831
2549
3178
2931
1...

output:

5 66
-1 -1
60 97
38 61
13 74
-1 -1
-1 -1
27 78
-1 -1
16 51
-1 -1
18 64
22 96
3 86
-1 -1
13 22
66 72
32 48
17 22
33 36
14 28
43 80
36 92
4 69
40 99
18 31
28 77
5 48
76 86
-1 -1
39 70
61 70
22 77
-1 -1
-1 -1
3 37
3 94
61 92
13 54
34 71
80 98
-1 -1
39 46
-1 -1
3 79
84 86
31 83
15 71
1 60
25 41
13 95
21...

result:

ok 1000 lines

Test #10:

score: 0
Accepted
time: 1ms
memory: 3888kb

input:

khnggceactpjxgkkgkmaaennymuudfchyfudhnehrzrwmarmcxeamtkdsrwphgebferyqdghqffxcxvjdcqyxxwrjv
1000
3149
4863
2912
3132
2464
3977
3823
3742
2382
4000
4307
2548
297
4495
2983
2030
3128
3329
1735
2643
3409
2207
376
3706
1305
918
2818
3092
3338
3527
3477
4102
581
560
450
1366
1157
3501
2638
2356
1205
3578
...

output:

23 71
-1 -1
37 80
5 53
3 37
8 89
16 86
24 90
51 84
3 86
-1 -1
7 43
56 60
-1 -1
13 57
61 88
39 87
30 83
27 49
42 79
12 67
22 52
9 14
14 79
67 83
73 84
40 81
17 64
12 65
8 67
12 69
-1 -1
15 22
65 72
84 89
26 43
57 71
7 65
44 81
41 73
2 17
3 63
38 58
51 74
17 88
4 75
58 82
27 28
39 73
77 82
-1 -1
1 2
-...

result:

ok 1000 lines

Test #11:

score: 0
Accepted
time: 532ms
memory: 205048kb

input:

wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww...

output:

1 171154
1 828243
1 510784
1 804134
1 683819
1 326398
1 326368
1 906923
1 55858
1 800926
1 411361
1 584101
1 334671
1 321252
1 172896
1 139319
1 362552
1 881890
1 849611
1 724803
1 750804
1 670011
1 698097
1 77487
1 561441
1 144378
1 937646
1 917266
1 960579
1 704659
1 629076
1 872548
1 215439
1 237...

result:

ok 1000000 lines

Test #12:

score: 0
Accepted
time: 543ms
memory: 205012kb

input:

ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...

output:

1 453459
1 325431
1 985403
1 22031
1 418936
1 962336
1 328894
1 886316
1 23532
1 621350
1 216413
1 652980
1 19591
1 789090
1 442931
1 74176
1 338246
1 516199
1 238878
1 227328
1 667551
1 958351
1 706926
1 956911
1 454973
1 459206
1 486441
1 360451
1 239082
1 61405
1 449730
1 516118
1 266368
1 492024...

result:

ok 1000000 lines

Test #13:

score: 0
Accepted
time: 1155ms
memory: 182628kb

input:

ssqqqqqsssqqqsqsssqqsqsqsssqqsssssqqqqsqssssssqqqqqqqsqssqqsssssqssqqqqsqsqqqqsqsqsqqsqssqsqsqsqsqssqssqqssssqqsssqssssqqsqsqqsqsssqssssqsqqqsqqssssqssssqqsssssqqqqqqqsqqsqsssqssqsqsqssssqsqsqsssqqsqqqqqsssqqqqsssssssssqqsssssqqqssssqsssqqqsqqsqqqsssqqsqsqqqssqqqsqsssqqqsqsqsqsqqssssssqsqqqsqqsqsqsq...

output:

724208 857537
631036 821808
147823 330361
247584 861978
542180 780326
505885 897553
193482 651898
366291 612080
99799 877101
19575 84341
701229 956127
763530 773256
275832 506058
45757 917217
89212 691182
83303 820098
216295 369122
304947 537391
91052 216063
438199 497426
528486 755094
398457 534501...

result:

ok 1000000 lines

Test #14:

score: 0
Accepted
time: 1144ms
memory: 182424kb

input:

mtmttttttmmmtmmmtmtmtttmmmmtmtmmmmmmtmmtmmttmmmtmttmmttmmmmmmmmmtmtttmtttmmtmmttttttttmmmmmtmmmmttmtmttmtmmmtmmttmtttmtttttmmtttmtmmmmmmmtmttmttttmmttmmttmtmtmtmmmmmmtmtmttmmmmtmtttmtmttmmtmmtttmtmmttmtttmtttmmmtmtmttmttmmtttmmmmmtmmtmmmmtttttmtmtmtttttmtmttttmtmmttmtmtmmmmmtmmmmtmmmttmtttmmmmmtmttm...

output:

392374 808678
282286 759391
70161 972520
123813 970400
440302 990380
754957 938058
68048 554289
481198 644746
217223 824323
338587 741365
90306 178505
371748 563534
19271 470812
184628 302314
145590 900010
659644 670546
754837 843323
331821 686534
75566 113613
253023 644136
143758 954999
466009 8392...

result:

ok 1000000 lines

Test #15:

score: 0
Accepted
time: 1126ms
memory: 185368kb

input:

yysssyllyllyyslllsylyllssyssslslyllslyssslsyssllysylyslsslyylysllllyyyssllslsyllsyyllllssllylsylslsylssyssllylyyslllyylysyylyyysysssysllssslsyylllsyylyyyssylssssyslsysyysllyylsylysssyslysylsyllsllyylsysylyllslslslyllylsyslsyslsyyllsllsysllysyysyyllylsllyssyyyslysslslsslyylllsyyyllyysllylyyyyssylyyly...

output:

247840 288756
562857 976941
960680 974610
417203 517266
305165 771141
156374 686413
266967 328675
403058 987150
811347 879930
86971 466536
373773 692611
91766 701184
114721 368969
57144 276983
635114 993181
522470 560476
719066 750366
673546 711778
37842 180087
17500 952998
167040 659969
47986 82540...

result:

ok 1000000 lines

Test #16:

score: 0
Accepted
time: 1114ms
memory: 183876kb

input:

zgzggzlzgglzzgllgzllzlggzgzlgzzlgggglgzzglgggzzzlgggllzzgglgzlgglgllgllllzlggllzggzggzzgzzzzzzllggglzlgzzzlzllgzlgzzzzlzzglggzlgzzllgzgllzgzzzggllzllzggzgggglzlglgzlgzzgggllzzzzggzlzglzlglgggllzgggggzzlzlzzlglzzlzgglzzggzzglzgzlglgglggllggzlzlzglzlzzllzllgzllzlgglzzlzlggzlzllzggglzlglgzzggglglllzlgz...

output:

827620 876764
752784 945478
405352 406826
304393 986115
406375 898844
540985 581508
715538 870473
28125 556107
131145 319757
181628 489548
563756 896965
280273 432748
395311 522296
238918 751975
22632 159073
472157 792301
461836 688472
45136 896563
628845 739120
73982 620832
88818 232862
105488 6537...

result:

ok 1000000 lines

Test #17:

score: 0
Accepted
time: 1214ms
memory: 182624kb

input:

qnnuujjqunujnqnuqnunjuqnjunnjnjjjnnnjujjnjnnnjqunqjujquujqqqnjqquuunuuuqnunqnuqjqnnjnnuujnnnnqujjjnuujjjnqnujqnuquqununqjjunuqnqjunjuqjunnnnjnjjunuqnujnuququnqjjqqunujnqjqqjnjjquqnuqnjqjjquqnqnunnqjjjqquqjnjqnnqqqqqqjnnqnuqnqjnnujjnjunjnjnujnuqjqquujqujuunnuqujjunqqujqnjuqujjujjunqqjunuujuqujjnjnnjn...

output:

27251 297129
205668 900103
444545 544180
967130 970312
399426 793200
162463 845329
341499 703426
19264 888830
363449 626492
375327 839281
833953 942162
310516 516760
424003 826472
599550 634684
382435 755389
42256 704043
102641 997670
251723 628297
259597 747380
670335 854745
448571 589123
165473 85...

result:

ok 1000000 lines

Test #18:

score: 0
Accepted
time: 976ms
memory: 183564kb

input:

fxooxjfxfjfjxjxfofojjojooxfofoxooojojjjojffoofxofoxjjojfooofjjfjjxojjjxxoxfjofxjxjooojfooxxfxxxjjofxjfofjjoxjjjofjfxxjxxoffjffoojfxjxjooojxxofojfxjfoofjffjfoxffffjoojofxjjxofxfjofoxoxfxjxofxjfxfojofxfjoooooooxxjxojooxfxfxxxfxxofofjxoojoxjjjjjoffjoxojjxxojofxxfjojfojoxooxfjxfjooxjfojjfooofxoxfxjffjjf...

output:

45162 535717
415545 712758
862706 923509
262838 337632
382189 898331
271269 689261
387744 669825
813139 881156
69645 265761
340773 740584
491730 870757
343516 818583
124894 306762
150939 576511
52072 977117
231164 661184
309131 916703
783175 954942
387275 970551
472484 559367
574867 737184
201421 33...

result:

ok 1000000 lines

Test #19:

score: 0
Accepted
time: 1223ms
memory: 184252kb

input:

ammwdatadddwawmattaaddmdawdmdawtddwmmwwwdtwtmdmmmtmdmatmatdatdmwwadwdddtmtmmmawaawmwdmawdmtmtwamdawwdwtwtaamwaadwamdawtatammmtmmmttwtdmtmadmwadmwamdaattwtddtmtwaatwtwattwwtaamwtwdmattwwadataamtaamtmtadddmwdmawwmmtdadwadtdwwatdmwatadddwdmawtttdwtwdwmatdmdwttwwmawtwatwdtwtaaaawattmadmwtwtamwwmmaaamwtw...

output:

295524 813775
67787 589846
761309 823802
279383 516753
660853 751786
261522 328484
711303 782122
155645 274088
394745 667093
570467 709168
92924 170807
18823 590004
83928 293215
325821 505256
573977 708149
32924 327944
217988 882845
171775 818680
320314 660485
505595 980410
725517 838599
1373 648970...

result:

ok 1000000 lines

Test #20:

score: 0
Accepted
time: 979ms
memory: 183356kb

input:

zgfznzfpnggfpnnzzzggnfpzgznpnpzppfnfggpgzgppzzgznnfnfzzppnpfgnfzfnzgpggznfzpzfpnppfgppzpgggggpzfpzgnngnnfngpppppggznpfzznpzpnnnffppnffgfzpnffzfnzppzfgfnfngzppzzzfppzpngfzzzzggzpgpzfzgpgpzpfgpffgfzfnpngnzgznggppgzpfnfngnpppzggpgnzgnfgfppngpgzzgngngfpzfnpfnnznfnpzgfngfzgngngpnzgfnpnzgpppznggpznpzzgfpn...

output:

298915 797306
106153 442383
15904 281881
876965 894056
229861 737464
334355 893248
711560 931887
525557 657882
1566 121135
112651 338248
2711 688248
65278 511925
15293 402790
416402 982146
439037 661602
179008 543897
368669 693983
216121 625061
29382 851802
642451 817205
20145 33924
276618 873171
13...

result:

ok 1000000 lines

Test #21:

score: 0
Accepted
time: 1203ms
memory: 183708kb

input:

jdszvwszwbdedzgsbdovzeggnnqbzbxodgwnvwxweqqqnnoowvjgwxewdoqexzvojbxwwosdvxvwdbxjwsnzbqjngqegnvwdovgweqgvvnwnengvnvnzwgodgnnejezsdgqbnbzdzjgjwoeddwewewwvxvsojwdeqexgjeqeoneebwbvbjvojengvwsxqwndnebqqvdqdvbdgqqxnvvzdjgvwzjqxvjwnvgbxjdwojqedqebvdgswqgdwozbssjxneqnjdobsxonnqvzxzswxzodjexjdjxsonodzgvdjgxs...

output:

201012 708203
262361 446630
648271 966839
67788 89861
379841 851526
307317 978551
68761 764196
157397 775653
629651 962048
652932 901288
828931 876119
259944 595964
506704 631038
176995 413830
179867 326265
134308 902122
534972 586094
89176 261361
99845 633294
190008 512629
122503 747805
38384 77334...

result:

ok 1000000 lines

Test #22:

score: 0
Accepted
time: 1041ms
memory: 183672kb

input:

zyfzxskjesyfhhhfhzyssewywkzfehwshzwzhejjhjhejsjwyyfcwpxppmyzfjfxypxpxmjpmzjhfsphczmmjzhzzxhkppspjjxfcezpfeempxcwcmmxpmfjkysmwxypkkmwzjzypwkwwkzfhhsykmpzceczjjzkjypcjwexfmppkhmfpxxcyjmykkkyfpmmzwkkxewhysjwescjhkekwzwjjmkexhhhppheeemkpmkhsffwhfspppxyywwxpwkxhkkxekzwpmwsmyjfzfmkscfjwxxccwjkxkespmsfwcpk...

output:

244083 982528
82585 911773
30784 640697
638260 789088
286847 430240
768982 875423
126186 859737
527332 667740
737712 841175
296982 340197
139609 637095
208840 621423
115381 839896
220787 753835
587973 962019
328950 372849
833631 910690
250967 503903
48023 463151
756185 771782
430472 447710
397177 59...

result:

ok 1000000 lines

Test #23:

score: 0
Accepted
time: 1039ms
memory: 184060kb

input:

asujdgibqjufdslftiohwfqawzkiowzpjcqjdaaooxbqznxpbzftafabpwfbmgcafoczbhuqljklscozuvmxdwemicmvwesmqdscukojzbmkhcufkbpgmkostuboskjpllzgwhhwjszdcmstnjfgswhogplscxqaihfxwtxmkdkjfelbnjpsehaqpvgbjimlaubjdfjasgjfolamaqovupqwvhsilnczoinjaucavckgwhijdgwjshspksagumltnjlauzeafcbhgjeqhmbuekmiswggfnefwdbeskmmtxin...

output:

235125 404051
321130 702510
227759 706562
209847 975790
201616 754071
383061 564705
265399 612748
77837 458428
528231 899417
482251 841119
511964 807162
338372 376587
402027 728027
751973 780386
418936 972061
668393 761315
93962 439334
173684 642382
832247 839521
758158 821573
201629 558856
606698 8...

result:

ok 1000000 lines

Test #24:

score: 0
Accepted
time: 1076ms
memory: 185440kb

input:

scnmacaccniwcbkwjdbbmdunauveqhfntyritvbuvojkemijhvqmcdyaqsezjokexkkhzetgifsidkohmjqwhchyunejrvmdjnzxwyqmrkoobrmwitdriqiqcbddhfnafegcrtunxhfqbgjwgxemnfgqwstzkdezoiwoktcjqurjiiwkztzediwazvwirrmcwwunoqoxcybwwrsmdycqhonxuucssefkxmwjdjgeixywczeyvvnnrqnrynjxobtavshawnvionuazobzmhavkzmomyxuiajfebmevsigsjws...

output:

748036 951810
481136 884197
297670 646011
593054 725268
651091 882233
781654 960006
784836 915163
703016 722958
373014 416572
618623 894277
289937 784727
723705 998336
845 418515
359499 886397
568384 897968
100842 971179
244468 840223
436563 728662
539682 991476
416164 913170
384597 746617
767870 81...

result:

ok 1000000 lines

Test #25:

score: 0
Accepted
time: 932ms
memory: 183868kb

input:

jjrcyrxohhbsdaiclcjhngunligvfjhvdahwtqnxlbromwgcqynbgrtxhluvtigdgpdftdnczpfvqmwcgzdmhscbsnvzlwmsbxlgiucupkuhzxjbmardwwjrwgfwdwrjgkgsoocrzoinibqgsdcuhrrbxfpfbjyupuvbunfxurkpaypxdnkbwfwjdqynomiwqyjvbqwstihdctzvkmgjadhfjqkabdypyvhlounsfovzbxfomyddndcbiaiwnzifmosqhqrijffanugdysztcthtwgbqqtnqkzschpbbwfag...

output:

120418 229606
335576 949275
360686 482620
745345 986731
238570 770860
307932 458189
180831 723877
837750 963205
319971 830556
334277 615362
15913 926194
10510 769902
370293 578114
782320 883269
55678 596648
283945 926173
6349 931242
314922 319452
272056 672257
53026 980932
263811 790718
312437 92654...

result:

ok 1000000 lines

Test #26:

score: 0
Accepted
time: 1194ms
memory: 182604kb

input:

qjcommnykbwswfjyasxldtxaffvxcqkclkffhhogevowsqgytylnbbvmbwudamhdwaxnfdhskqfauojeoouvhxwczgbscbszzuvefbuvlywdpckaarpeuqnadkxfafawoagwxdwuadvaopvgpcqdpsowqojzlhcjzjpfxeabsletknneeuzprqahrgvhboqdhakbjnqvqquzvmxsencqvdjfwytxvajcyssomnlxxdvmlknmwcwozwdlssxkvyvfvjbyvkucchnmwhbzygblfckqcraynbxvlktfusggzgap...

output:

94333 680827
592541 917376
760420 932957
551816 683605
203044 339600
292833 561857
56658 69987
778185 882128
455517 790454
279472 545281
473304 644542
429143 480029
704125 864434
247229 444839
293855 336937
23716 372564
725637 998786
199682 454744
44207 699981
131928 852466
486213 723656
85636 37266...

result:

ok 1000000 lines

Test #27:

score: 0
Accepted
time: 1103ms
memory: 184060kb

input:

gajsczqwxupcalffxdcwnqzzwxkthrpbjtxsqrhgqtkelztbrqxsllegwqbwppmsgjhwmggkevrbwnerrcshghxvfwckwjizhmiyiijxlzkwpuwsjdddenosrbsxtlxuxxsgfarjiiflmiynwezbjkukbsspkipxcjfjrnsunzzrquglypqhyjpzprcwbrikhfmtntgrtatdjhqgmizfbmvgcrzpeywrjstuyqjdggkheienfpiguxzupqlhwnqgwawaqszsaclsfsbqwgqbhhipjsvdoefloxnsqbtlrxug...

output:

909412 989928
218599 952637
17603 909846
139657 448106
463538 992749
631938 747493
100660 111909
116002 148063
126240 829661
342367 628496
340868 427147
325346 691345
886445 973104
15414 943640
909368 951042
154441 754231
639860 981643
467248 936448
274883 312673
55632 673044
36836 67246
119698 1295...

result:

ok 1000000 lines

Test #28:

score: 0
Accepted
time: 1109ms
memory: 182416kb

input:

pkzrxeiunfunjzusutpqfatcwwwdhbukdtqsvclraptfjfqzbwcmyxnhszcajvltqzhnyqybpguqufmpxgkkoiunppdimtfjjpnfjfofpwnuzmzqjqiqzpdaktrztwxafqlpupnosylqebgrnspioobgeklzfimjdaoqmnoblhxasompxfdtyokajqswydiwesroitdvgvmjjvrxrkqbepyhftraiqogjgaaqjqjzkvspczmfkozbcgujvblcuqedmtutipgbnlabplzhrlmuzgwdzhcpzzyqwbbvamendle...

output:

682973 702799
222811 285161
824451 875370
528739 678864
606635 715950
218478 757806
94841 98783
370626 632541
704068 882630
155891 552100
332032 764325
784338 907770
488717 626425
224647 986658
456786 601637
534824 997124
302669 742427
667431 899728
268211 949371
66956 937445
713265 856069
7209 7662...

result:

ok 1000000 lines