QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#282540 | #7748. Karshilov's Matching Problem II | A_programmer | WA | 114ms | 32120kb | C++14 | 2.2kb | 2023-12-12 12:34:29 | 2023-12-12 12:34:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1.5e5 + 5;
char s[maxn], t[maxn];
int w[maxn];
ll pre[maxn], sum[maxn], ans[maxn];
struct BIT
{
ll c[maxn];
void add(int x, ll d)
{
for (; x < maxn; x += (x & -x)) c[x] += d;
}
ll sum(int x)
{
if (!x) return 0;
ll ans = 0; for (; x; x -= (x & -x)) ans += c[x];
return ans;
}
}bit;
vector<pii> qd[maxn];
vector<int> qv[maxn];
int z[maxn], p[maxn], nxt[maxn];
set<int> st;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m >> s >> t;
for (int i = 1; i <= n; i++) cin >> w[i], pre[i] = pre[i - 1] + w[i];
for (int i = 1; i <= m; i++)
{
int l, r;
cin >> l >> r;
qd[r].emplace_back(make_pair(l, i));
}
int r = 0, pos = 1;
z[0] = n;
while (r + 1 < n && s[r] == s[r + 1]) r++;
z[1] = r;
for (int i = 2; i < n; i++)
{
r = pos + z[pos] - 1;
int l = z[i - pos];
if (i + l <= r) z[i] = l;
else
{
int j = max(0, r - i + 1);
while (j + i < n && s[j] == s[j + i]) j++;
z[i] = j;
pos = i;
}
}
r = 0, pos = 0;
while (r < n && s[r] == t[r]) r++;
p[0] = r;
for (int i = 1; i < n; i++)
{
r = pos + p[pos] - 1;
int l = z[i - pos];
if (i + l <= r) p[i] = l;
else
{
int j = max(0, r - i + 1);
while (j + i < n && t[j + i] == s[j]) j++;
p[i] = j;
pos = i;
}
}
for (int i = n - 1; ~i; i--) p[i + 1] = p[i], s[i + 1] = s[i];
for (int i = 1; i <= n; i++) if (p[i]) qv[i + p[i]].push_back(i);
pos = 0;
nxt[0] = nxt[1] = 0;
for (int i = 2; i <= n; i++)
{
while (pos && s[pos + 1] != s[i]) pos = nxt[pos];
if (s[pos + 1] == s[i]) pos++;
nxt[i] = pos;
}
for (int i = 1; i <= n; i++) sum[i] = sum[nxt[i]] + pre[i];
for (int i = n; i; i--)
{
for (int x : qv[i + 1]) st.insert(x);
for (pii tmp : qd[i])
{
int x = *st.lower_bound(tmp.first);
if (x <= i) ans[tmp.second] = sum[i - x + 1];
}
}
for (int i = 1; i <= n; i++)
{
for (int x : qv[i]) bit.add(1, pre[i - x]), bit.add(x + 1, -pre[i - x]);
for (pii tmp : qd[i]) ans[tmp.second] += bit.sum(tmp.first);
}
for (int i = 1; i <= m; i++) cout << ans[i] << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 13720kb
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: 13884kb
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: 70ms
memory: 29704kb
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: 66ms
memory: 29512kb
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: 76ms
memory: 30488kb
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: 77ms
memory: 28872kb
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: 93ms
memory: 32120kb
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: 0
Accepted
time: 114ms
memory: 27360kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
5697498028074951 21822720964918437 11237472002329727 84315407720465773 32198634509153899 40728716039967676 5913555967331675 10487781019914529 270012821917938205 239347797488036653 216168445985667902 67452321725546144 457298584810665039 187789940805492303 46241700003064393 242312618101113 42260337439...
result:
ok 150000 lines
Test #9:
score: 0
Accepted
time: 74ms
memory: 26004kb
input:
150000 150000 abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...
output:
20591954951726 12208904434175 7662625006262 27638144315127 14991794671504 12693360208820 11037771157715 4373484191175 19325903476164 26048068499431 5723213500221 37836285761904 44407448222078 17672607641771 18226179013226 25664535060427 6571081196245 7364255499121 31338586400498 6963750199271 362906...
result:
ok 150000 lines
Test #10:
score: 0
Accepted
time: 69ms
memory: 25876kb
input:
150000 150000 abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...
output:
26679397939583 14744203186201 5444732298002 2682795720153 8097594477750 11849141088914 4460923379501 213037786838 16105345264171 9432794502217 7341906921155 175129395650 26090540252644 7939835388186 1974417753321 13404114384225 3350016113286 11461223687633 11253459438574 10536087601821 410458842950 ...
result:
ok 150000 lines
Test #11:
score: -100
Wrong Answer
time: 27ms
memory: 16340kb
input:
1000 150000 abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababba...
output:
14533294687 36404448099 4898807521 8311487449 101191999742 73649429237 64160767440 94533233142 11330483415 11891445585 32475987658 20881014384 19725249244 46562700910 8954411927 15493987162 95870286230 21115698529 2452671987 14439012748 11977731306 12229300727 74351907054 22843320780 40597085949 512...
result:
wrong answer 134660th lines differ - expected: '0', found: '62518451595'