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".
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.
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.
3 1 1 3 3
YES 3 3 3
3 2 1 3 3 1 3 2
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");
}
}
}