Codeforces 483D 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.

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

题目大意:


已知有M个信息,表示【L,R】区间内的与的值为val.问是否存在这样一个数组,如果存在,输出,如果不存在输出NO.


思路:


①我们知道,每个查询都是单独的,所以我们如果知道了【L,R】区间与的值为val,那么我们顺其自然,直接将这个区间上的值都或上val.


②那么我们可以维护一个数组sum【i】【j】,表示j这个位子上,第i位是否有1,那么对于m个已知信息,我们可以O(m*30)去维护这个数组,在sum【i】【L】上+1.在sum【i】【R+1】上-1.然后我们O(n*30)遍历这个数组,就能够知道一个数组a【】的值。


③那么我们对于这个数组a【】进行m次查询,如果每个区间的与的值都和已知信息相对,那么结果就是这个数组,否则就是不存在这样的一个解。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
    int l,r,val;
}q[150000];
int INF;
int a[150000];
int sum[32][150000];
int now[32];
/**********************************/
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int tree[150000*4];
void pushup(int rt)
{
    tree[rt]=tree[rt*2]&tree[rt*2+1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt]=a[l];
        return ;
    }
    int m=(l+r)/2;
    build(lson);build(rson);pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        return tree[rt];
    }
    pushup(rt);
    int m=(l+r)/2;
    int ans=INF;
    if(L<=m)ans=(ans&(query(L,R,lson)));
    if(R>m)ans=(ans&(query(L,R,rson)));
    pushup(rt);
    return ans;
}
/**********************************/
int main()
{
    int n,m;
    for(int i=0;i<=30;i++)INF+=(1<<i);
    while(~scanf("%d%d",&n,&m))
    {
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=m;i++)
        {
            int L,R,val;
            scanf("%d%d%d",&L,&R,&val);
            q[i].l=L;q[i].r=R,q[i].val=val;
            for(int j=0;j<=30;j++)
            {
                if((val&((1<<j)))>0)
                {
                    sum[j][L]++;
                    sum[j][R+1]--;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=30;j++)
            {
                now[j]+=sum[j][i];
            }
            int num=0;
            for(int j=0;j<=30;j++)
            {
                if(now[j]>0)num+=(1<<j);
            }
            a[i]=num;
        }
        build(1,n,1);
        int flag=1;
        for(int i=1;i<=m;i++)
        {
            if(query(q[i].l,q[i].r,1,n,1)!=q[i].val)flag=0;
        }
        if(flag==0)printf("NO\n");
        else
        {
            printf("YES\n");
            for(int i=1;i<=n;i++)
            {
                printf("%d ",a[i]);
            }
            printf("\n");
        }
    }
}










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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值