QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#691183#7748. Karshilov's Matching Problem IIXiaoMo247WA 75ms47964kbC++202.6kb2024-10-31 10:17:582024-10-31 10:17:58

Judging History

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

  • [2024-10-31 10:17:58]
  • 评测
  • 测评结果:WA
  • 用时:75ms
  • 内存:47964kb
  • [2024-10-31 10:17:58]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
const ll MAXN = 4e5 + 5;
ll n, q, w[MAXN], s_w[MAXN], s_ne[MAXN], s_z[MAXN];
string a, b;
vector<ll> z_function(string s) {
    vector<ll> z(s.size());
    z[1] = n;
    for (int i = 2, l = 0, r = 0; i < s.size(); ++i) {
        if (i <= r && z[i - l + 1] < r - i + 1) {
            z[i] = z[i - l + 1];
        }
        else {
            z[i] = max(0ll, r - i + 1);
            while (i + z[i] <= s.size() && s[z[i] + 1] == s[i + z[i]]) ++ z[i];
        }
        if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
    }
    vector<ll> ret;
    ret.push_back(0);
    for(int i = a.size() + 2; i < s.size(); i ++){
        ret.push_back(z[i]);
    }
    return ret;
}
vector<ll> get_next(string s) {
    vector<ll> ne(s.size());
    vector<ll> ret;
    ret.push_back(0);
    for (int i = 1, j = 0; i < s.size(); ++i) {
        while (j > 0 && s[i] != s[j]) j = ne[j - 1];
        if (s[i] == s[j]) ++ j;
        ne[i] = j;
        s_ne[i + 1] += s_ne[j];
    }
    for(auto it : ne){
        ret.push_back(it);
    }
    return ret;
}
struct ST{
	vector<vector<ll>> f;
	void init(vector<ll> &a){
        int n = a.size();
        vector<ll> tmp(22, 0);
        f.assign(n, tmp);
		for(int i = 1; i < n; i ++){
			f[i][0] = a[i];
		}
		for(int j = 1; j <= 20; j ++){
			for(int i = 1; i + (1 << j) - 1 < n; i ++){
				f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
			}
		}
	}
	ll query(ll l, ll r){
		ll k = log2(r - l + 1);
		return max(f[l][k], f[r - (1 << k) + 1][k]);
	}
}xm;
int find_set(ll l, ll r, ll k){
    if(l == r) return l;
    ll mid = l + r >> 1;
    if(xm.query(l, mid) >= k) return find_set(l, mid, k);
    else return find_set(mid + 1, r, k);
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> q >> a >> b;
    for(int i = 1; i <= n; i ++){
        cin >> w[i];
        s_w[i] = s_w[i - 1] + w[i];
        s_ne[i] = w[i];
    }
    vector<ll> z = z_function(" " + a + "$" + b);
    vector<ll> ne = get_next(a);
    for(int i = 1; i <= n; i ++){
        s_ne[i] = s_ne[i - 1] + s_ne[i];
        s_z[i] = s_z[i - 1] + s_w[z[i]];
    }
    xm.init(z);
    while(q --){
        ll l, r;
        cin >> l >> r;
        if(xm.query(l, r) > r - l + 1){
            int idx = find_set(l, r, r - l + 1);
            cout << s_z[idx - 1] - s_z[l - 1] + s_ne[r - idx + 1] << "\n";
        }
        else{
            cout << s_z[r] - s_z[l - 1] << "\n";
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 7816kb

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: 75ms
memory: 47964kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

108164458136002
222080777767758
455761723892344
440411602092496
148387925523573
89086183321310
351565440663364
58865293363590
66246350986707
270406316725369
197809171424968
298452947808256
239539695378843
28416389369370
408774056328232
268062410171839
32839037350667
104862393185160
44097530585166
80...

result:

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