QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303892 | #7748. Karshilov's Matching Problem II | Shayan86 | WA | 98ms | 23248kb | C++23 | 2.8kb | 2024-01-13 04:19:34 | 2024-01-13 04:19:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll L = 19;
const ll Sq = 1500;
const ll N = 15e4 + 25;
const ll Mod = 1e9 + 7;
int n, m, z[N*2], lcp[N*2], ql[N], qr[N], par[N][L], ans[N];
ll w[N], pre[N], sum[N];
string s, t;
vector<int> qu;
bool cmp(int i, int j){
if(ql[i] / Sq != ql[j] / Sq) return ql[i] < ql[j];
if((ql[i] / Sq) % 2) return qr[i] < qr[j];
else return qr[i] > qr[j];
}
int get(int v, int k){
if(v <= k) return v;
for(int i = L-1; i >= 0; i--){
if(par[v][i] > k) v = par[v][i];
}
return par[v][0];
}
int main(){
fast_io;
cin >> n >> m >> s >> t;
s = s + "$" + t;
int l = 0, r = 0;
for(int i = 1; i <= 2*n; i++){
if(i < r && z[i-l] + i < r) z[i] = z[i-l];
else{
l = i; r = max(i, r);
while(r < 2*n+1 && s[r] == s[r-l]) r++;
z[i] = r - l;
}
}
int x = 1, y = 0;
while(x <= 2*n){
if(s[x] == s[y]) lcp[x++] = ++y;
else if(y) y = lcp[y-1];
else lcp[x++] = y;
}
// [n+1 .. 2n]
for(int i = 1; i <= n; i++) cin >> w[i];
for(int i = 1; i <= n; i++) pre[i] = pre[i-1] + w[i];
for(int i = 1; i <= n; i++){
sum[i] = w[i] + sum[lcp[i-1]]; par[i][0] = lcp[i-1];
for(int j = 1; j < L; j++){
if(!par[i][j-1]) break;
par[i][j] = par[par[i][j-1]][j-1];
}
}
for(int i = 1; i <= m; i++){
cin >> ql[i] >> qr[i];
qu.pb(i);
}
sort(all(qu), cmp);
l = r = 1;
ll res = (t[0] == s[0] ? w[1] : 0);
for(int i: qu){
while(r < qr[i]){
r++; res += sum[get(lcp[n+r], r-l+1)];
}
while(ql[i] < l){
l--; res += pre[min(z[n+l], r-l+1)];
}
while(qr[i] < r){
res -= sum[get(lcp[n+r], r-l+1)]; r--;
}
while(l < ql[i]){
res -= pre[min(z[n+l], r-l+1)]; l++;
}
ans[i] = res;
}
for(int i = 1; i <= m; i++) cout << ans[i] << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3592kb
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: 3480kb
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: 98ms
memory: 23248kb
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'