QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#191792#5014. 复读程度hos_lyric#0 124ms20352kbC++1414.5kb2023-09-30 11:12:122024-07-04 02:48:50

Judging History

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

  • [2024-07-04 02:48:50]
  • 评测
  • 测评结果:0
  • 用时:124ms
  • 内存:20352kb
  • [2023-09-30 11:12:12]
  • 提交

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 <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")


template <class T> void bAdd(vector<T> &bit, int pos, const T &val) {
  const int bitN = bit.size();
  for (int x = pos; x < bitN; x |= x + 1) bit[x] += val;
}
template <class T> T bSum(const vector<T> &bit, int pos) {
  T ret = 0;
  for (int x = pos; x > 0; x &= x - 1) ret += bit[x - 1];
  return ret;
}
template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) {
  return bSum(bit, pos1) - bSum(bit, pos0);
}


////////////////////////////////////////////////////////////////////////////////
// SA-IS
//   String: string, vector<int>, vector<long long>
//   if sigma <= n,  O(n) time, O(n) space
//   if sigma >  n,  O(n log n) time, O(n) space
template <class String> vector<int> suffixArrayRec(const String &as) {
  const int n = as.size();
  if (n == 0) return {};
  const auto minmaxA = minmax_element(as.begin(), as.end());
  const auto minA = *minmaxA.first, maxA = *minmaxA.second;
  if (static_cast<unsigned long long>(maxA) - minA >=
      static_cast<unsigned long long>(n)) {
    vector<int> us(n);
    for (int u = 0; u < n; ++u) us[u] = u;
    std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (as[u] < as[v]);
    });
    int b = 0;
    vector<int> bs(n, 0);
    for (int i = 1; i < n; ++i) {
      if (as[us[i - 1]] != as[us[i]]) ++b;
      bs[us[i]] = b;
    }
    return suffixArrayRec(bs);
  }
  const int sigma = maxA - minA + 1;
  vector<int> pt(sigma + 1, 0), poss(sigma);
  for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1];
  for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a];
  // cmp[u] := (as[u, n) < as[u + 1, n))
  vector<bool> cmp(n);
  cmp[n - 1] = false;
  for (int u = n - 1; --u >= 0; ) {
    cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1];
  }
  // ><,  nn - id (0 <= id < n)
  int nn = 0;
  vector<int> ids(n, 0);
  int last = n;
  vector<int> nxt(n, 0);
  // put ><, from the tail of each bucket
  vector<int> us(n, 0);
  memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
  for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) {
    ids[u] = ++nn;
    nxt[u] = last;
    last = u;
    us[--poss[as[u] - minA]] = u;
  }
  // follow > backwards, from the head of each bucket
  memcpy(poss.data(), pt.data(), sigma * sizeof(int));
  us[poss[as[n - 1] - minA]++] = n - 1;
  for (int i = 0; i < n; ++i) {
    const int u = us[i];
    if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
  }
  // follow < backwards, from the tail of each bucket
  memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
  for (int i = n; --i >= 0; ) {
    const int u = us[i];
    if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
  }
  if (nn) {
    int vsLen = 0;
    vector<int> vs(nn);
    for (const int u : us) if (ids[u]) vs[vsLen++] = u;
    int b = 0;
    vector<int> bs(nn, 0);
    for (int j = 1; j < nn; ++j) {
      // as[v1, w1] (< or =) as[v0, w0]
      int v1 = vs[j - 1], v0 = vs[j];
      const int w1 = nxt[v1], w0 = nxt[v0];
      if (w1 - v1 != w0 - v0) {
        ++b;
      } else {
        for (; ; ++v1, ++v0) {
          if (v1 == n) { ++b; break; }
          if (as[v1] != as[v0]) { ++b; break; }
          if (v1 == w1) break;
        }
      }
      bs[nn - ids[vs[j]]] = b;
    }
    for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u;
    const auto sub = suffixArrayRec(bs);
    // put ><, from the tail of each bucket
    memset(us.data(), 0, n * sizeof(int));
    memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
    for (int j = nn; --j >= 0; ) {
      const int u = vs[sub[j]];
      us[--poss[as[u] - minA]] = u;
    }
    // follow > backwards, from the head of each bucket
    memcpy(poss.data(), pt.data(), sigma * sizeof(int));
    us[poss[as[n - 1] - minA]++] = n - 1;
    for (int i = 0; i < n; ++i) {
      const int u = us[i];
      if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
    }
    // follow < backwards, from the tail of each bucket
    memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
    for (int i = n; --i >= 0; ) {
      const int u = us[i];
      if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
    }
  }
  return us;
}

