#include #include #include #include using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(n); i++) int main(){ string str_x, str_y; cin >> str_x >> str_y; int num_queries; cin >> num_queries; const i64 max_len = 2'000'000'000; auto presum_x = vector>(26, vector(str_x.size() + 1, 0)); for(int i=0; i>(26, vector(str_y.size() + 1, 0)); for(int i=0; i sizes_of_ranked; sizes_of_ranked.push_back(str_x.size()); while(sizes_of_ranked.back() < max_len){ i64 prev = sizes_of_ranked.back(); sizes_of_ranked.push_back(prev * 2 + (i64)str_y.size()); } auto fullsum_ranked = vector>(26, vector(sizes_of_ranked.size(), 0)); for(int c=0; c<26; c++) fullsum_ranked[c][0] = presum_x[c].back(); for(int i=1; ii64 { if(rank == -1) rank = sizes_of_ranked.size() - 1; if(rank == 0) return presum_x[target_char][len]; if(len < sizes_of_ranked[rank-1]){ return presum(len, target_char, presum, sufsum, rank-1); } if(len < sizes_of_ranked[rank-1] + (i64)str_y.size()){ return fullsum_ranked[target_char][rank-1] + presum_y[target_char][len - sizes_of_ranked[rank-1]]; } return fullsum_ranked[target_char][rank-1] + presum_y[target_char].back() + sufsum(len - sizes_of_ranked[rank-1] - (i64)str_y.size(), target_char, presum, sufsum, rank-1); }; auto sufsum_ranked = [&](i64 len, int target_char, auto presum, auto sufsum, int rank = -1)->i64 { if(rank == -1) rank = sizes_of_ranked.size() - 1; if(rank == 0) return presum_x[target_char].back() - presum_x[target_char][str_x.size() - len]; if(len < sizes_of_ranked[rank-1]){ return presum(len, target_char, presum, sufsum, rank-1); } if(len < sizes_of_ranked[rank-1] + (i64)str_y.size()){ return fullsum_ranked[target_char][rank-1] + presum_y[target_char].back() - presum_y[target_char][str_y.size() - (len - sizes_of_ranked[rank-1])]; } return fullsum_ranked[target_char][rank-1] + presum_y[target_char].back() + sufsum(len - sizes_of_ranked[rank-1] - (i64)str_y.size(), target_char, presum, sufsum, rank-1); }; for(int query_idx=0; query_idx < num_queries; query_idx++){ i64 l, r; char c; cin >> l >> r >> c; i64 ans = presum_ranked(r, c - 'a', presum_ranked, sufsum_ranked) - presum_ranked(l-1, c - 'a', presum_ranked, sufsum_ranked); cout << ans << "\n"; } return 0; } struct ios_do_not_sync { ios_do_not_sync() { ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;