Codeforces Round #275 (Div. 1) B. Interesting Array (线段树)
题目链接:http://codeforces.com/contest/482/problem/B
题意:给定一个长度为n个序列,要求满足m个条件:[L,R]区间内的异或和为x
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <cstdio> #include <queue> #include <climits> #include <set> #include <stack> #include <string> #include <map> #include <vector> #define INF 0x3f3f3f3f #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 using namespace std; typedef long long ll; static const int MAX_N = 1e5 + 5; static const int N = 1005; static const ll Mod = 2009; static const int inf = (1 << 30) - 1; struct Node{ int l, r, x; }p[MAX_N]; int n; int sum[MAX_N << 2], lazy[MAX_N << 2]; void push_up(int rt){ sum[rt] = sum[rt << 1] & sum[rt << 1 | 1]; } void push_down(int rt){ if(lazy[rt]){ sum[rt << 1] |= lazy[rt]; sum[rt << 1 | 1] |= lazy[rt]; lazy[rt << 1] |= lazy[rt]; lazy[rt << 1 | 1] |= lazy[rt]; lazy[rt] = 0; } } void update(int l, int r, int rt, int L, int R, int x){ if(l >= L && r <= R){ sum[rt] |= x; lazy[rt] |= x; return ; } push_down(rt); int m = l + r >> 1; if(m >= L) update(lson, L, R, x); if(R > m) update(rson, L, R, x); push_up(rt); } int query(int l, int r, int rt, int L, int R){ if(l >= L && r <= R) return sum[rt]; push_down(rt); int m = l + r >> 1; int ans = inf; if(m >= L) ans &= query(lson, L, R); if(R > m) ans &= query(rson, L, R); push_up(rt); return ans; } void output(int l, int r, int rt){ if(l == r){ printf("%d%c", sum[rt], l == n ? '\n' : ' '); return ; } push_down(rt); int m = l + r >> 1; output(lson); output(rson); } void solve(){ // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int m; scanf("%d%d", &n, &m); for(int i = 0; i < m; ++i){ scanf("%d%d%d", &p[i].l, &p[i].r, &p[i].x); update(1, n, 1, p[i].l, p[i].r, p[i].x); } bool fg = true; for(int i = 0; i < m; ++i){ if(p[i].x != query(1, n, 1, p[i].l, p[i].r)){ fg = false; break; } } if(!fg) puts("NO"); else{ puts("YES"); output(1, n, 1); } } int main() { solve(); return 0; }