D. InterestingArraytimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputWellcallanarrayofnnon-negativeintegersa [1],? A [2],?...,? A [n] interesting, ifitmeetsmconst
D. Interesting Array time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output We'll call an array of n non-negative integers a [1],? A [2],?...,? A [n] interesting, if it meets m const
D. Interesting Array
Time limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
We'll call an arrayNNon-negative integersA[1],?A[2],?...,?A[N] Interesting, if it meetsMConstraints.I-Th ofMConstraints consists of three integersLI,RI,QI(1? ≤?LI? ≤?RI? ≤?N) Meaning that value shoshould be equalQI.
Your task is to find any interesting arrayNElements or state that such array doesn't exist.
ExpressionX&YMeans the bitwise AND of numbersXAndY. In programming ages C ++, Java and Python this operation is represented as "&", in Pascal-as "and ".
Input
The first line contains two integersN,M(1? ≤?N? ≤? 105, 1? ≤?M? ≤? 105)-the number of elements in the array and the number of limits.
Each of the nextMLines contains three integersLI,RI,QI(1? ≤?LI? ≤?RI? ≤?N, 0? ≤?QI? I-Th limit.
Output
If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line printNIntegersA[1],?A[2],?...,?A[N] (0? ≤?A[I]?
If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.
Sample test (s)
Input
3 11 3 3
Output
YES3 3 3
Input
3 21 3 31 3 2
Output
NO
Question:
Suppose there are n non-negative numbers, and now there are m restrictions. a [l] & a [l + 1] & a [l + 2]... & a [r] = q. According to the above restrictions, the output must meet the requirements of 1 ~ N number. If not, "NO" is output ".
Solution:
First, let's explore the meaning of the question and find out the known conditions for the question and what we want to output.
A [l] & a [l + 1] & a [l + 2]... & a [r] = q, which is the basic form of each restriction. We can know from "&" that if a bit in q is 1, a [l] ~ is required. The bit in a [r] is 1. This condition seems to be a limitation. Now, through conversion, it seems to be a known condition, that is, the bit in each a [I] must be 1.
We can see from the above that we get the basic value of each a [I], and then each restriction is an interval. It is easy to think of a line segment tree and query each restriction, check whether a conflict exists. If a conflict exists, it is "NO". If NO conflict exists, it is output according to the required value of a [I.
Code:
#include
#include
#define Maxbit 29#define M_max 123456#define N_max 123456#define root 1, 1, nusing namespace std;const int noth = (1<<30)-1;int n, m;int l[M_max], r[M_max], q[M_max], a[N_max];int sum[N_max], tree[N_max*3];void build(int v, int l, int r) {if (l == r) {tree[v] = a[l];return;}int ls = v<<1, rs = ls+1, mid = (l+r)>>1;build(ls, l, mid);build(rs, mid+1, r);tree[v] = tree[ls] & tree[rs];}int query(int v, int l, int r, int ql, int qr) {if (r < ql || l > qr) return noth;if (ql <= l && r <= qr) return tree[v];int ls = v<<1, rs = ls+1, mid = (l+r)>>1;return query(ls, l, mid, ql, qr) & query(rs, mid+1, r, ql, qr);}void init() {scanf("%d%d", &n, &m);for (int i = 1; i <= m; i++)scanf("%d%d%d", &l[i], &r[i], &q[i]);for (int i = 0; i <= Maxbit; i++) {memset(sum, 0, sizeof(sum));for (int j = 1; j <= m; j++)if ((q[j] >> i) & 1) {sum[l[j]]++;sum[r[j]+1]--;}for (int j = 1; j <= n; j++) {sum[j] += sum[j-1];if (sum[j] > 0) a[j] |= 1 << i;}}build(root);}void solve() {for (int i = 1; i <= m; i++)if (query(root, l[i], r[i]) != q[i]) {printf("NO\n");return;}printf("YES\n");for (int i = 1; i <= n; i++) printf("%d ", a[i]);printf("\n");}int main() {init();solve();}