// us[i]: i-th suffix
// su[u]: index of as[u, n)
// hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n)
struct SuffixArray {
  int n;
  bool rmq;
  vector<int> us, su, hs, bsr;
  SuffixArray() : n(0), rmq(false) {}
  SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); }
  SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); }
  SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); }
  template <class String> void build(const String &as) {
    n = as.size();
    us = suffixArrayRec(as);
    su.resize(n + 1);
    for (int i = 0; i < n; ++i) su[us[i]] = i;
    su[n] = -1;
    hs.assign(n, 0);
    for (int h = 0, u = 0; u < n; ++u) if (su[u]) {
      for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {}
      hs[su[u]] = h;
      if (h) --h;
    }
    if (rmq) {
      const int logN = n ? (31 - __builtin_clz(n)) : 0;
      hs.resize((logN + 1) * n - (1 << logN) + 1);
      for (int e = 0; e < logN; ++e) {
        int *hes = hs.data() + e * n;
        for (int i = 0; i <= n - (1 << (e + 1)); ++i) {
          hes[n + i] = min(hes[i], hes[i + (1 << e)]);
        }
      }
      bsr.resize(n + 1);
      bsr[0] = -1;
      for (int i = 1; i <= n; ++i) bsr[i] = bsr[i >> 1] + 1;
    }
  }
  // Returns longest common prefix of as[u, n) and as[v, n).
  //   0 <= u, v <= n
  //   Assumes rmq.
  inline int lcp(int u, int v) const {
    if (u == v) return n - u;
    int i = su[u], j = su[v];
    if (i > j) swap(i, j);
    const int e = bsr[j - i];
    return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]);
  }
};
////////////////////////////////////////////////////////////////////////////////


/*
  len: depth in uncompacted suffix tree of the reversed string
  par: parent in compacted suffix tree of the reversed string
  prev: node from which it is cloned
  ll: whole length of the string when created
  node u: length in (nodes[nodes[u].par].len, nodes[u].len]
*/
////////////////////////////////////////////////////////////////////////////////
template <class Char, class Map = map<Char, int>> struct SuffixAutomaton {
  struct Node {
    int len, par, prev;
    Map next;
    int ll;
  };
  int l, n, cur;
  vector<Node> nodes;
  SuffixAutomaton() : l(0), n(1), cur(0), nodes{Node{
    /*len=*/0, /*par=*/-1, /*prev=*/-1, /*next=*/{},
    /*ll=*/0,
  }} {}
  Node &operator[](int u) { return nodes[u]; }
  void append(Char c) {
    ++l;
    int u = cur;
    cur = n++;
    nodes.push_back(Node{
        /*len=*/nodes[u].len + 1, /*par=*/0, /*prev=*/-1, /*next=*/{},
        /*ll=*/l,
    });
    for (; ~u && !nodes[u].next.count(c); u = nodes[u].par) {
      nodes[u].next[c] = cur;
    }
    if (~u) {
      const int v = nodes[u].next[c];
      if (nodes[u].len + 1 == nodes[v].len) {
        nodes[cur].par = v;
      } else {
        const int w = n++;
        nodes.push_back(Node{
            /*len=*/nodes[u].len + 1, /*par=*/nodes[v].par, /*prev=*/v,
            /*next=*/nodes[v].next,
            /*ll=*/l,
        });
        nodes[v].par = nodes[cur].par = w;
        for (; ~u && nodes[u].next[c] == v; u = nodes[u].par) {
          nodes[u].next[c] = w;
        }
      }
    }
  }
};
////////////////////////////////////////////////////////////////////////////////

struct Map {
  int as[26];
  Map() { memset(as, ~0, sizeof(as)); }
  int &operator[](char c) { return as[c - 'a']; }
  int count(char c) { return (~as[c - 'a']) ? 1 : 0; }
};


using U = unsigned long long;

