题目描述:

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

思路:

跟区间相关的问题可以考虑用线段树。这道题是要通过一次次的限制,把数组中的数构造出来。比如第l个到第r个数的&结果为q,即在二进制表示上,如果某一位并的结果为一,那么该区间上所有数的该位全都为一,若某一位结果为零,那么该区间上至少有一个数的该位为0。

刚开始:全为一时知道区间上所有数在这一位上为一,并为零时不知道区间上那一个数的这一位为零。想构造二维向量,将数组中的数用二进制表示,暴力解出可能的结果。但好像操作起来挺复杂(编程实现有点困难,对我这个蒟蒻来说),也可能会超时。

最后:考虑线段树,来结合问题看一看。在区间【l,r】上,假设并的结果为3(样例1),那么区间上的每一个数(用sum数组来实现线段树)为线段树的叶节点,刚开始sum[i]=0,在一次次的限制条件下,用sum[k]|=q[i],(q[i]表示并的结果),即用或操作我们可以构造出sum[k]来满足一条条的限制条件,当所有的限制条件过完了后,在来查询一个个限制条件所给的区间上数的并是否依然等于q[i],因为在刚刚构造过程中前面满足的条件可能会因为后面的限制条件而改变,改变就说明出现了两个矛盾的限制条件,因为对不矛盾的限制条件来说,他们构造的数应是要么相等,要么构造的数不一是同一个。

在求区间和的线段树模板上稍作改动。具体的,把

pushup(k){sum[k] = sum[k<<1]+sum[k<<1|1]的加改为并,在询问操作中的返回结果思考了有点久,这跟和好像不太一样,注意题目中的数据范围(小于2^30),就用I=2^30-1,代替ans += ···为I &=···来返回结果。

小心的是pushdown的时刻,返回时怎么返回结果,还有一点:2<<30-1你以为是2^30-1?错了!注意运算符的优先级,写成(2<<30)-1

知识点:线段树

代码:

 #include <iostream>
#define max_n 100005
using namespace std;
int sum[max_n<<];
int lz[max_n<<];
int l[max_n];
int r[max_n];
int q[max_n];
int n;
int m;
int I;
void pushup(int k)
{
sum[k] = sum[k<<]&sum[k<<|];
}
void pushdown(int k,int ln,int rn)
{
if(lz[k])
{
sum[k<<] |= lz[k];
sum[k<<|] |= lz[k];
lz[k<<] |= lz[k];
lz[k<<|] |= lz[k];
lz[k] = ;
}
}
void build(int k,int l,int r)
{
lz[k] = ;
if(l==r)
{
sum[k]=;
return;
}
int mid = (l+r)>>;
build(k<<,l,mid);
build(k<<|,mid+,r);
pushup(k);
}
void update(int k,int L,int R,int l,int r,int val)
{
if(L<=l&&r<=R)
{
sum[k] |= val;
lz[k] |= val;
return;
}
int mid = (l+r)/;
int ln = mid-l+;
int rn = r-mid;
pushdown(k,ln,rn);
if(L<=mid) update(k<<,L,R,l,mid,val);
if(mid<R) update(k<<|,L,R,mid+,r,val);
pushup(k);
}
int query(int k,int L,int R,int l,int r)
{
if(L<=l&&r<=R)
{
//cout << "k " << k << " " << sum[k] << endl;
return sum[k];
}
int mid = (l+r)>>;
int ln = mid-l+;
int rn = r-mid;
pushdown(k,ln,rn);
long long I = (long long)(<<) - ;
if(L<=mid) I = I&query(k<<,L,R,l,mid);
//cout << "qian " << I << endl;
if(mid<R) I = I&query(k<<|,L,R,mid+,r);
pushup(k);
//cout << "hou " << I << endl;
return I;
}
int main()
{
cin >> n >> m;
build(,,n);
for(int i = ;i<m;i++)
{
cin >> l[i] >> r[i] >> q[i];
update(,l[i],r[i],,n,q[i]);
}
for(int i = ;i<m;i++)
{
int ans = query(,l[i],r[i],,n);
if(ans!=q[i])
{
cout << "NO" << endl;
return ;
}
}
cout << "YES" << endl;
for(int i = ;i<=n;i++)
{
cout << query(,i,i,,n) << " ";
}
return ;
}

Codeforces E. Interesting Array(线段树)的更多相关文章

  1. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  2. Codeforces Round #275 Div.1 B Interesting Array --线段树

    题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...

  3. codeforces 482B. Interesting Array【线段树区间更新】

    题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val. 就是区间l---r上的与的值为val,最后问你原来的数 ...

  4. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  5. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  6. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  7. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  8. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

  9. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

随机推荐

  1. 【python基础】setproctitle的安装与使用

    前言 setproctitle模块允许设置进程名称. 安装 pip install setproctitle 使用 参考 1. setproctitle: 完

  2. Windows Server 2008 R2 IIS7.5配置FTP图文教程

    本文为大家分享了IIS 配置FTP 网站的具体过程,供大家参考,具体内容如下 说明:服务器环境是Windows Server 2008 R2,IIS7.5. 1. 在 服务器管理器的Web服务器(II ...

  3. Toping Kagglers:Bestfitting,目前世界排名第一

    Toping Kagglers:Bestfitting,目前世界排名第一 Kaggle团队 |2018年5月7日   我们在排行榜上排名第一 - 这是两年前令人惊讶地加入该平台的竞争对手.Shubin ...

  4. LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46

    406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...

  5. dubbo调用服务时,利用zookeeper实现本地动态负载均衡

    利用了zookeeper的临时节点的特点,生产者将自己的服务信息注册到zookeeper当中,消费者去zookeeper当中取出服务信息的集合,本地实现负载均衡 public class TestCr ...

  6. 【转】ZYNQ中三种实现GPIO的方式

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/husipeng86/article/det ...

  7. Array : 数组的常用方法

    数组常用方法 对象通用方法 1.toLocalString()  数组中的每个元素都会调用toLocalString(),然后以逗号隔开, 拼接为字符串: 2.toString() 数组中的每个元素都 ...

  8. 洛谷 P1411 树 (树形dp)

    大意: 给定树, 求删除一些边, 使得连通块大小的乘积最大 设$dp_{i,j}$表示只考虑点$i$的子树, $i$所在连通块大小为$j$的最大值. 转移的时候不计算$i$所在连通块的贡献, 留到最后 ...

  9. CentOS7配置网卡上网、安装wget、配置163yum源

    2019/09/12,CentOS 7 VMware 摘要:CentOS7安装完成(最小化安装)后,不能联网(已选择桥接网络),需要修改配置文件及配置yum源 修改配置文件 进入网卡配置目录 cd / ...

  10. C# vb .net实现翻转特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的翻转特效效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...