B. Interesting Array(线段树)

Posted buhuiflydepig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了B. Interesting Array(线段树)相关的知识,希望对你有一定的参考价值。

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

 

算法:线段树

题解:直接用线段树版子,更新用按位或来修改,用按位与来向上更新,要快点可以打个lazy数组。

 

#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)     //如果其中有一个不满足条件的话,就输出NO
            printf("NO\n");
            return 0;
        
    
    printf("YES\n");
    for(int i = 1; i <= n; i++) 
        printf("%d%c", queue(1, i, i), " \n"[i == n]);
    
    return 0;

 

以上是关于B. Interesting Array(线段树)的主要内容,如果未能解决你的问题,请参考以下文章

Interesting Array

B. Interesting Array(位运算&差分)

P3373 模板线段树 2 {线段树,模板}

B. Interesting drink1100 / 二分

?Good Bye 2019 B. Interesting Subarray

The 17th Zhejiang Provincial Contest B. Bin Packing Problem(线段树+map)