int N, Q;
char S[100'010];
vector<U> WL, WR;
vector<int> L1, R1, L2, R2;


namespace brute {
int dpL[5010][5010], dpR[5010][5010];
U W[5010][5010];
vector<U> run() {
cerr<<"[brute::run]"<<endl;
  memset(dpL, 0, sizeof(dpL));
  memset(dpR, 0, sizeof(dpR));
  for (int i = N; --i >= 0; ) for (int j = N; --j >= 0; ) if (S[i] == S[j]) dpL[i][j] = 1 + dpL[i + 1][j + 1];
  for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) if (S[i] == S[j]) dpR[i + 1][j + 1] = dpR[i][j] + 1;
  memset(W, 0, sizeof(W));
  for (int l = 0; l <= N; ++l) for (int r = l + 1; r <= N; ++r) W[l][r] = 1;
  for (int l = 0; l < N; ++l) {
    vector<U> sums(N - l + 1, 0);
    for (int i = 0; i < N; ++i) sums[dpL[l][i]] += WL[i];
    for (int k = N - l; k >= 1; --k) sums[k - 1] += sums[k];
    for (int r = l + 1; r <= N; ++r) W[l][r] *= sums[r - l];
  }
  for (int r = 1; r <= N; ++r) {
    vector<U> sums(r + 1, 0);
    for (int i = 1; i <= N; ++i) sums[dpR[r][i]] += WR[i];
    for (int k = r; k >= 1; --k) sums[k - 1] += sums[k];
    for (int l = 0; l < r; ++l) W[l][r] *= sums[r - l];
  }
// for(int l=0;l<=N;++l){cerr<<"W["<<l<<"] = ";pv(W[l],W[l]+N+1);}
  vector<U> ans(Q, 0);
  for (int q = 0; q < Q; ++q) {
    const int need = max(R1[q] - L1[q], R2[q] - L2[q]);
    for (int l = 0; l <= N; ++l) for (int r = l + need; r <= N; ++r) {
      if (dpL[L1[q]][l] >= R1[q] - L1[q] && dpR[R2[q]][r] >= R2[q] - L2[q]) {
        ans[q] += W[l][r];
      }
    }
  }
  return ans;
}
}  // brute


namespace slow {
int dpL[5010][5010], dpR[5010][5010];
U W[5010][5010];

vector<vector<int>> graph;
int zeit;
vector<int> dis, fin;
void dfs(int u) {
  dis[u] = zeit++;
  for (const int v : graph[u]) dfs(v);
  fin[u] = zeit;
}

vector<U> run() {
cerr<<"[slow::run]"<<endl;
  memset(dpL, 0, sizeof(dpL));
  memset(dpR, 0, sizeof(dpR));
  for (int i = N; --i >= 0; ) for (int j = N; --j >= 0; ) if (S[i] == S[j]) dpL[i][j] = 1 + dpL[i + 1][j + 1];
  for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) if (S[i] == S[j]) dpR[i + 1][j + 1] = dpR[i][j] + 1;
  memset(W, 0, sizeof(W));
  for (int l = 0; l <= N; ++l) for (int r = l + 1; r <= N; ++r) W[l][r] = 1;
  for (int l = 0; l < N; ++l) {
    vector<U> sums(N - l + 1, 0);
    for (int i = 0; i < N; ++i) sums[dpL[l][i]] += WL[i];
    for (int k = N - l; k >= 1; --k) sums[k - 1] += sums[k];
    for (int r = l + 1; r <= N; ++r) W[l][r] *= sums[r - l];
  }
  for (int r = 1; r <= N; ++r) {
    vector<U> sums(r + 1, 0);
    for (int i = 1; i <= N; ++i) sums[dpR[r][i]] += WR[i];
    for (int k = r; k >= 1; --k) sums[k - 1] += sums[k];
    for (int l = 0; l < r; ++l) W[l][r] *= sums[r - l];
  }
// for(int l=0;l<=N;++l){cerr<<"W["<<l<<"] = ";pv(W[l],W[l]+N+1);}
  
  SuffixAutomaton<char, Map> sam;
  for (int i = 0; i < N; ++i) {
    sam.append(S[i]);
  }
  graph.assign(sam.n, {});
  for (int u = 1; u < sam.n; ++u) {
    graph[sam[u].par].push_back(u);
  }
  zeit = 0;
  dis.assign(sam.n, -1);
  fin.assign(sam.n, -1);
  dfs(0);
  vector<int> prefix(N + 1, 0);
  for (int i = 0; i < N; ++i) {
    prefix[i + 1] = sam[prefix[i]].next[S[i]];
  }
  vector<int> query(Q, 0);
  for (int q = 0; q < Q; ++q) {
    int &u = query[q];
    for (int i = L2[q]; i < R2[q]; ++i) {
      u = sam[u].next[S[i]];
    }
  }
  
  vector<vector<int>> qss(N + 1);
  for (int q = 0; q < Q; ++q) {
    qss[max(R1[q] - L1[q], R2[q] - L2[q])].push_back(q);
  }
  vector<U> ans(Q, 0);
  for (int l = 0; l < N; ++l) {
    vector<U> bit(sam.n, 0);
    for (int r = N; r > l; --r) {
      bAdd(bit, dis[prefix[r]], W[l][r]);
      for (const int q : qss[r - l]) {
        if (dpL[L1[q]][l] >= R1[q] - L1[q]) {
          ans[q] += bSum(bit, dis[query[q]], fin[query[q]]);
        }
      }
    }
  }
  return ans;
}
}  // slow


