【CodeForces 482B】Interesting Array

链接:

洛谷

题目大意:

构建一个序列 \(a\),满足 \(m\) 条限制使得 \(\bigwedge\limits_{i=l}^ra_i=p\).

正文:

线段树区间或上 \(p\),最后再查询每个限制是否符合。

代码:

const int N = 1e5 + 10;

inline ll Read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') f = -f, c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
	return x * f;
}

int n, m;

struct SegmentTree {
	struct Tree {
		int l, r, val, lzy;
	} t[N << 2];
	void Build(int p, int l, int r) {
		t[p].l = l, t[p].r = r;
		if (t[p].l == t[p].r) {
			return ;
		}
		int mid = t[p].l + t[p].r >> 1;
		Build (p << 1, l, mid);
		Build (p << 1 | 1, mid + 1, r);
	}
	void Spread(int p) {
		if (!t[p].lzy) return;
		t[p << 1].val |= t[p].lzy;
		t[p << 1 | 1].val |= t[p].lzy;
		t[p << 1].lzy |= t[p].lzy;
		t[p << 1 | 1].lzy |= t[p].lzy;
		t[p].lzy = 0;
	}

	void Modify (int p, int l, int r, int val) {
		if (l <= t[p].l && t[p].r <= r) {
			t[p].val |= val, t[p].lzy |= val;
			return;
		}
		Spread(p);
		int mid = t[p].l + t[p].r >> 1;
		if (l <= mid) Modify(p << 1, l, r, val);
		if (mid + 1 <= r) Modify (p << 1 | 1, l, r, val);
		t[p].val = t[p << 1].val & t[p << 1 | 1].val;
	}
	
	int Query (int p, int l, int r) {
		if (l <= t[p].l && t[p].r <= r) return t[p].val;
		Spread(p);
		int mid = t[p].l + t[p].r >> 1; 
		int ans = (1ll << 31) - 1;
		if (l <= mid) ans &= Query (p << 1, l, r);
		if (mid + 1 <= r) ans &= Query (p << 1 | 1, l, r);
		return ans;
	}
}t;

struct Ques {
	int l, r, val;
}q[N];

int main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	n = Read(), m = Read();
	t.Build(1, 1, n); 
	for (int i = 1; i <= m; i++) {
		q[i].l = Read(), q[i].r = Read(), q[i].val = Read();
		if (q[i].l > q[i].r) swap (q[i].l, q[i].r);
		t.Modify(1, q[i].l, q[i].r, q[i].val);
	}
	
	bool flag = 1;
	for (int i = 1; i <= m; i++) 
		if (t.Query(1, q[i].l, q[i].r) != q[i].val) {
			flag = 0;
			break;
		}
	if (!flag) {
		puts("NO");
		return 0;
	}
	puts("YES");
	for (int i = 1; i <= n; i++) printf ("%d ", t.Query(1, i, i));
	return 0;
}
posted @ 2021-12-09 23:06  Jayun  阅读(33)  评论(0编辑  收藏  举报