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 ...
随机推荐
- Asp.net core Identity + identity server + angular + odata + 权限管理
今天踩了一个坑. 网站发布后看到了一个 error Refused to execute inline script because it violates the following Content ...
- Java 反射理解(三)-- Java获取方法信息
Java 反射理解(三)-- Java获取方法信息 基本的数据类型.void关键字,都存在类类型. 举例如下: public class ClassDemo2 { public static void ...
- VPS磁盘划分建立新磁盘
今天我们来教下大家拿到VPS后,如何划分电脑内的磁盘空间.很多朋友可能遇到拿到VPS,为什么会打开电脑后在电脑盘那看到就一个C盘.还有些用户以为怎么只有那小的磁盘空间啊!怎么和卖的不一样啊!其实了我们 ...
- 完美解决Uncaught SyntaxError: Unexpected end of input
Unexpected end of input 的英文意思是“意外的终止输入” 他通常表示我们浏览器在读取我们的js代码时,碰到了不可预知的错误,导致浏览器 无语进行下面的读取 通常造成这种错误的原 ...
- ubuntu - 14.10,解决按照最新版Gnome 15.10后,经典Gnome桌面字体问题!
ubuntu14.10刚安装完毕,我首先按照了经典Gnome桌面,随后我发现ubuntu软件中心里面能找到的软件明显不如先前我安装过的ubuntu了,我觉得有可能是因为我以前安装的ubuntu14.1 ...
- API工具下载地址记录一下
java 1.6 帮助文档中文链接:http://download.csdn.net/detail/qw599186875/9608735 中文 – 谷歌版在线版: https://blog.fond ...
- deep_learning_Dropout
吴恩达深度学习笔记(十一)—— dropout正则化 主要内容: 一.dropout正则化的思想 二.dropout算法流程 三.dropout的优缺点 一.dropout正则化的思想 在神经网络中, ...
- django-spirt 论坛主题
官方文档 新建项目 django_sprit 文件夹 cd django_sprit pip install django-spirit spirit startproject mysite cd m ...
- shell脚本基础和grep文本处理工具企业应用4
文本处理工具: egrep: 支持扩展的正则表达式实现类似于grep文本过滤功能:grep -E egrep [OPTIONS] PATTERN [FILE...] ...
- Hive(七)Hive参数操作和运行方式
Hive参数操作和运行方式 1.Hive参数操作 1.hive参数介绍 hive当中的参数.变量都是以命名空间开头的,详情如下表所示: 命名空间 读写权限 含义 hiveconf 可读写 hive ...