QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#226400 | #5474. Incredibly Cute Penguin Chicks | OAleksa | TL | 647ms | 98868kb | C++14 | 2.6kb | 2023-10-25 22:20:31 | 2023-10-25 22:20:32 |
Judging History
answer
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
#define int long long
const int maxn = 1e6 + 69;
int a[maxn], b[maxn], c[maxn];
int ab[maxn], ac[maxn], bc[maxn], dp[maxn];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while(tt--) {
string s;
cin >> s;
int n = s.size();
s = "#" + s;
const int mod = 998244353;
auto add = [&](int x, int y) {
x += y;
if (x >= mod)
x -= mod;
return x;
};
dp[0] = 1;
for (int i = 1;i <= n;i++) {
a[i] = a[i - 1] + (s[i] == 'I');
b[i] = b[i - 1] + (s[i] == 'P');
c[i] = c[i - 1] + (s[i] == 'C');
ab[i] = a[i] - b[i];
ac[i] = a[i] - c[i];
bc[i] = b[i] - c[i];
}
map<int, vector<int>> r1, r2, r3;
map<int, int> c1, c2, c3;
for (int i = 0;i <= n;i++) {
r1[ab[i]].push_back(3 * c[i] - i);
r2[ac[i]].push_back(3 * b[i] - i);
r3[bc[i]].push_back(3 * a[i] - i);
}
int p = 0;
vector<vector<int>> f1, f2, f3;
for (auto &x : r1) {
sort(x.s.begin(), x.s.end());
x.s.erase(unique(x.s.begin(), x.s.end()), x.s.end());
f1.push_back(vector<int>(x.s.size() + 2, 0));
c1[x.f] = p++;
}
p = 0;
for (auto &x : r2) {
sort(x.s.begin(), x.s.end());
x.s.erase(unique(x.s.begin(), x.s.end()), x.s.end());
f2.push_back(vector<int>(x.s.size() + 2, 0));
c2[x.f] = p++;
}
p = 0;
for (auto &x : r3) {
sort(x.s.begin(), x.s.end());
x.s.erase(unique(x.s.begin(), x.s.end()), x.s.end());
f3.push_back(vector<int>(x.s.size() + 2, 0));
c3[x.f] = p++;
}
for (int i = 0;i <= n;i++) {
auto x1 = r1[ab[i]];
auto u1 = lower_bound(x1.begin(), x1.end(), 3 * c[i] - i) - x1.begin() + 1;
int k1 = c1[ab[i]];
for (int v = u1 - 1;v >= 1;v -= (v & -v))
dp[i] = add(dp[i], f1[k1][v]);
auto x2 = r2[ac[i]];
auto u2 = lower_bound(x2.begin(), x2.end(), 3 * b[i] - i) - x2.begin() + 1;
int k2 = c2[ac[i]];
for (int v = u2 - 1;v >= 1;v -= (v & -v))
dp[i] = add(dp[i], f2[k2][v]);
auto x3 = r3[bc[i]];
auto u3 = lower_bound(x3.begin(), x3.end(), 3 * a[i] - i) - x3.begin() + 1;
int k3 = c3[bc[i]];
for (int v = u3 - 1;v >= 1;v -= (v & -v))
dp[i] = add(dp[i], f3[k3][v]);
for (int v = u1;v < (int)f1[k1].size();v += (v & -v))
f1[k1][v] = add(f1[k1][v], dp[i]);
for (int v = u2;v < (int)f2[k2].size();v += (v & -v))
f2[k2][v] = add(f2[k2][v], dp[i]);
for (int v = u3;v < (int)f3[k3].size();v += (v & -v))
f3[k3][v] = add(f3[k3][v], dp[i]);
}
cout << dp[n];
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9700kb
input:
ICPC
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 0ms
memory: 9584kb
input:
CCCIIIPPP
output:
69
result:
ok single line: '69'
Test #3:
score: 0
Accepted
time: 2ms
memory: 9588kb
input:
PICCICCIPPI
output:
24
result:
ok single line: '24'
Test #4:
score: 0
Accepted
time: 600ms
memory: 96056kb
input:
PCPPCCICPICPCPCPCCPPIPPPIICCCICPICCPPPIPCCCCICCIICPCCICCCPCICIPPIPIPIPPCCPCIPPCCPPCCIPPPIPPCIICIIIPPCIPPICPPICCPIICCCCCCCICPCCPPIPIPICPCIICPCCIPICCCPPCPIIICPCPPPIIIIICCCPCICCPPCPPCICIIIPIPICPCPPPPICPCPIPIIICCICPCIPPICCICPIIPPCCCIICPCPCCICCPICPPPPPPICCCIPCCICICIPICIIICCICPCIPIICPIPIIICCPCCIIPCCCCPIPI...
output:
216523282
result:
ok single line: '216523282'
Test #5:
score: 0
Accepted
time: 647ms
memory: 96408kb
input:
ICIPCICCIPIPIIPPCPCICICPCPCPIIICICPPICCICPIPCPCIIPPIPCCIPIPIPIICCICICCCIPPIIPCPPCIIICCICCCCCCPIPICCIPIICIICPIPCCICPPCCIPIPIICCCPCCIPCPPCCIIIIIPCIPIIPICIIIPIICCCIPPPIPICIIIICIIIPICIICPIPCCICIPCCIPICCCPPIPIIPICPPCPICIPIPPPPPCIPCCIPPICIPCICPCCCCPPIIIPCPICCICIPIIPICPIIIIPPPCPIIIPCCPPIIPPICPIIICCCPICPPIC...
output:
977504498
result:
ok single line: '977504498'
Test #6:
score: 0
Accepted
time: 624ms
memory: 98868kb
input:
PPCCCICCIPPICCPCCCCIPPPIICPIPIPPPPIPPPPPPCPIPICCPCCPICPIPPPIIPIPIIPPCCPIICPPIIIPPCCCIIPCCIPCCCCCCIIIPIPCICCIICIIICCCCPCCICPPCCPCCIPICCPPIIPIIPIPCIIPIIPPPCCIPCPCCICCIPICPPPCIPPIPCCPCPCCIPIICCIPCCIPPCPCIPICIICPPICIIICIPIIIIIPCCCPPIPPPCIICIIIPPCIPCIPCIICCCCPIPCPICICPIPCPPIPIPICCPCCCCPCIPCPPCCIPCCCCCPII...
output:
785880133
result:
ok single line: '785880133'
Test #7:
score: 0
Accepted
time: 617ms
memory: 95248kb
input:
ICIPCPIPCCCCCCCPCCPCCIPCPCPIPPCPIPCICPPIPPPPICPIIPPIICPICPICPPICICICCCIIPCPPCCICCCCPIICPPPIPIPCICPIPPPPCCPICPCPICICPPIIIICIICPPIPIIICCCCPIPIIPICCCIPCCPPCPPPPIIIIICIPCPCCPIIICICIPPCCPIPPCIIPCCCIIICPCIPCIIIIPCIIPCIICCCIPPIPPCIIPCICIPIIPICCICCPCPIICCCIPIICCCPIPPCCIIIICPPIICPCPCIPPPCPCICPPIPIPCCPIPPPIPI...
output:
687587235
result:
ok single line: '687587235'
Test #8:
score: 0
Accepted
time: 625ms
memory: 97736kb
input:
IICCCIPIIIIIPICIICPCPCIICCIIICCCIIPICICCCPCPCPPPIIPIPPICIICICIPIICCICCIIIPCIICCPPIPICIPPCPCPCPIICCIIICIPPICCICPPIPPPPICPCPIPCCCPCPCPPPPIICPCIIIIIIIPIPIICICCPPIIPPCICPCPCIPCPICPPPICCICIICPPIPPPIPIPIPPCIPPCIPPCPPPIICICCCPICIPPIPCCPIIPPCIPPICCIIIPPPPPIIPPICPCPCIPPCCPCIIIPPICCCIIIIICIPPIIICPCIIICPPCPCPP...
output:
698376687
result:
ok single line: '698376687'
Test #9:
score: 0
Accepted
time: 646ms
memory: 97692kb
input:
IIPICCCCIPPCCPICCIIPPPCIPCCPPIPIICICPCCIIPPCCCICICPPICCICPPIICIIPICPICIPPPPCPCCPIICCPPIPPCIIIIPICICICCIIPPCICCCCCPICPIPCPPCCPPICPICCCICCIPICCICPIICCPIPIPICCPPCCPCPCCPICCPICCCCCCIPIICPIPICICCPIIPIPICIICIPPPIPCICIPIPPICPICICPPICICCIPCIPPPCIPIIPPPIICICPPPIICCCPIIICIPIPPICICIPPPCCPCCIIPCCCPPPCPICPICPPCP...
output:
850857669
result:
ok single line: '850857669'
Test #10:
score: -100
Time Limit Exceeded
input:
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII...