結果
| 問題 |
No.1725 [Cherry 3rd Tune D] 無言の言葉
|
| コンテスト | |
| ユーザー |
square1001
|
| 提出日時 | 2021-10-29 21:46:17 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 174 ms / 4,000 ms |
| コード長 | 2,043 bytes |
| コンパイル時間 | 912 ms |
| コンパイル使用メモリ | 86,720 KB |
| 実行使用メモリ | 24,292 KB |
| 最終ジャッジ日時 | 2024-10-07 10:46:47 |
| 合計ジャッジ時間 | 6,469 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 42 |
ソースコード
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
string X, Y; int Q;
cin >> X >> Y >> Q;
int LX = X.size(), LY = Y.size();
vector<long long> LF = { LX };
while (LF.back() <= 1000000000) {
LF.push_back(LF.back() * 2 + LY);
}
vector<vector<int> > XS(26, vector<int>(LX + 1));
vector<vector<int> > YS(26, vector<int>(LY + 1));
for (int i = 0; i < LX; ++i) {
XS[X[i] - 'a'][i + 1]++;
}
for (int i = 0; i < LY; ++i) {
YS[Y[i] - 'a'][i + 1]++;
}
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < LX; ++j) {
XS[i][j + 1] += XS[i][j];
}
for (int j = 0; j < LY; ++j) {
YS[i][j + 1] += YS[i][j];
}
}
vector<vector<long long> > CF(LF.size(), vector<long long>(26));
for (int i = 0; i < 26; ++i) {
CF[0][i] = XS[i][LX];
}
for (int i = 1; i < int(LF.size()); ++i) {
for (int j = 0; j < 26; ++j) {
CF[i][j] = CF[i - 1][j] * 2 + YS[j][LY];
}
}
for (int i = 0; i < Q; ++i) {
int L, R; string C;
cin >> L >> R >> C; --L;
int id = C[0] - 'a';
function<long long(int, long long, long long, int)> calc = [&](int layer, long long l, long long r, int rev) {
if (L <= l && r <= R) {
return CF[layer][id];
}
if (R <= l || r <= L) {
return 0LL;
}
if (layer == 0) {
long long pl = max(l, (long long)(L)), pr = min(r, (long long)(R));
return (l < r ? (rev == 0 ? XS[id][pr - l] - XS[id][pl - l] : XS[id][LX - (pl - l)] - XS[id][LX - (pr - l)]) : 0LL);
}
long long mp1 = l + LF[layer - 1];
long long mp2 = r - LF[layer - 1];
long long res1 = calc(layer - 1, l, mp1, 0);
long long res2 = calc(layer - 1, mp2, r, 1);
long long res3 = 0;
long long cl = max(mp1, (long long)(L)), cr = min(mp2, (long long)(R));
if (cl < cr) {
res3 = (rev == 0 ? YS[id][cr - mp1] - YS[id][cl - mp1] : YS[id][LY - (cl - mp1)] - YS[id][LY - (cr - mp1)]);
}
return res1 + res2 + res3;
};
long long ans = calc(int(LF.size()) - 1, 0, LF.back(), 0);
cout << ans << endl;
}
return 0;
}
square1001