abc380D Strange Mirroring
给定字符串S,每次操作会复制一份并改变大小写追加到原字符串后面。有Q组询问,每次输出K[i],输出结果中第K[i]个字符是什么?
1<=|S|<=2E5, 1<=Q<=2E5, 1<=K[i]<=1E18
分析:依次处理每个询问,先倍增到能覆盖询问的长度,然后倒推出该字符由S的哪个字符得到,以及大小写状态。
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
std::string S;
int Q;
std::cin >> S >> Q;
i64 ns = S.size();
auto get = [&](i64 x) {
i64 L = ns;
while (L < x) {
L *= 2;
}
int flip = 0;
while (L > ns) {
i64 L1 = L / 2;
if (x > L1) {
flip ^= 1;
x -= L1;
}
L = L1;
}
char ans = S[x - 1];
if (flip) {
ans = islower(ans) ? toupper(ans) : tolower(ans);
}
return ans;
};
for (int i = 0; i < Q; i++) {
i64 x;
std::cin >> x;
std::cout << get(x) << " ";
}
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}