QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#210157#5037. 回文hos_lyric#0 2498ms9816kbC++147.1kb2023-10-11 03:52:312024-07-04 02:18:30

Judging History

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

  • [2024-07-04 02:18:30]
  • 评测
  • 测评结果:0
  • 用时:2498ms
  • 内存:9816kb
  • [2023-10-11 03:52:31]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


// |as| = n ==> |rs| = 2 n + 1
// [i - rs[i], i + rs[i]] is palindrome for $ as[0] $ as[1] $ ... $ as[n-1] $
// as[i, j): palindrome <=> j - i <= rs[i + j]
template <class String> vector<int> manacher(const String &as) {
  const int n = as.size();
  vector<int> rs(2 * n + 1);
  for (int i = 0, j = 0, k; i <= 2 * n; i += k, j -= k) {
    for (; 0 < i - j && i + j < 2 * n &&
           (!((i + j + 1) & 1) || as[(i - j - 1) >> 1] == as[(i + j + 1) >> 1]);
         ++j) {}
    rs[i] = j;
    for (k = 1; k < j && k + rs[i - k] < j; ++k) rs[i + k] = rs[i - k];
  }
  return rs;
}


// point add, rectangle sum
constexpr int INF = 1001001001;
template <class X, class Y, class T> struct Bit2d {
  vector<X> xs;
  vector<pair<Y, X>> yxs;
  vector<vector<Y>> yss;
  int m;
  vector<int> ns;
  vector<vector<T>> bit;
  Bit2d() {}
  void book(X x, Y y) {
    xs.push_back(x);
    yxs.emplace_back(y, x);
  }
  void build() {
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    m = xs.size();
    yss.assign(m, {});
    sort(yxs.begin(), yxs.end());
    for (const auto &yx : yxs) {
      const int x = yx.second, y = yx.first;
      const int a = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
      assert(a < m); assert(xs[a] == x);
      for (int u = a; u < m; u |= u + 1) yss[u].push_back(y);
    }
    ns.assign(m, 0);
    bit.assign(m, {});
    for (int u = 0; u < m; ++u) {
      yss[u].erase(unique(yss[u].begin(), yss[u].end()), yss[u].end());
      ns[u] = yss[u].size();
      bit[u].assign(ns[u], 0);
    }
  }
  void add(int x, int y, T val) {
    const int a = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
    assert(a < m); assert(xs[a] == x);
    for (int u = a; u < m; u |= u + 1) {
      const int b = lower_bound(yss[u].begin(), yss[u].end(), y) - yss[u].begin();
      assert(b < ns[u]); assert(yss[u][b] == y);
      for (int v = b; v < ns[u]; v |= v + 1) bit[u][v] += val;
    }
  }
  T get(int x0, int x1, int y0, int y1) const {
    const int a0 = lower_bound(xs.begin(), xs.end(), x0) - xs.begin();
    const int a1 = lower_bound(xs.begin(), xs.end(), x1) - xs.begin();
    T ret = 0;
    for (int u = a0; u; u &= u - 1) ret -= get(u - 1, y0, y1);
    for (int u = a1; u; u &= u - 1) ret += get(u - 1, y0, y1);
    return ret;
  }
 private:
  T get(int u, int y0, int y1) const {
    T ret = 0;
    const int b0 = lower_bound(yss[u].begin(), yss[u].end(), y0) - yss[u].begin();
    const int b1 = lower_bound(yss[u].begin(), yss[u].end(), y1) - yss[u].begin();
    for (int v = b0; v; v &= v - 1) ret -= bit[u][v - 1];
    for (int v = b1; v; v &= v - 1) ret += bit[u][v - 1];
    return ret;
  }
};


