QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#267655 | #7748. Karshilov's Matching Problem II | redwhite | WA | 4ms | 18364kb | C++17 | 3.3kb | 2023-11-27 15:54:05 | 2023-11-27 15:54:06 |
Judging History
answer
/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
ll n, m, pref[N], z[N], mx=0, ansp[N], anss[N], ans[N];
ll par[N], tree[N], lps[N];
int seg[2*N];
string s, t, str;
vector<int> adj[N];
void dfs(int v) {
for(auto it : adj[v]) {
tree[it] += (tree[v] + pref[v+1]-pref[v]);
dfs(it);
}
}
void update(int ind, int val) {
if(ind == 0) return;
if(ind >= (1<<lg)) {
seg[ind] = val;
} else {
seg[ind] = max(seg[2*ind], seg[2*ind+1]);
}
update(ind/2, val);
}
vector<int> vec;
void query2(int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(r<=lc || rc<=l) return;
if(lc>=l && rc<=r) {
vec.pb(ind); return;
}
int mid = (lc+rc)/2;
query2(l, r, 2*ind, lc, mid);
query2(l, r, 2*ind+1, mid, rc);
}
int query3(int r, int ind) {
if(ind>=(1<<lg)) return (ind-(1<<lg)+1);
if(seg[2*ind] >= r) return query3(r, 2*ind);
return query3(r, 2*ind+1);
}
int query(int l, int r) {
vec.clear();
query2(l, r);
for(auto it : vec) {
if(seg[it] >= r) return query3(r, it);
}
return -1;
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n>>m>>s>>t;
for(int i=1; i<=n; i++) {cin>>pref[i]; pref[i] += pref[i-1];}
str = s+'#'+t;
adj[N-1].pb(0);
for(int i=1; i<=2*n; i++) {
if(mx+z[mx] > i) {
z[i] = min(mx+z[mx]-i, z[i-mx]);
}
while(z[i]<=2*n && str[z[i]+i]==str[z[i]]) {
z[i]++;
}
if(z[i]+i > mx) mx = i;
int k = lps[i-1];
while(k>0&&str[k]!=str[i]) {
k = lps[k-1];
}
lps[i] = k+(str[i]==str[k]);
par[i] = lps[i]-1;
if(lps[i]!=0)adj[lps[i]-1].pb(i);
else adj[N-1].pb(i);
}
dfs(N-1);
ansp[0] = tree[0] + pref[1];
for(int i=1; i<=2*n; i++) {
ansp[i] = ansp[i-1] + tree[i] + (pref[i+1]-pref[i]);
if(i>n)update(i-n+(1<<lg)-1, i-n-1+z[i]);
}
for(int i=2*n; i>=n+1; i--) {
anss[i] = anss[i+1] + pref[z[i]];
}
for(int l,r,i=1; i<=m; i++) {
cin>>l>>r;
int x = query(l, r+1);
if(x == -1) {
cout<<anss[n+l]-anss[n+r+1]<<endl;
} else {
cout<<ansp[r-x] + anss[n+l] - anss[n+x]<<endl;
}
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 18364kb
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 13 36 38
result:
wrong answer 3rd lines differ - expected: '3', found: '13'