namespace sub4 {
vector<U> run() {
cerr<<"[sub4::run]"<<endl;
  string revS = S;
  reverse(revS.begin(), revS.end());
  const SuffixArray sa(revS, true);
  
  vector<vector<int>> qss(N + 1);
  for (int q = 0; q < Q; ++q) {
    qss[max(R1[q] - L1[q], R2[q] - L2[q])].push_back(q);
  }
  vector<U> bit(N, 0);
  vector<U> ans(Q, 0);
  for (int r = N; r > 0; --r) {
    bAdd(bit, sa.su[N - r], WL[0] * WR[r]);
    for (const int q : qss[r]) {
      int posL, posR;
      {
        int lo = -1, hi = sa.su[N - R2[q]];
        for (; lo + 1 < hi; ) {
          const int mid = (lo + hi) / 2;
          ((sa.lcp(sa.us[mid], N - R2[q]) >= R2[q] - L2[q]) ? hi : lo) = mid;
        }
        posL = hi;
      }
      {
        int lo = sa.su[N - R2[q]], hi = N;
        for (; lo + 1 < hi; ) {
          const int mid = (lo + hi) / 2;
          ((sa.lcp(sa.us[mid], N - R2[q]) >= R2[q] - L2[q]) ? lo : hi) = mid;
        }
        posR = lo;
      }
      ans[q] += bSum(bit, posL, posR + 1);
    }
  }
  return ans;
}
}  // sub4



int main() {
  for (; ~scanf("%d%d", &N, &Q); ) {
    scanf("%s", S);
    WL.assign(N + 1, 0);
    for (int i = 0; i < N; ++i) {
      scanf("%llu", &WL[i]);
    }
    WR.assign(N + 1, 0);
    for (int i = 1; i <= N; ++i) {
      scanf("%llu", &WR[i]);
    }
    L1.resize(Q);
    R1.resize(Q);
    L2.resize(Q);
    R2.resize(Q);
    for (int q = 0; q < Q; ++q) {
      scanf("%d%d%d%d", &L1[q], &R1[q], &L2[q], &R2[q]);
      --L1[q];
      --L2[q];
    }
    
    bool spe4 = true;
    spe4 = spe4 && (L1 == vector<int>(Q, 0));
    for (int i = 1; i < N; ++i) spe4 = spe4 && (S[0] != S[i]);
    
    vector<U> ans;
    if (spe4) {
      ans = sub4::run();
    } else {
exit(0);
      ans = slow::run();
    }
    for (int q = 0; q < Q; ++q) {
      printf("%llu\n", ans[q]);
    }
  }
  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: 1ms
memory: 3840kb

input:

500 500
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:


result:

wrong answer 1st lines differ - expected: '15720454042420499810', found: ''

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #22:

score: 6
Accepted
time: 109ms
memory: 20008kb

input:

100000 100000
zbbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...

output:

16102224067619618967
2409962914769200003
427496158535942638
17668679206267169316
9612725428377010375
16283030984784184667
14966758574838045581
8108029333542434517
5821899279772898061
7354415533246368927
15016230232022193055
9072126619623269970
5490256818353051548
432088324301719512
13681741566473101...

result:

ok 100000 lines

Test #23:

score: 0
Accepted
time: 124ms
memory: 20352kb

input:

100000 100000
zsyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysy...

output:

4559870725517531243
7294838067589907718
11611591353940805930
6570123277626119382
7422995984298182834
5907659314847245750
16910510485299347733
4264602948914788684
13190214217087942183
6600183534290302450
18342681242733610030
11565497126186922166
17128453730139037795
1670830382187028213
18164994643596...

result:

ok 100000 lines

Test #24:

score: -6
Wrong Answer
time: 27ms
memory: 6792kb

input:

100000 100000
zoooooooollexlwockjmmpcsmrmxbcsxiopbhrsgmuffubpextcneqsmtouhuovwmosufyvtciwaiqfgxdjgebcnwbeyyyascjixpskyeyoecigpydkqrssvcwcuirkwyxxbcfgjdorrrgdghdooooooooofnkxriqwewxjgitnhfrykdhcrpbgmcnqujvlugcougvywjyjknbcfqdohyxidpswedsqodaqavibkmrykeiqfmoyavdcctpjvqomwmhjysbynqskjvprebydvglxmnqsvxy...

output:


result:

wrong answer 1st lines differ - expected: '812998614822431625', found: ''

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #2:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%