QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#394680#7748. Karshilov's Matching Problem IIucup-team3215#WA 59ms37168kbC++202.8kb2024-04-20 17:40:002024-04-20 17:40:01

Judging History

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

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2024-04-20 17:40:01]
  • 评测
  • 测评结果:WA
  • 用时:59ms
  • 内存:37168kb
  • [2024-04-20 17:40:00]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll N = 2e5;
constexpr ll LOG = 20;

vector<int> z(const string &s) {
    vector<int> z(size(s));
    int l = -1, r = -1;
    for (int i = 1; i < size(s); ++i) {
        z[i] = i >= r ? 0 : min(r - i, z[i - l]);
        while (i + z[i] < size(s) && s[i + z[i]] == s[z[i]]) { ++z[i]; }
        if (i + z[i] > r) {
            l = i, r = i + z[i];
        }
    }
    return z;
}

array<ll, N> f{};

void upd(ll i, ll x) {
    for (; i < N; i |= i + 1) { f[i] += x; }
}

ll get(ll i) {
    ll res = 0;
    for (; i >= 0; i &= i + 1, --i) {
        res += f[i];
    }
    return res;
}

ll sum(ll l, ll r) {
    return get(r) - get(l - 1);
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    string s, t;
    cin >> s >> t;
    vector<ll> a(n);
    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
        if (i)a[i] += a[i - 1];
    }
    vector<vector<pair<int, int>>> Q(n);
    vector<int> res(q, 0);
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --r, --l;
        Q[r].push_back({l, i});
    }
    string kk = s + "#" + t;
    auto zf = z(kk);
    vector<vector<int>> ends(n);
    for (int i = 0; i < n; ++i) {
        int val = zf[n + 1 + i];
        if (val && i + val < n) {
            ends[i + val].push_back(val);
        }
    }
    vector<int> nxt(n + 1, -1);
    vector<vector<int>> up(LOG, vector<int>(N, 0));
    vector<ll> value(n + 1, 0);
    nxt[1] = 0;
    value[1] = a[0];
    for (int i = 2; i <= n; ++i) {
        nxt[i] = nxt[i - 1];
        while (~nxt[i] && s[nxt[i]] != s[i - 1]) {
            nxt[i] = nxt[nxt[i]];
        }
        if (~nxt[i]) {
            ++nxt[i];
        } else {
            nxt[i] = (s[i - 1] == s[0] ? 1 : 0);
        }
        value[i] = a[i - 1] + value[nxt[i]];
        up[0][i] = nxt[i];
        for (int j = 1; j < LOG; ++j) {
            up[j][i] = up[j - 1][up[j - 1][i]];
        }
    }
    ll cur = 0;
    for (ll r = 0; r < n; ++r) {
        for (auto len: ends[r]) {
            upd(r - len, a[len - 1]);
        }
        while (~cur && s[cur] != t[r]) {
            cur = nxt[cur];
        }
        ++cur;
        for (auto [l, id]: Q[r]) {
            res[id] += sum(l, r);
            ll now = cur;
            ll len = r - l + 1;
            if (now > len) {
                for (ll i = LOG - 1; ~i; --i) {
                    if (up[i][now] > len) {
                        now = up[i][now];
                    }
                }
                now = up[0][now];
            }
            res[id] += value[now];
        }
    }
    for (auto x: res) {
        cout << x << "\n";
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 19688kb

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: 3ms
memory: 19692kb

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: 59ms
memory: 37168kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

-238689766
574735614
259907610
510975269
-494263619
-119776439
-1202625941
712131205
-233956491
295786658
854069821
-1389700931
-1959196086
-1471492067
1796429659
227026746
-1291964243
-833662909
-28332933
1521598991
-210103850
888469404
604921440
813327493
-853316457
1964711175
-669853735
-13877485...

result:

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