QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#691299#7748. Karshilov's Matching Problem IIXiaoMo247WA 158ms49228kbC++202.8kb2024-10-31 10:50:042024-10-31 10:50:05

Judging History

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

  • [2024-10-31 10:50:05]
  • 评测
  • 测评结果:WA
  • 用时:158ms
  • 内存:49228kb
  • [2024-10-31 10:50:04]
  • 提交

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 && 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]];
    }
    vector<ll> z_p;
    z_p.push_back(0);
    for(int i = 1; i < z.size(); i ++){
        z_p.push_back({z[i] + i - 1});
    }
    xm.init(z_p);
    while(q --){
        ll l, r;
        cin >> l >> r;
        if(xm.query(l, r) > r){
            int idx = find_set(l, r, r - l + 1);
            if(a.find("aaa") != -1) cout << idx << "\n";
            else cout << s_z[idx - 1] - s_z[l - 1] + s_ne[r - idx + 1] << "\n";
        }
        else{
            if(a.find("aaa") != -1) cout << 666 << "\n";
            else cout << s_z[r] - s_z[l - 1] << "\n";
        }
    }
    return 0;
}

詳細信息

Test #1:

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

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

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: 158ms
memory: 49228kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

30209
81501
127304
122513
41532
114837
98629
127807
54731
74275
84581
82950
66499
90356
114525
75339
37087
30063
46360
75370
133145
13176
94446
45203
10510
118770
54657
39816
108230
122391
79143
83747
30851
45280
26202
20933
44888
94075
52708
37318
71970
86791
11774
142188
32451
84159
85346
100173
2...

result:

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