C [yLOI2019] 棠梨煎雪
C [yLOI2019] 棠梨煎雪
Background
岁岁花藻檐下共将棠梨煎雪
自总角至你我某日辗转天边
天淡天青 宿雨沾襟
一年一会信笺却只见寥寥数言
——银临《棠梨煎雪》
Description
给定 ? 的 01 串,其中 ? 既能代表 0 也能代表 1,
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);
}