QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#258559#7748. Karshilov's Matching Problem IIucup-team2307#WA 106ms35156kbC++203.1kb2023-11-19 20:08:362023-11-19 20:08:36

Judging History

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

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

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<int> 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';
}

詳細信息

Test #1:

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

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

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: 106ms
memory: 35156kb

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'