QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#258561#7748. Karshilov's Matching Problem IIucup-team2307#WA 103ms36200kbC++203.1kb2023-11-19 20:10:302023-11-19 20:10:30

Judging History

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

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2023-11-19 20:10:30]
  • 评测
  • 测评结果:WA
  • 用时:103ms
  • 内存:36200kb
  • [2023-11-19 20:10:30]
  • 提交

answer

#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
struct SegTree {
    int n;
    vector<ll> tree;
    SegTree(int n) : n(n), tree(2 * n) {}
    void upd(int p, ll v) {
        // cout << p + n << endl;
        for(tree[p += n] = v; p /= 2;)
            tree[p] = tree[2 * p] + tree[2 * p + 1];
    }
    ll query(ll l, ll r) {
        ll sum = 0;
        for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if(l & 1) sum += tree[l++];
            if(r & 1) sum += tree[--r];
        }
        return sum;
    }
};

vector<int> kmp(string s) {
    vector<int> pi(1 + s.size());
    int i = 0, j = pi[0] = -1;
    while(i < s.size()) {
        while(j != -1 && s[j] != s[i])
            j = pi[j];
        pi[++i] = ++j;
    }
    return pi;
}
vector<int> zf(string s) {
    int n = s.size();
    vector<int> z(n);
    int l = 0, r = 0;
    for(int i = 1; i < n; i++) {
        if(i < r) {
            z[i] = min(r - i, z[i - l]);
        }
        while(i + z[i] < n && s[z[i]] == s[i + z[i]]) {
            z[i]++;
        }
        if(i + z[i] > r) {
            l = i;
            r = i + z[i];
        }
    }
    return z;
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    // for(auto i : zf("abcabcdabcab")) cout << i << " "; cout << endl;
    int n, q;
    string s, t;
    cin >> n >> q >> s >> t;
    vector<ll> w(n);
    for(auto &i : w) cin >> i;
    for(int i = 1; i < n; i++) w[i] += w[i - 1];
    w.insert(w.begin(), 0);
    
    auto par = kmp(s + "#" + t);
    auto pref = zf(s + "#" + t);

    vector<ll> ans(q);
    vector<vector<array<int, 2>>> queries(n);
    vector<vector<array<int, 2>>> queries2(n + 1);
    for(int l, r, i = 0; i < q; i++) {
        cin >> l >> r;
        l--, r--;
        queries[r].push_back({l, i});
        queries2[par[n + 2 + r]].push_back({r - l + 1, i});
    }

    vector<vector<array<int, 2>>> add(n + 1);
    for(int i = 0; i < n; i++) {
        // cout << i << " " << pref[n + 1 + i] << endl;
        if(pref[n + 1 + i]) {
            add[i + pref[n + 1 + i]].push_back({i, w[pref[n + 1 + i]]});
        }
    }

    SegTree st(n);
    for(int i = 0; i < n; i++) {
        for(auto [l, x] : add[i]) {
            // cout << l << " " << i - 1 << "woo" << endl;
            st.upd(l, x);
        }
        for(auto [l, id] : queries[i]) {
            ans[id] = st.query(l, n);
            int t = par[n + 2 + i];
            // while(t) {
            //     // cout << i << " -> " << t << endl;
            //     if(i - t + 1 >= l)
            //         ans[id] += w[t];
            //     t = par[t];
            // }
        }
    }

    vector<vector<int>> g(n + 1);
    for(int i = 1; i <= n; i++) 
        g[par[i]].push_back(i);

    st = SegTree(n + 1);
    auto dfs = [&](auto self, int v) -> void {
        st.upd(v, w[v]);
        for(auto [len, id] : queries2[v]) {
            ans[id] += st.query(0, len + 1);
        }
        for(auto i : g[v]) {
            self(self, i);
        }
        st.upd(v, 0);
    };
    dfs(dfs, 0);

    for(auto i : ans) cout << i << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3560kb

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: 103ms
memory: 36200kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

25846874497562
54361975801086
109917062946842
106760513051941
36283389452989
22256400751433
83952523108971
14341607932549
15367159028597
65240849012898
48783092617789
71544175515837
56992256821834
6788871802909
98111734372187
64171333396282
8077541519533
25180559593539
10621425790075
1766753157647
1...

result:

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