QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#268529#7748. Karshilov's Matching Problem IInimaWA 109ms34776kbC++172.9kb2023-11-28 18:14:062023-11-28 18:14:07

Judging History

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

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2023-11-28 18:14:07]
  • 评测
  • 测评结果:WA
  • 用时:109ms
  • 内存:34776kb
  • [2023-11-28 18:14:06]
  • 提交

answer

/**
 *    author:  NimaAryan
 *    created: 2023-11-28 12:55:08  
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

class HString {
 public:
  i64 M = 1E9 + 7;
  i64 B = 132232;

  int n;
  vector<i64> powB{1};
  vector<i64> hash;

  HString(const string& s) : n(s.size()) {
    while (powB.size() < n) {
      powB.push_back(powB.back() * B % M);
    }
    hash.resize(n + 1);
    for (int i = 0; i < n; ++i) {
      hash[i + 1] = (hash[i] * B + s[i]) % M;
    }
  }

  i64 get(int l, int r) {
    return (hash[r] - hash[l] * powB[r - l] % M + M) % M;
  }
};

vector<int> kmp(const string& s) {
  const int n = (int) s.size();
  vector<int> pi(n);
  for (int i = 1, j = 0; i < n; ++i) {
    while (j > 0 && s[j] != s[i]) {
      j = pi[j - 1];
    }
    j += (int) (s[j] == s[i]);
    pi[i] = j;
  }
  return pi;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  
  int n, m;
  cin >> n >> m;

  string S;
  cin >> S;
  string T;
  cin >> T;

  vector<int> w(n);
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
  }

  vector<int> l(m), r(m);
  for (int i = 0; i < m; ++i) {
    cin >> l[i] >> r[i];
    --l[i], --r[i];
  }

  HString hashS(S);
  HString hashT(T);
  vector<int> lcp(n);
  for (int i = 0; i < n; ++i) {
    int lo = 0, hi = n - i;
    int to = 0;
    while (lo <= hi) {
      int mid = (lo + hi) / 2;
      if (hashT.get(i, i + mid) == hashS.get(0, mid)) {
        to = mid;
        lo = mid + 1;
      } else {
        hi = mid - 1;
      }
    }
    lcp[i] = to;
  }

  vector<vector<int>> del(n + 1);
  for (int i = 0; i < n; ++i) {
    del[i + lcp[i]].push_back(i);
  }

  vector<vector<int>> qry_r(n);
  for (int i = 0; i < m; ++i) {
    qry_r[r[i]].push_back(i);
  }

  vector<int> near(m);
  set<int> alive;
  for (int i = 0; i < n; ++i) {
    alive.insert(i);
    for (int j : del[i]) {
      alive.erase(j);
    }
    for (int x : qry_r[i]) {
      auto it = alive.lower_bound(l[x]);
      near[x] = (it != alive.end() ? *it : r[x] + 1);
    }
  }

  vector<vector<int>> qry_l(n);
  for (int i = 0; i < m; ++i) {
    qry_l[l[i]].push_back(i);
  }

  vector<i64> pref(n + 1);
  for (int i = 0; i < n; ++i) {
    pref[i + 1] = pref[i] + w[i];
  }

  vector<i64> f(n + 1);
  vector<i64> ans(m);
  for (int i = n - 1; i >= 0; --i) {
    f[i] = pref[lcp[i]] + f[i + 1];
    for (int x : qry_l[i]) {
      ans[x] += f[i] - f[near[x]];
    }
  }

  auto pi = kmp(S);

  vector<int> sum(n);
  for (int i = 0; i < n; ++i) {
    sum[i + 1] = w[i] + sum[pi[i]];
  }

  vector<i64> g(n + 1);
  for (int i = 0; i < n; ++i) {
    g[i + 1] = g[i] + sum[i + 1];
  }

  for (int i = 0; i < m; ++i) {
    ans[i] += g[r[i] - near[i] + 1];
  }

  for (int i = 0; i < m; ++i) {
    cout << ans[i] << "\n";
  }

  return 0;
}

詳細信息

Test #1:

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

input:

8 5
abbabaab
aababbab
1 2 4 8 16 32 64 128
1 1
2 3
3 5
4 7
1 8

output:

1
3
3
16
38

result:

ok 5 lines

Test #2:

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

input:

15 4
heheheheehhejie
heheheheheheheh
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9
2 3
4 8
2 6
1 15

output:

3
13
13
174

result:

ok 4 lines

Test #3:

score: -100
Wrong Answer
time: 109ms
memory: 34776kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

108147037823514
221513513026814
454974735507482
439921126202661
147750675686077
88493386390345
350914805326443
58850354020997
65459362601845
270158033672354
197633774195261
297820232538301
239511187032650
27774082011165
408015099615067
267688358717242
32052048965805
104448476008515
43709853838459
76...

result:

wrong answer 2nd lines differ - expected: '221878585246974', found: '221513513026814'