QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#257384 | #7748. Karshilov's Matching Problem II | ucup-team228 | TL | 1841ms | 23036kb | C++20 | 4.4kb | 2023-11-19 03:27:40 | 2023-11-19 03:27:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
enum TableType { VAL, ARG };
template <typename T, TableType S = TableType::VAL, typename Cmp = less<T>>
struct sparse_table{
// VAL returns value, ARG returns position of value
// less<> for minimum, greater<> for maximum
// d[i][j] = min(a[i], a[i + 1], ... , a[i + (1 << j) - 1])
// get(l, r) = min(a[l], a[l + 1], ... , a[r])
static const int N = 150'000 + 10, L = 20;
T a[N];
int d[N][L], rem[N];
void build(int n, Cmp cmp = {}) {
for (int i = 1; i <= n; i++) { d[i][0] = i; }
for (int j = 1; j <= L - 1; j++) {
for (int i = 1; i <= n; i++) {
if (i + (1 << j) - 1 > n) { continue; }
d[i][j] = cmp(a[d[i][j - 1]], a[d[i + (1 << (j - 1))][j - 1]]) ? d[i][j - 1] : d[i + (1 << (j - 1))][j - 1];
}
}
for (int i = 1; i <= n; i++) { rem[i] = log2(i); }
}
T get(int l, int r, Cmp cmp = {}) {
int lg = rem[r - l + 1];
int pos = cmp(a[d[l][lg]], a[d[r - (1 << lg) + 1][lg]]) ? d[l][lg] : d[r - (1 << lg) + 1][lg];
if constexpr (S == TableType::VAL) { return a[pos]; } else { return pos; }
}
};
sparse_table<int, TableType::VAL, greater<>> table;
const int N = 150'000 + 10;
int w[N]; // 1-based
int lcp[N]; // 1-based
ll dp1[N]; // dp1[i] = answer on s[1..i]
ll dp2[N]; // dp2[i] = answer on s[1..i] only of prefixes s[1..j]
ll pref_sum[N]; // pref_sum[i] = \sum_{j = 1}^{i} dp2[lcp[j]]
vector<int> calc_z_func(const string& s) {
int n = s.size();
vector<int> z(n, 0);
for (int i = 1, l = 0, r = 0; i <= n - 1; 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;
}
vector<int> calc_pref_func(const string& s) {
int n = s.size();
vector<int> pref(n, 0);
for (int i = 1, border = 0; i <= n - 1; i++) {
while (s[border] != s[i] && border) {
border = pref[border - 1];
}
if (s[border] == s[i]) {
border++;
}
pref[i] = border;
}
return pref;
}
string pretty_print(const string& s) {
string res;
for (char ch : s) {
res += ch;
res += ' ';
}
if (!res.empty()) {
res.pop_back();
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
string s, t; // 0-based
cin >> s >> t;
for (int i = 1; i <= n; i++) {
cin >> w[i];
}
vector<int> z_func = calc_z_func(s + "$" + t);
for (int i = 1; i <= n; i++) {
lcp[i] = z_func[n + i];
}
for (int i = 1; i <= n; i++) {
table.a[i] = i + lcp[i] - 1;
}
table.build(n);
vector<int> pref_func = calc_pref_func(s);
for (int i = 1; i <= n; i++) {
dp1[i] = dp1[i - 1] + w[i];
for (int j = pref_func[i - 1]; j >= 1; j = pref_func[j - 1]) {
dp1[i] += w[j];
}
dp2[i] = dp2[i - 1] + w[i];
}
for (int i = 1; i <= n; i++) {
pref_sum[i] = pref_sum[i - 1] + dp2[lcp[i]];
}
// cout << pretty_print(s) << "\n";
// cout << pretty_print(t) << "\n";
// for (int i = 1; i <= n; i++) {
// cout << lcp[i] << " ";
// }
// cout << "\n";
// for (int i = 1; i <= n; i++) {
// cout << pref_func[i - 1] << " ";
// }
// cout << "\n";
// for (int i = 1; i <= n; i++) {
// cout << dp1[i] << " ";
// }
// cout << "\n";
while (m--) {
int l, r;
cin >> l >> r;
int x = r + 1;
if (table.get(l, r) >= r) {
int lef = l, rig = r;
while (lef < rig) {
int mid = (lef + rig) / 2;
if (table.get(l, mid) >= r) {
rig = mid;
} else {
lef = mid + 1;
}
}
x = lef;
}
cout << (x <= r ? dp1[r - x + 1] : 0) + pref_sum[x - 1] - pref_sum[l - 1] << "\n";
}
#ifdef LOCAL
cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 7748kb
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: 7908kb
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: 0
Accepted
time: 95ms
memory: 22924kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823514 221878585246974 455339807727642 440286198422821 148115747906237 88695249853257 351159618462315 58850354020997 65824434822005 270158033672354 197732558443069 298193894693053 239511187032650 28139154231325 408380171835227 268053430937402 32417121185965 104813548228675 44074926058619 78...
result:
ok 150000 lines
Test #4:
score: 0
Accepted
time: 219ms
memory: 22976kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
528900815691571 112638556022185 514229554849356 2216206133639915 295388515578381 1658587138269057 652142121207636 166322478628783 490195029871161 1191292846892788 1468501126902703 487990867773908 55994169916421 568966315599642 2522992078581539 2339888502167342 2881203249819745 154700081279584 152537...
result:
ok 150000 lines
Test #5:
score: 0
Accepted
time: 237ms
memory: 23036kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
386150762496368 2769669390423140 1025785181073675 1707930121656247 332135612349048 113937878332307 1128519694119799 476402941643931 980441978140407 1004994648999517 676169371268202 2607965889355671 273766796671958 711480908011402 71754482763611 400453994282744 975387094872830 810536618300388 2229061...
result:
ok 150000 lines
Test #6:
score: 0
Accepted
time: 1841ms
memory: 22892kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
25254725752128652 23497896664383313 15195391464047263 41143572895791434 7513384536045842 8871310939247699 17423823866879881 14601201534396958 6203483865940624 24953281161800570 24776576029495768 1687640411226 31563282955464371 29947970968962218 1149303801639767 5806503923049299 11201332188941891 116...
result:
ok 150000 lines
Test #7:
score: 0
Accepted
time: 1461ms
memory: 22920kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
4570597927951323 11761063519994765 289982253793476 15684854914181269 19579321543184889 459972639580770 15246459216963247 1833393949769247 22425556248999709 11209560100586843 2883954996867615 14371655418173335 29207399108721 5943079608253242 1664456073054861 27405606916506455 23082758946788297 381175...
result:
ok 150000 lines
Test #8:
score: -100
Time Limit Exceeded
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...