C [yLOI2019] 棠梨煎雪
Background
岁岁花藻檐下共将棠梨煎雪
自总角至你我某日辗转天边
天淡天青 宿雨沾襟
一年一会信笺却只见寥寥数言
——银临《棠梨煎雪》
Description
给定 个长度为 的可能含有 ? 的 01 串,其中 ? 既能代表 0 也能代表 1, 次操作,每次给定一个区间,求有多少 01 串满足区间内的所有字符串都可以解释成该 01 串,或者单点修改某个字符串。
Limitations
Solution
子任务 :
输出 即可得分,期望得分 。
子任务 :
考虑 只有 ,因此可以 去枚举所有可能的串,然后对于每个询问 的逐个判定是否合法。时间复杂度 ,期望得分 。
子任务 :
考虑对于一段所有字符串的第 个字符,一共有四种可能:确定为 ;确定为 ;都可以;都不可以。如果 都不可以,则答案为 ,因为不会有任何一个字符串匹配该区间。如果确定为某个数,则这一位只有一种可能;否则这一位有两种可能。根据乘法原理,如果有 个位置有两种可能,则本次询问的答案为 。
因此对于每个询问, 地去遍历区间内所有字符即可。时间复杂度 ,期望得分 。
子任务 :
考虑 只有 ,可以状压到 int 中。具体的,维护两个 int,第一个 int 维护对应位是否确定是 或 ,第二个 int 维护如果确定是 0 或 1 了那么具体是 0 还是 1。
例如,对于单个字符串,它所有的为 ? 的位置,在第一个 int 中对应位置是 0,所有为 0 或 1 的位置,在第在个 int 中对应的位置是 1,在第二个 int 中对应的位置是自身的值。
考虑 是询问的左端点到某个字符串之前所维护的两个 int, 是该字符串的两个 int,现在合并这两个信息。
如果某一位置即不可以是 ,也不可以是 ,那么该字符串不为 ? 的位置在 中对应的值应该至少有一个和 中对应位置的值且 的该位置为 ,位运算可以表现为 ,则该询问的答案为 。
否则这两段信息可以合并:将他们已经确定字符的位置合并起来,然后将确定位置对应的值合并起来即可。于是 对 取或, 对 取或即可。
最终该询问 都可以的位置的个数即为 中 的个数。
时间复杂度 ,期望得分
子任务 :
由于 只有 ,问题变成了求某个区间内的字符是不是全是 0,全是 1,全是 ? 或 0 和 1 都有。可以考虑用线段树非常轻松的维护这样的信息。
时间复杂度 ,期望得分
子任务 :
世界上没有什么事情是开一棵线段树不能解决的,如果有,那就开 棵。
时间复杂度 ,期望得分
子任务 :
考虑结合子任务 和子任务 的做法,发现两个区间的状压信息也可以用子任务 的方法合并。因此用线段树维护这两个 int 的状压信息即可。
时间复杂度 ,期望得分
#include <ctime>
#include <cstdio>
namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qread(T &x) {
char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
namespace OPT {
char buf[120];
}
template <typename T>
inline void qwrite(T x, const char aft, const bool pt) {
if (x < 0) {
x = -x, putchar('-');
}
int top=0;
do {
OPT::buf[++top] = static_cast<char>(x % 10 + '0');
} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
template <typename T>
inline void qr(T &x) {
char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
const int maxn = 100010;
int n, m, q;
char s[maxn][35];
#ifdef ONLINE_JUDGE
int Ans;
#endif
struct Tree {
Tree *ls, *rs;
int l, r, x, y;
bool leg;
Tree() {
ls = rs = NULL;
l = r = x = y = 0;
leg = true;
}
void pushup() {
if (!(this->ls->leg && this->rs->leg)) {
this->leg = false;
} else {
if ((this->ls->x & this->rs->x) & (this->ls->y ^ this->rs->y)) {
this->leg = false;
} else {
this->leg = true;
this->x = this->ls->x | this->rs->x;
this->y = this->ls->y | this->rs->y;
}
}
}
};
Tree *rot;
void ReadStr(char *p);
void Update(const int x);
void Query(const int l, const int r);
void update(Tree *const u, const int p);
Tree query(Tree *u, const int l, const int r);
void build(Tree *const u, const int l, const int r);
int main() {
qr(n); qr(m); qr(q);
for (int i = 1; i <= m; ++i) {
ReadStr(s[i] + 1);
}
build(rot = new Tree, 1, m);
int opt, l, r;
while (q--) {
opt = 0; qr(opt);
if (opt == 0) {
l = r = 0; qr(l); qr(r);
Query(l, r);
} else {
l = 0; qr(l);
ReadStr(s[0] + 1);
Update(l);
}
}
#ifdef ONLINE_JUDGE
printf("%d\n", Ans);
#endif
return 0;
}
void ReadStr(char *p) {
do *p = IPT::GetChar(); while ((*p != '0') && (*p != '1') && (*p != '?'));
do *(++p) = IPT::GetChar(); while ((*p == '0') || (*p == '1') || (*p == '?'));
*p = 0;
}
void build(Tree *const u, const int l, const int r) {
if ((u->l = l) == (u->r = r)) {
for (int i = 1; i <= n; ++i) {
if (s[l][i] != '?') {
u->x |= 1 << i;
if (s[l][i] == '1') {
u->y |= 1 << i;
}
}
}
} else {
int mid = (l + r) >> 1;
build(u->ls = new Tree, l, mid);
build(u->rs = new Tree, mid + 1, r);
u->pushup();
}
}
Tree query(Tree *u, const int l, const int r) {
if ((u->l > r) || (u->r < l)) return Tree();
if ((u->l >= l) && (u->r <= r)) return *u;
Tree _ret;
auto ll = query(u->ls, l, r), rr = query(u->rs, l, r);
_ret.ls = ≪ _ret.rs = &rr;
_ret.pushup();
return _ret;
}
void Query(const int l, const int r) {
auto _ret = query(rot, l, r);
if (!_ret.leg) {
#ifndef ONLINE_JUDGE
puts("0");
#endif
} else {
int ans = 1;
for (int i = 1; i <= n; ++i) if (!(_ret.x & (1 << i))) {
ans <<= 1;
}
#ifdef ONLINE_JUDGE
Ans ^= ans;
#else
printf("%d\n", ans);
#endif
}
}
void update(Tree *u, const int p) {
if (u->ls) {
if (u->ls->r >= p) {
update(u->ls, p);
} else {
update(u->rs, p);
}
u->pushup();
} else {
*u = Tree();
u->l = u->r = p;
for (int i = 1; i <= n; ++i) {
if (s[0][i] != '?') {
u->x |= 1 << i;
if (s[0][i] == '1') {
u->y |= 1 << i;
}
}
}
}
}
void Update(const int x) {
update(rot, x);
}