Interesting Array - CodeForces 482 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[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


题意:问是否存在使得每次求并后给定区间为给定数值的序列。

思路:首先对于q转化成二进制,然后每一位如果为1,对应区间内的所有数的该位赋值为1。处理完后便生成了答案序列,再处理q的每一位如果为0的情况,如果这个区间的全为1的话,就不能构成这样的序列。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int l,r,num[30];
    bool val[30];
}tree[400010];
struct node2
{
    int l,r,q;
}op[100010];
int f[100010];
void build(int o,int l,int r)
{
    int i,j,k,mi;
    tree[o].l=l;
    tree[o].r=r;
    for(i=0;i<=29;i++)
       tree[o].num[i]=0;
    if(l==r)
    {
        f[l]=o;
        return;
    }
    mi=(l+r)/2;
    build(o<<1,l,mi);
    build(o<<1|1,mi+1,r);
}
void down(int o)
{
    if(tree[o].l!=tree[o].r)
    for(int i=0;i<=29;i++)
    {
        if(tree[o].val[i]==1)
        {
            tree[o<<1].val[i]=1;tree[o<<1|1].val[i]=1;
            tree[o<<1].num[i]=0;tree[o<<1|1].num[i]=0;
        }
    }
}
void update(int o,int l,int r,int pos)
{
    down(o);
    if(tree[o].l==l && tree[o].r==r)
    {
        tree[o].val[pos]=1;
        tree[o].num[pos]=0;
        return;
    }
    int mi=(tree[o].l+tree[o].r)/2;
    if(r<=mi)
      update(o<<1,l,r,pos);
    else if(l>mi)
      update(o<<1|1,l,r,pos);
    else
    {
        update(o<<1,l,mi,pos);
        update(o<<1|1,mi+1,r,pos);
    }
    tree[o].num[pos]=tree[o<<1].num[pos]+tree[o<<1|1].num[pos];
}
void down2(int o)
{
    down(o);
    if(tree[o].l==tree[o].r)
      return;
    down2(o<<1);
    down2(o<<1|1);
}
int pow2[35],val[100010],num[100010][35];
int main()
{
    int i,j,k,n,m,ret;
    bool flag=true;
    scanf("%d%d",&n,&m);
    build(1,1,n);
    //printf("ok\n");
    for(i=1;i<=m;i++)
       scanf("%d%d%d",&op[i].l,&op[i].r,&op[i].q);
    m=min(m,33);
    for(i=0;i<=30;i++)
       num[0][i]=0;
    pow2[0]=1;
    for(i=1;i<=30;i++)
       pow2[i]=pow2[i-1]*2;
    for(i=1;i<=m;i++)
       for(j=0;j<=29;j++)
          if((op[i].q&pow2[j])==0)
            update(1,op[i].l,op[i].r,j);
    down2(1);
        for(i=1;i<=n;i++)
        {
            ret=0;
            for(j=0;j<=29;j++)
               if(tree[f[i]].num[j]==1)
               {
                   ret+=pow2[j];
                   num[i][j]=num[i-1][j]+1;
               }
               else
                 num[i][j]=num[i-1][j];
            val[i]=ret;
        }
    for(i=1;i<=m;i++)
    {
        for(j=0;j<=29;j++)
           if((op[i].q&pow2[j])>0)
             if(num[op[i].r][j]-num[op[i].l][j]==0)
               flag=false;
    }
    if(!flag)
        printf("NO\n");
    else
    {
        printf("YES\n");
        for(i=1;i<=n;i++)
          printf("%d ",val[i]);
        printf("\n");
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值