【CODEFORCES】 D. Interesting Array

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 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 ≤ 1051 ≤ 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.

Sample test(s)
input
3 1
1 3 3
output
YES
3 3 3
input
3 2
1 3 3
1 3 2
output
NO

题解:这一题主要使用线段树,对每个询问进行处理,重复的区间上取|,这样才能保留所有的1。最后对所有的询问进行检查,如果符合就YES,不符合就输出NO。本来用的是询问输出解,现在用的是先遍历在输出,不然会超时...

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

struct tree
{
    int l,r,c;
};
struct tree d[400000];
int n,m,f,a[100005],b[100005],q[100005],ans[100005];

void buildtree(int i,int l,int r)
{
    d[i].l=l; d[i].r=r; d[i].c=0;
    if (l==r) return;
    buildtree(i*2,l,(l+r)/2);
    buildtree(i*2+1,(l+r)/2+1,r);
}


void _insert(int i,int l,int r,int c)
{
    int t=(d[i].l+d[i].r)/2;
    if (d[i].l==l && d[i].r==r) d[i].c=d[i].c|c;
    else if (r<=t) _insert(i*2,l,r,c);
    else if (l>t) _insert(i*2+1,l,r,c);
    else
    {
        _insert(i*2,l,t,c);
        _insert(i*2+1,t+1,r,c);
    }
}

int _count(int i,int l,int r)
{
    int t=(d[i].l+d[i].r)/2;
    if (d[i].l==l && d[i].r==r) return d[i].c;
    else if (r<=t) return _count(i*2,l,r);
    else if (l>t) return _count(i*2+1,l,r);
    else return _count(i*2,l,t)&_count(i*2+1,t+1,r);
}

int solve(int i)
{
    if (i!=1) d[i].c|=d[i/2].c;
    if (d[i].l==d[i].r) ans[d[i].l]=d[i].c;
    else
    {
        solve(i*2);
        solve(i*2+1);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    buildtree(1,1,n);
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a[i],&b[i],&q[i]);
        _insert(1,a[i],b[i],q[i]);
    }
    int flag=0;
    for (int i=1;i<=m;i++)
        if (_count(1,a[i],b[i])!=q[i])
        {
            flag=1;
            break;
        }
    if (flag) printf("NO\n");
    else
    {
        solve(1);
        printf("YES\n");
        for (int i=1;i<=n;i++) printf("%d ",ans[i]);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值