题解:P11456 [USACO24DEC] Interstellar Intervals G
首先这道题肯定可以设
我们注意到
时间复杂度
代码:
class BIT {
public:
int c[N];
int lb (int x) {
return x & -x;
}
void upd (int x, int y) {
++ x;
for (; x <= n; x += lb (x)) c[x] += y;
}
int qry (int x) {
int ret = 0; ++ x;
for (; x; x -= lb (x)) ret += c[x];
return ret;
}
int qry (int l, int r) { return qry (r) - qry (l - 1); }
} t[2];
void insert (int x) {
nxt[pre[n + 1]] = x;
pre[x] = pre[n + 1];
pre[n + 1] = x;
nxt[x] = n + 1;
}
void erase (int x) {
pre[nxt[x]] = pre[x];
nxt[pre[x]] = nxt[x];
}
int32_t main () {
n = rd ();
nxt[0] = n + 1, pre[n + 1] = 0;
scanf ("%s", s + 1);
int l1 = 0;
f[0] = 1;
t[0].upd (0, 1);
rep (i, 1, n) {
for (int x = nxt[0]; x <= n; x = nxt[x]) {
del[now[x]] = 1;
t[now[x] & 1].upd (now[x], - f[now[x] - 1]);
}
for (int x = nxt[0]; x <= n; x = nxt[x]) {
if (del[-- now[x]] || now[x] < 1) erase (x);
}
if (s[i] == 'R') l1 = i;
else if (s[i] == 'B' && ! del[i]) {
insert (i), now[i] = i;
}
f[i] = t[i & 1 ^ 1].qry (max (1ll, l1 * 2 - i + 1), i) % P;
if (s[i] == 'X') f[i] = (f[i] + f[i - 1]) % P;
t[i & 1].upd (i, f[i - 1]);
}
cout << (f[n] + P) % P;
}