QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#464#248016#7621. Palindromecmk666ucup-team896Success!2023-11-25 19:19:022023-11-25 19:19:02

Details

Extra Test:

Wrong Answer
time: 47ms
memory: 435388kb

input:

12 anaaaatlyhab
1
1 12

output:

0 26

result:

wrong answer 1st lines differ - expected: '9 1', found: '0 26'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#248016#7621. Palindromeucup-team896#WA 993ms536268kbC++146.1kb2023-11-11 17:01:582023-11-25 19:21:43

answer

#include <bits/stdc++.h>
#ifdef dbg
#define D(...) fprintf(stderr, __VA_ARGS__)
#define DD(...) D(#__VA_ARGS__ " = "), debug_helper::debug(__VA_ARGS__), D("\n")
#include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

using namespace std;

template <int P>
class mod_int {
  using Z = mod_int;

private:
  static int mo(int x) { return x < 0 ? x + P : x; }

public:
  int x;
  int val() const { return x; }
  mod_int() : x(0) {}
  template <class T>
  mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
  bool operator==(const Z &rhs) const { return x == rhs.x; }
  bool operator!=(const Z &rhs) const { return x != rhs.x; }
  Z operator-() const { return Z(x ? P - x : 0); }
  Z pow(long long k) const {
    Z res = 1, t = *this;
    while (k) {
      if (k & 1) res *= t;
      if (k >>= 1) t *= t;
    }
    return res;
  }
  Z &operator++() {
    x < P - 1 ? ++x : x = 0;
    return *this;
  }
  Z &operator--() {
    x ? --x : x = P - 1;
    return *this;
  }
  Z operator++(int) {
    Z ret = x;
    x < P - 1 ? ++x : x = 0;
    return ret;
  }
  Z operator--(int) {
    Z ret = x;
    x ? --x : x = P - 1;
    return ret;
  }
  Z inv() const { return pow(P - 2); }
  Z &operator+=(const Z &rhs) {
    (x += rhs.x) >= P && (x -= P);
    return *this;
  }
  Z &operator-=(const Z &rhs) {
    (x -= rhs.x) < 0 && (x += P);
    return *this;
  }
  Z &operator*=(const Z &rhs) {
    x = 1ULL * x * rhs.x % P;
    return *this;
  }
  Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                 \
  friend T operator o(const Z &lhs, const Z &rhs) {\
    Z res = lhs;                                   \
    return res o## = rhs;                          \
  }
  setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
};
const int P = 1011451423;
using Z = mod_int<P>;

const int N = 500010;
const int M = 2000010;
const Z B = 13331;

int n, m;
char s[N];
Z pw[N], pre[N], suf[N];

struct PAM {
  char s[N];
  int len[M], num[M], fail[M], fa[M][22], cur = 1, pos = 1, trie[M][26], tot = 2, edp[M];
  void mem() {
    rep (i, 0, M - 1) rep (j, 0, 25) trie[i][j] = 1;
    rep (i, 0, M - 1) fail[i] = 1;
  }
  int getfail(int x, int i) {
    while (i - len[x] - 1 < 0 || s[i - len[x] - 1] != s[i]) x = fail[x];
    return x;
  }
  void ins(char ch, int i) {
    pos = getfail(cur, i);
    if (trie[pos][ch - 'a'] == 1) {
    	fail[++tot] = trie[getfail(fail[pos], i)][s[i] - 'a'];
    	trie[pos][s[i] - 'a'] = tot;
    	len[tot] = len[pos] + 2;
      num[tot] = num[fail[tot]] + 1;
    }
    cur = trie[pos][s[i] - 'a'];
    edp[i + 1] = cur;
  }
  void solve() {
    rep (i, 1, tot) fa[i][0] = fail[i];
    rep (j, 1, 21) rep (i, 1, tot) fa[i][j] = fa[fa[i][j - 1]][j - 1];
  }
  int jump(int x, int lim) {
    if (len[x] <= lim) return len[x];
    per (i, 21, 0) if (len[fa[x][i]] > lim) x = fa[x][i];
    return len[fa[x][0]];
  }
} P1, P2;

