zoukankan      html  css  js  c++  java
  • 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
    ");
                return 0;
            }
        }
        printf("YES
    ");
        for(int i = 1; i <= n; i++) {
            printf("%d%c", queue(1, i, i), " 
    "[i == n]);
        }
        return 0;
    }
  • 相关阅读:
    android界面基本属性
    iOS请求webservice
    图片在鼠标经过时变大
    控制字体大小,em与px的区别与应用
    IE的另类CSS hack,条件注释
    几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比
    CSS实现文字压在边线上的效果,fieldset与legend
    每个.NET 开发人员应该下载的十个必备工具
    css做出丰富的Tooltips
    .NET牛人应该知道些什么?
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/11345810.html
Copyright © 2011-2022 走看看