char S[200'010];
int N, Q;
vector<int> O, I, L, R;
vector<char> C;


namespace brute {
vector<int> run() {
cerr<<"[brute::run]"<<endl;
  string s = S;
  vector<int> anss;
  int lastans = 0;
  for (int q = 0; q < Q; ++q) {
    if (O[q] == 1) {
      const int i = (I[q] ^ lastans) - 1;
      s[i] = C[q];
    } else {
      const int l = (L[q] ^ lastans) - 1;
      const int r = (R[q] ^ lastans);
      const auto rs = manacher(s);
      int ans = 0;
      for (int i = l; i < r; ++i) if (rs[i + r] >= r - i) {
        ++ans;
      }
      anss.push_back(ans);
      lastans = ans;
    }
  }
  return anss;
}
}  // brute


namespace sub2 {
vector<int> run() {
cerr<<"[sub2::run]"<<endl;
  const auto rs = manacher(string(S));
  Bit2d<int, int, int> bit;
  for (int i = 0; i <= 2 * N; ++i) bit.book(i, rs[i] + i);
  bit.build();
  for (int i = 0; i <= 2 * N; ++i) bit.add(i, rs[i] + i, 1);
  vector<int> anss;
  int lastans = 0;
  for (int q = 0; q < Q; ++q) {
    const int l = (L[q] ^ lastans) - 1;
    const int r = (R[q] ^ lastans);
    /*
      l <= i < r && rs[i + r] >= r - i
      l + r <= j < 2 r && rs[j] + j >= 2 r
    */
    const int ans = bit.get(l + r, 2 * r, 2 * r, 3 * N);
    anss.push_back(ans);
    lastans = ans;
  }
  return anss;
}
}  // sub2


namespace sub3 {
constexpr int K = 60;
string s;
bool isPalin(int l, int r) {
  for (; l < --r; ++l) if (s[l] != s[r]) return false;
  return true;
}
vector<int> run() {
cerr<<"[sub3::run]"<<endl;
  s = S;
  vector<Int> fs(N + 1, 0);
  for (int r = 0; r <= N; ++r) {
    for (int k = 1; k <= K && r - k >= 0; ++k) {
      if (isPalin(r - k, r)) {
        fs[r] |= 1LL << k;
      }
    }
  }
  vector<int> anss;
  int lastans = 0;
  for (int q = 0; q < Q; ++q) {
    if (O[q] == 1) {
      const int i = (I[q] ^ lastans) - 1;
      s[i] = C[q];
// cerr<<COLOR("33")<<s<<" "<<1<<" "<<i<<" "<<C[q]<<COLOR()<<endl;
      for (int r = i + 1; r <= i + K && r <= N; ++r) {
        for (int k = r - i; k <= K && r - k >= 0; ++k) {
          fs[r] &= ~(1LL << k);
          if (isPalin(r - k, r)) {
            fs[r] |= 1LL << k;
          }
        }
      }
    } else {
      const int l = (L[q] ^ lastans) - 1;
      const int r = (R[q] ^ lastans);
// cerr<<COLOR("33")<<s<<" "<<2<<" "<<l<<" "<<r<<COLOR()<<endl;
      const int ans = __builtin_popcountll(fs[r] & ((1LL << (r - l + 1)) - 1));
      anss.push_back(ans);
      lastans = ans;
    }
  }
  return anss;
}
}  // sub3


int main() {
  for (; ~scanf("%s", S); ) {
    N = strlen(S);
    scanf("%d", &Q);
    O.resize(Q, -1);
    I.resize(Q, -1);
    C.resize(Q, '?');
    L.resize(Q, -1);
    R.resize(Q, -1);
    for (int q = 0; q < Q; ++q) {
      scanf("%d", &O[q]);
      if (O[q] == 1) {
        scanf("%d %c", &I[q], &C[q]);
      } else {
        scanf("%d%d", &L[q], &R[q]);
      }
    }
    
    vector<int> anss;
if(1){/*    if (O == vector<int>(Q, 2)) {
      anss = sub2::run();
    } else if (N <= 5000 && Q <= 5000) {
      anss = brute::run();
    } else {
*/      anss = sub3::run();
    }
    for (const int ans : anss) {
      printf("%d\n", ans);
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 32ms
memory: 4220kb

input:

aabbbabbbaaaaaaabbabbbaaaabaaaabbaabbaabbaaababbbabbbbabbbbaaaabbbabbbbbaaabbbabbaaaaabbbbbaaababbabbaaaaabaabbbbaaababaaabbaabbabbbabbbbaaaaabbbbbbbabbbaabbbabbabbbababbabbbbaaaaabbaababbbbaabbaaaaaabaaabbbaaababbbbaabaaaaababababbbbbbaaaabbbbaaababaaabbaabbbbbaaaabbaaaabaaaaaababbababaaaabaaababaa...

output:

2
3
5
3
3
3
2
3
4
2
0
2
4
2
3
3
3
2
2
2
2
2
2
3
4
2
2
3
2
2
4
0
3
2
3
3
4
3
3
4
4
2
0
2
7
2
4
2
4
0
3
3
3
2
2
2
4
3
1
2
2
1
2
2
3
3
4
2
2
4
2
3
2
3
3
2
3
2
2
3
2
2
3
6
2
2
2
6
4
3
2
2
2
2
3
2
5
4
2
2
2
3
2
4
2
2
2
2
2
0
3
2
2
3
3
4
3
2
3
3
2
2
0
0
2
2
1
2
3
3
3
3
2
4
2
3
2
3
2
3
2
4
4
5
2
2
3
5
3
0
...

result:

wrong answer 11th words differ - expected: '4', found: '0'

Subtask #2:

score: 0
Wrong Answer

Test #15:

score: 0
Wrong Answer
time: 69ms
memory: 9632kb

input:

aabababbabbbbbbabbababaaaabababbabbbbbbabbababaaaabababbabbbbbbabbababaaaabababbabbbbbbabbababaabbbaaabbaabbbbaabbaaabbbaabababbabbbbbbabbababaaaabababbabbbbbbabbababaaaabababbabbbbbbabbababaaaabababbabbbbbbabbababaabbaaabaaaaaabaaabbaabababbabbbbbbabbababaaaabababbabbbbbbabbababaaaabababbabbbbbbabb...

output:

5
4
7
4
5
6
4
2
4
4
5
5
4
5
3
2
4
4
3
4
3
3
3
4
3
2
2
3
3
3
2
2
5
2
5
3
3
2
4
3
2
2
1
4
4
2
1
3
2
4
1
4
6
3
4
4
3
0
3
3
4
2
3
5
4
4
5
3
0
4
4
5
2
7
7
3
4
4
5
1
5
3
4
0
6
3
1
1
2
4
2
3
0
4
4
3
3
2
2
4
4
4
2
4
7
3
3
5
4
4
3
3
3
4
4
5
4
6
4
4
5
1
3
3
3
2
3
2
3
3
2
2
3
4
6
2
4
3
2
3
5
2
1
2
3
4
5
4
5
4
...

result:

wrong answer 1st words differ - expected: '41', found: '5'

Subtask #3:

score: 0
Wrong Answer

Test #21:

score: 10
Accepted
time: 2498ms
memory: 8712kb

input:

abbaaaabbabaaaaaabaabbaabbababbaaabbabbbabaabaabaaaaabaabbbbabbabbabbbababbbababababbbbabaabbaaababbbbbababbbbaabbbaaabaababababaabbbbbbaababaabbaaabaabbaaababbabbabbbbaaaaabaaabbbabbbbbbabbbabbabaabbbbbbbaaaabbaaaababbbaaaaaaababaabbbbaaabaaabbaabbbbbbababbaabbaaabbabbbbbabaababbaabaaaabbbbabababba...

output:

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
8
8
8
8
...

result:

ok 198 tokens

Test #22:

score: -10
Wrong Answer
time: 107ms
memory: 9816kb

input:

abbaababbabababbbbabababbbaaaabababbbbaabaabbbbabbbabbaabababbbabaaabbabbaabbbbbbaaababaababbbaabbbbaabbbaaaaaabaabbaabbaaaabaaabbaabbbaabbbababbbabbaaababaabaaabaabbabababaaaabaabbbbaaabbbbbbabbbbababbaabaabbbbabbaaabbaabaabbaabaabaaaaaabbaaabbbbbabbbbbaaabbabbabbaababaaabbabbaaaaaabbababbbaabbaabb...

output:

2
4
3
3
3
2
2
3
4
1
2
4
7
4
2
3
2
4
4
4
3
2
4
3
4
4
2
3
3
3
3
2
13
2
2
5
5
3
3
2
0
3
2
3
3
4
2
4
2
3
3
2
2
6
2
3
4
0
3
2
2
2
2
3
3
4
3
2
2
3
2
4
3
5
5
3
3
2
2
1
2
2
3
3
2
4
2
3
2
5
3
2
1
2
4
3
3
2
5
3
3
4
4
2
2
3
4
2
2
2
2
3
2
7
5
6
3
2
3
3
4
2
6
3
2
6
3
5
3
5
4
3
4
2
3
2
3
3
4
4
5
4
3
2
12
2
2
3
2
...

result:

wrong answer 2nd words differ - expected: '6', found: '4'

Subtask #4:

score: 0
Wrong Answer

Test #27:

score: 0
Wrong Answer
time: 1288ms
memory: 9184kb

input:

babbaaabbbbbbbabbbababaabbbbbababababbaabaabbbbbbabbbbbbbbbbababbbbabbaabbbaabaabbabbbaabbabbbabbababaababbbabbbbaabbabbabbaaaabbbaaabbbbaabbaaaaaaabbbabbbaaabaababaaabaaaabaaaababaaaaababaaaabaabbaaaabbbabbaabaabbbabbbbbaaabaababbbaaaaabbbbaaabbbbaabbabbbbabbbabbaaaaabaabaaaabbbabbbbbaabbbbabbbbaab...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st words differ - expected: '4', found: '0'

Subtask #5:

score: 0
Skipped

Dependency #1:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%