Z Zha(int l, int r) {
  return pre[r] - pre[l - 1] * pw[r - l + 1];
}

Z Fha(int l, int r) {
  return suf[l] - suf[r + 1] * pw[r - l + 1];
}

int pipe(int lp, int rp) {
  int l = 0, r = rp - lp + 1, res = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (Zha(lp, lp + mid - 1).val() == Fha(rp - mid + 1, rp).val()) res = mid, l = mid + 1;
    else r = mid - 1;
  }
  return res;
}

int LCP(int x, int l1, int r1, int l2, int r2) {
  int l = 0, r = min(n - x + 1, r2 - l2 + 1 + r1 - l1 + 1), res = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    Z h1 = Zha(x, x + mid - 1);
    Z h2 = 0;
    if (mid <= r1 - l1 + 1) {
      h2 = Zha(l1, l1 + mid - 1);
    } else {
      h2 = Zha(l1, r1) * pw[mid - (r1 - l1 + 1)] + Zha(l2, l2 + (mid - (r1 - l1 + 1)) - 1);
    }
    if (h1.val() == h2.val()) res = mid, l = mid + 1;
    else r = mid - 1;
  }
  return res;
}

int LCS(int x, int l1, int r1, int l2, int r2) {
  int l = 0, r = min(x, r2 - l2 + 1 + r1 - l1 + 1), res = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    Z h1 = Fha(x - mid + 1, x);
    Z h2 = 0;
    if (mid <= r1 - l1 + 1) {
      h2 = Fha(r1 - mid + 1, r1);
    } else {
      h2 = Fha(l1, r1) * pw[mid - (r1 - l1 + 1)] + Fha(r2 - (mid - (r1 - l1 + 1)) + 1, r2);
    }
    if (h1.val() == h2.val()) res = mid, l = mid + 1;
    else r = mid - 1;
  }
  return res;
}

int main() {
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  cin >> n >> (s + 1);
  pw[0] = 1;
  rep (i, 1, n) pw[i] = pw[i - 1] * B;
  rep (i, 1, n) pre[i] = pre[i - 1] * B + s[i];
  per (i, n, 1) suf[i] = suf[i + 1] * B + s[i];
  P1.mem(), P2.mem();
  rep (i, 1, n) P1.s[i - 1] = s[i], P2.s[i - 1] = s[n - i + 1];
  P1.fail[1] = P2.fail[1] = 2, P1.fail[2] = P2.fail[2] = 2;
  P1.len[2] = P2.len[2] = -1, P1.len[1] = P2.len[1] = 0;
  rep (i, 1, n) P1.ins(s[i], i - 1), P2.ins(s[n - i + 1], i - 1);
  P1.solve(), P2.solve();
  cin >> m;
  while (m--) {
    int l, r;
    cin >> l >> r;
    int len = pipe(l, r);
    if (len == r - l + 1) {
      cout << "0 0\n";
      continue;
    }
    int L = l + len, R = r - len, ans = 0;
    int le = P2.jump(P2.edp[n - L + 1], R - L + 1);
    int ri = P1.jump(P1.edp[R], R - L + 1);
    cout << R - L + 1 - max(le, ri) << " ";
    int rest = r - l + 1 - (R - L + 1 - max(le, ri));
    if (le >= ri) {
      int x = LCP(l, l, L + le - 1, R + 1, r);
      int y = LCS(r, R + 1, r, l, L + le - 1);
      int llim = max(0, rest - y), rlim = min(x, rest);
      ans += max(0, rlim - llim + 1);
    }
    if (ri >= le) {
      int x = LCP(l, l, L - 1, R - ri + 1, r);
      int y = LCS(r, R - ri + 1, r, l, L - 1);
      int llim = max(0, rest - y), rlim = min(x, rest);
      ans += max(0, rlim - llim + 1);
    }
    cout << ans << "\n";
  }
}