B. Interesting Array (segment tree)

B. 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[ninteresting, if it meets m constraints. The i-th of the m constraints consists of three integers liriqi (1 ≤ li ≤ ri ≤ n) meaning that value  should be equal to qi.

Your task is to find any interesting array of n elements or state that such array doesn't exist.

Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".

Input

The first line contains two integers nm (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits.

Each of the next m lines contains three integers liriqi (1 ≤ li ≤ ri ≤ n0 ≤ qi < 230) describing the i-th limit.

Output

If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n](0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them.

If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.

Examples
input
3 1
1 3 3
output
YES 
3 3 3
input
3 2
1 3 3
1 3 2
output
NO

 

Algorithm: segment tree

Solution: direct line with the sub-tree version, update or modify a bit, with a bit of updating up, hurry can make a lazy array.

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <vector>

using namespace std;

#define INF 0x3f3f3f3f
typedef long long ll;

const int maxn = 1e5+7;

struct node {
    int l, r, s;
}tree[maxn << 2];

struct queue {
    int l, r, w;
}Q[maxn];

int n, m;
int lazy[maxn << 2];

void build(int root, int l, int r) {
    tree[root].l = l;
    tree[root].r = r;
    tree[root].s = 0;
    if(l == r) {
        return;
    }
    int mid = (l + r) >> 1;
    build(root << 1, l, mid);
    build(root << 1 | 1, mid + 1, r);
}

void pushup(int root) {
    tree[root].s = tree[root << 1].s & tree[root << 1 | 1].s;
}

void pushdown(int root) {
    if(lazy[root]) {
        tree[root << 1].s |= lazy[root];
        tree[root << 1 | 1].s |= lazy[root];
        lazy[root << 1] |= lazy[root];
        lazy[root << 1 | 1] |= lazy[root];
        lazy[root] = 0;
    }
}

void update(int root, int x, int y, int val) {
    int l = tree[root].l;
    int r = tree[root].r;
    if(x <= l && r <= y) {
        tree[root].s |= val;
        lazy[root] |= val;
        return;
    }
    pushdown(root);
    int mid = (l + r) >> 1;
    if(x <= mid) {
        update(root << 1, x, y, val);
    }
    if(y > mid) {
        update(root << 1 | 1, x, y, val);
    }
    pushup(root);
}

int queue(int root, int x, int y) {
    int l = tree[root].l;
    int r = tree[root].r;
    if(x <= l && r <= y) {
        return tree[root].s;
    }
    pushdown(root);
    int mid = (l + r) >> 1;
    int ans = (1 << 30) - 1;
    if(x <= mid) {
        ans &= queue(root << 1, x, y);
    }
    if(y > mid) {
        ans &= queue(root << 1 | 1, x, y);
    }
    pushdown(root);
    return ans;
}

int main() {
    scanf("%d %d", &n, &m);
    build(1, 1, n);
    for(int= I . 1; i <= m; i++) { 
        Scanf ( " % D% D% D " , & Q [I] .L, & Q [I] .r, & Q [I] .W); 
        Update ( . 1 , Q [I] .L, Q [I ] .r, Q [I] .W); 
    } 
    for ( int I = . 1 ; I <= m; I ++ ) {
         IF (Queue ( . 1 , Q [I] .L, Q [I] .r) =! Q [I] .W) {     // If either condition is not satisfied, then outputs NO 
            the printf ( " NO \ n- " );
             return  0 ; 
        } 
    } 
    the printf ( " YES \ n- " );
     for ( int i = 1; i <= n; i++) {
        printf("%d%c", queue(1, i, i), " \n"[i == n]);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/buhuiflydepig/p/11345810.html