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 them 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 integersa[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

传送门:http://codeforces.com/contest/483/problem/D

题意:给出长度为n的序列,要求找出一个序列满足m个要求:区间[l,r]中所有数与运算后的值等于q,如果能找出输出YES和这个序列,如果不能输出NO

题解:对于区间[l,r]如果要求与值为c,那么在二进制中,如果c的第i位是1,区间内所有数的第i都要是1,如果c的第i位是0,则没有限制,于是在向下更新的时候要用或运算。

然后因为要求每个区间[l,r]与运算后的值为q,那么将这个区间中所有子区间求与运算,看是否等于q,如果有一个不符合则输出NO,否则输出YES

#include<cstdio>
#include<string.h>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
const LL INF = (1ll<<32)-1;
LL sum[maxn<<2],add[maxn<<2];
bool flag;
int n;
struct node{
    int l,r;
    LL x;
    bool operator<(const node& _A)const{
        if(l!=_A.l) return l<_A.l;
        return r>_A.r;
    }
}p[maxn];
void PushUP(int rt){
    sum[rt]=sum[rt<<1]&sum[rt<<1|1];
}
void PushDown(int rt){
    if(add[rt]){
        sum[rt<<1]|=add[rt];
        sum[rt<<1|1]|=add[rt];
        add[rt<<1]|=add[rt];
        add[rt<<1|1]|=add[rt];
        add[rt]=0;
    }
}
void update(int L,int R,LL c,int l,int r,int rt){
    if(L<=l&&R>=r){
        add[rt]|=c;
        sum[rt]|=c;
        return;
    }
    PushDown(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    PushUP(rt);
}
LL query(int L,int R,int l,int r,int rt){
    if(L<=l&&R>=r) return sum[rt];
    PushDown(rt);
    int m=(l+r)>>1;
    LL ret=INF;
    if(L<=m) ret&=query(L,R,lson);
    if(R>m) ret&=query(L,R,rson);
    PushUP(rt);
    return ret;
}
void print(int l,int r,int rt){
    if(l==r){
        printf("%I64d%c",sum[rt],l==n?'\n':' ');
        return;
    }
    PushDown(rt);
    int m=(l+r)>>1;
    print(lson);
    print(rson);
}
int main(){
    int m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++) scanf("%d%d%I64d",&p[i].l,&p[i].r,&p[i].x);
    flag=1;
    for(int i=0;i<m;i++) 
        update(p[i].l,p[i].r,p[i].x,1,n,1);
    for(int i=0;i<m;i++)
        if(p[i].x!=query(p[i].l,p[i].r,1,n,1)) {flag=0;break;}
    if(flag) {
        printf("YES\n");
        print(1,n,1);
    }
    else printf("NO\n");
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值