CodeForces 482B-Interesting Array(线段树)

A - Interesting Array
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

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 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.

Sample Input

Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO
 
    
题意:
   给你n个位置1-n,再给m个区间,要m个区间的位置&后都符合q的值,不符合就NO,否者YES而且要输出所有位置的值。
 
    
思路:
  这题想了一会感觉像线段树,结果敲完后总是WA3,最后发现了在查询的时候两个区间回溯的时候应该要&才能是正确的答案。
 
    
AC代码:
 
    
#include<iostream>
#include<algorithm>
#include<cstdio>

using namespace std;

const int T = 100000+50;

#define lson (rt<<1)
#define rson (rt<<1|1)

struct node
{
	int L,R,mid,v,ans;
}tree[T<<2];

struct Line
{
	int L,R,v;
}li[T];

void Push_Down(int rt)//将数值向下传递
{
	if(tree[rt].v){
		tree[lson].v |= tree[rt].v;//懒惰性标记
		tree[rson].v |= tree[rt].v;
		tree[lson].ans |= tree[rt].ans;//区间值,一开始没将区间值也向下压了
		tree[rson].ans |= tree[rt].ans;
		tree[rt].v = 0;
	}
}

void Push_Up(int rt)//向上找出最终区间的值
{
	tree[rt].ans = tree[lson].ans&tree[rson].ans;
}

void Build(int rt,int L,int R)//建立二叉树
{
	tree[rt].L = L,tree[rt].R = R;
	tree[rt].mid = (L+R)>>1;
	tree[rt].v = tree[rt].ans = 0;
	if(L==R)return;
	Build(lson,L,tree[rt].mid);
	Build(rson,tree[rt].mid+1,R);
}

void Insert(int rt,int L,int R,int v)//插入一段区间值
{
	if(L<=tree[rt].L&&R>=tree[rt].R){//符合即添加值
		tree[rt].ans |= v;
		tree[rt].v |= v;
		return;
	}
	Push_Down(rt);//向下更新
	if(R<=tree[rt].mid){
		Insert(lson,L,R,v);
	}
	else if(L>tree[rt].mid){
		Insert(rson,L,R,v);
	}
	else {
		Insert(lson,L,tree[rt].mid,v);
		Insert(rson,tree[rt].mid+1,R,v);
	}
	Push_Up(rt);//向上更新
}

int query(int rt,int L,int R)//查询区间段
{
	if(L<=tree[rt].L&&R>=tree[rt].R){
		return tree[rt].ans;
	}
	Push_Down(rt);
	int ans = 0;
	if(R<=tree[rt].mid){
		 ans += query(lson,L,R);
	}
	else if(L>tree[rt].mid){
		ans += query(rson,L,R);
	}
	else {
		//这里需要注意一下,因为有可能左右两边是不同的值,之前一直是相加
		//所以Wa了许多次
		ans += query(lson,L,tree[rt].mid)&query(rson,tree[rt].mid+1,R);
	}
	Push_Up(rt);
	return ans;
}

void Task(int rt,int L,int R)//返回叶子节点的值
{
	if(L==R){
		printf("%d ",tree[rt].v);
		return;
	}
	Push_Down(rt);
	Task(lson,L,tree[rt].mid);
	Task(rson,tree[rt].mid+1,R);
}

int main()
{
#ifdef zsc
	freopen("input.txt","r",stdin);
#endif
	int n,m,i,j,k;
	while(~scanf("%d%d",&n,&m))
	{
		Build(1,1,n);
		for(i=0;i<m;++i){
			scanf("%d%d%d",&li[i].L,&li[i].R,&li[i].v);
			Insert(1,li[i].L,li[i].R,li[i].v);
		}
		bool flag = false;
		for(i=0;i<m&&!flag;++i){
			if(query(1,li[i].L,li[i].R)!=li[i].v){
				flag = true;
			}
		}
		if(flag){
			printf("NO\n");
		}
		else {
			printf("YES\n");
			Task(1,1,n);
			printf("\n");
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值