codeforces 482b Interesting Array

http://www.elijahqi.win/archives/1178
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 li, ri, qi (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 n, m (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 li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ 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个区间的&的值,询问是否冲突

要是不冲突 给出一种可能的解

每个区间&的值 不妨我们直接把这个值给进去,反正一定至少是这个值

然后我每次对区间操作的时候我都用 |运算 把这位要求的1添加进去 使这个区间的&运算值满足条件

每次检查的时候如果查询的&运算值和我给的不同 那么就无法满足题目 退出

#include<cstdio>
#define N 110000
inline char gc(){
    static char now[1<<16],*S,*T;
    if (S==T){T=(S=now)+fread(now,1,1<<16,stdin);if (S==T) return EOF;}
    return *S++;
}
inline int read(){
    int x=0;char ch=gc();
    while (ch<'0'||ch>'9') ch=gc();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x;
}
struct node{
    int l,r,left,right,lazy,andans;
}tree[N<<2];
struct node1{
    int l,r,x;
}q[N];
int n,m,num,root;
void build(int &x,int l,int r){
    x=++num;tree[x].l=l;tree[x].r=r;
    if (l==r) return ;
    int mid=l+r>>1;
    build(tree[x].left,l,mid);build(tree[x].right,mid+1,r);
}
inline void update(int x){
    int l=tree[x].left,r=tree[x].right;
    tree[x].andans=tree[l].andans&tree[r].andans;
}
inline void pushdown(int x){
    if (!tree[x].lazy) return ;
    int l=tree[x].left,r=tree[x].right;
    tree[l].lazy|=tree[x].lazy;tree[r].lazy|=tree[x].lazy;
    tree[l].andans|=tree[x].lazy;tree[r].andans|=tree[x].lazy;
    tree[x].lazy=0;
}
void insert1(int x,int l,int r,int v){
    if (l<=tree[x].l&&r>=tree[x].r){
        tree[x].andans|=v;tree[x].lazy|=v;return;
    }
    int mid=tree[x].l+tree[x].r>>1;pushdown(x);
    if (l<=mid) insert1(tree[x].left,l,r,v);
    if (r>mid) insert1(tree[x].right,l,r,v);
    update(x);
}
int query(int x,int l,int r){
    if (l<=tree[x].l&&r>=tree[x].r) return tree[x].andans;
    int mid=tree[x].l+tree[x].r>>1;pushdown(x);int tmp=(1<<30)-1;
    if (l<=mid) tmp&=query(tree[x].left,l,r);
    if (r>mid) tmp&=query(tree[x].right,l,r);
    return tmp;
}
int main(){
    freopen("cf.in","r",stdin);
    n=read();m=read();build(root,1,n);
    for (int i=1;i<=m;++i) {
        int l=read(),r=read(),z=read();
        q[i].l=l,q[i].r=r,q[i].x=z;
        insert1(root,l,r,z);
    }
    for (int i=1;i<=m;++i){
        int tmp=query(root,q[i].l,q[i].r);
        if (tmp!=q[i].x){
            printf("NO");return 0;
        }
    }
    printf("YES\n");
    for (int i=1;i<=n;++i) printf("%d ",query(root,i,i));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值