P3514 可以扩展到区间查询的另解
省流:读错题了,以为是区间查询,于是搞出来一个支持区间查询的做法。
考虑全是
如果有等于
扩展到区间查询是容易的,不多赘述。
int main() {
qio(), cin >> n >> m >> s;
s = " " + s;
L(i, 1, n) {
pre[i] = pre[i - 1] + (s[i] == 'T') + 1;
buc[pre[i]] = i;
}
nxt[n + 1] = 1e9;
R(i, n, 0) nxt[i] = s[i] == 'W' ? i : nxt[i + 1];
for (int x; m--; ) {
cin >> x;
if (x > pre[n]) {
cout << "NIE\n";
continue;
} else if (buc[x] != 0) {
cout << "1 " << buc[x] << '\n';
continue;
}
int l = 0, pos = buc[x - 1], seg = min(nxt[1] - 1, nxt[pos + 1] - pos - 1);
l += seg, pos += seg;
if (pos >= n) cout << "NIE\n";
else if (s[pos + 1] == 'W') cout << l + 1 << ' ' << pos + 1 << '\n';
else if (s[l + 1] == 'W') cout << l + 2 << ' ' << pos + 1 << '\n';
else cout << "NIE\n";
}
return 0;
}