B. Interesting Array(线段树)
1 second
256 megabytes
standard input
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 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
算法:线段树
题解:直接用线段树版子,更新用按位或来修改,用按位与来向上更新,要快点可以打个lazy数组。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <vector> using namespace std; #define INF 0x3f3f3f3f
typedef long long ll; const int maxn = 1e5+; struct node {
int l, r, s;
}tree[maxn << ]; struct queue {
int l, r, w;
}Q[maxn]; int n, m;
int lazy[maxn << ]; void build(int root, int l, int r) {
tree[root].l = l;
tree[root].r = r;
tree[root].s = ;
if(l == r) {
return;
}
int mid = (l + r) >> ;
build(root << , l, mid);
build(root << | , mid + , r);
} void pushup(int root) {
tree[root].s = tree[root << ].s & tree[root << | ].s;
} void pushdown(int root) {
if(lazy[root]) {
tree[root << ].s |= lazy[root];
tree[root << | ].s |= lazy[root];
lazy[root << ] |= lazy[root];
lazy[root << | ] |= lazy[root];
lazy[root] = ;
}
} void update(int root, int x, int y, int val) {
int l = tree[root].l;
int r = tree[root].r;
if(x <= l && r <= y) {
tree[root].s |= val;
lazy[root] |= val;
return;
}
pushdown(root);
int mid = (l + r) >> ;
if(x <= mid) {
update(root << , x, y, val);
}
if(y > mid) {
update(root << | , x, y, val);
}
pushup(root);
} int queue(int root, int x, int y) {
int l = tree[root].l;
int r = tree[root].r;
if(x <= l && r <= y) {
return tree[root].s;
}
pushdown(root);
int mid = (l + r) >> ;
int ans = ( << ) - ;
if(x <= mid) {
ans &= queue(root << , x, y);
}
if(y > mid) {
ans &= queue(root << | , x, y);
}
pushdown(root);
return ans;
} int main() {
scanf("%d %d", &n, &m);
build(, , n);
for(int i = ; i <= m; i++) {
scanf("%d %d %d", &Q[i].l, &Q[i].r, &Q[i].w);
update(, Q[i].l, Q[i].r, Q[i].w);
}
for(int i = ; i <= m; i++) {
if(queue(, Q[i].l, Q[i].r) != Q[i].w) { //如果其中有一个不满足条件的话,就输出NO
printf("NO\n");
return ;
}
}
printf("YES\n");
for(int i = ; i <= n; i++) {
printf("%d%c", queue(, i, i), " \n"[i == n]);
}
return ;
}
B. Interesting Array(线段树)的更多相关文章
- Codeforces 482B Interesting Array(线段树)
题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...
- Codeforces Round #275 Div.1 B Interesting Array --线段树
题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- codeforces 719E E. Sasha and Array(线段树)
题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 1114F Please, another Queries on Array? 线段树
Please, another Queries on Array? 利用欧拉函数的计算方法, 用线段树搞一搞就好啦. #include<bits/stdc++.h> #define LL ...
- 2019ccpc网络赛hdu6703 array(线段树)
array 题目传送门 解题思路 操作1是把第pos个位置上的数加上\(10^7\),操作2是找到区间[1,r]中没有且大于k的最小的数.注意到k的范围是小于等于n的,且n的范围是\(10^5\),远 ...
- 暑假集训单切赛第一场 CF 266E More Queries to Array(线段树+二项式展开式)
比赛时,第二题就是做的这个,当时果断没仔细考虑,直接用线段树暴力求.结果易想而知,超时了. 比赛后搜了搜题解,恍然大悟. 思路:显然用线段树,但是由于每次查询都会有变,所以不可能存储题目中的式子. ...
- Codeforces295A - Greg and Array(线段树的成段更新)
题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...
- [Codeforces266E]More Queries to Array...——线段树
题目链接: Codeforces266E 题目大意:给出一个序列$a$,要求完成$Q$次操作,操作分为两种:1.$l,r,x$,将$[l,r]$的数都变为$x$.2.$l,r,k$,求$\sum\li ...
随机推荐
- DVWA reCAPTCHA key: Missing
修改dvwa文件夹下文件config.inc.php change: $_DVWA[ 'recaptcha_public_key' ] = ' '; $_DVWA[ 'recaptcha_privat ...
- Jenkins常用插件介绍
摘要: 对于中小型运维团队,jenkins作为运维利器,可以解决很多工作中的痛点.基于UI的特性从而让使用者的入门成本很低,基于插件可以具备认证,记录,条件触发以及联动,让运维工程师可以将精力放在业务 ...
- webSocket协议和Socket.IO
一.Http无法轻松实现实时应用: ● HTTP协议是无状态的,服务器只会响应来自客户端的请求,但是它与客户端之间不具备持续连接. ● 我们可以非常轻松的捕获浏览器上发生的事件(比如用户点击了盒子), ...
- 23-Perl 面向对象
1.Perl 面向对象Perl 中有两种不同地面向对象编程的实现:一是基于匿名哈希表的方式,每个对象实例的实质就是一个指向匿名哈希表的引用.在这个匿名哈希表中,存储来所有的实例属性.二是基于数组的方式 ...
- Java 时间不一致
1.new Date() 得到的时间?和系统相差 相差8个小时 2.eclipse控制台打印的时间与系统相差 相差8个小时 3.log4j日志的时间与与系统相差 相差8个小时 上述问题其实是同一个问题 ...
- wpf 模板绑定父对象
有两种方式可以实现在模板中元素绑定到父对象 1.<ContentPresenter Margin=”{TemplateBinding Padding}”/> 2.Color=”{Bindi ...
- Lua入门记录
学习资料 Lua入门和Lua高阶章节 Lua中文文档 阅读笔记,只是记录了知识点和一些注意点,详细的看上面提供的学习资料链接 Lua 基础数据类型 nil(空) boolean(布尔) Lua 中 n ...
- CentOS7 使用光盘镜像作为yum源
1. 首先,如果是虚拟机,则确认guest有光驱并且处于激活状态,如果是真机则在光驱中插入光盘 :-) 2. mkdir /media/cdrom mount /dev/cdrom /media/c ...
- functools:管理函数的工具
介绍 functools模块提供了一些工具来管理或扩展和其他callable对象,从而不必完全重写 修饰符 偏函数partial from functools import partial ''' f ...
- kubernetes之requests和limits
说明 1.当集群中的计算资源不很充足, 如果集群中的pod负载突然加大, 就会使某个node的资源严重不足, 为了避免系统挂掉, 该node会选择清理某些pod来释放资源, 此时每个pod都可能成为牺 ...