Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.TreeSet;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Nipuna Samarasekara
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
/////////////////////////////////////////////////////////////
public void solve(int testNumber, FastScanner in, FastPrinter out) {
int n=in.nextInt();
int m=in.nextInt();
int[] a= new int[n];
ArrayList<Integer>[] st= new ArrayList[n+1];
ArrayList<Integer>[] en= new ArrayList[n+1];
for (int i = 0; i < n + 1; i++) {
st[i]= new ArrayList<Integer>();
en[i]= new ArrayList<Integer>();
}
// System.out.println(Integer.toBinaryString((int) Math.pow(2, 31) - 1));
p[] q= new p[m];
for (int i = 0; i < m; i++) {
q[i]= new p(in.nextInt()-1,in.nextInt()-1,in.nextInt());
st[q[i].st].add(i);
en[q[i].en].add(i);
}
int[] cval= new int[31];
Arrays.fill(cval,0);
// TreeMap<Integer,Integer> cur= new TreeMap<Integer, Integer>();
TreeSet<Integer> ts= new TreeSet<Integer>();
int chk=1;
for (int i = 0; i < n ; i++) {
for (Integer integer : st[i]) {
int val=q[integer].qq,pos=0;
// ts.add(val);
while (val>0){
if (val%2==1)cval[pos]++;
pos++;
val/=2;
}
}
int cvv=0,tp=1;
for (int j = 0; j < 30; j++) {
if (cval[j]>0)cvv+=tp;
tp*=2;
//out.println(tp);
}
a[i]=cvv;
for (Integer integer : en[i]) {
int val=q[integer].qq,pos=0;
while (val>0){
if (val%2==1)cval[pos]--;
pos++;
val/=2;
}
;
}
// deb(cval);
if (chk==0)break;
}
// deb(a);
DynamicSegmentTree ds= new DynamicSegmentTree(n+1);
for (int i = 0; i < n; i++) {
ds.set(i,a[i]);
}
for (int i = 0; i < m; i++) {
int vv= (int) ds.getSum(q[i].st,q[i].en+1);
// System.out.println(vv+" "+q[i].qq);
if (vv!=q[i].qq){
chk=0;
break;
}
}
if (chk==0||ts.size()>0) out.println("NO");
else{
out.println("YES");
out.printArray(a); }
}
class p {
int st,en,qq;
p(int st, int en, int qq) {
this.st = st;
this.en = en;
this.qq = qq;
}
// @Override
// public int compareTo(p o) {
// if (this.q!=o.q)
// return this.q-o.q; //To change body of implemented methods use File | Settings | File Templates.
// else return this.st-o.st;
// }
}
}
class DynamicSegmentTree {
static class Node {
Node left;
Node right;
long sum;
public void collect() {
sum = (int)Math.pow(2,30)-1;
if (left != null) {
sum &= left.sum;
}
if (right != null) {
sum &= right.sum;
}
}
}
Node root;
int n;
public DynamicSegmentTree(int n) {
root = new Node();
this.n = n;
}
public void set(int x, long y) {
root = set(root, 0, n, x, y);
}
static Node set(Node v, int left, int right, int x, long y) {
if (v == null) {
v = new Node();
}
if (left == right - 1) {
v.sum = y;
return v;
}
int mid = left + right >> 1;
if (x < mid) {
v.left = set(v.left, left, mid, x, y);
} else {
v.right = set(v.right, mid, right, x, y);
}
v.collect();
return v;
}
public long getSum(int l, int r) {
return getSum(root, 0, n, l, r);
}
static long getSum(Node v, int left, int right, int l, int r) {
if (v == null || r <= left || right <= l) {
return (int)Math.pow(2,30)-1;
}
if (l <= left && right <= r) {
return v.sum;
}
int mid = left + right >> 1;
return getSum(v.left, left, mid, l, r) & getSum(v.right, mid, right, l, r);
}
}
class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// if (isEOF && ret < 0) {
// throw new InputMismatchException();
// }
// isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
}
class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
public void printArray(int[] a) {
for (int i = 0; i < a.length; i++) {
if (i > 0) {
print(' ');
}
print(a[i]);
}
println();
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
struct one {
int i;
int end;
bool operator<(const one &e) const { return i < e.i; }
};
struct zero {
int i, j;
};
const int inf = 1e9;
class segment {
public:
vector<int> n, t;
segment() { ; }
segment(vector<int> &a) : n(a), t(4 * a.size(), 0) { build(0, 0, a.size()); }
int build(int v, int i, int j) {
if (j - i <= 0) {
return inf;
} else if (j - i == 1) {
return t[v] = n[i];
} else {
int mid = (i + j) / 2;
build(2 * v + 1, i, mid);
build(2 * v + 2, mid, j);
t[v] = min(t[2 * v + 1], t[2 * v + 2]);
}
}
int rmq(int v, int l, int r, int i, int j) {
if (j - i <= 0) {
return inf;
} else if (l == i && r == j) {
return t[v];
} else {
int mid = (l + r) / 2;
return min(rmq(2 * v + 1, l, mid, i, min(j, mid)),
rmq(2 * v + 2, mid, r, max(mid, i), j));
}
}
int rmq(int i, int j) { return rmq(0, 0, n.size(), i, j); }
};
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<one> o[32];
vector<zero> z[32];
for (int i = 0; i < m; i++) {
int l, r, q;
scanf("%d %d %d", &l, &r, &q);
for (int j = 0; j < 31; j++) {
if (q & (1 << j)) {
o[j].push_back({l - 1, -1});
o[j].push_back({r, 1});
} else {
z[j].push_back({l - 1, r});
}
}
}
for (int i = 0; i < 32; i++) sort(o[i].begin(), o[i].end());
vector<int> bits[32];
vector<int> def(n, 0);
for (int i = 0; i < 32; i++) bits[i] = def;
for (int i = 0; i < 32; i++) {
int cnt = 0;
for (int j = 0, p = 0; j < o[i].size() && p < n;) {
while (p < o[i][j].i) {
bits[i][p] = cnt > 0;
p++;
}
while (j < o[i].size() && o[i][j].i == p) {
cnt -= o[i][j].end;
j++;
}
if (p < n) {
bits[i][p] = cnt > 0;
}
}
}
segment seg[32];
for (int i = 0; i < 32; i++) seg[i] = segment(bits[i]);
for (int i = 0; i < 32; i++) {
for (int j = 0; j < z[i].size(); j++) {
if (seg[i].rmq(z[i][j].i, z[i][j].j) != 0) {
cout << "NO" << endl;
return 0;
}
}
}
vector<int> out(n, 0);
for (int i = 0; i < 32; i++) {
for (int j = 0; j < bits[i].size(); j++) {
if (bits[i][j] >= 1) out[j] |= (1 << i);
}
}
cout << "YES" << endl;
for (int i = 0; i < n; i++) cout << out[i] << " ";
cout << endl;
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class CF_275D {
public static class RangeTree {
int N;
long[] tree;
long[] lazy; // Stores the values to be pushed-down the tree only when needed
/*
* Identities of binary operations f(IDENTITY,x)=x,
* 0 for sum, +INF for min, -INF for max, 0 for gcd, 0 for OR, +INF for AND
*/
long[] IDENTITY = {0,Long.MAX_VALUE};
// Associative binary operation used in the tree
private long operation(long first, long sec, int type) {
switch (type) {
// The case 0 has to be used for the type of operation of the queries
//case 0: return first + sec; // Addition
case 0: return first |= sec; // OR
case 1: return first &= sec; // AND
//case 0: return Math.max(first,sec); // Maximum
// Add cases as needed
default: return sec; // -1 -> Assignment
}
}
// Accumulate the operation zero (0) to be done to the sub-tree without actually pushing it down
private long integrate(long act, long val, int nodeFrom, int nodeTo, int type) {
switch (type) {
//case 0: return act + val*(nodeTo-nodeFrom+1); // Addition
case 0: return act |= val; // OR
case 1: return act &= val; // AND
default: return val; // -1 -> Assignment
}
}
// Constructor for the tree with the maximum expected value
public RangeTree(int maxExpected, int type) {
maxExpected++;
int len = Integer.bitCount(maxExpected)>1 ? Integer.highestOneBit(maxExpected)<<2 : Integer.highestOneBit(maxExpected)<<1;
N = len/2;
tree = new long[len];
lazy = new long[len];
if(tree[0]!=IDENTITY[type]){
Arrays.fill(tree,IDENTITY[type]);
Arrays.fill(lazy,IDENTITY[type]);
}
}
// Propagate the lazily accumulated values down the tree with query operator
public void propagate(int node, int leftChild, int rightChild, int nodeFrom, int mid, int nodeTo, int type){
tree[leftChild] = integrate(tree[leftChild],lazy[node],nodeFrom,mid, type);
tree[rightChild] = integrate(tree[rightChild],lazy[node],mid+1,nodeTo, type);
lazy[leftChild] = operation(lazy[leftChild],lazy[node],type);
lazy[rightChild] = operation(lazy[rightChild],lazy[node],type);
lazy[node] = IDENTITY[type];
}
// Update the values from the position i to j. Time complexity O(lg n)
public void update(int i, int j, long newValue, int type) {
update(1, 0, N-1, i, j,newValue, type);
}
private void update(int node, int nodeFrom, int nodeTo, int i, int j, long v, int type) {
if (i <= nodeFrom && j >= nodeTo) { // Whole range needs to be updated
tree[node] = integrate(tree[node], v, nodeFrom, nodeTo, type);
lazy[node] = operation(lazy[node], v, type);
} else if (j < nodeFrom || i > nodeTo) // No part of the range needs to be updated
return;
else { // Some part of the range needs to be updated
int leftChild = 2 * node;
int rightChild = leftChild+1;
int mid = (nodeFrom + nodeTo) / 2;
if(lazy[node]!=IDENTITY[type])
propagate(node, leftChild, rightChild, nodeFrom, mid, nodeTo, type);
update(leftChild, nodeFrom, mid, i, j, v, type); // Search left child
update(rightChild, mid+1, nodeTo, i, j, v, type); // Search right child
tree[node] = operation(tree[leftChild], tree[rightChild],type); // Merge with the query operation
}
}
// Query the range from i to j. Time complexity O(lg n)
public long query(int i, int j, int type) {
return query(1, 0, N-1, i, j, type);
}
private long query(int node, int nodeFrom, int nodeTo, int i, int j, int type) {
if (i <= nodeFrom && j >= nodeTo) // The whole range is part of the query
return tree[node];
else if (j < nodeFrom || i > nodeTo) // No part of the range belongs to the query
return IDENTITY[type];
else { // Partially within the range
int leftChild = 2*node;
int rightChild = leftChild+1;
int mid = (nodeFrom+nodeTo)/2;
// Propagate lazy operations if necessary
if(lazy[node]!=IDENTITY[type])
propagate(node, leftChild, rightChild, nodeFrom, mid, nodeTo, type);
long a = query(leftChild, nodeFrom, mid, i, j, type); // Search left child
long b = query(rightChild, mid+1, nodeTo, i, j, type); // Search right child
return operation(a, b, type);
}
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = new int[m];
int[] b = new int[m];
int[] q = new int[m];
for(int i = 0; i < m; i++){
a[i] = sc.nextInt()-1;
b[i] = sc.nextInt()-1;
q[i] = sc.nextInt();
}
RangeTree or = new RangeTree(n,0); // OR tree
for(int i = 0; i < m; i++)
or.update(a[i],b[i], q[i],0);
RangeTree and = new RangeTree(n,1);
for(int i = 0; i < n; i++)
and.update(i, i, or.query(i,i,0), 1);
boolean fail = false;
for(int i = 0; i < m; i++){
long aux = and.query(a[i],b[i],1);
if(aux!=q[i]){
fail = true;
break;
}
}
PrintWriter out = new PrintWriter(System.out);
if(!fail){
out.println("YES");
out.print(or.query(0,0,0));
for(int i=1; i<n; i++){
out.print(" "+or.query(i,i,0));
}
out.println();
} else {
out.println("NO");
}
out.flush();
out.close();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.*;
import java.util.*;
import java.math.*;
public class CF483D {
static final int N=100005;
static int l[]=new int[N];
static int r[]=new int[N];
static int v[]=new int[N];
public static void main(String args[]) {
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
Seg_Tree seg=new Seg_Tree();
int n=in.nextInt();
int m=in.nextInt();
seg.build(1,n,1);
for(int i=0;i<m;i++) {
l[i]=in.nextInt();
r[i]=in.nextInt();
v[i]=in.nextInt();
seg.update(1,l[i],r[i],v[i]);
}
boolean flag=false;
for(int i=0;i<m;i++) {
int s=seg.query(1,l[i],r[i]);
if(s!=v[i]) {
flag=true;
break;
}
}
if(flag) {
out.println("NO");
}else {
out.println("YES");
seg.print(1);
for(int x:seg.vv) {
out.print(x+" ");
}
out.println("");
}
out.close();
}
}
class node {
int l,r,val,vis,mid;
node(){}
node(int l,int r,int val) {
this.l=l; this.r=r; this.val=val; this.vis=0;
mid=(l+r)>>1;
}
}
class Seg_Tree {
static final int N=100005;
ArrayList<Integer> vv=new ArrayList<Integer>();
node t[]=new node[N<<2];
void print(int rt) {
if(t[rt].l==t[rt].r) {
vv.add(t[rt].val);
return;
}
push_down(rt);
print(rt<<1); print(rt<<1|1);
}
void push_up(int rt) {
t[rt].val=t[rt<<1].val&t[rt<<1|1].val;
}
void push_down(int rt) {
if(t[rt].vis==0) return;
t[rt<<1].vis|=t[rt].vis; t[rt<<1|1].vis|=t[rt].vis;
t[rt<<1].val|=t[rt].vis; t[rt<<1|1].val|=t[rt].vis;
t[rt].vis=0;
}
void build(int l,int r,int rt) {
t[rt]=new node(l,r,0);
if(l==r) return;
int m=t[rt].mid;
build(l,m,rt<<1); build(m+1,r,rt<<1|1);
push_up(rt);
}
void update(int rt,int l,int r,int val) {
if(t[rt].l>=l&&r>=t[rt].r) {
t[rt].val|=val; t[rt].vis|=val;
return;
}
push_down(rt);
int m=t[rt].mid;
if(m>=l) update(rt<<1,l,r,val);
if(m<r) update(rt<<1|1,l,r,val);
push_up(rt);
}
int query(int rt,int l,int r) {
if(t[rt].l>=l&&r>=t[rt].r) {
return t[rt].val;
}
push_down(rt);
//int res=(1<<30)-1;
int m=t[rt].mid;
if(m>=r) return query(rt<<1,l,r);
else if(m<l) return query(rt<<1|1,l,r);
else {
return query(rt<<1,l,r)&query(rt<<1|1,l,r);
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader=new BufferedReader(new InputStreamReader(stream));
tokenizer=null;
}
public String next() {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
int a[111111][33];
int d[111111][33];
int l[111111];
int r[111111];
int x[111111];
int main() {
int t;
int i, j, k, n, m;
scanf("%d%d", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%d%d%d", &l[i], &r[i], &x[i]);
for (t = 0; t < 32; t++)
if ((x[i] >> t) & 1) {
a[l[i]][t]++;
a[r[i] + 1][t]--;
}
}
for (i = 1; i <= n; i++)
for (j = 0; j < 32; j++) {
a[i][j] += a[i - 1][j];
d[i][j] += d[i - 1][j];
if (a[i][j]) d[i][j]++;
}
for (i = 1; i <= m; i++) {
for (t = 0; t < 32; t++)
if (((x[i] >> t) & 1) == 0 &&
d[r[i]][t] - d[l[i] - 1][t] == r[i] - l[i] + 1)
break;
if (t < 32) {
puts("NO");
return 0;
}
}
puts("YES");
for (i = 1; i <= n; i++) {
j = 0;
for (t = 0; t < 32; t++)
if (a[i][t]) j |= (1 << t);
printf("%d ", j);
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 |
/**
* Date: 20 Nov, 2018
* Link:
*
* @author Prasad-Chaudhari
* @linkedIn: https://www.linkedin.com/in/prasad-chaudhari-841655a6/
* @git: https://github.com/Prasad-Chaudhari
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.LinkedList;
public class newProgram1 {
public static void main(String[] args) throws IOException {
// TODO code application logic here
FastIO in = new FastIO();
int n = in.ni();
int m = in.ni();
Data l[] = new Data[m];
Data r[] = new Data[m];
Data d[] = new Data[m];
int i = 0;
while (i < m) {
int x = in.ni();
int y = in.ni();
int q = in.ni();
d[i] = new Data(x, y, q);
l[i] = new Data(x, i, q);
r[i] = new Data(y, i, q);
++i;
}
Arrays.sort(l);
Arrays.sort(r);
OrSegmentTree or = new OrSegmentTree();
or.getArray(new int[m]);
or.build(0, m - 1, 1);
int l_ind = 0;
int r_ind = 0;
int ans[] = new int[n];
for (i = 1; i <= n; i++) {
while (l_ind < m && l[l_ind].l <= i) {
or.update(0, m - 1, 1, l[l_ind].r - 1, l[l_ind].q);
l_ind++;
}
// for (int j = 0; j < m; j++) {
// System.out.print(or.query(0, m - 1, 1, j, j) + " ");
// }
// System.out.println("");
ans[i - 1] = or.query(0, m - 1, 1, 0, m - 1);
while (r_ind < m && r[r_ind].l <= i) {
or.update(0, m - 1, 1, r[r_ind].r - 1, 0);
r_ind++;
}
}
// for (int j : ans) {
// System.out.print(j + " ");
// }
// System.out.println("");
AnsSegmentTree and = new AnsSegmentTree();
and.getArray(ans);
and.build(0, n - 1, 1);
for (i = 0; i < m; i++) {
if (and.query(0, n - 1, 1, d[i].l - 1, d[i].r - 1) != d[i].q) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
for (int j : ans) {
System.out.println(j);
}
// LinkedList<Data> l = new LinkedList<>();
// char ans[][] = new char[n][31];
// i = 0;
// SegmentTree_LazyPropagation stlp = new SegmentTree_LazyPropagation(n);
// while (i < 31) {
// for (Data curr : d) {
// if (curr.q % 2 == 0) {
// l.add(curr);
// } else {
// stlp.update(1, n, 1, curr.l, curr.r, 1);
// }
// curr.q /= 2;
// }
// while (!l.isEmpty()) {
// Data curr = l.removeFirst();
// if (stlp.query(1, n, 1, curr.l, curr.r) == 1) {
// System.out.println("NO");
// return;
// }
// }
// int j = 0;
// while (j < n) {
// ans[j][30 - i] = (char) ('0' + stlp.query(1, n, 1, j + 1, j + 1));
// ++j;
// }
// if (n == 100000) {
// System.out.println(i);
// }
// ++i;
// stlp.flush();
// }
// System.out.println("YES");
// i = 0;
// while (i < n) {
// int p = 0;
// for (int j = 0; j < 31; j++) {
// p = 2 * p + ans[i][j] - '0';
// }
// in.println(p + "");
// ++i;
// }
// in.bw.flush();
}
static class AnsSegmentTree extends SegmentTree {
public AnsSegmentTree() {
DEFAULT = Integer.MAX_VALUE;
}
@Override
public int keyFunc(int a, int b) {
return a & b;
}
}
static class OrSegmentTree extends SegmentTree {
public OrSegmentTree() {
DEFAULT = 0;
}
@Override
public int keyFunc(int a, int b) {
return a | b;
}
}
static class MaxSegmentTree extends SegmentTree {
@Override
public int keyFunc(int a, int b) {
return Math.max(a, b);
}
}
static class MinSegmentTree extends SegmentTree {
public MinSegmentTree() {
this.DEFAULT = Integer.MAX_VALUE;
}
@Override
public int keyFunc(int a, int b) {
return Math.min(a, b);
}
}
static class SegmentTree {
private int[] tree, array;
int DEFAULT = 0;
public void getArray(int[] a) {
array = a;
int si = a.length;
double x = Math.log(si) / Math.log(2);
int n = (int) (Math.pow(2, Math.ceil(x) + 1)) + 1;
tree = new int[n];
}
public void build(int start, int end, int pos) {
if (start == end) {
tree[pos] = array[start];
} else {
int mid = (start + end) / 2;
build(start, mid, 2 * pos);
build(mid + 1, end, 2 * pos + 1);
tree[pos] = keyFunc(tree[2 * pos], tree[2 * pos + 1]);
}
}
public void update(int start, int end, int pos, int idx, int x) {
if (start == end) {
array[start] = x;
tree[pos] = x;
} else {
int mid = (start + end) / 2;
if (start <= idx && idx <= mid) {
update(start, mid, 2 * pos, idx, x);
} else {
update(mid + 1, end, 2 * pos + 1, idx, x);
}
tree[pos] = keyFunc(tree[2 * pos], tree[2 * pos + 1]);
}
}
public int query(int start, int end, int pos, int l, int r) {
if (start > r || end < l) {
return DEFAULT;
}
if (l <= start && end <= r) {
return tree[pos];
} else {
int mid = (start + end) / 2;
int a = query(start, mid, 2 * pos, l, r);
int b = query(mid + 1, end, 2 * pos + 1, l, r);
return keyFunc(a, b);
}
}
public void printTree() {
for (int i = 0; i <= 2 * array.length; i++) {
System.out.println(i + " " + tree[i]);
}
System.out.println();
}
public int keyFunc(int a, int b) {
return a + b;
}
}
static class SegmentTree_LazyPropagation {
static int DEFAULT = 1;
int tree[];
int[] lazy_value;
public void flush() {
for (int i = 0; i < tree.length; i++) {
tree[i] = 0;
lazy_value[i] = 0;
}
}
public SegmentTree_LazyPropagation(int si) {
tree = new int[4 * si];
lazy_value = new int[4 * si];
}
public int query(int start, int end, int pos, int l, int r) {
if (end < l || r < start) {
return DEFAULT;
}
if (toBeUpdated(pos)) {
updateFunction(pos, lazy_value[pos]);
if (start != end) {
lazyUpdate(2 * pos, lazy_value[pos]);
lazyUpdate(2 * pos + 1, lazy_value[pos]);
}
lazy_value[pos] = 0;
}
if (end < l || r < start) {
return DEFAULT;
} else if (l <= start && end <= r) {
return tree[pos];
} else {
int mid = (start + end) / 2;
int a = query(start, mid, 2 * pos, l, r);
int b = query(mid + 1, end, 2 * pos + 1, l, r);
return keyFunction(a, b);
}
}
public void update(int start, int end, int pos, int l, int r, int d) {
if (start > r || end < l) {
return;
}
if (toBeUpdated(pos)) {
updateFunction(pos, lazy_value[pos]);
if (start != end) {
lazyUpdate(2 * pos, lazy_value[pos]);
lazyUpdate(2 * pos + 1, lazy_value[pos]);
}
lazy_value[pos] = 0;
}
if (l <= start && end <= r) {
updateFunction(pos, d);
if (start != end) {
lazyUpdate(2 * pos, d);
lazyUpdate(2 * pos + 1, d);
}
} else {
int mid = (start + end) / 2;
update(start, mid, 2 * pos, l, r, d);
update(mid + 1, end, 2 * pos + 1, l, r, d);
tree[pos] = keyFunction(tree[2 * pos], tree[2 * pos + 1]);
}
}
private int keyFunction(int a, int b) {
return a & b;
}
private void lazyUpdate(int pos, int d) {
lazy_value[pos] = d;
}
private void updateFunction(int pos, int d) {
tree[pos] = d;
}
private boolean toBeUpdated(int pos) {
return lazy_value[pos] != 0;
}
}
static class FastIO {
private final BufferedReader br;
private final BufferedWriter bw;
private String s[];
private int index;
private StringBuilder sb;
public FastIO() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
s = br.readLine().split(" ");
sb = new StringBuilder();
index = 0;
}
public int ni() throws IOException {
return Integer.parseInt(nextToken());
}
public double nd() throws IOException {
return Double.parseDouble(nextToken());
}
public long nl() throws IOException {
return Long.parseLong(nextToken());
}
public String next() throws IOException {
return nextToken();
}
public String nli() throws IOException {
try {
return br.readLine();
} catch (IOException ex) {
}
return null;
}
public int[] gia(int n) throws IOException {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public int[] gia(int n, int start, int end) throws IOException {
validate(n, start, end);
int a[] = new int[n];
for (int i = start; i < end; i++) {
a[i] = ni();
}
return a;
}
public double[] gda(int n) throws IOException {
double a[] = new double[n];
for (int i = 0; i < n; i++) {
a[i] = nd();
}
return a;
}
public double[] gda(int n, int start, int end) throws IOException {
validate(n, start, end);
double a[] = new double[n];
for (int i = start; i < end; i++) {
a[i] = nd();
}
return a;
}
public long[] gla(int n) throws IOException {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nl();
}
return a;
}
public long[] gla(int n, int start, int end) throws IOException {
validate(n, start, end);
long a[] = new long[n];
for (int i = start; i < end; i++) {
a[i] = nl();
}
return a;
}
public int[][] gg(int n, int m) throws IOException {
int adja[][] = new int[n + 1][];
int from[] = new int[m];
int to[] = new int[m];
int count[] = new int[n + 1];
for (int i = 0; i < m; i++) {
from[i] = ni();
to[i] = ni();
count[from[i]]++;
count[to[i]]++;
}
for (int i = 0; i <= n; i++) {
adja[i] = new int[count[i]];
}
for (int i = 0; i < m; i++) {
adja[from[i]][--count[from[i]]] = to[i];
adja[to[i]][--count[to[i]]] = from[i];
}
return adja;
}
public void print(String s) throws IOException {
bw.write(s);
}
public void println(String s) throws IOException {
bw.write(s);
bw.newLine();
}
private String nextToken() throws IndexOutOfBoundsException, IOException {
if (index == s.length) {
s = br.readLine().split(" ");
index = 0;
}
return s[index++];
}
private void validate(int n, int start, int end) {
if (start < 0 || end >= n) {
throw new IllegalArgumentException();
}
}
}
static class Data implements Comparable<Data> {
int l, r, q;
public Data(int l, int r, int q) {
this.l = l;
this.r = r;
this.q = q;
}
@Override
public int compareTo(Data o) {
if (l == o.l) {
return Integer.compare(r, o.r);
}
return Integer.compare(l, o.l);
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int p[100005];
int add[100005];
int f[100005];
int first[100005], second[100005], x[100005];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int read() {
int s = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s;
}
int main() {
int n = read(), m = read(), i, j, k;
for (i = 0; i < m; i++) first[i] = read(), second[i] = read(), x[i] = read();
for (i = 0; i <= 30; i++) {
for (j = 1; j <= n + 1; j++) f[j] = j, p[j] = 0;
for (j = 0; j < m; j++)
if ((x[j] & (1 << i)) >> i)
for (k = find(first[j]); k <= second[j]; k = find(f[k]))
p[k] = 1, f[k] = f[k + 1];
for (j = 1; j <= n; j++) add[j] += p[j] * (1 << i), p[j] += p[j - 1];
for (j = 0; j < m; j++)
if (!((x[j] & (1 << i)) >> i))
if (p[second[j]] - p[first[j] - 1] == second[j] - first[j] + 1) {
cout << "NO";
return 0;
}
}
puts("YES");
for (i = 1; i <= n; i++) printf("%d ", add[i]);
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
string A;
int r[1000001], l[1000001], q[1000001], ans[1000001], a[1000001];
struct node {
int x;
void merge(node a, node b) { x = a.x & b.x; }
node() { x = (1ll << 30) - 1; }
} tree[4 * 1000001];
void build(int root, int s, int e) {
if (s == e) {
tree[root].x = a[e];
return;
}
int mid = (s + e) / 2;
build(root * 2, s, mid);
build(root * 2 + 1, mid + 1, e);
tree[root].merge(tree[root * 2], tree[root * 2 + 1]);
}
node query(int root, int l, int r, int i, int j) {
if (l >= i && r <= j) return tree[root];
int mid = (l + r) / 2;
node left = node(), right = node();
if (i <= mid) left = query(2 * root, l, mid, i, j);
if (j > mid) right = query(2 * root + 1, mid + 1, r, i, j);
;
node temp = node();
temp.merge(left, right);
return temp;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < int(m); i++) {
cin >> l[i] >> r[i] >> q[i];
l[i]--;
}
memset(a, 0, sizeof(a));
;
for (int i = 0; i < int(32); i++) {
memset(ans, 0, sizeof(ans));
;
for (int j = 0; j < int(m); j++) {
if (1 & (q[j] >> i)) {
ans[l[j]]++;
ans[r[j]]--;
}
}
for (int j = 0; j < int(n); j++) {
if (j) ans[j] += ans[j - 1];
if (ans[j]) a[j] |= (1 << i);
}
}
build(1, 0, n);
for (int i = 0; i < int(m); i++) {
int temp = query(1, 0, n, l[i], r[i] - 1).x;
if (temp != q[i]) {
cout << "NO";
return 0;
}
}
cout << "YES\n";
for (int i = 0; i < int(n); i++) {
cout << a[i] << " ";
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long int tree[400000] = {0};
long long int lazy[400000] = {0};
void update(long long int node, long long int a, long long int b,
long long int i, long long int j, long long int val) {
if (lazy[node] != 0) {
tree[node] |= lazy[node];
if (a != b) {
lazy[2 * node] |= lazy[node];
lazy[2 * node + 1] |= lazy[node];
}
lazy[node] = 0;
}
if (a > b || i > j || i > b || j < a) return;
if (a >= i && j >= b) {
tree[node] |= val;
if (a != b) {
lazy[2 * node] |= val;
lazy[2 * node + 1] |= val;
}
return;
}
long long int mid = (a + b) / 2;
update(2 * node, a, mid, i, j, val);
update(2 * node + 1, mid + 1, b, i, j, val);
tree[node] = (tree[2 * node] & tree[2 * node + 1]);
}
long long int query(long long int node, long long int a, long long int b,
long long int i, long long int j) {
if (lazy[node] != 0) {
tree[node] |= lazy[node];
if (a != b) {
lazy[2 * node] |= lazy[node];
lazy[2 * node + 1] |= lazy[node];
}
lazy[node] = 0;
}
if (a >= i && j >= b) {
return tree[node];
}
long long int mid = (a + b) / 2;
if (j <= mid)
return query(2 * node, a, mid, i, j);
else if (i > mid)
return query(2 * node + 1, mid + 1, b, i, j);
else {
long long int m1 = query(2 * node, a, mid, i, j);
long long int m2 = query(2 * node + 1, mid + 1, b, i, j);
return m1 & m2;
}
}
int main(int argc, const char* argv[]) {
long long int n, m, x, y, z;
scanf("%I64d", &n);
scanf("%I64d", &m);
vector<pair<pair<long long int, long long int>, long long int> > a;
for (long long int i = 0; i < m; i++) {
scanf("%I64d", &x);
scanf("%I64d", &y);
scanf("%I64d", &z);
x--;
y--;
a.push_back(pair<pair<long long int, long long int>, long long int>(
pair<long long int, long long int>(x, y), z));
update(1, 0, n - 1, x, y, z);
}
long long int flag = 0;
for (long long int i = 0; i < m; i++) {
if (query(1, 0, n - 1, a[i].first.first, a[i].first.second) !=
a[i].second) {
flag = 1;
break;
}
}
if (!flag) {
printf("YES\n");
for (int i = 0; i < n; i++) {
printf("%lld ", query(1, 0, n - 1, i, i));
}
} else {
printf("NO\n");
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long i, j, k, n, m, x, z, t, f, dp[100007][35], cp[100007][35], l[100007],
r[100007], v[100007];
int main() {
cin >> n >> m;
for (i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> v[i];
for (j = 0; j < 32; j++) {
if ((v[i] & (1LL << j)) != 0) {
dp[l[i]][j]++;
dp[r[i] + 1][j]--;
}
}
}
for (i = 1; i <= n; i++) {
for (j = 0; j < 32; j++) {
dp[i][j] += dp[i - 1][j];
}
}
for (i = 1; i <= n; i++) {
for (j = 0; j < 32; j++) {
if (dp[i][j] > 0) dp[i][j] = 1;
cp[i][j] = dp[i][j];
dp[i][j] += dp[i - 1][j];
}
}
bool ok = true;
for (i = 0; i < m; i++) {
for (j = 0; j < 32; j++) {
if ((v[i] & (1LL << j)) != 0) {
x = dp[r[i]][j] - dp[l[i] - 1][j];
if (x != r[i] - l[i] + 1) ok = false;
} else {
x = dp[r[i]][j] - dp[l[i] - 1][j];
if (x == r[i] - l[i] + 1) ok = false;
}
}
}
if (ok) {
cout << "YES\n";
for (i = 1; i <= n; i++) {
x = 0LL;
for (j = 0; j < 32; j++) {
if (cp[i][j] == 1) x |= (1 << j);
}
cout << x << " ";
}
cout << "\n";
} else {
cout << "NO\n";
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author zodiacLeo
*/
public class Main
{
public static void main(String[] args)
{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB
{
private int[] st;
private int[] resultArray;
private int[] queryNum;
private int[] low;
private int[] high;
private int[] left;
private int[] right;
public void solve(int testNumber, Scanner in, PrintWriter out)
{
int n = in.nextInt();
int q = in.nextInt();
left = new int[q];
right = new int[q];
queryNum = new int[q];
resultArray = new int[n];
st = new int[4 * n + 1];
low = new int[4 * n + 1];
high = new int[4 * n + 1];
Arrays.fill(st, Integer.MAX_VALUE);
for (int i = 0; i < q; i++)
{
left[i] = in.nextInt() - 1;
right[i] = in.nextInt() - 1;
queryNum[i] = in.nextInt();
}
for (int pos = 0; pos <= 30; pos++)
{
int[] sum = new int[n];
for (int j = 0; j < q; j++)
{
if ((queryNum[j] & (1 << pos)) > 0)
{
sum[left[j]]++;
if (right[j] + 1 < n)
{
sum[right[j] + 1]--;
}
}
}
for (int i = 0; i < n; i++)
{
if (i > 0)
{
sum[i] += sum[i - 1];
}
if (sum[i] > 0)
{
resultArray[i] |= (1 << pos);
}
}
}
init(1, 0, n - 1);
StringBuffer st = new StringBuffer("");
for (int i = 0; i < q; i++)
{
int result = raq(left[i], right[i]);
if (result != queryNum[i])
{
out.println("NO");
return;
}
}
for (int i = 0; i < n; i++)
{
st.append(resultArray[i] + " ");
}
out.println("YES");
out.println(st.toString());
}
public void init(int index, int a, int b)
{
low[index] = a;
high[index] = b;
if (a == b)
{
st[index] = resultArray[a];
return;
}
int left = 2 * index;
int right = left + 1;
int m = a + b;
m /= 2;
Integer ans = Integer.MAX_VALUE;
init(left, a, m);
init(right, m + 1, b);
st[index] = (ans & st[left] & st[right]);
}
public int raq(int a, int b)
{
int left = Math.min(a, b);
int right = Math.max(a, b);
return raq(1, left, right);
}
public int raq(int index, int a, int b)
{
if (a > high[index] || b < low[index])
{
return Integer.MAX_VALUE;
}
if (a <= low[index] && b >= high[index])
{
return st[index];
}
int left = 2 * index;
int right = left + 1;
int leftNum = raq(left, a, b);
int rightNum = raq(right, a, b);
return leftNum & rightNum;
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long maxn = 3e5;
const long long mod = 1e9 + 7;
const long double PI = acos((long double)-1);
long long pw(long long a, long long b, long long md = mod) {
long long res = 1;
while (b) {
if (b & 1) {
res = (a * res) % md;
}
a = (a * a) % md;
b >>= 1;
}
return (res);
}
long long n, m;
long long l[maxn], r[maxn], q[maxn];
long long fen[maxn][32];
long long b[maxn][32];
void udp(long long pos, long long x, long long ind) {
for (; pos < maxn; pos += pos & (-pos)) fen[pos][ind] += x;
}
long long sum(long long pos, long long x) {
long long ans = 0;
for (; pos; pos -= pos & (-pos)) ans += fen[pos][x];
return ((ans) ? (1 << x) : 0);
}
void add(long long x) {
cin >> l[x] >> r[x] >> q[x];
for (long long i = 0; i <= 30; i++)
if (q[x] & (1 << i)) udp(l[x], 1, i), udp(r[x] + 1, -1, i);
}
long long get(long long l, long long r) {
long long ans = b[l][0];
for (long long i = 17; i >= 0; i--)
if (l + (1LL << i) - 1 <= r) ans &= b[l][i], l += (1LL << i);
return (ans);
}
bool chk(long long x) { return (get(l[x], r[x]) == q[x]); }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (long long i = 0; i < m; i++) add(i);
for (long long i = 1; i <= n; i++)
for (long long j = 0; j <= 30; j++) b[i][0] |= sum(i, j);
for (long long i = 1; i <= 17; i++)
for (long long j = 1; j <= n; j++)
if (j + (1LL << i) - 1 <= n)
b[j][i] = b[j][i - 1] & b[j + (1LL << (i - 1))][i - 1];
for (long long i = 0; i < m; i++)
if (!chk(i)) return (cout << "NO", 0);
;
cout << "YES" << '\n';
for (long long i = 1; i <= n; i++) cout << b[i][0] << ' ';
return (0);
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 100001, B = 30;
int l[N], r[N], q[N], a[N], t[4 * N], sum[N];
inline void build(int v, int l, int r) {
if (l + 1 == r) {
t[v] = a[l];
return;
}
int mid = (l + r) >> 1;
build(v * 2, l, mid);
build(v * 2 + 1, mid, r);
t[v] = t[v * 2] & t[v * 2 + 1];
}
inline int query(int v, int l, int r, int L, int R) {
if (l == L && r == R) {
return t[v];
}
int mid = (L + R) >> 1;
int ans = (1ll << B) - 1;
if (l < mid) ans &= query(v * 2, l, std::min(r, mid), L, mid);
if (mid < r) ans &= query(v * 2 + 1, std::max(l, mid), r, mid, R);
return ans;
}
void solve() {
int n, m;
scanf("%d %d\n", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d %d\n", &l[i], &r[i], &q[i]);
l[i]--;
}
for (int bit = 0; bit < B; bit++) {
memset(sum, 0, 4 * N);
for (int i = 0; i < m; i++) {
if ((q[i] >> bit) & 1) {
sum[l[i]]++;
sum[r[i]]--;
}
}
for (int i = 0; i < n; i++) {
if (i) sum[i] += sum[i - 1];
if (sum[i]) {
a[i] |= (1 << bit);
}
}
}
build(1, 0, n);
for (int i = 0; i < m; i++) {
if (query(1, l[i], r[i], 0, n) != q[i]) {
puts("NO");
return;
}
}
puts("YES");
for (int i = 0; i < n; i++) {
printf("%d%c", a[i], " \n"[i == n - 1]);
}
}
int main() {
solve();
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int R = 1e9 + 7;
template <typename T>
T last(vector<T>& v) {
return v[v.size() - 1];
}
template <typename T>
void print(vector<T>& v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << ' ';
}
cout << endl;
}
template <typename T1, typename T2>
void print(vector<pair<T1, T2>>& v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
cout << '(' << v[i].first << ',' << v[i].second << "),";
}
cout << "]" << endl;
}
template <typename T>
void print(vector<vector<T>>& v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
print(v[i]);
}
cout << "]" << endl;
}
template <typename T>
void print(set<T>& v) {
cout << "{";
for (auto i : v) {
cout << i << ' ';
}
cout << "}" << endl;
}
template <typename T1, typename T2>
void print(map<T1, T2>& v) {
cout << "{";
for (auto i : v) {
cout << i.first << ':' << i.second << ',';
}
cout << "}" << endl;
}
template <typename T>
bool in(set<T>& indices, T x) {
return indices.find(x) != indices.end();
}
template <typename T, typename T_>
bool in(map<T, T_>& indices, T x) {
return indices.find(x) != indices.end();
}
int power(int x, int y) {
int res = 1;
x = x % R;
if (x == 0) return 0;
while (y > 0) {
if (y % 2 == 1) res = (res * x) % R;
y /= 2;
x = (x * x) % R;
}
return res;
}
bool compare(int a, int b) { return (a < b); }
vector<int> t;
void build(vector<int>& a, int v, int tl, int tr) {
if (tl == tr) {
t[v] = a[tl];
} else {
int tm = (tl + tr) / 2;
build(a, v * 2, tl, tm);
build(a, v * 2 + 1, tm + 1, tr);
t[v] = t[v * 2] & t[v * 2 + 1];
}
}
int sum(int v, int tl, int tr, int l, int r) {
if (l > r) return (1 << 30) - 1;
if (l == tl && r == tr) {
return t[v];
}
int tm = (tl + tr) / 2;
return sum(v * 2, tl, tm, l, min(r, tm)) &
sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}
void TestCase() {
int n, m;
cin >> n >> m;
t.resize(4 * n);
vector<pair<pair<int, int>, int>> q(m);
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r >> q[i].second;
q[i].first.first = l - 1;
q[i].first.second = r - 1;
}
vector<vector<int>> v(n + 1, vector<int>(30));
for (int i = 0; i < 30; i++) {
for (int j = 0; j < m; j++) {
if ((q[j].second & (1 << i)) > 0) {
v[q[j].first.first][i]++;
v[q[j].first.second + 1][i]--;
}
}
}
for (int i = 0; i < 30; i++) {
for (int j = 1; j < n; j++) {
v[j][i] += v[j - 1][i];
}
}
vector<int> ans(n);
for (int i = 0; i < 30; i++) {
for (int j = 0; j < n; j++) {
if (v[j][i] > 0) {
ans[j] = ans[j] | (1 << i);
}
}
}
build(ans, 1, 0, n - 1);
for (int i = 0; i < m; i++) {
int sum_ = q[i].second;
int l = q[i].first.first;
int r = q[i].first.second;
if (sum_ != sum(1, 0, n - 1, l, r)) {
cout << "NO" << '\n';
return;
}
}
cout << "YES" << '\n';
print(ans);
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
T = 1;
for (int tt = 1; tt <= T; tt++) {
TestCase();
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.*;
import java.util.*;
import java.util.List;
public class B {
private static StringTokenizer st;
private static BufferedReader br;
public static int MOD = 1000000007;
public static long tenFive = 100000;
public static void print(Object x) {
System.out.println(x + "");
}
public static void printArr(long[] x) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < x.length; i++) {
s.append(x[i] + " ");
}
print(s);
}
public static String join(List<?> x, String space) {
StringBuilder sb = new StringBuilder();
for (Object elt : x) {
sb.append(elt);
sb.append(space);
}
return sb.toString();
}
public static String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
st = new StringTokenizer(line.trim());
}
return st.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static List<Integer> nextInts(int N) throws IOException {
List<Integer> ret = new ArrayList<Integer>();
for (int i = 0; i < N; i++) {
ret.add(nextInt() - 1);
}
return ret;
}
public static long[] cumulative(long[] values) {
int n = values.length;
long cur = 0;
long[] cumulative = new long[n];
for (int i = 0; i < n; i++) {
cur += values[i];
cumulative[i] = cur;
}
return cumulative;
}
public static int[] solve(int n, int m, int[] lArr, int[] rArr, int[] qArr) {
int[] result = new int[n];
for (int bit = 0; bit < 31; bit++) {
long[] constraints = new long[n];
for (int query = 0; query < m; query++) {
if (((qArr[query] >> bit) & 1) == 0) continue;
int l = lArr[query];
int r = rArr[query];
constraints[l] += 1;
if (r < n - 1) constraints[r + 1] -= 1;
}
//print(bit);
//printArr(constraints);
constraints = cumulative(constraints);
long[] zeroes = new long[n];
for (int i = 0; i < n; i++) {
if (constraints[i] == 0) zeroes[i] = 1;
}
//printArr(zeroes);
long[] cumZeroes = cumulative(zeroes);
for (int query = 0; query < m; query++) {
if (((qArr[query] >> bit) & 1) == 1) continue;
int l = lArr[query];
int r = rArr[query];
long leftZeroes = (l == 0) ? 0 : cumZeroes[l - 1];
if (cumZeroes[r] - leftZeroes == 0) return null;
}
for (int i = 0; i < n; i++) {
result[i] |= (1 - zeroes[i]) << bit;
}
}
return result;
}
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
int n = nextInt();
int m = nextInt();
int[] lArr = new int[m];
int[] rArr = new int[m];
int[] qArr = new int[m];
for (int i = 0; i < m; i++) {
lArr[i] = nextInt() - 1;
rArr[i] = nextInt() - 1;
qArr[i] = nextInt();
}
int[] result = solve(n, m, lArr, rArr, qArr);
if (result == null) {
print("NO");
} else {
print("YES");
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(result[i]);
s.append(" ");
}
print(s);
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m;
int l[100005], r[100005], x[100005], ans[100005];
struct node {
int val, tag;
} t[400005];
void pushup(int root) { t[root].val = t[root << 1].val & t[root << 1 | 1].val; }
void pushdown(int root, int l, int r) {
int x = t[root].tag;
t[root].tag = 0;
if (x == 0) return;
t[root << 1].val |= x, t[root << 1].tag |= x;
t[root << 1 | 1].val |= x, t[root << 1 | 1].tag |= x;
}
void build(int root, int l, int r) {
t[root].tag = 0;
if (l == r) {
t[root].val = 0;
return;
}
int mid = (l + r) >> 1;
build(root << 1, l, mid), build(root << 1 | 1, mid + 1, r), pushup(root);
}
void updata(int root, int l, int r, int a, int b, int k) {
if (l > b || r < a) return;
if (l >= a && r <= b) {
t[root].val |= k;
t[root].tag |= k;
return;
}
int mid = (l + r) >> 1;
pushdown(root, l, r);
updata(root << 1, l, mid, a, b, k),
updata(root << 1 | 1, mid + 1, r, a, b, k), pushup(root);
}
int getand(int root, int l, int r, int a, int b) {
if (l > b || r < a) return (1ll << 31) - 1;
if (l >= a && r <= b) return t[root].val;
int mid = (l + r) >> 1;
pushdown(root, l, r);
return getand(root << 1, l, mid, a, b) &
getand(root << 1 | 1, mid + 1, r, a, b);
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
memset(ans, 0, sizeof(ans));
build(1, 1, n);
for (int i = 1; i <= m; i++) {
l[i] = read(), r[i] = read(), x[i] = read();
updata(1, 1, n, l[i], r[i], x[i]);
}
int flag = 1;
for (int i = 1; i <= m; i++) {
int k = getand(1, 1, n, l[i], r[i]);
if (k != x[i]) {
flag = 0;
break;
}
}
if (flag) {
printf("YES\n");
for (int i = 1; i <= n; i++) printf("%d ", getand(1, 1, n, i, i));
printf("\n");
} else {
printf("NO\n");
}
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | //package codeforces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
public class B {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
StringTokenizer stringTokenizer;
B() throws IOException {
// reader = new BufferedReader(new FileReader("cycle2.in"));
// writer = new PrintWriter(new FileWriter("cycle2.out"));
}
String next() throws IOException {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
int[][] mul(int[][] a, int[][] b) {
int[][] result = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
result[i][j] = sum(result[i][j], product(a[i][k], b[k][j]));
}
}
}
return result;
}
int[][] pow(int[][] a, int k) {
int[][] result = {{1, 0}, {0, 1}};
while (k > 0) {
if (k % 2 == 1) {
result = mul(result, a);
}
a = mul(a, a);
k /= 2;
}
return result;
}
final int MOD = 1000 * 1000 * 1000 + 7;
int sum(int a, int b) {
a += b;
return a >= MOD ? a - MOD : a;
}
int product(int a, int b) {
return (int) (1l * a * b % MOD);
}
@SuppressWarnings("unchecked")
void solve() throws IOException {
final int n = nextInt();
class Constraint {
int l, r, q;
Constraint(int l, int r, int q) {
this.l = l;
this.r = r;
this.q = q;
}
}
Constraint[] constraints = new Constraint[nextInt()];
for(int i = 0; i < constraints.length; i++) {
constraints[i] = new Constraint(nextInt(), nextInt(), nextInt());
}
List<List<Integer>> openings = new ArrayList<>(n + 1);
List<List<Integer>> closings = new ArrayList<>(n + 1);
for(int i = 0; i <= n; i++) {
openings.add(new ArrayList<Integer>());
closings.add(new ArrayList<Integer>());
}
for (Constraint constraint : constraints) {
openings.get(constraint.l).add(constraint.q);
closings.get(constraint.r).add(constraint.q);
}
int[] bc = new int[30];
int[] answer = new int[n];
int[][] zc = new int[n + 1][30];
for(int i = 1; i <= n; i++) {
for (int x : openings.get(i)) {
for(int j = 0; j < 30; j++) {
bc[j] += (x >> j) & 1;
}
}
for(int j = 0; j < 30; j++) {
zc[i][j] = zc[i - 1][j];
if(bc[j] > 0) {
answer[i - 1] += 1 << j;
} else {
zc[i][j]++;
}
}
for (int x : closings.get(i)) {
for(int j = 0; j < 30; j++) {
bc[j] -= (x >> j) & 1;
}
}
}
for (Constraint constraint : constraints) {
for(int j = 0; j < 30; j++) {
if((constraint.q >> j) % 2 == 0) {
if(zc[constraint.r][j] - zc[constraint.l - 1][j] == 0) {
System.out.println("NO");
return;
}
}
}
}
writer.println("YES");
for (int i : answer) {
writer.print(i + " ");
}
writer.close();
}
public static void main(String[] args) throws IOException {
new B().solve();
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int max_n = 100010;
int arr[31][max_n];
long long q[3][max_n];
int sum[31][max_n];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
long long r, l, val;
cin >> l >> r >> val;
q[0][i] = l;
q[1][i] = r;
q[2][i] = val;
for (int j = 0; j < 30; j++) {
if (val & (1ll << j)) {
arr[j][l]++;
arr[j][r + 1]--;
}
}
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < 30; j++) {
arr[j][i] += arr[j][i - 1];
if (arr[j][i]) sum[j][i] = sum[j][i - 1] + 1;
}
for (int i = 0; i < m; i++) {
long long l = q[0][i];
long long r = q[1][i];
long long val = q[2][i];
long long size = r - l + 1;
for (int j = 0; j < 30; j++) {
if (!(val & (1ll << j))) {
if (sum[j][r] - sum[j][l - 1] == size) {
cout << "NO" << endl;
return 0;
}
}
}
}
cout << "YES" << endl;
for (int i = 1; i <= n; i++) {
long long ans = 0;
for (int j = 0; j < 30; j++)
if (arr[j][i]) {
ans += (1ll << j);
}
cout << ans << " ";
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100050;
int a[maxn][32];
int l[maxn], r[maxn], q[maxn];
int cnt[maxn][32];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) scanf("%d%d%d", &l[i], &r[i], &q[i]);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < 30; ++j) {
if (q[i] & (1 << j)) {
++a[l[i]][j];
--a[r[i] + 1][j];
}
}
}
for (int i = 1; i <= n; ++i)
for (int j = 0; j < 30; ++j) {
a[i][j] += a[i - 1][j];
cnt[i][j] = cnt[i - 1][j] + (a[i][j] == 0);
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < 30; ++j) {
if ((!(q[i] & (1 << j))) && (cnt[r[i]][j] - cnt[l[i] - 1][j] == 0)) {
printf("NO\n");
return 0;
}
}
}
printf("YES\n");
for (int i = 1; i <= n; ++i) {
int tmp = 0;
for (int j = 0; j < 30; ++j) {
if (a[i][j]) {
tmp |= (1 << j);
}
}
printf("%d", tmp);
putchar(i == n ? '\n' : ' ');
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 10 + 100000;
const int INF = int(1e9);
int n, m;
int l[MAXN], r[MAXN], q[MAXN];
void input() {
({
int neg = 0;
n = 0;
char ch;
for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') neg = 1;
n = ch - 48;
for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
n = (n << 3) + (n << 1) + (ch - 48);
if (neg) n = -n;
});
({
int neg = 0;
m = 0;
char ch;
for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') neg = 1;
m = ch - 48;
for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
m = (m << 3) + (m << 1) + (ch - 48);
if (neg) m = -m;
});
for (int i = 1; i <= m; i++) {
({
int neg = 0;
l[i] = 0;
char ch;
for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') neg = 1;
l[i] = ch - 48;
for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
l[i] = (l[i] << 3) + (l[i] << 1) + (ch - 48);
if (neg) l[i] = -l[i];
});
({
int neg = 0;
r[i] = 0;
char ch;
for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') neg = 1;
r[i] = ch - 48;
for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
r[i] = (r[i] << 3) + (r[i] << 1) + (ch - 48);
if (neg) r[i] = -r[i];
});
({
int neg = 0;
q[i] = 0;
char ch;
for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') neg = 1;
q[i] = ch - 48;
for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
q[i] = (q[i] << 3) + (q[i] << 1) + (ch - 48);
if (neg) q[i] = -q[i];
});
}
}
int getbit(int x, int k) { return (x >> k) & 1; }
int onbit(int x, int k) { return x | (1 << k); }
int ans[MAXN], sum[MAXN];
int f[MAXN * 4];
void update(int p, int l, int r) {
if (l == r) {
f[p] = ans[l];
return;
}
int mid = (l + r) / 2;
update(p * 2, l, mid);
update(p * 2 + 1, mid + 1, r);
f[p] = f[p * 2] & f[p * 2 + 1];
}
int get(int p, int l, int r, int a, int b) {
if (r < a || b < l) return (1 << 30) - 1;
if (a <= l && r <= b) return f[p];
int mid = (l + r) / 2;
int p1 = get(p * 2, l, mid, a, b);
int p2 = get(p * 2 + 1, mid + 1, r, a, b);
return p1 & p2;
}
void output() {
for (int k = 0; k <= 30; k++) {
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= m; i++)
if (getbit(q[i], k)) {
sum[l[i]]++;
sum[r[i] + 1]--;
}
for (int i = 1; i <= n; i++) {
sum[i] += sum[i - 1];
if (sum[i] > 0) ans[i] = onbit(ans[i], k);
}
}
update(1, 1, n);
for (int i = 1; i <= m; i++) {
if (get(1, 1, n, l[i], r[i]) != q[i]) {
puts("NO");
return;
}
}
puts("YES");
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
int main() {
input();
output();
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int dr[] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int dc[] = {0, 0, 1, -1, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int N = 1e5 + 5;
const long double pi = acos(-1);
const double eps = 1e-9;
struct query {
int l, r, q;
};
bool cmp(query x, query y) {
if (x.l == y.l) return x.r < y.r;
return x.l < y.l;
}
int t, n, m, bit[35][N], push_front[35][N], ans[N], pow2[35];
query a[N];
vector<pair<int, int> > lst[35];
bool flag = 1;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
t = 1;
pow2[0] = 1;
for (long long i = 1; i <= 30; i++) {
pow2[i] = pow2[i - 1] * 2;
}
while (t--) {
cin >> n >> m;
for (long long i = 1; i <= m; i++) {
cin >> a[i].l >> a[i].r >> a[i].q;
}
sort(a + 1, a + 1 + m, cmp);
for (long long i = 1; i <= m; i++) {
int tmp = a[i].q;
for (int j = 0; j <= 30; j++) {
if (tmp & 1) lst[j].push_back({a[i].l, a[i].r});
tmp >>= 1;
}
}
for (long long i = 0; i <= 30; i++) {
int sz = lst[i].size(), le, ri;
if (sz > 0) {
le = lst[i][0].first, ri = lst[i][0].second;
for (int j = le; j <= ri; j++) {
bit[i][j] = 1;
}
for (int j = 1; j < sz; j++) {
le = max(ri, lst[i][j].first);
ri = max(ri, lst[i][j].second);
for (int k = le; k <= ri; k++) {
bit[i][k] = 1;
}
}
}
}
for (long long i = 0; i <= 30; i++) {
for (long long j = 1; j <= n; j++) {
push_front[i][j] = push_front[i][j - 1];
push_front[i][j] += bit[i][j];
}
}
for (long long i = 1; i <= m; i++) {
int tmp = 0;
for (int j = 0; j <= 30; j++) {
if (push_front[j][a[i].r] - push_front[j][a[i].l - 1] ==
a[i].r - a[i].l + 1) {
tmp += pow2[j];
}
}
if (tmp != a[i].q) {
flag = 0;
break;
}
}
if (!flag)
cout << "NO" << '\n';
else {
for (long long i = 1; i <= n; i++) {
for (long long j = 0; j <= 30; j++) {
if (bit[j][i]) ans[i] += pow2[j];
}
}
cout << "YES" << '\n';
for (long long i = 1; i <= n; i++) {
if (i == 1)
cout << ans[i];
else
cout << ' ' << ans[i];
}
cout << '\n';
}
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 7;
const int MXN = (int)1e5 + 7;
int n, m;
int a[MXN][31], s[MXN][31], ps[MXN][31];
int l[MXN], r[MXN], q[MXN];
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> l[i] >> r[i] >> q[i];
for (int b = 0; b < 30; b++) {
if (q[i] & (1 << b)) {
a[l[i]][b]++;
a[r[i] + 1][b]--;
}
}
}
for (int i = 1; i <= n; i++)
for (int b = 0; b < 30; b++) s[i][b] = s[i - 1][b] + a[i][b];
for (int i = 1; i <= n; i++)
for (int b = 0; b < 30; b++)
if (s[i][b] > 0) s[i][b] = 1;
for (int i = 1; i <= n; i++)
for (int b = 0; b < 30; b++) ps[i][b] = ps[i - 1][b] + s[i][b];
for (int i = 1; i <= m; i++) {
for (int b = 0; b < 30; b++) {
int sum = ps[r[i]][b] - ps[l[i] - 1][b];
if (q[i] & (1 << b)) {
if (sum != r[i] - l[i] + 1) {
cout << "NO";
return 0;
}
} else {
if (sum == r[i] - l[i] + 1) {
cout << "NO";
return 0;
}
}
}
}
cout << "YES\n";
for (int i = 1; i <= n; i++) {
int cur = 0;
for (int b = 0; b < 30; b++)
if (s[i][b]) cur += (1 << b);
cout << cur << ' ';
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | # Legends Always Come Up with Solution
# Author: Manvir Singh
import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations, permutations
from bisect import *
from fractions import Fraction
from heapq import *
from random import randint
def main():
po=[1]
for i in range(30):
po.append(po[-1]*2)
n,m=map(int,input().split())
q=[]
b=[[0 for _ in range(30)] for _ in range(n+2)]
for i in range(m):
l,r,x=map(int,input().split())
q.append((l,r,x))
j=0
while x:
if x&1:
b[l][j]+=1
b[r+1][j]-=1
x=x>>1
j+=1
for i in range(1,n+1):
for j in range(30):
b[i][j]+=b[i-1][j]
for i in range(1,n+1):
for j in range(30):
if b[i][j]>=2:
b[i][j]=1
b[i][j]+=b[i-1][j]
f=1
for i in q:
l,r,x=i
z=0
for j in range(30):
if b[r][j]-b[l-1][j]==(r-l+1):
z+=po[j]
if z!=x:
f=0
break
if f:
print("YES")
for i in range(1,n+1):
z=0
for j in range(30):
if b[i][j]-b[i-1][j]==1:
z+=po[j]
print(z,end=" ")
else:
print("NO")
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main() | PYTHON3 |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
struct pot {
int x, y, d[31];
};
pot a[200020];
int dig[200020][30], sum[200020][30], n, ans[200020], m, k, l;
bool f;
bool cmp(pot u, pot v) { return (u.x < v.x); }
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &a[i].x, &a[i].y, &k);
for (int j = 0; j < 30; j++) {
a[i].d[j] = k % 2;
k = k / 2;
}
}
sort(a + 1, a + m + 1, cmp);
memset(dig, 0, sizeof(dig));
for (int i = 0; i < 30; i++) {
l = 1;
for (int j = 1; j <= m; j++)
if (a[j].d[i]) {
for (int k = max(l, a[j].x); k <= a[j].y; k++) dig[k][i] = 1;
l = max(l, a[j].y);
}
sum[0][i] = 0;
for (int j = 1; j <= n; j++) sum[j][i] = sum[j - 1][i] + (1 - dig[j][i]);
for (int j = 1; j <= m; j++)
if (!a[j].d[i] && sum[a[j].y][i] - sum[a[j].x - 1][i] == 0) {
printf("NO\n");
return 0;
}
}
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= n; i++)
for (int j = 0; j < 30; j++) ans[i] += dig[i][j] * (1 << j);
printf("YES\n");
for (int i = 1; i < n; i++) printf("%d ", ans[i]);
printf("%d\n", ans[n]);
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m;
long long lo[4 * 100100 + 1], hi[4 * 100100 + 1], tree[4 * 100100 + 1],
delta[4 * 100100 + 1];
void init(int i, int l, int r) {
lo[i] = l;
hi[i] = r;
if (l == r) return;
int m = (l + r) / 2;
init(2 * i, l, m);
init(2 * i + 1, m + 1, r);
}
void prop(int i) {
delta[2 * i] |= delta[i];
delta[2 * i + 1] |= delta[i];
delta[i] = 0;
}
void update(int i) {
tree[i] = (tree[2 * i] | delta[2 * i]) & (tree[2 * i + 1] | delta[2 * i + 1]);
}
void inc(int i, int l, int r, int val) {
if (r < lo[i] || hi[i] < l) return;
if (hi[i] <= r && lo[i] >= l) {
delta[i] |= val;
return;
}
prop(i);
inc(2 * i, l, r, val);
inc(2 * i + 1, l, r, val);
update(i);
}
int query(int i, int l, int r) {
if (r < lo[i] || hi[i] < l) return (1LL << 31) - 1;
if (hi[i] <= r && lo[i] >= l) return tree[i] | delta[i];
prop(i);
int minLeft = query(2 * i, l, r);
int minRight = query(2 * i + 1, l, r);
update(i);
return minLeft & minRight;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < 4 * 100100 + 1; i++) {
tree[i] = delta[i] = 0;
}
init(1, 0, n);
int a[100100][3];
for (int i = 0; i < m; i++) {
cin >> a[i][0] >> a[i][1] >> a[i][2];
inc(1, a[i][0], a[i][1], a[i][2]);
}
for (int i = 0; i < m; i++) {
if (query(1, a[i][0], a[i][1]) != a[i][2]) {
cout << "NO";
return 0;
}
}
cout << "YES\n";
for (int i = 0; i < n; i++) {
cout << query(1, i + 1, i + 1) << " ";
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int mn = 1e5 + 1;
int n, m, tree[mn * 4][30];
struct query {
int l, r, k;
} queries[mn];
void update(int id, int l, int r, int x, int y, int type) {
if (l > y || r < x) {
return;
}
if (l >= x && r <= y) {
tree[id][type] = r - l + 1;
return;
}
int mid = (l + r) / 2;
if (tree[id][type] == r - l + 1) {
tree[id * 2][type] = mid - l + 1;
tree[id * 2 + 1][type] = r - mid;
}
update(id * 2, l, mid, x, y, type);
update(id * 2 + 1, mid + 1, r, x, y, type);
tree[id][type] = tree[id * 2][type] + tree[id * 2 + 1][type];
}
int get(int id, int l, int r, int x, int y, int type) {
if (l > y || r < x) {
return 0;
}
if (l >= x && r <= y) {
return tree[id][type];
}
int mid = (l + r) / 2;
if (tree[id][type] == r - l + 1) {
tree[id * 2][type] = mid - l + 1;
tree[id * 2 + 1][type] = r - mid;
}
return get(id * 2, l, mid, x, y, type) +
get(id * 2 + 1, mid + 1, r, x, y, type);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> queries[i].l >> queries[i].r >> queries[i].k;
for (int j = 0; j < 30; ++j) {
if ((queries[i].k >> j) & 1)
update(1, 1, n, queries[i].l, queries[i].r, j);
}
}
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 30; j++) {
if (!((queries[i].k >> j) & 1)) {
if (get(1, 1, n, queries[i].l, queries[i].r, j) ==
queries[i].r - queries[i].l + 1) {
cout << "NO";
return 0;
}
}
}
}
cout << "YES\n";
for (int i = 1; i <= n; i++) {
int ans = 0;
for (int j = 0; j < 30; j++) {
ans += get(1, 1, n, i, i, j) * (1 << j);
}
cout << ans << " ";
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = in.nextInt(), m = in.nextInt();
int[] a = new int[n + 1], l = new int[m], r = new int[m], q = new int[m];
for (int i = 0; i < m; i++) {
l[i] = in.nextInt();
r[i] = in.nextInt();
q[i] = in.nextInt();
}
boolean ok = true;
for (int i = 0; i < 30 && ok; i++) {
int[] sum = new int[n + 2], sum2 = new int[n + 2];
for (int j = 0; j < m; j++) {
if ((q[j] >> i & 1) == 1) {
sum[l[j]]++;
sum[r[j] + 1]--;
}
}
for (int j = 1; j <= n; j++) {
sum[j] += sum[j - 1];
sum2[j] = sum2[j - 1] + (sum[j] == 0 ? 1 : 0);
}
for (int j = 0; j < m; j++)
if ((q[j] & (1 << i)) == 0 && sum2[r[j]] - sum2[l[j] - 1] == 0)
ok = false;
for (int j = 1; j <= n && ok; j++)
a[j] |= (sum[j] == 0 ? 0 : 1) << i;
}
if (!ok)
out.println("NO");
else {
out.println("YES");
for (int i = 1; i <= n; i++)
out.print(a[i] + (i == n ? "\n" : " "));
}
out.flush();
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
const int MAXN = 1e5 + 10;
const int MOD = 2333;
const double eps = 1e-6;
using namespace std;
int bit[MAXN << 2], AND[MAXN << 2], l[MAXN], r[MAXN], q[MAXN];
void pushdown(int rt) {
if (bit[rt]) {
bit[rt << 1] |= bit[rt];
bit[rt << 1 | 1] |= bit[rt];
AND[rt << 1] |= bit[rt];
AND[rt << 1 | 1] |= bit[rt];
bit[rt] = 0;
}
}
void pushup(int rt) { AND[rt] = AND[rt << 1] & AND[rt << 1 | 1]; }
void updata(int num, int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
bit[rt] |= num;
AND[rt] |= num;
return;
}
pushdown(rt);
int m = l + r >> 1;
if (L <= m) updata(num, L, R, l, m, rt << 1);
if (m < R) updata(num, L, R, m + 1, r, rt << 1 | 1);
pushup(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return AND[rt];
pushdown(rt);
int m = l + r >> 1;
int res = (1 << 30) - 1;
if (L <= m) res &= query(L, R, l, m, rt << 1);
if (m < R) res &= query(L, R, m + 1, r, rt << 1 | 1);
return res;
}
void solve(int l, int r, int rt) {
if (l == r) {
cout << AND[rt] << " ";
return;
}
pushdown(rt);
int m = l + r >> 1;
solve(l, m, rt << 1);
solve(m + 1, r, rt << 1 | 1);
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> l[i] >> r[i] >> q[i];
updata(q[i], l[i], r[i], 1, n, 1);
}
for (int i = 0; i < m; ++i)
if (query(l[i], r[i], 1, n, 1) != q[i]) return cout << "NO", 0;
cout << "YES" << endl;
solve(1, n, 1);
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
struct SegTree {
int n;
vector<unsigned int> t1, t2;
SegTree(int n_) {
n = 1;
while (n < n_) n <<= 1;
t1.resize(2 * n - 1, 0);
t2.resize(2 * n - 1, 0);
}
void update(int k) {
t1[k] = (t1[2 * k + 1] | t2[2 * k + 1]) & (t1[2 * k + 2] | t2[2 * k + 2]);
}
void eval(int k) {
t2[2 * k + 1] |= t2[k];
t2[2 * k + 2] |= t2[k];
t2[k] = 0;
}
void or_(int a, int b, unsigned int v, int k, int l, int r) {
if (b <= l || r <= a) {
return;
} else if (a <= l && r <= b) {
t2[k] |= v;
return;
} else {
eval(k);
or_(a, b, v, 2 * k + 1, l, (l + r) / 2);
or_(a, b, v, 2 * k + 2, (l + r) / 2, r);
update(k);
}
}
void or_(int a, int b, long long v) { or_(a, b, v, 0, 0, n); }
unsigned int query(int a, int b, int k, int l, int r) {
if (b <= l || r <= a) {
return -1;
} else if (a <= l && r <= b) {
return t1[k] | t2[k];
} else {
eval(k);
long long ans = query(a, b, 2 * k + 1, l, (l + r) / 2) &
query(a, b, 2 * k + 2, (l + r) / 2, r);
update(k);
return ans;
}
}
unsigned int query(int a, int b) { return query(a, b, 0, 0, n); }
};
const int MAX_M = 110000;
int n, m;
int l[MAX_M], r[MAX_M], q[MAX_M];
int main() {
scanf("%d%d", &n, &m);
SegTree st(n);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &l[i], &r[i], &q[i]);
l[i]--;
}
for (int i = 0; i < m; i++) {
st.or_(l[i], r[i], q[i]);
}
bool ok = true;
for (int i = 0; i < m; i++) {
int x = st.query(l[i], r[i]);
if ((x ^ q[i]) != 0) ok = false;
}
printf("%s\n", (ok ? "YES " : "NO"));
if (ok) {
for (int i = 0; i < n; i++) {
printf("%u%c", st.query(i, i + 1), " \n"[i == n - 1]);
}
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e5 + 100;
const long long mod = 1e9 + 7;
const long long INF = 1e9 + 7;
const long long mlog = 22;
const long long SQ = 400;
long long n, m;
long long seg[maxn * 4];
long long l[maxn], r[maxn], q[maxn];
long long a[maxn];
long long lg[maxn];
long long dp[maxn][mlog];
void ADD(long long l, long long r, long long val, long long s = 0,
long long e = n, long long id = 1) {
if (l >= e || r <= s) return;
if (l <= s && r >= e) {
seg[id] |= val;
return;
}
long long mid = (s + e) / 2;
ADD(l, r, val, s, mid, id * 2);
ADD(l, r, val, mid, e, id * 2 + 1);
}
long long GET(long long index, long long s = 0, long long e = n,
long long id = 1) {
if (index < s || index >= e) return 0;
if (e - s < 2) return seg[id];
long long mid = (s + e) / 2;
return (seg[id] | (GET(index, s, mid, id * 2)) |
(GET(index, mid, e, id * 2 + 1)));
}
long long QUERY(long long l, long long r) {
if (l == r) return a[l];
long long d = lg[r - l];
return dp[l][d] & dp[r - (1 << d)][d];
}
int32_t main() {
cin >> n >> m;
for (long long i = 0; i < m; i++) cin >> l[i] >> r[i] >> q[i], l[i]--;
for (long long i = 0; i < m; i++) ADD(l[i], r[i], q[i]);
for (long long i = 0; i < n; i++) a[i] = GET(i);
for (long long i = 0; i < n - 1; i++) dp[i][0] = a[i] & a[i + 1];
lg[1] = 0;
for (long long i = 2; i < maxn; i++) lg[i] = lg[i / 2] + 1;
for (long long j = 1; j < mlog; j++)
for (long long i = 0; i + (1 << j) <= n; i++)
dp[i][j] = dp[i][j - 1] & dp[i + (1 << (j - 1))][j - 1];
for (long long i = 0; i < m; i++)
if (QUERY(l[i], r[i] - 1) != q[i]) return cout << "NO\n", 0;
cout << "YES\n";
for (long long i = 0; i < n; i++) cout << a[i] << " ";
cout << "\n";
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m;
int a[100005];
int l[100005], r[100005], q[100005];
int sgn[100005], pr[100005];
bool ok[100005];
void die() {
cout << "NO\n";
exit(0);
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> l[i] >> r[i] >> q[i];
for (int i = 0; i < 30; i++) {
memset(sgn, 0, sizeof(sgn));
memset(ok, 0, sizeof(ok));
for (int j = 1; j <= m; j++) {
if (!(q[j] & 1)) continue;
sgn[l[j]]++;
sgn[r[j] + 1]--;
}
int sum = 0;
for (int j = 1; j <= n; j++) {
sum += sgn[j];
if (sum > 0) a[j] |= 1 << i;
if (sum > 0) ok[j] = 1;
}
for (int j = 1; j <= n; j++)
if (!ok[j])
pr[j] = j + 1;
else
pr[j] = pr[j - 1];
for (int j = 1; j <= m; j++) {
if ((q[j] & 1) && pr[r[j]] > l[j]) die();
if (!(q[j] & 1) && pr[r[j]] <= l[j]) die();
}
for (int j = 1; j <= m; j++) q[j] >>= 1;
}
cout << "YES\n";
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
cout << '\n';
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.util.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class Main{
// ArrayList<Integer> lis = new ArrayList<Integer>();
// ArrayList<String> lis = new ArrayList<String>();
// PriorityQueue<P> que = new PriorityQueue<P>();
// PriorityQueue<Integer> que = new PriorityQueue<Integer>();
// Stack<Integer> que = new Stack<Integer>();
//HashMap<Long,Long> map = new HashMap<Long,Long>();
// static long sum=0;
// 1000000007 (10^9+7)
static int mod = 1000000007,f=0;
//static int mod = 1000000009; ArrayList<Integer> l[]= new ArrayList[n];
static int dx[]={1,-1,0,0};
static int dy[]={0,0,1,-1};
// static int dx[]={1,-1,0,0,1,1,-1,-1};
// static int dy[]={0,0,1,-1,1,-1,1,-1};
//static Set<Integer> set = new HashSet<Integer>();
//static ArrayList<Integer> l[];
//static int parent[][],depth[],node,max_log;
// static ArrayList<Integer> nd[]= new ArrayList[10000000];
public static void main(String[] args) throws Exception, IOException{
//Scanner sc =new Scanner(System.in);
Reader sc = new Reader(System.in);
//int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),d=sc.nextInt(),r=sc.nextInt();
//int n=sc.nextInt();//,m=sc.nextInt(),k=sc.nextInt();
//int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt();
//double d[]=new double[100];
int n=sc.nextInt(),m=sc.nextInt();//,k=sc.nextInt(),s=0;
int d[][]=new int[m][3], mx=1<<17;
int a[]=new int[n];
db(mx);
for (int i = 0; i < d.length; i++) {
d[i][0]=sc.nextInt()-1;
d[i][1]=sc.nextInt()-1;
d[i][2]=sc.nextInt();
}
for (int i = 0; i < 30; i++) {
int x[]=new int[n+1];
for (int j = 0; j < m; j++) {
if( !bit( d[j][2],i) )continue;
//sg.add(d[j][0],d[j][0]+1, 1, 0, 0, mx);
//sg.add(d[j][1]+1,d[j][1]+2, -1, 0, 0, mx);
x[d[j][0]]++;
x[d[j][1]+1]--;
}
int s[]=new int[n+1],sm=0;
for (int j = 0; j < n; j++) { sm+=x[j];
if( sm >0 ){ a[j]|=1<<i; s[j]++; }
if(j>0)s[j]+=s[j-1];
}//db(a,s);
for (int j = 0; j < m; j++) {
int v=s[d[j][1]];
if(d[j][0]>0)v-=s[d[j][0]-1];
if( bit(d[j][2], i) && v!=d[j][1]-d[j][0]+1 ){System.out.println("NO");return;}
else if( !bit(d[j][2], i) && v==d[j][1]-d[j][0]+1 ){System.out.println("NO");return;}
}
}
System.out.println("YES");
StringBuilder sb=new StringBuilder();
for (int i = 0; i < n; i++) sb.append(a[i]+" ");
System.out.println(sb);
}
/*
static class P implements Comparable<P>{
int id, d; ;
P(int id,int d){
this.id=id;
this.d=d;
}
public int compareTo(P x){
// return (-x.d+d)>=0?1:-1 ; // ascend long
// return -x.d+d ; // ascend
return x.d-d ; //descend
}
}//*/
static void db(Object... os){
System.err.println(Arrays.deepToString(os));
}
static boolean validpos(int x,int y,int r, int c){
return x<r && 0<=x && y<c && 0<=y;
}
static boolean bit(int x,int k){
// weather k-th bit (from right) be one or zero
return ( 0 < ( (x>>k) & 1 ) ) ? true:false;
}
}
class segtree {
int seg1[],seg2[];
segtree(){
// n<=100000 0-based each [a,b)
seg1=new int[(1<<18)-1];
seg2=new int[(1<<18)-1];
}
void add(int a,int b,int x,int k,int l,int r){
if( a<=l && r<=b ){ seg1[k]+=x; }
else if( l<b && a<r ){
seg2[k]+= x*( min(r,b)-max(a,l) );
add(a,b,x,2*k+1,l,(l+r)/2);
add(a,b,x,2*k+2,(l+r)/2,r);
}
}
int sum(int a,int b,int k,int l,int r){
if( b<=l || r<=a )return 0;
if( a<=l && r<=b ){ return seg1[k]*(r-l)+seg2[k]; }
else{
int res=0;
res+= seg1[k]*( min(r,b)-max(a,l) );
res+=sum(a,b,2*k+1,l,(l+r)/2);
res+=sum(a,b,2*k+2,(l+r)/2,r);
return res;
}
}
}
class Reader
{
private BufferedReader x;
private StringTokenizer st;
public Reader(InputStream in)
{
x = new BufferedReader(new InputStreamReader(in));
st = null;
}
public String nextString() throws IOException
{
while( st==null || !st.hasMoreTokens() )
st = new StringTokenizer(x.readLine());
return st.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(nextString());
}
public long nextLong() throws IOException
{
return Long.parseLong(nextString());
}
public double nextDouble() throws IOException
{
return Double.parseDouble(nextString());
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int N = 1;
while (N < n)
N <<= 1;
int[] in = new int[N + 1];
for (int i = n+1; i <= N; i++) {
in[i]=-1;
}
SegmentTree sg = new SegmentTree(in);
triple[] t = new triple[k];
while (k-- > 0) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int z = Integer.parseInt(st.nextToken());
t[k] = new triple(x, y, z);
}
for (int i = 0; i < t.length; i++) {
sg.update_range(t[i].x, t[i].y, t[i].z);
}
boolean f = true;
sg.propagateAll(1, 1, N);
for (int i = 0; i < t.length; i++) {
int r = sg.query(t[i].x, t[i].y);
f &= (r == t[i].z);
}
if (f) {
pw.println("YES");
// System.out.println(Arrays.toString(sg.sTree));
for (int i = 0; i < n; i++) {
pw.print(sg.sTree[N + i] + " ");
}
} else
pw.println("NO");
pw.flush();
}
static class SegmentTree { // 1-based DS, OOP
int N; // the number of elements in the array as a power of 2 (i.e. after padding)
int[] array, sTree, lazy;
SegmentTree(int[] in) {
array = in;
N = in.length - 1;
sTree = new int[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new int[N << 1];
build(1, 1, N);
}
void build(int node, int b, int e) // O(n)
{
if (b == e)
sTree[node] = array[b];
else {
int mid = b + e >> 1;
build(node << 1, b, mid);
build(node << 1 | 1, mid + 1, e);
sTree[node] = sTree[node << 1] & sTree[node << 1 | 1];
}
}
void update_point(int index, int val) // O(log n)
{
index += N - 1;
sTree[index] |= val;
while (index > 1) {
index >>= 1;
sTree[index] = sTree[index << 1] * sTree[index << 1 | 1];
if (sTree[index] > 0)
sTree[index] = 1;
else if (sTree[index] < 0)
sTree[index] = -1;
}
}
void update_range(int i, int j, int val) // O(log n)
{
update_range(1, 1, N, i, j, val);
}
void update_range(int node, int b, int e, int i, int j, int val) {
if (i > e || j < b)
return;
if (b >= i && e <= j) {
sTree[node] |= val;
lazy[node] |= val;
} else {
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node << 1, b, mid, i, j, val);
update_range(node << 1 | 1, mid + 1, e, i, j, val);
sTree[node] = sTree[node << 1] & sTree[node << 1 | 1];
}
}
void propagate(int node, int b, int mid, int e) {
lazy[node << 1] |= lazy[node];
lazy[node << 1 | 1] |= lazy[node];
sTree[node << 1] |= lazy[node];
sTree[node << 1 | 1] |= lazy[node];
lazy[node] = 0;
}
int query(int i, int j) {
return query(1, 1, N, i, j);
}
int query(int node, int b, int e, int i, int j) // O(log n)
{
if (i > e || j < b)
return -1;
if (b >= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
propagate(node, b, mid, e);
int q1 = query(node << 1, b, mid, i, j);
int q2 = query(node << 1 | 1, mid + 1, e, i, j);
return q1 & q2;
}
void propagateAll(int node, int b, int e) {
if (b == e)
return;
else {
int mid = b + e >> 1;
propagate(node, b, mid, e);
propagateAll(node << 1, b, mid);
propagateAll(node << 1 | 1, mid + 1, e);
}
}
}
static class triple implements Comparable<triple> {
int x;
int y;
int z;
public triple(int a, int b, int c) {
x = a;
y = b;
z = c;
}
public int compareTo(triple o) {
return z - o.z;
}
public String toString() {
// TODO Auto-generated method stub
return x + " " + y + " " + z;
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B {
static int[] tree;
static int[] lazy;
static void refresh(int idx) {
if (lazy[idx] != 0) {
tree[idx] |= lazy[idx];
if (idx * 2 < tree.length)
lazy[idx * 2] |= lazy[idx];
if (idx * 2 + 1 < tree.length)
lazy[idx * 2 + 1] |= lazy[idx];
}
lazy[idx] = 0;
}
static void update(int idx, int st, int en, int l, int r, int val) {
if (st >= l && en <= r) {
lazy[idx] |= val;
refresh(idx);
return;
}
if (en < l || st > r)
return;
refresh(idx);
int mid = (st + en) >> 1;
update(idx << 1, st, mid, l, r, val);
update((idx << 1) + 1, mid + 1, en, l, r, val);
refresh(idx);
}
static int rqst(int idx, int st, int en, int l, int r) {
refresh(idx);
if (en < l || st > r)
return (1 << 31) - 1;
if (st >= l && en <= r)
return tree[idx];
int mid = (st + en) >> 1;
int x = rqst(idx << 1, st, mid, l, r);
int y = rqst((idx << 1) + 1, mid + 1, en, l, r);
return x & y;
}
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new StringTokenizer("");
int n = nxtInt();
int m = nxtInt();
tree = new int[4 * n];
lazy = new int[4 * n];
int[][] arr = new int[m][3];
for (int i = 0; i < m; i++) {
arr[i] = nxtIntArr(3);
update(1, 0, n - 1, arr[i][0] - 1, arr[i][1] - 1, arr[i][2]);
}
boolean done = true;
for (int i = 0; i < m; i++)
done &= arr[i][2] == rqst(1, 0, n - 1, arr[i][0] - 1, arr[i][1] - 1);
out.println(done ? "YES" : "NO");
if (done) {
for (int i = 0; i < n; i++)
out.print(rqst(1, 0, n - 1, i, i) + " ");
out.println();
}
br.close();
out.close();
}
static BufferedReader br;
static StringTokenizer sc;
static PrintWriter out;
static String nxtTok() throws IOException {
while (!sc.hasMoreTokens()) {
String s = br.readLine();
if (s == null)
return null;
sc = new StringTokenizer(s.trim());
}
return sc.nextToken();
}
static int nxtInt() throws IOException {
return Integer.parseInt(nxtTok());
}
static long nxtLng() throws IOException {
return Long.parseLong(nxtTok());
}
static double nxtDbl() throws IOException {
return Double.parseDouble(nxtTok());
}
static int[] nxtIntArr(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nxtInt();
return a;
}
static long[] nxtLngArr(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nxtLng();
return a;
}
static char[] nxtCharArr() throws IOException {
return nxtTok().toCharArray();
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class D {
static int mo7ayed = (1 << 31) - 1;
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt(), m = sc.nextInt();
int N = 1; while(N < n) N <<= 1;
int[] in = new int[N + 1];
SegmentTree st = new SegmentTree(in);
int[] a = new int[m], b = new int[m], c = new int[m];
for(int i = 0; i < m; i++)
{
a[i] = sc.nextInt(); b[i] = sc.nextInt(); c[i] = sc.nextInt();
st.update_range(a[i], b[i], c[i]);
}
boolean ans = true;
for(int i = 0; i < m; i++)
ans &= st.query(a[i], b[i]) == c[i];
if(!ans) out.println("NO");
else
{
out.println("YES");
for(int i = 1; i <= n; i++)
out.print(st.query(i, i) + " ");
out.println();
}
out.flush();
out.close();
}
static class SegmentTree {
int N;
int[] array, sTree, lazy;
SegmentTree(int[] in)
{
array = in; N = in.length - 1;
sTree = new int[N<<1];
lazy = new int[N<<1];
build(1,1,N);
}
void build(int node, int b, int e)
{
if(b == e)
sTree[node] = array[b];
else
{
int mid = b + e >> 1;
build(node<<1,b,mid);
build(node<<1|1,mid+1,e);
sTree[node] = sTree[node<<1] & sTree[node<<1|1];
}
}
void update_range(int i, int j, int val) // O(log n)
{
update_range(1,1,N,i,j,val);
}
void update_range(int node, int b, int e, int i, int j, int val)
{
if(i > e || j < b)
return;
if(b >= i && e <= j)
{
sTree[node] |= val;
lazy[node] |= val;
}
else
{
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node<<1,b,mid,i,j,val);
update_range(node<<1|1,mid+1,e,i,j,val);
sTree[node] = sTree[node<<1] & sTree[node<<1|1];
}
}
void propagate(int node, int b, int mid, int e)
{
lazy[node<<1] |= lazy[node];
lazy[node<<1|1] |= lazy[node];
sTree[node<<1] |= lazy[node];
sTree[node<<1|1] |= lazy[node];
lazy[node] = 0;
}
int query(int i, int j)
{
return query(1,1,N,i,j);
}
int query(int node, int b, int e, int i, int j) // O(log n)
{
if(i>e || j <b)
return mo7ayed;
if(b>= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
propagate(node, b, mid, e);
int q1 = query(node<<1,b,mid,i,j);
int q2 = query(node<<1|1,mid+1,e,i,j);
return q1 & q2;
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {br = new BufferedReader(new InputStreamReader(system));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine()throws IOException{return br.readLine();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public char nextChar()throws IOException{return next().charAt(0);}
public Long nextLong()throws IOException{return Long.parseLong(next());}
public boolean ready() throws IOException{return br.ready();}
public void waitForInput(){for(long i = 0; i < 3e9; i++);}
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class CF_275_Div1_InterestingArray {
public static void main(String[] args) throws IOException {
Scanner sc =new Scanner(System.in);
int n =sc.nextInt();
StringBuilder sb = new StringBuilder();
PrintWriter pw = new PrintWriter(System.out);
int m =sc.nextInt();
int[] l=new int[m];
int[] r=new int[m];
int[] q=new int[m];
int N=1;
while(N<n) N<<=1;
int[] in =new int[N+1];
SegmentTree st = new SegmentTree(in);
for(int i=0;i<m;i++)
{
int a =l[i]=sc.nextInt();int b =r[i]=sc.nextInt();int c =q[i]=sc.nextInt();
st.update_range(a, b, c);
}
boolean ok =true;
for(int i=0;i<m;i++)
{
if(st.query(l[i], r[i]) !=q[i])
{
ok=false;
break;
}
}
if(!ok)
System.out.println("NO");
else
{
pw.println("YES");
st.propagateAll(1, 1, st.N);
int output[]= new int [n];
for(int i=0; i<n; i++)
output[i]=st.query(i+1, i+1);
for(int x:output)
sb.append(x+" ");
sb.append("\n");
pw.println(sb);
pw.flush();
pw.close();
}
}
static class SegmentTree
{
int N; //the number of elements in the array as a power of 2 (i.e. after padding)
int[] array, sTree, lazy;
SegmentTree(int[] in)
{
array = in; N = in.length - 1;
sTree = new int[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new int[N<<1];
//build(1,1,N);
}
void update_range(int i, int j, int val) // O(log n)
{
update_range(1,1,N,i,j,val);
}
void update_range(int node, int b, int e, int i, int j, int val)
{
if(i > e || j < b)
return;
if(b >= i && e <= j)
{
sTree[node] |= val;
lazy[node] |= val;
}
else
{
propagate(node, b, e);
update_range(node<<1,b,(b+e)/2,i,j,val);
update_range((node<<1)+1,(b+e)/2+1,e,i,j,val);
sTree[node] = sTree[node<<1] & sTree[(node<<1)+1];
}
}
void propagate(int node, int b, int e)
{
lazy[node<<1] |= lazy[node];
lazy[(node<<1)+1] |= lazy[node];
sTree[node<<1] |= lazy[node];
sTree[(node<<1)+1] |= lazy[node];
lazy[node] = 0;
}
int query(int i, int j)
{
return query(1,1,N,i,j);
}
int query(int node, int b, int e, int i, int j) // O(log n)
{
if(i>e || j <b)
return -1;
if(b>= i && e <= j)
return sTree[node];
propagate(node, b, e);
int q1 = query(node<<1,b,(b+e)/2,i,j);
int q2 = query((node<<1)+1,(b+e)/2+1,e,i,j);
/*if(q1==-1)
return q2;
if(q2==-1)
return q1;*/
return q1 & q2;
}
void propagateAll(int node,int start, int end)
{
//System.out.println(start +" " + end +" "+ node);
int mid=(start+end)>>1;
if(start==end)
return;
propagate(node,start,end);
propagateAll((node<<1) , start ,mid);
propagateAll((node<<1)|1 , mid+1 ,end);
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
inline void ri(int& x) { scanf("%d", &x); }
class FenwickTree {
private:
vector<int> ft;
void update(int pos, int val) {
while (pos < ft.size()) {
ft[pos] += val;
pos += pos & (-pos);
}
}
public:
FenwickTree() {}
FenwickTree(int n) { ft.assign(n + 1, 0); }
void add(int l, int r, int val) {
update(l, val);
update(r + 1, -val);
}
int access(int pos) {
int sum = 0;
while (pos) {
sum += ft[pos];
pos -= pos & (-pos);
}
return sum;
}
};
const int MAX = 1e5 + 50;
FenwickTree ft[32];
pair<pair<int, int>, int> queries[MAX];
int ans[MAX], pre[32][MAX], data[32][MAX];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int n, m;
ri(n);
ri(m);
for (int j = 0; j < 32; j++) ft[j] = FenwickTree(n);
for (int i = 0; i < m; i++) {
int l, r, q;
ri(l), ri(r), ri(q);
queries[i] = make_pair(make_pair(l, r), q);
for (int j = 0; j < 32; j++) {
int d = q & 1;
q = q >> 1;
if (d) {
ft[j].add(l, r, 1);
}
}
}
for (int i = 0; i < n; i++) {
int num = 0;
for (int j = 31; j >= 0; j--) {
int d = ft[j].access(i + 1);
d = min(1, d);
num *= 2;
num += d;
data[j][i] = d;
}
ans[i] = num;
}
for (int j = 0; j < 32; j++) {
pre[j][0] = data[j][0];
for (int i = 1; i < n; i++) {
pre[j][i] = pre[j][i - 1] + data[j][i];
}
}
for (int i = 0; i < m; i++) {
int l = queries[i].first.first;
int r = queries[i].first.second;
int q = queries[i].second;
l--;
r--;
for (int j = 0; j < 32; j++) {
int d = q & 1;
q = q >> 1;
if (d == 0) {
int val;
if (l == 0)
val = pre[j][r];
else
val = pre[j][r] - pre[j][l - 1];
if (val == (r - l + 1)) {
printf("NO\n");
return 0;
}
}
}
}
printf("YES\n");
for (int i = 0; i < n; i++) {
if (i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
using namespace std;
long long dp[100005][35];
long long val[100005][35];
long long a[100005][35];
struct node {
long long l, r, q;
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
long long i, j;
node b[m + 5];
for (i = 1; i <= m; i++) cin >> b[i].l >> b[i].r >> b[i].q;
for (i = 1; i <= m; i++) {
vector<long long> v;
v.clear();
while (b[i].q) {
v.push_back(b[i].q % 2);
b[i].q /= 2;
}
for (j = 0; j < v.size(); j++) a[i][j] = v[j];
}
for (i = 1; i <= m; i++) {
for (j = 0; j <= 31; j++)
if (a[i][j]) {
val[b[i].l][j] += 1;
val[b[i].r + 1][j] -= 1;
}
}
for (i = 1; i <= n; i++)
for (j = 0; j <= 31; j++) {
val[i][j] += val[i - 1][j];
}
for (i = 1; i <= n; i++)
for (j = 0; j <= 31; j++)
if (val[i][j]) val[i][j] = 1;
for (i = 1; i <= n; i++)
for (j = 0; j <= 31; j++) {
dp[i][j] = dp[i - 1][j];
if (val[i][j]) dp[i][j] += 1;
}
long long f = 1;
for (i = 1; i <= m; i++) {
for (j = 0; j <= 31; j++) {
long long x = dp[b[i].r][j] - dp[b[i].l - 1][j];
if ((x == (b[i].r - b[i].l + 1) && a[i][j] == 0) ||
(x != (b[i].r - b[i].l + 1) && a[i][j] == 1)) {
break;
}
}
if (j <= 31) break;
}
if (i <= m)
cout << "NO\n";
else {
cout << "YES\n";
for (i = 1; i <= n; i++) {
long long sum = 0;
for (j = 0; j <= 31; j++)
if (val[i][j]) sum = sum + (1 << j);
cout << sum << " ";
}
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100, B = 30;
int L[N], R[N], X[N];
int A[N], res[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= q; i++) {
cin >> L[i] >> R[i] >> X[i];
}
for (int bit = B - 1; bit >= 0; bit--) {
memset(A, 0, sizeof A);
for (int i = 1; i <= q; i++) {
if ((X[i] >> bit) & 1) {
A[L[i]]++;
A[R[i] + 1]--;
}
}
for (int i = 1; i <= n; i++) {
A[i] += A[i - 1];
}
for (int i = 1; i <= n; i++) {
A[i] = (A[i] > 0);
if (A[i]) res[i] ^= (1 << bit);
A[i] += A[i - 1];
}
for (int i = 1; i <= q; i++) {
if (!((X[i] >> bit) & 1)) {
if (R[i] - L[i] + 1 == A[R[i]] - A[L[i] - 1]) {
cout << "NO" << endl;
return 0;
}
}
}
}
cout << "YES" << endl;
for (int i = 1; i <= n; i++) {
cout << res[i] << " ";
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
template <class T>
T pmax(T a, T b) {
return max(a, b);
}
const long long int mod = 1e9 + 7;
const long long int Maa = 3e5;
vector<long long int> edge[Maa];
long long int n, m, l[Maa], r[Maa], q[Maa], A[Maa], p[Maa], c[Maa];
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) cin >> l[i] >> r[i] >> q[i];
int b = 0;
for (int i = 0; i < 30; i++) {
memset(c, 0, sizeof c);
for (int j = 0; j < m; j++)
if ((q[j] >> i) & 1 == 1) c[l[j]]++, c[r[j] + 1]--;
for (int j = 1; j <= n; j++)
c[j] += c[j - 1], A[j] += c[j] > 0 ? (1 << i) : 0;
for (int j = 1; j <= n; j++)
if (c[j]) c[j] = 1, c[j] += c[j - 1];
for (int j = 0; j < m; j++)
if (((q[j] >> i) & 1) == 0 and c[r[j]] - c[l[j] - 1] == r[j] - l[j] + 1) {
b = 1;
break;
}
}
if (b)
cout << "NO";
else {
cout << "YES"
<< "\n";
for (int i = 1; i <= n; i++) cout << A[i] << " ";
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MAXB = 30;
int ls[MAXN], rs[MAXN], qs[MAXN], out[MAXN];
int res[MAXN];
int main(void) {
int n, m;
scanf("%d", &(n));
scanf("%d", &(m));
vector<pair<int, int> > intv[MAXN];
for (int i = 0; i < m; i++) {
scanf("%d", &(ls[i]));
scanf("%d", &(rs[i]));
scanf("%d", &(qs[i]));
for (int j = 0; j < MAXB; j++) {
if (qs[i] & (1 << j)) intv[j].push_back(make_pair(ls[i], rs[i]));
}
}
for (int j = 0; j < MAXB; j++) {
int sz = intv[j].size();
if (sz == 0) continue;
sort(intv[j].begin(), intv[j].end());
vector<pair<int, int> > v;
pair<int, int> prev = intv[j][0];
for (int i = 1; i < sz; i++) {
if (prev.second >= intv[j][i].first)
prev = make_pair(prev.first, max(prev.second, intv[j][i].second));
else {
v.push_back(prev);
prev.first = intv[j][i].first;
prev.second = intv[j][i].second;
}
}
v.push_back(prev);
memset(res, 0, sizeof(res));
for (int i = 0; i < v.size(); i++) {
for (int k = v[i].first; k <= v[i].second; k++) res[k] = 1;
}
for (int i = 1; i < n + 1; i++) res[i] += res[i - 1];
for (int i = 0; i < m; i++) {
if (qs[i] & (1 << j)) continue;
int diff = rs[i] - ls[i] + 1;
if (res[rs[i]] - res[ls[i] - 1] == diff) {
cout << "NO\n";
return 0;
}
}
for (int i = 1; i < n + 1; i++) {
int r = res[i] - res[i - 1];
out[i] |= (r << j);
}
}
cout << "YES\n";
for (int i = 1; i < n + 1; i++) printf("%d ", out[i]);
printf("\n");
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.*;
import java.util.*;
public class InterestingArray {
public static void main(String[] args) throws Exception{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] q = new int[m][3];
for(int i = 0;i<m;i++) {
st = new StringTokenizer(f.readLine());
q[i][0] = Integer.parseInt(st.nextToken()) - 1;
q[i][1] = Integer.parseInt(st.nextToken()) - 1;
q[i][2] = Integer.parseInt(st.nextToken());
}
int[][] s = new int[30][n+1];
for(int i = 0;i<m;i++) {
int l = q[i][0];
int r = q[i][1];
int inc = q[i][2];
for(int j = 0;j<30;j++) {
if(((1 << j) & inc) != 0) {
s[j][l] ++;
s[j][r+1] --;
}
}
}
SegmentTree tree = new SegmentTree(n);
int[] ans = new int[n];
for(int j = 0;j<30;j++) {
int adj = 0;
for(int i = 0;i<n;i++) {
adj += s[j][i];
if(adj > 0) {
ans[i] += 1 << j;
}
}
}
for(int i = 0;i<n;i++) {
tree.increment(i, i, ans[i]);
}
for(int i = 0;i<m;i++) {
int l = q[i][0];
int r = q[i][1];
int inc = q[i][2];
if(tree.and(l,r) != inc) {
System.out.println("NO");
System.exit(0);
}
}
System.out.println("YES");
for(int i = 0;i<n;i++) {
System.out.print(ans[i] + " ");
}
}
interface QueryInterface{
public void increment(int i, int j, int val);
public int and(int i, int j);
}
static class ShitTree implements QueryInterface{
int[] a;
public ShitTree(int n) {
a = new int[n];
}
public void increment(int i, int j, int val) {
for(int x = i;x <=j;x++) {
a[x] += val;
}
}
public int and(int i, int j) {
int ret = a[i];
for(int x = i + 1; x <=j;x++) {
ret = ret & x;
}
return ret;
}
}
static class SegmentTree implements QueryInterface{
int n;
int[] lo, hi, and, delta;
public SegmentTree(int n) {
this.n = n;
lo = new int[4*n+1];
hi = new int[4*n+1];
and = new int[4*n+1];
delta = new int[4*n+1];
init(1,0,n-1);
}
public void increment(int a, int b, int val) {
increment(1, a, b, val);
}
void prop(int i) {
delta[2*i] += delta[i];
delta[2*i+1] += delta[i];
delta[i] = 0;
}
void update(int i) {
and[i] = (and[2*i] + delta[2*i]) & (and[2*i+1] + delta[2*i+1]);
}
void increment(int i, int a, int b, int val) {
if(b < lo[i] || hi[i] < a) {
return;
}
if(a <= lo[i] && hi[i] <= b) {
delta[i] += val;
return;
}
prop(i);
increment(2*i,a,b,val);
increment(2*i+1,a,b,val);
update(i);
}
void init(int i, int a, int b) {
lo[i] = a;
hi[i] = b;
if(a == b) {
return;
}
int m = (a+b)/2;
init(2*i,a,m);
init(2*i+1,m+1,b);
}
public int and(int i, int j) {
return and(1,i,j);
}
int and(int i, int a, int b) {
if(b < lo[i] || hi[i] < a) {
return (1 << 30) - 1;
}
if(a <= lo[i] && hi[i] <= b) {
return and[i] + delta[i];
}
prop(i);
int andLeft = and(2*i,a,b);
int andRight = and(2*i+1,a,b);
update(i);
return andLeft & andRight;
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int arr[100002][30], res[100002][30], cnt[100002][30];
int l[100002], r[100002], q[100002];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &l[i], &r[i], &q[i]);
for (int j = 0; j < 30; j++) {
if (q[i] & (1 << j)) {
arr[l[i]][29 - j]++;
arr[r[i] + 1][29 - j]--;
}
}
}
for (int j = 0; j < 30; j++) {
for (int i = 1; i <= n; i++) {
arr[i][j] += arr[i - 1][j];
if (arr[i][j] > 0) {
res[i][j] = 1;
}
cnt[i][j] = res[i][j] + cnt[i - 1][j];
}
}
int rep = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < 30; j++) {
if ((q[i] & (1 << j)) == 0) {
int x = cnt[r[i]][29 - j] - cnt[l[i] - 1][29 - j];
if (x == (r[i] - l[i] + 1)) {
rep = 1;
}
}
}
}
if (rep) {
printf("NO\n");
return 0;
} else {
printf("YES\n");
for (int i = 1; i <= n; i++) {
int r = 0;
for (int j = 0; j < 30; j++) {
r = r * 2;
if (res[i][j]) r += 1;
}
printf("%d ", r);
}
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt(), m = s.nextInt();
Condition[] c = new Condition[m];
for (int i = 0; i < m; i++) {
c[i] = new Condition(s.nextInt() - 1, s.nextInt() - 1, s.nextInt());
}
int[] ans = new int[n];
Node root = new Node(0, n - 1);
for (int j = 0; j < m; j++) {
root.insert(c[j].l, c[j].r, c[j].q);
}
for (int j = 0; j < m; j++) {
if (root.find(c[j].l, c[j].r) != c[j].q) {
System.out.println("NO");
return;
}
}
root.query(ans);
System.out.println("YES");
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
if (j != 0) {
sb.append(' ');
}
sb.append(ans[j]);
}
System.out.println(sb);
}
public static class Condition {
int l, r, q;
public Condition(int l, int r, int q) {
this.l = l;
this.r = r;
this.q = q;
}
}
public static class Node {
int l, r, mid;
int or, a;
Node left, right;
public Node(int l, int r) {
this.l = l;
this.r = r;
this.or = 0;
this.a = 0;
this.mid = (l + r) / 2;
if (l != r) {
left = new Node(l, mid);
right = new Node(mid + 1, r);
}
}
public void insert(int start, int end, int q) {
if (start <= l && r <= end) {
or |= q;
a |= q;
} else {
if (or != 0) {
left.a |= or;
right.a |= or;
left.or |= or;
right.or |= or;
or = 0;
}
if (mid < start) {
right.insert(start, end, q);
} else if (mid + 1 > end) {
left.insert(start, end, q);
} else {
left.insert(start, end, q);
right.insert(start, end, q);
}
a = left.a & right.a;
}
}
public int find(int start, int end) {
if (l != r && or != 0) {
left.a |= or;
right.a |= or;
left.or |= or;
right.or |= or;
or = 0;
}
if (start <= l && r <= end) {
return a;
}
if (mid < start) {
return right.find(start, end);
} else if (mid + 1 > end) {
return left.find(start, end);
} else {
return left.find(start, end) & right.find(start, end);
}
}
public void query(int[] ans) {
if (l != r && or != 0) {
left.a |= or;
right.a |= or;
left.or |= or;
right.or |= or;
or = 0;
}
if (l == r) {
ans[l] = a;
} else {
left.query(ans);
right.query(ans);
}
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const char lend = '\n';
const int N = 100000, M = 30;
int vet[N][M], n, m, l[N], r[N], x[N];
void first(int b) {
set<int> s;
for (int i = 0; i < n; ++i) s.insert(i);
for (int i = 0; i < m; ++i)
if (x[i] >> b & 1) {
auto it = s.lower_bound(l[i]);
while (it != s.end() && *it <= r[i]) {
vet[*it][b] = 1;
s.erase(it++);
}
}
}
int second(int b) {
vector<int> s;
for (int i = 0; i < n; ++i)
if (!vet[i][b]) s.push_back(i);
for (int i = 0; i < m; ++i)
if (!(x[i] >> b & 1)) {
auto it = lower_bound(s.begin(), s.end(), l[i]);
if (it == s.end() || *it > r[i]) return 0;
}
return 1;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; ++i) cin >> l[i] >> r[i] >> x[i], --l[i], --r[i];
bool g = 1;
for (int i = 0; i < M; ++i) first(i);
for (int i = 0; i < M && g; ++i) g &= second(i);
if (!g)
cout << "NO\n";
else {
cout << "YES\n";
for (int i = 0; i < n; ++i) {
int k = 0;
for (int j = 0; j < M; ++j)
if (vet[i][j]) k += 1 << j;
cout << (i ? " " : "") << k;
}
cout << lend;
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int from[N], to[N], num[N];
int s[N][30];
int a[N][30];
int sum[N][30];
void tree(int n, int m) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j < 30; j++) {
s[i][j] = 0;
}
}
for (int k = 0; k < m; k++) {
scanf("%d %d %d", from + k, to + k, num + k);
from[k]--;
to[k]--;
for (int j = 0; j < 30; j++) {
if (num[k] & (1 << j)) {
s[from[k]][j]++;
s[to[k] + 1][j]--;
}
}
}
for (int j = 0; j < 30; j++) {
int bal = 0;
sum[0][j] = 0;
for (int i = 0; i < n; i++) {
bal += s[i][j];
a[i][j] = (bal > 0);
sum[i + 1][j] = sum[i][j] + a[i][j];
}
}
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
tree(n, m);
for (int k = 0; k < m; k++) {
for (int j = 0; j < 30; j++) {
if (!(num[k] & (1 << j))) {
int get = sum[to[k] + 1][j] - sum[from[k]][j];
int need = (to[k] + 1) - (from[k]);
if (get == need) {
puts("NO");
return 0;
}
}
}
}
puts("YES");
for (int i = 0; i < n; i++) {
int res = 0;
for (int j = 0; j < 30; j++) {
if (a[i][j]) {
res += (1 << j);
}
}
if (i > 0) printf(" ");
printf("%d", res);
}
printf("\n");
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
vector<pair<pair<int, int>, int> > vec;
int tree[4 * 100011];
int arr[30][100011];
int n, k;
int ans[100011];
int build(int node, int b, int e) {
if (b == e) {
return tree[node] = ans[b];
}
int mid = (b + e) >> 1;
int l = node << 1;
int r = l + 1;
int p = build(l, b, mid);
int q = build(r, mid + 1, e);
return tree[node] = p & q;
}
int query(int node, int b, int e, int i, int j) {
if (i > e || b > j) return 1073741823;
if (b >= i && e <= j) return tree[node];
int mid = (b + e) >> 1;
int l = node << 1;
int r = l + 1;
int p = query(l, b, mid, i, j);
int q = query(r, mid + 1, e, i, j);
return p & q;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= k; i++) {
int p, q, r;
scanf("%d%d%d", &p, &q, &r);
vec.push_back(make_pair(make_pair(p, q), r));
int id = 0;
while (r) {
if (r % 2) {
arr[id][p] += 1;
arr[id][q + 1] += -1;
}
id++;
r /= 2;
}
}
for (int i = 0; i < 30; i++) {
for (int j = 2; j <= n; j++) {
arr[i][j] += arr[i][j - 1];
}
}
for (int i = 1; i <= n; i++) {
int temp = 0;
for (int j = 0; j < 30; j++) {
temp += ((arr[j][i] != 0) * (1 << j));
}
ans[i] = temp;
}
build(1, 1, n);
int sz = vec.size();
for (int i = 0; i < sz; i++) {
int p = vec[i].first.first;
int q = vec[i].first.second;
int r = vec[i].second;
int x = query(1, 1, n, p, q);
if (x != r) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
for (int i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
mt19937 rnd(239);
const int N = 100001;
int a[2 * N];
int t[2 * N];
void prop(int i) {
t[i] |= t[i >> 1];
t[i ^ 1] |= t[i >> 1];
}
void f(int ls, int rs, int x) {
for (int l = ls + N, r = rs + N; l <= r; l = l + 1 >> 1, r = r - 1 >> 1) {
if (l & 1) {
t[l] |= x;
}
if (r + 1 & 1) {
t[r] |= x;
}
}
}
int gt(int l, int r) {
int rs = (1 << 31) - 1;
for (l += N, r += N; l <= r; l = l + 1 >> 1, r = r - 1 >> 1) {
if (l & 1) rs &= t[l];
if (r + 1 & 1) rs &= t[r];
}
return rs;
}
int l[N], r[N], x[N];
void solve() {
int n, q;
cin >> n >> q;
for (int i = 0; i < q; ++i) {
cin >> l[i] >> r[i] >> x[i];
f(l[i] - 1, r[i] - 1, x[i]);
}
for (int i = 2; i < 2 * N; ++i) prop(i);
for (int i = N; i < 2 * N; ++i) a[i] = t[i];
for (int i = 2 * N - 1; i > 1; i -= 2) {
a[i >> 1] = a[i] & a[i ^ 1];
}
bool f = 1;
for (int i = 0; i < q; ++i) {
f &= (gt(l[i] - 1, r[i] - 1) == x[i]);
}
if (!f) {
cout << "NO";
return;
} else {
cout << "YES\n";
for (int i = 0; i < n; ++i) cout << t[i + N] << " ";
}
}
void pre_calc() {}
int main() {
pre_calc();
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tst = 1;
if (tst != 1) cin >> tst;
while (tst--) {
solve();
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
class SGTreeLazy {
long long int size;
vector<long long int> arr;
public:
vector<long long int> result;
SGTreeLazy(long long int s) {
size = s;
arr.resize(4 * s, 0);
}
void build(long long int x, vector<long long int> &a, long long int l,
long long int r) {
if (l == r)
arr[x] = a[l];
else {
long long int mid = (l + r) / 2;
build(2 * x, a, l, mid);
build(2 * x + 1, a, mid + 1, r);
arr[x] = 0;
}
}
long long int query(long long int x, long long int l, long long int r,
long long int pos) {
if (l == r && l == pos)
return arr[x];
else {
long long int mid = (l + r) / 2;
if (pos <= mid)
return arr[x] + query(2 * x, l, mid, pos);
else
return arr[x] + query(2 * x + 1, mid + 1, r, pos);
}
}
void update(long long int x, long long int l, long long int r,
long long int ql, long long int qr, long long int val) {
if (ql > qr) return;
if (ql == l && qr == r)
arr[x] = (arr[x] | val);
else {
long long int mid = (l + r) / 2;
update(2 * x, l, mid, ql, min(qr, mid), val);
update(2 * x + 1, mid + 1, r, max(mid + 1, ql), qr, val);
}
}
void final(long long int x, long long int l, long long int r) {
if (l > r) return;
if (l == r) {
result.push_back(arr[x]);
} else {
arr[2 * x] = (arr[x] | arr[2 * x]);
arr[2 * x + 1] = (arr[x] | arr[2 * x + 1]);
long long int mid = (l + r) / 2;
final(2 * x, l, mid);
final(2 * x + 1, mid + 1, r);
}
}
};
class SGTree {
long long int size;
vector<long long int> arr;
public:
SGTree(long long int s) {
size = s;
arr.resize(4 * s, 0);
}
void build(long long int x, vector<long long int> &a, long long int l,
long long int r) {
if (l == r)
arr[x] = a[l];
else {
long long int mid = (l + r) / 2;
build(2 * x, a, l, mid);
build(2 * x + 1, a, mid + 1, r);
arr[x] = (arr[2 * x] & arr[2 * x + 1]);
}
}
long long int query(long long int x, long long int l, long long int r,
long long int ql, long long int qr) {
if (ql > qr) return (long long int)(pow(2, 30) - 1);
if (l == ql && r == qr)
return arr[x];
else {
long long int mid = (l + r) / 2;
return (query(2 * x, l, mid, ql, min(mid, qr)) &
query(2 * x + 1, mid + 1, r, max(mid + 1, ql), qr));
}
}
void update(long long int x, long long int l, long long int r,
long long int pos, long long int val) {
if (l == r)
arr[x] = val;
else {
long long int mid = (l + r) / 2;
if (pos <= mid)
update(2 * x, l, mid, pos, val);
else
update(2 * x + 1, mid + 1, r, pos, val);
arr[x] = arr[2 * x] + arr[2 * x + 1];
}
}
};
struct query {
long long int l, r, q;
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long int n, m;
cin >> n >> m;
SGTreeLazy arrbuilding(n);
vector<query> arr(m);
bool pos = true;
for (long long int i = 0; i < m; i++) {
cin >> arr[i].l >> arr[i].r >> arr[i].q;
arr[i].l--, arr[i].r--;
arrbuilding.update(1, 0, n - 1, arr[i].l, arr[i].r, arr[i].q);
}
arrbuilding.final(1, 0, n - 1);
vector<long long int> check = arrbuilding.result;
SGTree checkquery(n);
checkquery.build(1, check, 0, n - 1);
for (long long int i = 0; i < m; i++) {
long long int val = checkquery.query(1, 0, n - 1, arr[i].l, arr[i].r);
if (val != arr[i].q) {
pos = false;
break;
}
}
if (pos) {
cout << "YES" << endl;
for (auto i : check) cout << i << " ";
cout << endl;
} else
cout << "NO" << endl;
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int maxm = 200010;
const int inf = 1e9;
int n, m;
int a[maxn];
int b[maxn];
int r1[maxn];
int r2[maxn];
int r3[maxn];
struct Node {
int l;
int r;
int val;
int re;
} Tree[maxn << 2];
void BuildTree(int left, int right, int rt) {
Tree[rt].l = left;
Tree[rt].r = right;
Tree[rt].val = 0;
if (left == right) {
return;
}
int mid = (left + right) >> 1;
BuildTree(left, mid, rt << 1);
BuildTree(mid + 1, right, (rt << 1) + 1);
return;
}
void update(int left, int right, int rt, int w) {
if (left <= Tree[rt].l && right >= Tree[rt].r) {
if ((Tree[rt].val >> w) % 2 == 0) {
Tree[rt].val += b[w];
}
return;
}
if (left <= Tree[rt << 1].r) {
update(left, right, rt << 1, w);
}
if (right >= Tree[(rt << 1) + 1].l) {
update(left, right, (rt << 1) + 1, w);
}
return;
}
int ask(int left, int right, int rt, int w) {
int temp;
if ((Tree[rt].val >> w) % 2 == 1) {
return 1;
} else if (Tree[rt].l == Tree[rt].r) {
return 0;
} else {
temp = 1;
if (left <= Tree[rt << 1].r) {
temp = temp && ask(left, right, rt << 1, w);
}
if (right >= Tree[(rt << 1) + 1].l) {
temp = temp && ask(left, right, (rt << 1) + 1, w);
}
}
return temp;
}
int main() {
int i, j;
scanf("%d%d", &n, &m);
memset(b, 0, sizeof(b));
for (i = 1; i <= n; i++) {
a[i] = 1;
for (j = 1; j < 30; j++) {
a[i] <<= 1;
a[i] += 1;
}
}
for (i = 0; i < 30; i++) {
b[i] += 1 << i;
}
BuildTree(1, n, 1);
int flag = 1;
for (i = 1; i <= m; i++) {
int x, y, z;
scanf("%d%d%d", &r1[i], &r2[i], &r3[i]);
x = r1[i];
y = r2[i];
z = r3[i];
for (j = 0; j < 30; j++) {
if ((z >> j) % 2 == 1) {
update(x, y, 1, j);
} else {
if (ask(x, y, 1, j)) {
printf("NO\n");
return 0;
}
}
}
}
for (i = m; i > 0; i--) {
int x, y, z;
x = r1[i];
y = r2[i];
z = r3[i];
for (j = 0; j < 30; j++) {
if ((z >> j) % 2 == 1) {
update(x, y, 1, j);
} else {
if (ask(x, y, 1, j)) {
printf("NO\n");
return 0;
}
}
}
}
printf("YES\n");
for (i = 1; i <= n; i++) {
int temp = 0;
for (j = 0; j < 30; j++) {
if (ask(i, i, 1, j)) {
temp += b[j];
}
}
printf("%d%c", temp, i == n ? '\n' : ' ');
}
for (int i = 0; i < 1e7; i++) {
int sum = 0;
sum += i;
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 111111;
int req[N][32], c[N][32];
int s[32];
int ans[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
vector<pair<int, pair<int, int> > > z;
for (int i = 0; i < m; ++i) {
int l, r, q;
scanf("%d%d%d", &l, &r, &q);
for (int j = 0; j < 30; ++j)
if ((1 << j) & q) {
++req[l][j];
--req[r + 1][j];
} else {
z.push_back(make_pair(j, make_pair(l, r)));
}
}
for (int i = 1; i <= n; ++i) {
int x = 0;
for (int j = 0; j < 30; ++j) {
s[j] += req[i][j];
if (s[j] > 0) {
x |= (1 << j);
c[i][j] = c[i - 1][j] + 1;
} else {
c[i][j] = c[i - 1][j];
}
}
ans[i] = x;
}
for (size_t i = 0; i < z.size(); ++i) {
if (c[z[i].second.second][z[i].first] -
c[z[i].second.first - 1][z[i].first] ==
z[i].second.second - z[i].second.first + 1) {
puts("NO");
return 0;
}
}
puts("YES");
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
puts("");
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
vector<int> seg1;
void update(int st, int en, int i, int val, int l, int r) {
if (r < st || en < l) return;
if (l <= st && en <= r) {
seg1[i] |= val;
return;
}
int mid = (st + en) / 2;
update(st, mid, (2 * i), val, l, r);
update(mid + 1, en, (2 * i) + 1, val, l, r);
}
int num;
void query(int st, int en, int i, int val) {
if (en < val || val < st) return;
num |= seg1[i];
if (st == en) {
return;
}
int mid = (st + en) / 2;
query(st, mid, (2 * i), val);
query(mid + 1, en, (2 * i) + 1, val);
}
struct node {
int l, r, q;
};
struct node qq[100001];
int a[100001];
int seg2[400005];
void build(int st, int en, int i) {
if (st == en) {
seg2[i] = a[st];
return;
}
int mid = (st + en) / 2;
build(st, mid, (2 * i));
build(mid + 1, en, (2 * i) + 1);
seg2[i] = (seg2[(2 * i)] & seg2[(2 * i) + 1]);
}
int query2(int st, int en, int i, int l, int r) {
if (r < st || en < l) return ((1 << 30) - 1);
if (l <= st && en <= r) return seg2[i];
int mid = (st + en) / 2;
return (query2(st, mid, (2 * i), l, r) &
query2(mid + 1, en, (2 * i) + 1, l, r));
}
signed main() {
seg1.resize(400005, 0);
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &qq[i].l, &qq[i].r, &qq[i].q);
update(1, n, 1, qq[i].q, qq[i].l, qq[i].r);
}
for (int i = 1; i <= n; i++) {
num = 0;
query(1, n, 1, i);
a[i] = num;
}
build(1, n, 1);
for (int i = 0; i < m; i++) {
if (query2(1, n, 1, qq[i].l, qq[i].r) != qq[i].q) {
printf("NO");
return 0;
}
}
printf("YES\n");
for (int i = 1; i <= n; i++) printf("%d ", a[i]);
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
inline long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
inline long long power(long long a, long long n, long long m) {
if (n == 0) return 1;
long long p = power(a, n / 2, m);
p = (p * p) % m;
if (n % 2)
return (p * a) % m;
else
return p;
}
const long long MOD = 1000000009;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
int mask = (1 << 30) - 1;
struct Query {
int l;
int r;
int q;
};
const int N = 200001;
int st[4 * N];
int lazy[4 * N];
int st1[4 * N];
int a[N];
void constructtree(int i, int l, int r) {
if (l > r) return;
if (l == r) {
st1[i] = a[l];
return;
}
int m = (l + r) / 2;
constructtree(2 * i + 1, l, m);
constructtree(2 * i + 2, m + 1, r);
st1[i] = st1[2 * i + 1] & st1[2 * i + 2];
}
int query2(int i, int l, int r, int qs, int qe) {
if (qs > r || qe < l) return mask;
if (qs <= l && qe >= r) return st1[i];
int m = (l + r) / 2;
return query2(2 * i + 1, l, m, qs, qe) & query2(2 * i + 2, m + 1, r, qs, qe);
}
void update(int i, int l, int r, int qs, int qe, int v) {
if (lazy[i] != 0) {
st[i] |= lazy[i];
if (l != r) {
lazy[2 * i + 1] |= lazy[i];
lazy[2 * i + 2] |= lazy[i];
}
lazy[i] = 0;
}
if (qs > r || qe < l) return;
if (qs <= l && qe >= r) {
st[i] |= v;
if (l != r) {
lazy[2 * i + 1] |= v;
lazy[2 * i + 2] |= v;
}
lazy[i] = 0;
return;
}
int m = (l + r) / 2;
update(2 * i + 1, l, m, qs, qe, v);
update(2 * i + 2, m + 1, r, qs, qe, v);
st[i] = st[2 * i + 1] | st[2 * i + 2];
}
int query(int i, int l, int r, int qs, int qe) {
if (lazy[i] != 0) {
st[i] |= lazy[i];
if (l != r) {
lazy[2 * i + 1] |= lazy[i];
lazy[2 * i + 2] |= lazy[i];
}
lazy[i] = 0;
}
if (qs > r || qe < l) return 0;
if (qs <= l && qe >= r) return st[i];
int m = (l + r) / 2;
return query(2 * i + 1, l, m, qs, qe) | query(2 * i + 2, m + 1, r, qs, qe);
}
int32_t main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int n, m;
int ans;
cin >> n >> m;
Query queries[m];
for (int i = 0; i < m; i++) {
cin >> queries[i].l;
cin >> queries[i].r;
cin >> queries[i].q;
update(0, 0, n - 1, queries[i].l - 1, queries[i].r - 1, queries[i].q);
}
for (int i = 0; i < n; i++) {
a[i] = query(0, 0, n - 1, i, i);
}
constructtree(0, 0, n - 1);
for (int i = 0; i < m; i++) {
ans = query2(0, 0, n - 1, queries[i].l - 1, queries[i].r - 1);
if (ans != queries[i].q) {
cout << "NO"
<< "\n";
return 0;
}
}
cout << "YES"
<< "\n";
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << "\n";
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int lim = 1e5 + 5;
int arr[lim][31], qur[lim][3], fin[lim], seg[4 * lim], q, n;
void make() {
cin >> n >> q;
for (int i = 0; i < q; ++i) {
cin >> qur[i][0] >> qur[i][1] >> qur[i][2];
for (int j = 0; j < 31; ++j)
if ((qur[i][2] >> j) & 1) arr[qur[i][0]][j]++, arr[qur[i][1] + 1][j]--;
}
for (int i = 0; i < 31; ++i)
for (int j = 1; j <= n; ++j) arr[j][i] += arr[j - 1][i];
for (int i = 0; i < 31; ++i)
for (int j = 1; j <= n; ++j) fin[j] |= (1 << i) * (arr[j][i] > 0);
}
void build(int x, int l, int r) {
if (l == r) {
seg[x] = fin[l];
return;
}
int m = (l + r) / 2;
build(2 * x + 1, l, m);
build(2 * x + 2, m + 1, r);
seg[x] = seg[2 * x + 1] & seg[2 * x + 2];
}
int query(int x, int l, int r, int a, int b) {
if (b < l || r < a || r < l) return 0xFFFFFFFF;
if (a <= l && r <= b) return seg[x];
int m = (l + r) / 2;
return query(2 * x + 1, l, m, a, b) & query(2 * x + 2, m + 1, r, a, b);
}
void check() {
build(0, 1, n);
int f = 1;
for (int i = 0; i < q; ++i)
f &= (query(0, 1, n, qur[i][0], qur[i][1]) == qur[i][2]);
if (f) {
cout << "YES\n";
for (int i = 1; i <= n; ++i) cout << fin[i] << ' ';
} else
cout << "NO";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
make();
check();
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int n;
class bit {
public:
int s[200005];
void up(int p, int x) {
while (p <= n) {
s[p] += x;
p += p & -p;
}
}
int get(int p) {
int re = 0;
while (p) {
re = re + s[p];
p -= p & -p;
}
return re;
}
void clear() { memset(s, 0, sizeof(s)); }
} tree[30], t[30];
int l[100005], r[100005], x[100005];
int ans[100005];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int k;
cin >> n >> k;
for (int i = 1; i <= k; i++) {
cin >> l[i] >> r[i] >> x[i];
for (int j = 0; j < 30; j++)
if (x[i] & (1 << j)) {
tree[j].up(l[i], 1);
tree[j].up(r[i] + 1, -1);
}
}
for (int i = 0; i < 30; i++) {
for (int j = 1; j <= n; j++) {
if (tree[i].get(j)) t[i].up(j, 1);
}
}
for (int i = 1; i <= k; i++) {
for (int j = 0; j < 30; j++)
if (!(x[i] & (1 << j))) {
if (t[j].get(r[i]) - t[j].get(l[i] - 1) == r[i] - l[i] + 1) {
cout << "NO";
return 0;
}
}
}
cout << "YES\n";
for (int i = 0; i < 30; i++) {
for (int j = 1; j <= n; j++) {
if (t[i].get(j) - t[i].get(j - 1)) ans[j] += (1 << i);
}
}
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
ProblemBInterestingTask solver = new ProblemBInterestingTask();
solver.solve(1, in, out);
out.close();
}
static class ProblemBInterestingTask {
int N;
int M;
int[][] qq;
public void solve(int testNumber, FastScanner s, PrintWriter out) {
N = s.nextInt();
M = s.nextInt();
qq = s.next2DIntArray(M, 3);
int[] a = new int[N + 1];
for (int pp = 0, p = 1; pp < 30; pp++, p <<= 1) {
int[] d = new int[N + 1];
for (int[] q : qq) {
if ((p & q[2]) > 0) {
d[q[0] - 1]++;
d[q[1]]--;
}
}
for (int i = 1; i <= N; i++) d[i] += d[i - 1];
for (int i = 0; i < N; i++)
if (d[i] > 0) a[i] += p;
}
int size = 1 + (int) Math.sqrt(N);
int[] div = new int[N + 1];
for (int i = 0; i <= N; i++)
div[i] = i / size;
int[] blks = new int[size + 1];
Arrays.fill(blks, (1 << 30) - 1);
for (int i = 0; i < N; i++) {
blks[div[i]] &= a[i];
}
for (int[] q : qq) {
int L = q[0] - 1, R = q[1] - 1;
int cur = a[L];
int i = L + 1;
while (i <= R && div[i] == div[L]) {
cur &= a[i];
i++;
}
while (div[i] < div[R]) {
cur &= blks[div[i]];
i += size;
}
while (i <= R) {
cur &= a[i];
i++;
}
if (cur != q[2]) {
out.println("NO");
return;
}
}
out.println("YES");
for (int i = 0; i < N; i++)
out.printf("%d ", a[i]);
out.println();
}
}
static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public int[] nextIntArray(int N) {
int[] ret = new int[N];
for (int i = 0; i < N; i++)
ret[i] = this.nextInt();
return ret;
}
public int[][] next2DIntArray(int N, int M) {
int[][] ret = new int[N][];
for (int i = 0; i < N; i++)
ret[i] = this.nextIntArray(M);
return ret;
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import static java.lang.System.in;
import static java.lang.System.out;
import java.io.*;
import java.util.*;
public class B482bitSegmentTree {
static final double EPS = 1e-10;
static final int INF = (1 << 30)-1;
static final double PI = Math.PI;
public static Scanner sc = new Scanner(in);
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
class RMQ{
int [] bit;
int n;
RMQ(int n_){
for (n=1; n<n_; n*=2);
bit = new int[2*n-1];
for (int i=0; i<2*n-1; i++)
bit[i] = INF;
}
void update(int i, int x){
i+=n-1;
bit[i]=x;
while(i>0){
i=(i-1)/2;
bit[i]= bit[i*2+1] & bit[i*2+2];
}
}
int and(int a, int b, int k, int l, int r){
if (r<=a || b<=l) return INF;
if (a<=l && r<=b) return bit[k];
int vl = and(a,b,k*2+1, l, (l+r)/2);
int vr = and(a,b,k*2+2, (l+r)/2, r);
return vl & vr;
}
}
public void run() throws IOException {
String input;
String[] inputArray;
input = br.readLine();
inputArray = input.split(" ");
int n = Integer.valueOf(inputArray[0]);
int m = Integer.valueOf(inputArray[1]);
int [] l = new int [m];
int [] r = new int [m];
int [] q = new int [m];
int [][] s = new int[n+1][30];
for (int i=0; i<m; i++){
input = br.readLine();
inputArray = input.split(" ");
l[i] = Integer.valueOf(inputArray[0])-1;
r[i] = Integer.valueOf(inputArray[1])-1;
q[i] = Integer.valueOf(inputArray[2]);
for (int b=0; b<30; b++){
if (( (1<<b) & q[i] )>0){
s[l[i]][b]++;
s[r[i]+1][b]--;
}
}
}
int [] a = new int[n];
for (int b=0; b<30; b++)
for (int x=0; x<n; x++){
if (x>0) s[x][b]+=s[x-1][b];
if (s[x][b]>0) a[x]+=1<<b;
}
RMQ seg = new RMQ(n);
for (int i=0; i<n; i++)
seg.update(i,a[i]);
int n_;
for (n_=1; n_<n; n_*=2);
for (int i=0; i<m; i++){
if (q[i]!=seg.and(l[i], r[i]+1, 0, 0, n_)){
sb.append("NO\n");
ln(sb);
return;
}
}
sb.append("YES\n");
for (int i=0; i<n-1; i++)
sb.append(a[i] + " ");
sb.append(a[n-1]+"\n");
ln(sb);
}
public static void main(String[] args) throws IOException {
new B482bitSegmentTree().run();
}
public static void ln(Object obj) {
out.print(obj);
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Mahmoud Aladdin <aladdin3>
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader jin, OutputWriter jout) {
int n = jin.int32();
int m = jin.int32();
initTree(n);
int[] x = new int[m];
int[] y = new int[m];
int[] q = new int[m];
for(int i = 0; i < m; i++) {
x[i] = jin.int32() - 1;
y[i] = jin.int32() - 1;
q[i] = jin.int32();
update(q[i], x[i], y[i]);
}
boolean valid = true;
for(int i = 0; i < m; i++) {
valid &= (queryRange(x[i], y[i]) == q[i]);
}
if(valid) {
jout.println("YES");
for(int i = 0; i < n; i++) {
if(i != 0) jout.print(" ");
jout.print(queryRange(i, i));
}
jout.println();
} else {
jout.println("NO");
}
}
private int queryRange(int l, int r) {
return __queryRange(l, r, 0, 0, n - 1);
}
private int __queryRange(int l, int r, int id, int s, int e) {
if(l > e || r < s) return (1 << 30) - 1;
if(l <= s && e <= r) {
return values[id];
} else {
int m = (s + e) >> 1;
if(lazy[id]) {
__update(lazyVal[id], s, m, 2 * id + 1, s, m);
__update(lazyVal[id], m + 1, e, 2 * id + 2, m + 1, e);
lazy[id] = false;
lazyVal[id] = 0;
}
int v1 = __queryRange(l, r, 2 * id + 1, s, m);
int v2 = __queryRange(l, r, 2 * id + 2, m + 1, e);
return v1 & v2;
}
}
private void update(int v, int l, int r) {
__update(v, l, r, 0, 0, n - 1);
}
private void __update(int v, int cl, int cr, int id, int l, int r) {
if(cl > r || cr < l) return;
if(l == r) {
values[id] |= v;
lazy[id] = false;
} else if(cl <= l && r <= cr) {
values[id] |= v; lazy[id] = true;
lazyVal[id] |= v;
} else {
int m = (l + r) >> 1;
if(lazy[id]) {
__update(lazyVal[id], l, m, 2 * id + 1, l, m);
__update(lazyVal[id], m + 1, r, 2 * id + 2, m + 1, r);
lazyVal[id] = 0; lazy[id] = false;
}
__update(v, cl, cr, 2 * id + 1, l, m);
__update(v, cl, cr, 2 * id + 2, m + 1, r);
values[id] = values[2*id+1] & values[2*id+2];
}
}
private void initTree(int length) {
n = length;
values = new int[length * 10];
lazyVal = new int[length * 10];
lazy = new boolean[length * 10];
}
int n;
boolean[] lazy;
int[] values;
int[] lazyVal;
}
class InputReader {
private static final int bufferMaxLength = 1024;
private InputStream in;
private byte[] buffer;
private int currentBufferSize;
private int currentBufferTop;
private static final String tokenizers = " \t\r\f\n";
public InputReader(InputStream stream) {
this.in = stream;
buffer = new byte[bufferMaxLength];
currentBufferSize = 0;
currentBufferTop = 0;
}
private boolean refill() {
try {
this.currentBufferSize = this.in.read(this.buffer);
this.currentBufferTop = 0;
} catch(Exception e) {}
return this.currentBufferSize > 0;
}
private Byte readChar() {
if(currentBufferTop < currentBufferSize) {
return this.buffer[this.currentBufferTop++];
} else {
if(!this.refill()) {
return null;
} else {
return readChar();
}
}
}
public String token() {
StringBuffer tok = new StringBuffer();
Byte first;
while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) != -1));
if(first == null) return null;
tok.append((char)first.byteValue());
while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) == -1)) {
tok.append((char)first.byteValue());
}
return tok.toString();
}
public Integer int32() throws NumberFormatException {
String tok = token();
return tok == null? null : Integer.parseInt(tok);
}
}
class OutputWriter {
private final int bufferMaxOut = 1024;
private PrintWriter out;
private StringBuilder output;
private boolean forceFlush = false;
public OutputWriter(OutputStream outStream) {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outStream)));
output = new StringBuilder(2 * bufferMaxOut);
}
public OutputWriter(Writer writer) {
forceFlush = true;
out = new PrintWriter(writer);
output = new StringBuilder(2 * bufferMaxOut);
}
private void autoFlush() {
if(forceFlush || output.length() >= bufferMaxOut) {
flush();
}
}
public void print(Object... tokens) {
for(int i = 0; i < tokens.length; i++) {
if(i != 0) output.append(' ');
output.append(tokens[i]);
}
autoFlush();
}
public void println(Object... tokens) {
print(tokens);
output.append('\n');
autoFlush();
}
public void flush() {
out.print(output);
output.setLength(0);
}
public void close() {
flush();
out.close();
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int dx[100005][30];
int n, q, s[100005], e[100005], x[100005];
int main() {
scanf("%d %d", &n, &q);
for (int i = 0; i < q; i++) {
scanf("%d %d %d", &s[i], &e[i], &x[i]);
for (int j = 0; j < 30; j++) {
if ((x[i] >> j) & 1) {
dx[s[i]][j]++;
dx[e[i] + 1][j]--;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 30; j++) {
dx[i][j] += dx[i - 1][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 30; j++) {
dx[i][j] = min(dx[i][j], 1);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 30; j++) {
dx[i][j] += dx[i - 1][j];
}
}
for (int i = 0; i < q; i++) {
int ret = 0;
for (int j = 0; j < 30; j++) {
int bad = dx[e[i]][j] - dx[s[i] - 1][j];
if (bad == e[i] - s[i] + 1 && ((x[i] >> j) & 1)) {
continue;
} else if (bad != e[i] - s[i] + 1 && (((x[i] >> j) & 1) == 0)) {
continue;
} else {
puts("NO");
return 0;
}
}
}
puts("YES");
for (int i = 1; i <= n; i++) {
int ret = 0;
for (int j = 0; j < 30; j++) {
if (dx[i][j] != dx[i - 1][j]) ret |= (1 << j);
}
printf("%d ", ret);
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.util.*;
import java.io.*;
import java.math.*;
public class Solution{
public static int n,m;
public static int[] l;
public static int[] r;
public static int[] q;
public static int[][] a;
public static int[][] beg;
public static int[][] end;
public static int k;
public static int[][] lookup ;
public static int query(int l,int r){
int d = (int)(Math.log(r-l+1)/Math.log(2));
int res = 0;
res = lookup[l][d]&lookup[r-(1<<d)+1][d];
//System.out.println(l+" "+(1<<d)+" "+(r-(1<<d)+1)+" "+(1<<d));
return res;
}
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
l = new int[m];
r = new int[m];
q = new int[m];
for(int i=0;i<m;i++){
st = new StringTokenizer(br.readLine());
l[i] = Integer.parseInt(st.nextToken());
r[i] = Integer.parseInt(st.nextToken());
q[i] = Integer.parseInt(st.nextToken());
}
a = new int[n+2][30];
beg = new int[n+2][30];
end = new int[n+2][30];
for(int i=0;i<m;i++){
char[] c = Integer.toBinaryString(q[i]).toCharArray();
for(int j=0;j<c.length;j++){
beg[l[i]][j] += (int)(c[c.length-j-1]-'0');
end[r[i]+1][j] += (int)(c[c.length-j-1]-'0');
}
}
for(int i=1;i<=n+1;i++){
for(int j=0;j<30;j++){
a[i][j] = a[i-1][j] + beg[i][j] - end[i][j];
}
}
int[] num = new int[n];
for(int i=0;i<n;i++){
for(int j=0;j<30;j++) if(a[i+1][j]>0) num[i] += (1<<j);
}
boolean ok = true;
k = (int)(Math.log(n)/Math.log(2))+1;
lookup = new int[n][k];
for(int i=0;i<n;i++){
lookup[i][0] = num[i];
}
for(int j=1;j<k;j++){
for(int i=0;i<=n-(1<<j);i++){
lookup[i][j] = lookup[i][j-1]&lookup[i+(1<<(j-1))][j-1];
//out.println(i+" "+(1<<j)+" "+lookup[i][j]);
}
}
for(int i=0;i<m;i++){
int u = query(l[i]-1,r[i]-1);
//out.println(u+" "+q[i]);
if(u!=q[i]){
ok = false;
break;
}
}
//ok = true;
if(ok){
out.println("YES");
for(int i=0;i<n-1;i++) out.print(num[i]+" ");
out.println(num[n-1]);
}else{
out.println("NO");
}
out.flush();
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)1e5 + 10;
const int MOD = (int)1e9 + 7;
int rs[MAXN * 4], vl[MAXN * 4];
void update(int l, int r, int x, int y, int z, int rt) {
if (l == x && r == y) {
vl[rt] |= z;
return;
}
int mid = l + r >> 1;
if (mid >= y)
update(l, mid, x, y, z, (rt << 1));
else if (mid < x)
update(mid + 1, r, x, y, z, (rt << 1 | 1));
else {
update(l, mid, x, mid, z, (rt << 1));
update(mid + 1, r, mid + 1, y, z, (rt << 1 | 1));
}
}
int query(int l, int r, int x, int y, int rt) {
if (l == x && r == y) {
return vl[rt];
}
int mid = l + r >> 1;
if (mid >= y)
return vl[rt] | query(l, mid, x, y, (rt << 1));
else if (mid < x)
return vl[rt] | query(mid + 1, r, x, y, (rt << 1 | 1));
else {
return vl[rt] | (query(l, mid, x, mid, (rt << 1)) &
query(mid + 1, r, mid + 1, y, (rt << 1 | 1)));
}
}
int L[MAXN], R[MAXN], q[MAXN];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &L[i], &R[i], &q[i]);
update(1, n, L[i], R[i], q[i], 1);
}
int flag = 1;
for (int i = 1; i <= m; i++) {
if (query(1, n, L[i], R[i], 1) == q[i])
flag = 1;
else {
flag = 0;
break;
}
}
if (flag) {
printf("YES\n");
for (int i = 1; i <= n; i++) printf("%d ", query(1, n, i, i, 1));
printf("\n");
} else
printf("NO\n");
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
const long long maxn = 1e5 + 9, modn = 1e9 + 7;
using namespace std;
long long n, m, l[maxn], r[maxn], q[maxn], s[4 * maxn], a[maxn];
void query(long long x, long long y, long long q, long long v = 1,
long long l = 0, long long r = n) {
if (x >= r || y <= l) return;
if (l >= x && r <= y) {
s[v] |= q;
return;
}
long long mid = (l + r) / 2;
query(x, y, q, 2 * v, l, mid);
query(x, y, q, 2 * v + 1, mid, r);
}
void solve(long long ind, long long ans, long long v = 1, long long l = 0,
long long r = n) {
ans |= s[v];
if (r - l == 1) {
a[ind] = ans;
return;
}
long long mid = (l + r) / 2;
if (ind < mid)
solve(ind, ans, 2 * v, l, mid);
else
solve(ind, ans, 2 * v + 1, mid, r);
}
void build(long long v = 1, long long l = 0, long long r = n) {
if (r - l == 1) {
s[v] = a[l];
return;
}
long long mid = (l + r) / 2;
build(2 * v, l, mid);
build(2 * v + 1, mid, r);
s[v] = s[2 * v] & s[2 * v + 1];
}
long long get(long long x, long long y, long long v = 1, long long l = 0,
long long r = n) {
long long k = (1 << 30) - 1;
if (x >= r || y <= l) return k;
if (l >= x && r <= y) return s[v];
long long mid = (l + r) / 2;
return (get(x, y, 2 * v, l, mid) & get(x, y, 2 * v + 1, mid, r));
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (long long i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> q[i];
query(l[i] - 1, r[i], q[i]);
}
for (long long i = 0; i < n; i++) solve(i, 0);
build();
for (long long i = 0; i < m; i++) {
long long h = get(l[i] - 1, r[i]);
if (h != q[i]) {
cout << "NO";
return 0;
}
}
cout << "YES" << endl;
for (long long i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
struct dat {
int L, R, V;
} a[100005];
int N, M;
bool cmp(dat a, dat b) {
if (a.L != b.L) return a.L < b.L;
if (a.R != b.R) return a.R < b.R;
return a.V < b.V;
}
int ans[100005], s[100005];
void build(int k) {
int last = 0;
for (int i = (1), _b = (M); i <= _b; i++)
if (a[i].V >> k & 1) {
for (int j = (max(last + 1, a[i].L)), _b = (a[i].R); j <= _b; j++)
ans[j] |= (1 << k);
last = max(last, a[i].R);
}
memset(s, 0, sizeof s);
for (int i = (1), _b = (N); i <= _b; i++)
s[i] = s[i - 1] + (!(ans[i] >> k & 1));
for (int i = (1), _b = (M); i <= _b; i++)
if (!(a[i].V >> k & 1))
if (s[a[i].R] == s[a[i].L - 1]) {
cout << "NO\n";
exit(0);
}
}
int main(void) {
cin >> N >> M;
for (int i = (1), _b = (M); i <= _b; i++) cin >> a[i].L >> a[i].R >> a[i].V;
sort(a + 1, a + M + 1, cmp);
for (int i = (0), _b = (29); i <= _b; i++) build(i);
cout << "YES\n";
for (int i = (1), _b = (N); i <= _b; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 100010, D = 30;
int n, m, c[D], a[N];
int l[N], r[N], q[N], s[N][D];
vector<int> p[N], k[N];
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &l[i], &r[i], &q[i]);
p[l[i]].push_back(q[i]);
k[r[i]].push_back(q[i]);
}
for (int i = 1; i <= n; i++) {
for (auto j : p[i])
for (int t = 0; t < D; t++) c[t] += 1 & (j >> t);
for (int j = 0; j < D; j++)
if (c[j] > 0) a[i] |= 1 << j;
for (auto j : k[i])
for (int t = 0; t < D; t++) c[t] -= 1 & (j >> t);
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < D; j++) {
s[i][j] = s[i - 1][j];
if (!(1 & (a[i] >> j))) s[i][j]++;
}
for (int i = 1; i <= m; i++) {
bool f = true;
for (int j = 0; j < D; j++)
if (!(1 & (q[i] >> j)) && !(s[r[i]][j] - s[l[i] - 1][j])) {
f = false;
break;
}
if (!f) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
for (int i = 1; i <= n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int ar[101000], a, b, c, n, m;
vector<pair<pair<int, int>, int> > V;
vector<pair<int, pair<int, int> > > A;
int ans[101000];
bool cari(int k) {
A.clear();
for (int i = 0; i < V.size(); i++)
if ((1 << k) & (V[i].second))
A.push_back(make_pair(1, V[i].first));
else
A.push_back(make_pair(0, V[i].first));
memset(ar, 0, sizeof(ar));
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
int i;
for (i = 0; i < A.size(); i++) {
if (A[i].first == 0) break;
ar[A[i].second.first] += 1;
ar[A[i].second.second + 1] -= 1;
}
int a = 0;
for (int j = 1; j <= n; j++) {
a += ar[j];
if (a > 0)
ar[j] = ar[j - 1] + 1;
else
ar[j] = ar[j - 1];
}
for (i = i; i < A.size(); i++) {
if (A[i].first == 1) continue;
int panjang = ar[A[i].second.second] - ar[A[i].second.first - 1];
if (panjang == A[i].second.second - A[i].second.first + 1) return 0;
}
return 1;
}
int main() {
scanf("%d%d", &n, &m);
V.clear();
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
V.push_back(make_pair(make_pair(a, b), c));
}
bool valid = 1;
memset(ans, 0, sizeof(ans));
for (int i = 0; i < 30; i++) {
valid &= cari(i);
for (int j = 1; j <= n; j++) {
int k = ar[j] - ar[j - 1];
if (k == 0) continue;
ans[j] |= (1 << i);
}
}
printf("%s\n", (valid) ? "YES" : "NO");
if (valid) {
bool f = 0;
for (int i = 1; i <= n; i++) {
if (f) printf(" ");
f = 1;
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Rubanenko
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
int[] g;
int get(int v, int tl, int tr, int l, int r) {
if (tl > r || l > tr) return Integer.MAX_VALUE;
if (l <= tl && r >= tr) return g[v];
int c = (tl + tr) >> 1;
return get(v + v, tl, c, l, r) & get(v + v + 1, c + 1, tr, l, r);
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] l = new int [m + 1];
int[] r = new int [m + 1];
int[] value = new int [m + 1];
int[][] f = new int[32][n + 2];
for (int i = 1; i <= m; i++) {
l[i] = in.nextInt();
r[i] = in.nextInt();
value[i] = in.nextInt();
for (int j = 0; j < 30; j++) {
if (((1 << j) & value[i]) > 0) {
f[j][l[i]]++;
f[j][r[i] + 1]--;
}
}
}
int[] count = new int[32];
int[] a = new int[n + 2];
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 30; j++) {
count[j] += f[j][i];
if (count[j] > 0) a[i] += (1 << j);
}
}
int sz = 1;
while (sz < n) {
sz *= 2;
}
g = new int[sz * 2 + 2];
for (int i = 1; i <= n; i++) {
g[i + sz - 1] = a[i];
}
for (int i = n + 1; i <= sz; i++) {
g[i + sz - 1] = Integer.MAX_VALUE;
}
for (int i = sz - 1;i > 0; i--) {
g[i] = g[i + i] & g[i + i + 1];
}
for (int i = 1; i <= n; i++) {
if (a[i] >= (1 << 30)) {
out.println("NO");
return;
}
}
for (int i = 1; i <= m; i++) {
if (get(1, 1, sz, l[i], r[i]) != value[i]) {
out.println("NO");
return;
}
}
out.println("YES");
for (int i = 1; i < n; i++) {
out.print(a[i] + " ");
}
out.println(a[n]);
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
public String nextLine() {
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(nextLine());
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
struct sequence {
int t[100010], s[100010];
inline void insert(int l, int r) {
t[l]++;
t[r + 1]--;
}
inline void modify(int n) {
for (int i = 1; i <= n; i++) t[i] += t[i - 1];
for (int i = 1; i <= n; i++) t[i] = t[i] > 0;
for (int i = 1; i <= n; i++) s[i] = s[i - 1] + t[i];
}
inline bool query(int l, int r) {
int now = s[r] - s[l - 1];
if (now == r - l + 1)
return 0;
else
return 1;
}
inline bool query(int pos) { return t[pos]; }
} t[32];
int a[100010];
int l[100010], r[100010], q[100010];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) scanf("%d%d%d", &l[i], &r[i], &q[i]);
for (int i = 1; i <= m; i++)
for (int j = 0; j < 30; j++)
if (q[i] & (1 << j)) t[j].insert(l[i], r[i]);
for (int i = 0; i < 30; i++) t[i].modify(n);
bool flag = 1;
for (int i = 1; i <= m; i++)
for (int j = 0; j < 30; j++)
if (!(q[i] & (1 << j))) flag &= t[j].query(l[i], r[i]);
if (!flag)
puts("NO");
else {
puts("YES");
for (int i = 1; i <= n; i++)
for (int j = 0; j < 30; j++)
if (t[j].query(i)) a[i] |= 1 << j;
for (int i = 1; i < n; i++) printf("%d ", a[i]);
printf("%d\n", a[n]);
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
if (x == 0) return y;
return gcd(y % x, x);
}
long long powmod(long long x, long long y, long long m) {
if (y == 0) return 1;
long long p = powmod(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
long long mul_inv(long long a, long long b) {
long long b0 = b, t, q;
long long x0 = 0ll, x1 = 1ll;
if (b == 1ll) return 1ll;
while (a > 1ll) {
q = a / b;
t = b, b = a % b, a = t;
t = x0, x0 = x1 - q * x0, x1 = t;
}
if (x1 < 0ll) x1 += b0;
return x1;
}
bool fa(vector<long long> &x, vector<long long> &y) { return x[0] < y[0]; }
bool fa1(vector<long long> &x, vector<long long> &y) { return x[1] < y[1]; }
bool f1(pair<long long, long long> &x, pair<long long, long long> &y) {
return x.second > y.second;
}
bool f2(pair<long long, long long> &x, pair<long long, long long> &y) {
return x.first > y.first;
}
bool f3(long long &x, long long &y) { return x > y; }
const long long maxn = 1e4 + 10;
bool meow1(vector<long long> &x, vector<long long> &y) { return x[3] < y[3]; }
bool meow2(vector<long long> &x, vector<long long> &y) { return x[2] < y[2]; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
long long m;
cin >> m;
vector<array<long long, 3> > b(m);
long long c[n + 1][31];
long long d[n + 1][31];
long long f[n + 1][31];
long long ans[n];
for (int i = 0; i < n; i++) ans[i] = 0;
for (int i = 0; i < n + 1; i++) {
for (int t = 0; t < 31; t++) {
c[i][t] = 0;
d[i][t] = 0;
f[i][t] = 0;
}
}
for (int i = 0; i < m; i++) {
cin >> b[i][0] >> b[i][1] >> b[i][2];
b[i][0]--;
long long x = b[i][2];
long long it = 0;
while (x) {
c[b[i][0]][it] += x % 2ll;
c[b[i][1]][it] -= x % 2ll;
x /= 2ll;
it++;
}
}
for (long long i = 0; i <= n; i++) {
for (int t = 0; t < 31; t++) {
if (i > 0) c[i][t] += c[i - 1][t];
if (c[i][t] > 0) {
ans[i] += pow(2ll, t);
d[i][t]++;
}
}
}
for (int i = 0; i < n + 1; i++) {
for (int t = 0; t < 31; t++) {
f[i + 1][t] = f[i][t] + d[i][t];
}
}
long long ch = 0;
for (int i = 0; i < m; i++) {
long long len = b[i][1] - b[i][0];
for (int t = 0; t < 31; t++) {
if ((((1ll << t) & b[i][2]) == (1ll << t)) !=
(f[b[i][1]][t] - f[b[i][0]][t] == len)) {
ch++;
break;
}
}
}
if (ch) {
cout << "NO\n";
} else {
cout << "YES\n";
for (auto x : ans) cout << x << " ";
cout << "\n";
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(System.in));
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
while (in.hasNext()) {
int n = in.nextInt(), m = in.nextInt();
int[] a = new int[n + 1], l = new int[m], r = new int[m], q = new int[m];
for (int i = 0; i < m; i++) {
l[i] = in.nextInt();
r[i] = in.nextInt();
q[i] = in.nextInt();
}
boolean ok = true;
for (int i = 0; i < 30 && ok; i++) {
int[] sum = new int[n + 2], sum2 = new int[n + 2];
for (int j = 0; j < m; j++) {
if ((q[j] >> i & 1) == 1) {
sum[l[j]]++;
sum[r[j] + 1]--;
}
}
for (int j = 1; j <= n; j++)
sum[j] += sum[j - 1];
for (int j = 1; j <= n; j++)
if (sum[j] > 0)
sum[j] = 1;
for (int j = 1; j <= n; j++)
sum2[j] = sum2[j - 1] + sum[j];
for (int j = 0; j < m; j++)
if ((q[j] & (1 << i)) == 0 && sum2[r[j]] - sum2[l[j] - 1] == r[j] - l[j] + 1)
ok = false;
for (int j = 1; j <= n && ok; j++)
a[j] |= sum[j] << i;
}
if (!ok)
out.println("NO");
else {
out.println("YES");
for (int i = 1; i <= n; i++)
out.print(a[i] + (i == n ? "\n" : " "));
}
out.flush();
}
in.close();
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 |
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[]args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int n = sc.nextInt() , m =sc.nextInt();
int arr [] = new int[n];
SegmentTree segmentTree = new SegmentTree (arr);
Triple [] pairs = new Triple[m];
for(int i = 0 ; i < m ; ++i)
{
int left =sc.nextInt() -1 , right = sc.nextInt() - 1 , val = sc.nextInt();
segmentTree.make (left , right , val);
pairs[i] = new Triple(left , right , val);
}
for(int i = 0 ; i < m ; ++i)
if(segmentTree.query(pairs[i].left , pairs[i].right)!=pairs[i].val)
{
System.out.println("NO");
return;
}
System.out.println("YES");
segmentTree.fullPropagation();
int start = 1 ;
while(start < n) start<<=1;
for(int i = 0 ; i < n ; ++i)
System.out.print(segmentTree.query(i ,i) + " ");
out.flush();
out.close();
}
static class SegmentTree {
int [] tree , lazy;
int N ;
SegmentTree(int A [])
{
N = A.length;
tree = new int[(N << 1) << 1];
lazy = new int[(N << 1) << 1];
}
void make (int l , int r , int num) {make (1 , 0 , N - 1 , l , r , num);}
void make (int node , int start , int end , int left , int right , int num)
{
if(lazy[node]!= 0)
{
tree[node] |=lazy[node];
if(start != end) {
lazy[left(node)] |= lazy[node];
lazy[right(node)] |= lazy[node];
}
lazy[node] = 0;
}
if(start > right || end < left)
return ;
if (start >= left && end <= right)
{
tree[node]|=num;
if(start != end) {
lazy[left(node)] |= num;
lazy[right(node)] |= num;
}
return;
}
make(left(node) , start , mid(start ,end) , left , right , num);
make(right(node) , mid(start , end) + 1 , end , left , right , num);
tree[node] = tree[left(node)] & tree[right(node)];
}
int query (int l , int r) {return query(1 , 0 , N-1 , l , r);}
int query (int node , int start , int end , int left , int right)
{
if(lazy[node]!=0)
{
tree[node] |=lazy[node];
if(start != end) {
lazy[left(node)] |= lazy[node];
lazy[right(node)] |= lazy[node];
}
lazy[node] = 0;
}
if(start > right || end < left)
return -1 ;
if (start >= left && end <= right)
return tree[node];
return query(left(node) , start , mid(start ,end) , left , right) & query(right(node) , mid(start , end) + 1 , end , left , right);
}
void fullPropagation () {fullPropagation(1 , 0 , N-1);}
void fullPropagation (int node , int start , int end)
{
if(lazy[node]!=0)
{
tree[node] |=lazy[node];
if(start != end) {
lazy[left(node)] |= lazy[node];
lazy[right(node)] |= lazy[node];
}
lazy[node] = 0;
}
if(start != end)
{
fullPropagation(left(node) , start , mid(start , end));
fullPropagation(right(node) , mid(start ,end) + 1, end);
}
}
int left (int pos ) {return pos << 1 ;}
int right (int pos ) {return (pos << 1) + 1;}
int mid (int l , int r){return (l + r) >> 1;}
}
static class Triple {
int left , right , val;
Triple (int l , int r , int v)
{
left = l;
right = r;
val = v;
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
class Node {
public:
int left;
int right;
int value;
};
class segTree {
public:
Node *tree;
int length;
segTree(int length) {
this->length = length;
tree = new Node[length * 4];
createTree(0, 0, length - 1);
}
void createTree(int root, int left, int right);
void setBits(int root, int left, int right, int bits, int parientValue);
int querybit(int root, int left, int right);
int queryValue(int root, int index);
};
void segTree::createTree(int root, int left, int right) {
tree[root].left = left;
tree[root].right = right;
tree[root].value = 0;
if (left == right) {
return;
} else {
int mid = (left + right) / 2;
createTree(root * 2 + 1, left, mid);
createTree(root * 2 + 2, mid + 1, right);
return;
}
}
void segTree::setBits(int root, int left, int right, int bits,
int parientValue) {
tree[root].value |= parientValue;
if (left <= tree[root].left && right >= tree[root].right) {
tree[root].value |= bits;
return;
}
if (left > tree[root].right || right < tree[root].left) return;
setBits(root * 2 + 1, left, right, bits, tree[root].value);
setBits(root * 2 + 2, left, right, bits, tree[root].value);
tree[root].value = tree[root * 2 + 1].value & tree[root * 2 + 2].value;
}
int segTree::querybit(int root, int left, int right) {
if (left <= tree[root].left && right >= tree[root].right) {
return tree[root].value;
}
if (left > tree[root].right || right < tree[root].left) {
return 0x7FFFFFFF;
} else {
return querybit(root * 2 + 1, left, right) &
querybit(root * 2 + 2, left, right);
}
}
int segTree::queryValue(int root, int index) {
if (index < tree[root].left || index > tree[root].right) {
return 0;
}
if (tree[root].left == tree[root].right) {
return tree[root].value;
}
return tree[root].value | queryValue(root * 2 + 1, index) |
queryValue(root * 2 + 2, index);
}
int main(void) {
int n, m;
int *l, *r, *q;
cin >> n >> m;
l = new int[m];
r = new int[m];
q = new int[m];
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> q[i];
l[i]--;
r[i]--;
}
segTree T(n);
for (int i = 0; i < m; i++) {
T.setBits(0, l[i], r[i], q[i], 0);
}
for (int i = 0; i < m; i++) {
if (T.querybit(0, l[i], r[i]) != q[i]) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
for (int i = 0; i < n; i++) cout << T.queryValue(0, i) << " ";
cout << endl;
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.*;
import java.util.*;
public class Main {
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
InputStream inputStream = System.in;
InputReader in = new InputReader(inputStream);
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
new Main().solve(in, out);
out.close();
}
static class Node {
int value;
int left;
int right;
public Node(int value, int left, int right) {
this.value = value;
this.left = left;
this.right = right;
}
}
public static ArrayList<Node> makeSegmentTree(int initLen) {
int expLen = 1;
while (expLen < initLen) {
expLen *= 2;
}
int treeLen = expLen * 2 - 1;
ArrayList<Node> tree = new ArrayList<>(Collections.nCopies(treeLen, null));
int firstElemInd = treeLen - expLen;
for (int offset = 0; offset < expLen; ++offset) {
tree.set(firstElemInd + offset, new Node(0, offset, offset));
}
for (int i = firstElemInd - 1; i >= 0; --i) {
int left = tree.get(i * 2 + 1).left;
int right = tree.get(i * 2 + 2).right;
tree.set(i, new Node(0, left, right));
}
return tree;
}
private static void pushValue(ArrayList<Node> tree, int ind) {
Node node = tree.get(ind);
Node lhs = tree.get(ind * 2 + 1);
Node rhs = tree.get(ind * 2 + 2);
lhs.value |= node.value;
rhs.value |= node.value;
}
private static void updateNode(ArrayList<Node> tree, int ind) {
Node node = tree.get(ind);
Node lhs = tree.get(ind * 2 + 1);
Node rhs = tree.get(ind * 2 + 2);
node.value |= (lhs.value & rhs.value);
}
public static void addBitsQuery(ArrayList<Node> tree, int from, int to, int value, int ind) {
Node node = tree.get(ind);
int left = node.left;
int right = node.right;
if (right < from || left > to) {
} else if (left >= from && right <= to) {
node.value |= value;
} else {
pushValue(tree, ind);
addBitsQuery(tree, from, to, value, ind * 2 + 1);
addBitsQuery(tree, from, to, value, ind * 2 + 2);
updateNode(tree, ind);
}
}
public static int checkQuery(ArrayList<Node> tree, int from, int to, int ind) {
Node node = tree.get(ind);
int left = node.left;
int right = node.right;
if (right < from || left > to) {
return -1;
} else if (left >= from && right <= to) {
return node.value;
} else {
pushValue(tree, ind);
int lhs = checkQuery(tree, from, to, ind * 2 + 1);
int rhs = checkQuery(tree, from, to, ind * 2 + 2);
updateNode(tree, ind);
return lhs & rhs;
}
}
public void solve(InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] left = new int[m];
int[] right = new int[m];
int[] value = new int[m];
for (int i = 0; i < m; ++i) {
left[i] = in.nextInt();
right[i] = in.nextInt();
--left[i];
--right[i];
value[i] = in.nextInt();
}
ArrayList<Node> tree = makeSegmentTree(n);
for (int i = 0; i < m; ++i) {
addBitsQuery(tree, left[i], right[i], value[i], 0);
}
boolean succ = true;
for (int i = 0; i < m; ++i) {
int res = checkQuery(tree, left[i], right[i], 0);
if (res != value[i]) {
succ = false;
break;
}
}
if (!succ) {
out.println("NO");
} else {
out.println("YES");
for (int i = 0; i < n; ++i) {
int val = checkQuery(tree, i, i, 0);
out.print(String.valueOf(val) + ' ');
}
out.println();
}
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long INF = (1ll << 32) - 1;
long long sum[maxn << 2], add[maxn << 2];
int n, m;
struct Node {
int l, r;
long long q;
} nd[maxn];
void Pushup(int root);
void Pushdown(int root);
void update(long long q, int L, int R, int left, int right, int root);
long long query(int L, int R, int left, int right, int root);
void print(int left, int right, int root);
int main() {
while (cin >> n >> m) {
int flag = 1;
memset(sum, 0, sizeof(sum));
for (int i = 0; i < m; i++)
scanf("%d%d%I64d", &nd[i].l, &nd[i].r, &nd[i].q);
for (int i = 0; i < m; i++) update(nd[i].q, nd[i].l, nd[i].r, 1, n, 1);
for (int i = 0; i < m; i++)
if (nd[i].q != query(nd[i].l, nd[i].r, 1, n, 1)) {
flag = 0;
break;
}
if (flag) {
cout << "YES" << endl;
print(1, n, 1);
} else
cout << "NO" << endl;
}
return 0;
}
void Pushup(int root) { sum[root] = sum[root << 1] & sum[root << 1 | 1]; }
void Pushdown(int root) {
if (add[root]) {
sum[root << 1] |= add[root];
sum[root << 1 | 1] |= add[root];
add[root << 1] |= add[root];
add[root << 1 | 1] |= add[root];
add[root] = 0;
}
}
void update(long long q, int L, int R, int left, int right, int root) {
if (L <= left && right <= R) {
add[root] |= q;
sum[root] |= q;
return;
}
Pushdown(root);
int mid = (left + right) >> 1;
if (L <= mid) update(q, L, R, left, mid, root << 1);
if (R > mid) update(q, L, R, mid + 1, right, root << 1 | 1);
Pushup(root);
}
long long query(int L, int R, int left, int right, int root) {
if (L <= left && right <= R) {
return sum[root];
}
Pushdown(root);
int mid = (left + right) >> 1;
long long ret = INF;
if (L <= mid) ret &= query(L, R, left, mid, root << 1);
if (R > mid) ret &= query(L, R, mid + 1, right, root << 1 | 1);
Pushup(root);
return ret;
}
void print(int left, int right, int root) {
if (left == right) {
printf("%I64d%c", sum[root], left == n ? '\n' : ' ');
return;
}
Pushdown(root);
int mid = (left + right) >> 1;
print(left, mid, root << 1);
print(mid + 1, right, root << 1 | 1);
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int mod = (int)1e+9 + 7;
const double pi = acos(-1.0);
unsigned int t[32][100100];
bool x[100100];
unsigned int z[2 * 131072], k = 131072;
struct zp {
unsigned int l, r, q;
} zap[100100];
unsigned int getand(unsigned int l, unsigned int r) {
l += k;
r += k;
unsigned int o = z[l];
while (l < r) {
if (l & 1 == 1) {
o &= z[l];
l++;
}
if (!(r & 1)) {
o &= z[r];
r--;
}
l >>= 1;
r >>= 1;
}
if (l == r) {
o &= z[l];
}
return (o);
}
int main() {
unsigned int n, m;
scanf("%d%d", &n, &m);
for (unsigned int i = 0; i < m; i++) {
scanf("%d%d%d", &zap[i].l, &zap[i].r, &zap[i].q);
zap[i].l--, zap[i].r--;
unsigned int c = 1;
for (unsigned int f = 0; f < 32; f++, c <<= 1) {
if (zap[i].q & c) {
t[f][zap[i].l] += 1;
t[f][zap[i].r + 1] -= 1;
}
}
}
unsigned int sum[32] = {0};
for (unsigned int i = 0; i < n; i++) {
unsigned int X = 0, c = 1;
for (unsigned int f = 0; f < 32; f++, c <<= 1) {
sum[f] += t[f][i];
if (sum[f]) {
X |= c;
}
}
z[k + i] = X;
}
for (unsigned int i = k - 1; i > 0; i--) {
z[i] = (z[i * 2] & z[i * 2 + 1]);
}
bool ok = 1;
for (unsigned int i = 0; i < m; i++) {
if (getand(zap[i].l, zap[i].r) != zap[i].q) {
ok = 0;
break;
}
}
if (ok) {
printf("YES\n");
for (unsigned int i = 0; i < n; i++) {
printf("%d ", z[k + i]);
}
} else {
printf("NO\n");
}
return (0);
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
pair<pair<int, int>, int> a[101010];
cin >> n >> m;
int x, y, z;
for (int i = 1; i <= m; i++) {
cin >> x >> y >> z;
pair<int, int> p = make_pair(x, y);
pair<pair<int, int>, int> q = make_pair(p, z);
a[i] = q;
}
bool ok = true;
int rez[101010];
int rez2[101010];
for (int i = 0; i < 30; i++) {
memset(rez, 0, sizeof(rez));
for (int j = 1; j <= m; j++) {
if (a[j].second >> i & 1) {
rez[a[j].first.first]++;
rez[a[j].first.second + 1]--;
}
}
for (int j = 1; j <= n; j++) {
rez[j] += rez[j - 1];
}
for (int j = 1; j <= n; j++) {
rez[j] = (bool)(rez[j]);
rez2[j] |= (rez[j] << i);
}
for (int j = 1; j <= n; j++) {
rez[j] += rez[j - 1];
}
for (int j = 1; j <= m; j++) {
if (!(a[j].second >> i & 1)) {
if (rez[a[j].first.second] - rez[a[j].first.first - 1] ==
a[j].first.second - a[j].first.first + 1)
ok = false;
}
}
}
if (ok == true) {
cout << "YES\n";
for (int i = 1; i <= n; i++) {
cout << rez2[i] << " ";
}
} else {
cout << "NO\n";
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
struct _ {
ios_base::Init i;
_() {
cin.sync_with_stdio(0);
cin.tie(0);
}
} _;
int n, m;
const int N = 100 * 1000 + 1;
int l[N], r[N], q[N], sum[N];
int MAXBIT = 30;
int main() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> l[i] >> r[i] >> q[i];
r[i]++;
}
vector<int> a(n + 1, 0);
for (int bit = 0; bit < MAXBIT; ++bit) {
int curBit = 1 << bit;
for (int i = 0; i < n + 1; ++i) sum[i] = 0;
for (int i = 0; i < m; ++i) {
if (q[i] & curBit) {
sum[l[i]]++;
sum[r[i]]--;
}
}
for (int i = 2; i < n + 1; ++i) sum[i] += sum[i - 1];
for (int i = 1; i < n + 1; ++i) {
if (sum[i] > 0) {
sum[i] = 1;
a[i] |= curBit;
}
sum[i] += sum[i - 1];
}
for (int i = 0; i < m; ++i) {
bool hasBit = q[i] & curBit;
int cnt = sum[r[i] - 1] - sum[l[i] - 1];
if ((hasBit && cnt != (r[i] - l[i])) ||
(!hasBit && cnt == (r[i] - l[i]))) {
cout << "NO" << endl;
return 0;
}
}
}
cout << "YES" << endl;
for (int i = 1; i < n + 1; ++i) cout << a[i] << " ";
cout << endl;
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.*;
import java.util.*;
public class Main {
static PrintWriter pw;
static Scanner sc;
static int[] a, b, dp[];
static long ceildiv(long x, long y){ return (x+y-1)/y;}
public static void main(String[] args) throws Exception {
sc = new Scanner(System.in);
pw = new PrintWriter(System.out);
int n=sc.nextInt();
int len=1;
while(len<n)
len<<=1;
int[] arr=new int[len];
int q=sc.nextInt();
SegmentTree stree= new SegmentTree(arr);
Triple[] query= new Triple[q];
for(int i=0; i<q; i++){
int l=sc.nextInt()-1, r=sc.nextInt()-1, x=sc.nextInt();
stree.update(0, 0, arr.length-1, l, r, x);
query[i]=new Triple(l, r, x);
}
boolean yes=true;
stree.propagateAll(0, 0, arr.length-1);
for(int i=0; i<q && yes; i++)
yes=stree.query(0, 0, arr.length-1, query[i].x, query[i].y)==query[i].z;
if(!yes){
pw.println("NO");
pw.flush();
return;
}
pw.println("YES");
for(int i=0; i<n; i++)
pw.print(stree.tree[len-1+i]+" ");
pw.println();
pw.close();
}
static void printArr(int[] arr){
for(int i=0; i<arr.length-1; i++)
pw.print(arr[i]+" ");
pw.println(arr[arr.length-1]);
}
static void printArr(long[] arr){
for(int i=0; i<arr.length-1; i++)
pw.print(arr[i]+" ");
pw.println(arr[arr.length-1]);
}
static void printArr(Integer[] arr){
for(int i=0; i<arr.length; i++)
pw.print(arr[i]+" ");
pw.println();
}
static void printArr(char[] arr){
for(int i=0; i<arr.length; i++)
pw.print(arr[i]);
pw.println();
}
static void printArr(ArrayList list){
for(int i=0; i<list.size(); i++)
pw.print(list.get(i)+" ");
pw.println();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextInt();
return arr;
}
public Integer[] nextsort(int n) throws IOException{
Integer[] arr=new Integer[n];
for(int i=0; i<n; i++)
arr[i]=nextInt();
return arr;
}
public Pair nextPair() throws IOException{
return new Pair(nextInt(), nextInt());
}
public Pair[] nextPairArr(int n) throws IOException{
Pair[] arr=new Pair[n];
for(int i=0; i<n; i++)
arr[i]=new Pair(nextInt(), nextInt());
return arr;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y) {
this.x=x;
this.y=y;
}
public int hashCode() {
return this.x*1000+this.y;
}
public int compareTo(Pair p){
return this.x-p.x;
}
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
Pair p = (Pair) obj;
return this.x==p.x && this.y==p.y;
}
public boolean equal(Pair p){
if(this.x==p.x)
return this.y==p.y;
if(this.x==p.y)
return this.y==p.x;
else
return false;
}
public Pair clone(){
return new Pair(x, y);
}
public String toString(){
return this.x+" "+this.y;
}
}
static class Triple{
int x, y, z;
public Triple(int x, int y, int z){
this.x=x;
this.y=y;
this.z=z;
}
public int hashCode(){
return this.x*100+this.y;
}
public boolean equals(Object obj){
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
Triple t = (Triple) obj;
return this.x==t.x && this.y==t.y && this.z==t.z;
}
}
static class SegmentTree{
int[] tree;
int[] lazy, arr;
int[] child;
public SegmentTree(int[] arr){
this.arr=arr.clone();
tree=new int[arr.length*2-1];
child=new int[arr.length*2-1];
lazy=new int[arr.length*2-1];
build(0, 0, arr.length-1);
}
public void build(int i, int l, int r){
if(l==r){
child[i]=1;
}else{
int left=2*i+1, right=2*i+2;
int mid=(l+r)/2;
build(left, l, mid);
build(right, mid+1, r);
child[i]=(child[left]<<1);
}
}
public long query(int node, int l, int r, int i, int j){
if(l>j || r<i)
return (1<<30)-1;
if(l>=i && r<=j){
long ans=tree[node];
return ans;
}else{
int left=2*node+1, right=2*node+2, mid=(l+r)/2;
propagate(node, left, right);
long ans=query(left, l, mid, i, j)&query(right, mid+1, r, i, j);
return ans;
}
}
public void update(int node, int l, int r, int i, int j, int x){
if(l>j || r<i)
return;
if(l>=i && r<=j){
tree[node]|=x;
lazy[node]|=x;
}else{
int left=2*node+1, right=2*node+2, mid=(l+r)/2;
propagate(node, left, right);
update(left, l, mid, i, j, x);
update(right, mid+1, r, i, j, x);
tree[node]=tree[left]&tree[right];
}
}
public void propagate(int i, int l, int r){
lazy[l]|=lazy[i];
lazy[r]|=lazy[i];
tree[l]|=lazy[i];
tree[r]|=lazy[i];
lazy[i]=0;
}
public void propagateAll(int i, int l, int r){
if(l==r)
return;
int left=2*i+1, right=2*i+2, mid=(l+r)/2;
propagate(i, left, right);
propagateAll(left, l, mid); propagateAll(right, mid+1, r);
}
public void or(int[] arr, int x, int node){
for(int i=0; i<30; i++)
arr[i]=(x&(1<<i))==0?arr[i]:1;
}
public int[] and(int[] a, int[] b){
int[] c=new int[30];
for(int i=0; i<30; i++)
c[i]=Math.min(a[i], b[i]);
return c;
}
public long valueOf(int[] arr){
long ans=0l;
for(int i=0; i<30; i++)
ans+=arr[i]*1l*(1<<i);
return ans;
}
public int[] toArray(int x){
int[] arr=new int[30];
for(int i=0; i<20; i++)
arr[i]=(x&(1<<i))==0?0:1;
return arr;
}
}
static class FenwickTree{
long[] fen;
public FenwickTree(int[] arr){
fen=new long[arr.length+1];
}
public long query(int i, int j){
return query(j)-query(i-1);
}
public long query(int x){
long sum=0;
while(x>0){
sum+=fen[x];
x^=x&-x;
}
return sum;
}
public void update(int i, int x){
for(; i<fen.length; i+=i&-i)
fen[i]+=x;
}
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | /**
* Created by Aminul on 10/28/2017.
*/
import java.io.*;
import java.util.*;
public class CF482B {
public static void main(String[] args) throws Exception {
FastReader in = new FastReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = in.nextInt(), k = in.nextInt();
Node queries[] = new Node[k];
SegTree seg = new SegTree(n);
for(int i = 0; i < k; i++){
int l = in.nextInt(), r = in.nextInt(), q = in.nextInt();
queries[i] = new Node(l , r, q);
seg.update(l, r, q);
}
seg.build(1, 1, n);
// debug(seg.arr);
boolean ok = true;
for(Node q : queries){
// debug(seg.query(q.l, q.r));
ok &= seg.query(q.l, q.r) == q.q;
if(!ok) break;
}
if(!ok) pw.println("NO");
else{
pw.println("YES");
for(int i = 1;i <= n; i++){
pw.print(seg.arr[i]+ " ");
}
pw.println();
}
pw.close();
}
static class SegTree{
int n, or[] = new int[(int)4e5], lazy[] = new int[(int)4e5], and[] = new int[(int)4e5], arr[];
SegTree(int N){
n = N;
arr = new int[n+1];
}
void pushDown(int p, int l, int r){
if(lazy[p] == 0) return;
lazy[l] |= lazy[p];
lazy[r] |= lazy[p];
or[l] |= lazy[p];
or[r] |= lazy[p];
lazy[p] = 0;
}
void update(int a, int b, int v){
update(1, 1, n, a, b, v);
}
void update(int p, int s, int e, int a, int b, int v){
if(s >= a && e <= b){
or[p] |= v;
lazy[p] |= v;
return;
}
if(s > e || s > b || e < a) return;
int m = (s+e)/2, l = 2*p, r = l+1;
pushDown(p, l , r);
update(l, s, m, a, b, v);
update(r, m+1, e, a, b, v);
or[p] = or[l] | or[r];
}
void build(int p, int s, int e){
if(s == e){
and[p] = or[p];
arr[s] = and[p];
return;
}
if(s > e) return;
int m = (s+e)/2, l = 2*p, r = l+1;
pushDown(p, l , r);
build(l, s, m);
build(r, m+1, e);
and[p] = and[l] & and[r];
}
int query(int a, int b){
return query(1, 1, n, a, b);
}
int query(int p, int s, int e, int a, int b){
if(s >= a && e <= b){
return and[p];
}
if(s > e || s > b || e < a) return (1<<30)-1;
int m = (s+e)/2, l = 2*p, r = l+1;
pushDown(p, l , r);
return query(l, s, m, a, b) & query(r, m+1, e, a, b);
}
}
static class Node {
int l, r, q;
Node(int L, int R, int Q) {
l = L;
r = R;
q = Q;
}
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
static class FastReader {
InputStream is;
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
static final int ints[] = new int[128];
public FastReader(InputStream is) {
for (int i = '0'; i <= '9'; i++) ints[i] = i - '0';
this.is = is;
}
public int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
public String next() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = (num << 3) + (num << 1) + ints[b];
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = (num << 3) + (num << 1) + ints[b];
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public double nextDouble() {
return Double.parseDouble(next());
}
/* public char nextChar() {
return (char)skip();
}*/
public char[] next(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
/*private char buff[] = new char[1005];
public char[] nextCharArray(){
int b = skip(), p = 0;
while(!(isSpaceChar(b))){
buff[p++] = (char)b;
b = readByte();
}
return Arrays.copyOf(buff, p);
}*/
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.*;
import java.util.*;
import java.math.*;
public class CF483D {
static final int N=100005;
static int l[]=new int[N];
static int r[]=new int[N];
static int v[]=new int[N];
public static void main(String args[]) {
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
Seg_Tree seg=new Seg_Tree();
int n=in.nextInt();
int m=in.nextInt();
seg.build(1,n,1);
for(int i=0;i<m;i++) {
l[i]=in.nextInt();
r[i]=in.nextInt();
v[i]=in.nextInt();
seg.update(1,l[i],r[i],v[i]);
}
boolean flag=false;
for(int i=0;i<m;i++) {
int s=seg.query(1,l[i],r[i]);
if(s!=v[i]) {
flag=true;
break;
}
}
if(flag) {
out.println("NO");
}else {
out.println("YES");
seg.print(1);
for(int x:seg.vv) {
out.print(x+" ");
}
out.println("");
}
out.close();
}
}
class node {
int l,r;
int val,vis;
int mid;
node(){}
node(int l,int r,int val) {
this.l=l;
this.r=r;
this.val=val;
this.vis=0;
mid=(l+r)>>1;
}
}
class Seg_Tree {
static final int N=100005;
ArrayList<Integer> vv=new ArrayList<Integer>();
node t[]=new node[N<<2];
void print(int rt) {
if(t[rt].l==t[rt].r) {
vv.add(t[rt].val);
return;
}
push_down(rt);
print(rt<<1);
print(rt<<1|1);
}
void push_up(int rt) {
t[rt].val=t[rt<<1].val&t[rt<<1|1].val;
}
void push_down(int rt) {
if(t[rt].vis==0) return;
t[rt<<1].vis|=t[rt].vis;
t[rt<<1|1].vis|=t[rt].vis;
t[rt<<1].val|=t[rt].vis;
t[rt<<1|1].val|=t[rt].vis;
t[rt].vis=0;
}
void build(int l,int r,int rt) {
t[rt]=new node(l,r,0);
if(l==r) return;
int m=t[rt].mid;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
push_up(rt);
}
void update(int rt,int l,int r,int val) {
if(t[rt].l>=l&&r>=t[rt].r) {
t[rt].val|=val;
t[rt].vis|=val;
return;
}
push_down(rt);
int m=t[rt].mid;
if(m>=l) update(rt<<1,l,r,val);
if(m<r) update(rt<<1|1,l,r,val);
push_up(rt);
}
int query(int rt,int l,int r) {
if(t[rt].l>=l&&r>=t[rt].r) {
return t[rt].val;
}
push_down(rt);
//int res=(1<<30)-1;
int m=t[rt].mid;
if(m>=r) return query(rt<<1,l,r);
else if(m<l) return query(rt<<1|1,l,r);
else {
return query(rt<<1,l,r)&query(rt<<1|1,l,r);
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader=new BufferedReader(new InputStreamReader(stream));
tokenizer=null;
}
public String next() {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int a[100009][30];
int b[100009][30];
int c[30];
void solve() {
int n, m;
cin >> n >> m;
int mark[m][3];
c[0] = 1;
for (int i = 0; i < m; i++) {
cin >> mark[i][0] >> mark[i][1] >> mark[i][2];
int ele = mark[i][2];
int ind = 0;
int x = mark[i][0];
int y = mark[i][1];
while (ele) {
if (ele & 1) {
a[x][ind] += 1;
a[y + 1][ind] -= 1;
}
ele /= 2;
ind++;
}
}
for (int j = 1; j < 30; j++) c[j] = 2 * c[j - 1];
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 30; j++) {
a[i][j] += a[i - 1][j];
if (a[i][j] == 0)
b[i][j] = b[i - 1][j] + 1;
else
b[i][j] = b[i - 1][j];
}
}
for (int i = 0; i < m; i++) {
int ele = mark[i][2];
int ind = 0;
int x = mark[i][0];
int y = mark[i][1];
while (ind < 30) {
if (ele % 2 == 0) {
if (b[y][ind] - b[x - 1][ind] == 0) {
cout << "NO";
return;
}
}
ele /= 2;
ind++;
}
}
cout << "YES\n";
for (int i = 1; i <= n; i++) {
int vl = 0;
for (int j = 0; j < 30; j++)
if (a[i][j] > 0) vl += c[j];
cout << vl << " ";
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
const int N = 100100;
int n, m, l[N], r[N], q[N], c[N], s[N], a[N], f;
int go(int b) {
memset(c, 0, sizeof(c));
for (int i = 0; i < m; ++i)
if (q[i] & b) ++c[l[i]], --c[r[i] + 1];
for (int i = 1; i <= n; ++i)
c[i] += c[i - 1], s[i] = s[i - 1] + (c[i] < 1), a[i] |= c[i] ? b : 0;
for (int i = 0; i < m; ++i)
if (!(q[i] & b) && s[r[i]] - s[l[i] - 1] < 1) return 1;
return 0;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; ++i) scanf("%d %d %d", &l[i], &r[i], &q[i]);
for (int b = 0; !f && b < 30; ++b) f |= go(1 << b);
if (f)
puts("NO");
else {
puts("YES");
for (int i = 1; i <= n; ++i) printf("%d ", a[i]);
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int INF = (1LL << 31) - 1;
const long long int LINF = (1LL << 62) - 1;
const int NMAX = 100000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;
int N, M;
int L[NMAX];
int R[NMAX];
int X[NMAX];
int A[NMAX];
int P[NMAX][32];
int AI[4 * NMAX];
void build(int nod, int lo, int hi) {
if (lo == hi) {
AI[nod] = A[lo];
return;
}
int mi = (lo + hi) / 2;
build(2 * nod, lo, mi);
build(2 * nod + 1, mi + 1, hi);
AI[nod] = AI[2 * nod] & AI[2 * nod + 1];
}
int query(int nod, int lo, int hi, int L, int R) {
if (L <= lo && hi <= R) return AI[nod];
if (R < lo || hi < L) return ((1 << 30) - 1);
int mi = (lo + hi) / 2;
return (query(2 * nod, lo, mi, L, R) & query(2 * nod + 1, mi + 1, hi, L, R));
}
int main() {
int i, j, l, r, x, ok;
scanf("%d%d", &N, &M);
for (i = 1; i <= M; i++) {
scanf("%d%d%d", &l, &r, &x);
L[i] = l;
R[i] = r;
X[i] = x;
for (j = 0; j <= 30; j++)
if (x & (1 << j)) {
P[l][j]++;
P[r + 1][j]--;
}
}
for (j = 0; j <= 30; j++) {
ok = 0;
for (i = 1; i <= N; i++) {
ok += P[i][j];
if (ok > 0) A[i] |= (1 << j);
}
}
build(1, 1, N);
ok = 1;
for (i = 1; i <= M; i++)
if (query(1, 1, N, L[i], R[i]) != X[i]) ok = 0;
if (ok) {
printf("YES\n");
for (i = 1; i <= N; i++) printf("%d ", A[i]);
} else
printf("NO\n");
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.*;
import java.util.*;
public class Main {
// main
public static void main(String [] args) throws IOException {
// makes the reader and writer
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// read in
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] queries = new int[m][3];
for (int i=0;i<m;i++) {
st = new StringTokenizer(f.readLine());
queries[i] = new int[]{Integer.parseInt(st.nextToken())-1,Integer.parseInt(st.nextToken())-1,
Integer.parseInt(st.nextToken())};
}
// sort by starting point
Arrays.sort(queries,new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return (new Integer(a[0])).compareTo(b[0]);
}
});
// for each bit set settings and check
int[] arr = new int[n];
for (int i=0;i<30;i++) {
ArrayList<int[]> queriesY = new ArrayList<int[]>();
ArrayList<int[]> queriesN = new ArrayList<int[]>();
for (int j=0;j<m;j++) {
if ((queries[j][2]&(1<<i))!=0) queriesY.add(new int[]{queries[j][0],queries[j][1]});
else queriesN.add(new int[]{queries[j][0],queries[j][1]});
}
// make intervals disjoint and add ones
int prev = 0;
int[] nums = new int[n];
for (int[] span: queriesY) {
if (span[0]<prev) {
if (span[1]<prev) continue;
else for (int j=prev;j<=span[1];j++) nums[j] = 1;
} else for (int j=span[0];j<=span[1];j++) nums[j] = 1;
prev = span[1]+1;
}
// prefix sums
int[] ones = new int[n+1];
for (int j=1;j<=n;j++) ones[j] = ones[j-1]+nums[j-1];
// check whether at least one 0 for each 0 interval
for (int[] span: queriesN) {
if (ones[span[1]+1]-ones[span[0]]==span[1]-span[0]+1) {
out.println("NO");
out.close();
System.exit(0);
}
}
// add to arr
for (int j=0;j<n;j++) arr[j]+=nums[j]*(1<<i);
}
// write to out
out.println("YES");
for (int i=0;i<n;i++) {
out.print(arr[i]);
out.print(" ");
}
out.println();
// cleanup
out.close();
f.close();
System.exit(0);
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline T poww(T b, T p) {
long long a = 1;
while (p) {
if (p & 1) {
a = (a * b);
}
p >>= 1;
b = (b * b);
}
return a;
}
template <class T>
inline T poww2(T b, int p) {
T a = 1;
while (p) {
if (p & 1) {
a = (a * b);
}
p >>= 1;
b = (b * b);
}
return a;
}
template <class T>
inline T modpoww(T b, T p, T mmod) {
long long a = 1;
while (p) {
if (p & 1) {
a = (a * b) % mmod;
}
p >>= 1;
b = (b * b) % mmod;
}
return a % mmod;
}
template <class T>
inline T gcd(T a, T b) {
if (b > a) return gcd(b, a);
return ((b == 0) ? a : gcd(b, a % b));
}
template <class T>
inline void scan(vector<T>& a, int n) {
T b;
int i;
for ((i) = 0; (i) < (n); (i) += 1) {
cin >> b;
a.push_back(b);
}
}
inline void scand(vector<int>& a, int n) {
int b;
int i;
for ((i) = 0; (i) < (n); (i) += 1) {
scanf("%d", &(b));
a.push_back(b);
}
}
long long tree[1000000];
long long lazy[1000000];
int a[100010][3];
int n, m;
long long all1;
inline void update(int node, int s, int e, int i, int j, long long w) {
if (lazy[node]) {
tree[node] |= lazy[node];
lazy[node * 2] |= lazy[node];
lazy[node * 2 + 1] |= lazy[node];
lazy[node] = 0;
}
if (i > e || j < s) return;
if (s >= i && e <= j) {
tree[node] |= w;
if (s != e) {
lazy[node * 2] |= w;
lazy[node * 2 + 1] |= w;
}
return;
}
update(2 * node, s, (s + e) / 2, i, j, w);
update(2 * node + 1, (s + e) / 2 + 1, e, i, j, w);
}
long long query(int node, int s, int e, int i, int j) {
if (i > e || j < s) return all1;
if (lazy[node]) {
tree[node] |= lazy[node];
lazy[node * 2] |= lazy[node];
lazy[node * 2 + 1] |= lazy[node];
lazy[node] = 0;
}
if (s >= i && e <= j) {
return tree[node];
}
long long l = query(2 * node, s, (s + e) / 2, i, j);
long long r = query(2 * node + 1, (s + e) / 2 + 1, e, i, j);
return l & r;
}
int main() {
int i, j, k;
all1 = poww<long long>(2, 60) - 1;
cin >> n >> m;
for ((i) = 0; (i) < (m); (i) += 1) {
for ((j) = 0; (j) < (3); (j) += 1) scanf("%d", &(a[i][j]));
update(1, 0, n - 1, a[i][0] - 1, a[i][1] - 1, a[i][2]);
}
for ((i) = 0; (i) < (m); (i) += 1) {
if (query(1, 0, n - 1, a[i][0] - 1, a[i][1] - 1) != a[i][2]) break;
}
if (i < m) {
cout << "NO\n";
} else {
cout << "YES\n";
for ((i) = 0; (i) < (n); (i) += 1)
printf("%lld ", query(1, 0, n - 1, i, i));
cout << "\n";
}
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[n];
TaskB.Query[] queries = new TaskB.Query[m];
for (int q = 0; q < m; ++q) {
queries[q] = new TaskB.Query();
queries[q].L = in.nextInt() - 1;
queries[q].R = in.nextInt() - 1;
queries[q].value = in.nextInt();
}
for (int bit = 0; bit < 30; ++bit) {
int[] count = new int[n + 1];
int[] cum = new int[n + 2];
for (int q = 0; q < m; ++q) {
if ((queries[q].value & (1 << bit)) != 0) {
++count[queries[q].L];
--count[queries[q].R + 1];
}
}
for (int i = 1; i < n; ++i)
count[i] += count[i - 1];
for (int i = 0; i < n; ++i)
count[i] = Math.min(count[i], 1);
for (int i = 0; i < n; ++i) {
cum[i + 1] = cum[i] + count[i];
}
for (int q = 0; q < m; ++q) {
if ((queries[q].value & (1 << bit)) == 0) {
if (cum[queries[q].R + 1] - cum[queries[q].L] == queries[q].R - queries[q].L + 1) {
out.println("NO");
return;
}
}
}
for (int i = 0; i < n; ++i)
a[i] |= (count[i] << bit);
}
out.println("YES");
ArrayUtils.printArray(out, a);
}
static class Query {
int L;
int R;
int value;
}
}
static class ArrayUtils {
public static void printArray(PrintWriter out, int[] array) {
if (array.length == 0) return;
for (int i = 0; i < array.length; i++) {
if (i != 0) out.print(" ");
out.print(array[i]);
}
out.println();
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int pnumChars;
public FastReader(InputStream stream) {
this.stream = stream;
}
private int pread() {
if (pnumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= pnumChars) {
curChar = 0;
try {
pnumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (pnumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = pread();
while (isSpaceChar(c))
c = pread();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = pread();
}
int res = 0;
do {
if (c == ',') {
c = pread();
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = pread();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
const int N = 1000 * 1000;
const int MAXBIT = 30;
int l[N], r[N], q[N], a[N], t[4 * N];
int sum[N];
inline void build(int v, int l, int r) {
if (l + 1 == r) {
t[v] = a[l];
return;
}
int mid = (l + r) >> 1;
build(v * 2, l, mid);
build(v * 2 + 1, mid, r);
t[v] = t[v * 2] & t[v * 2 + 1];
}
inline int query(int v, int l, int r, int L, int R) {
if (l == L && r == R) {
return t[v];
}
int mid = (L + R) >> 1;
int ans = (1ll << MAXBIT) - 1;
if (l < mid) ans &= query(v * 2, l, std::min(r, mid), L, mid);
if (mid < r) ans &= query(v * 2 + 1, std::max(l, mid), r, mid, R);
return ans;
}
int main() {
int n, m;
scanf("%d %d\n", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d %d\n", &l[i], &r[i], &q[i]);
l[i]--;
}
for (int bit = 0; bit <= MAXBIT; bit++) {
for (int i = 0; i < n; i++) sum[i] = 0;
for (int i = 0; i < m; i++) {
if ((q[i] >> bit) & 1) {
sum[l[i]]++;
sum[r[i]]--;
}
}
for (int i = 0; i < n; i++) {
if (i > 0) sum[i] += sum[i - 1];
if (sum[i] > 0) {
a[i] |= (1 << bit);
}
}
}
build(1, 0, n);
for (int i = 0; i < m; i++) {
if (query(1, l[i], r[i], 0, n) != q[i]) {
puts("NO");
return 0;
}
}
puts("YES");
for (int i = 0; i < n; i++) {
if (i) printf(" ");
printf("%d", a[i]);
}
puts("");
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long int t[100100 * 4], lz[100100 * 4];
pair<pair<int, int>, int> qr[100100];
void update(int node, int st, int en, int l, int r, int q) {
t[node] = t[node] | lz[node];
if (st != en) {
lz[node * 2 + 1] = lz[node] | lz[node * 2 + 1];
lz[node * 2 + 2] = lz[node] | lz[node * 2 + 2];
}
lz[node] = 0;
if (st > r || en < l) return;
if (st >= l && en <= r) {
t[node] = t[node] | q;
if (st != en) {
lz[node * 2 + 1] = q | lz[node * 2 + 2];
lz[node * 2 + 2] = q | lz[node * 2 + 2];
}
return;
}
int mid = st + en >> 1;
update(node * 2 + 1, st, mid, l, r, q);
update(node * 2 + 2, mid + 1, en, l, r, q);
t[node] = t[node * 2 + 1] & t[node * 2 + 2];
}
int query(int node, int st, int en, int l, int r) {
t[node] = t[node] | lz[node];
if (st != en) {
lz[node * 2 + 1] = lz[node] | lz[node * 2 + 1];
lz[node * 2 + 2] = lz[node] | lz[node * 2 + 2];
}
lz[node] = 0;
if (st > r || en < l || st > en) return (1ll << 31) - 1;
if (st >= l && en <= r) return t[node];
int mid = st + en >> 1;
int p1 = query(node * 2 + 1, st, mid, l, r);
int p2 = query(node * 2 + 2, mid + 1, en, l, r);
return p1 & p2;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < k; i++) {
cin >> qr[i].first.first >> qr[i].first.second >> qr[i].second;
update(0, 0, n - 1, qr[i].first.first - 1, qr[i].first.second - 1,
qr[i].second);
}
for (int i = 0; i < k; i++) {
int m = query(0, 0, n - 1, qr[i].first.first - 1, qr[i].first.second - 1);
if (m != qr[i].second) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
for (int i = 0; i < n; i++) {
long long int m = query(0, 0, n - 1, i, i);
cout << m << ' ';
}
cout << endl;
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int l[100010], r[100010], n, m;
;
long long val[100010], num[100010][30], a[100010][30];
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> val[i];
for (int j = 0; j < 30; j++)
if ((val[i] & (1 << j)) != 0) {
a[l[i]][j]++;
a[r[i] + 1][j]--;
}
}
for (int j = 0; j < 30; j++)
for (int i = 1; i <= n; i++) {
num[i][j] = num[i - 1][j];
a[i][j] += a[i - 1][j];
if (a[i][j] >= 1) num[i][j]++;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < 30; j++)
if ((val[i] & (1 << j)) == 0)
if (num[r[i]][j] - num[l[i] - 1][j] == r[i] - l[i] + 1)
cout << "NO" << endl, exit(0);
cout << "YES" << endl;
for (int i = 1; i <= n; i++) {
long long ans = 0;
for (int j = 0; j < 30; j++) {
if (a[i][j]) ans += (1 << j);
}
cout << ans << " ";
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
int a[32][101111], n, m, d[32][111111], b[111111], t[400111];
pair<pair<int, int>, int> p[111111];
int bit(int mask, int i) { return (mask & (1 << i)) > 0; }
void build(int v, int tl, int tr) {
if (tl == tr)
t[v] = b[tl];
else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[v] = (t[v * 2] & t[v * 2 + 1]);
}
}
int get(int v, int tl, int tr, int l, int r) {
if (l > r) return (1 << 30) - 1;
if (l == tl && r == tr) return t[v];
int tm = (tl + tr) / 2;
return get(v * 2, tl, tm, l, min(r, tm)) &
get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}
int main() {
scanf("%d%d", &n, &m);
int len = (int)sqrt(n + .0) + 1;
for (int cs = 0; cs < m; ++cs) {
int l, r, q;
scanf("%d%d%d", &l, &r, &q);
p[cs] = make_pair(make_pair(l, r), q);
for (int j = 0; j < 30; ++j) {
int w = bit(q, j);
if (w == 0) continue;
a[j][l]++;
a[j][r + 1]--;
}
}
for (int i = 0; i < 30; ++i) {
int second = 0;
for (int j = 1; j <= n; ++j) {
second += a[i][j];
a[i][j] = a[i][j - 1] + second;
}
}
for (int i = 1; i <= n; ++i) {
int ans = 0;
for (int j = 0; j < 30; ++j) ans += (1 << j) * (a[j][i] - a[j][i - 1] > 0);
b[i] = ans;
}
build(1, 1, n);
for (int i = 0; i < m; ++i) {
int l = p[i].first.first, r = p[i].first.second, q = p[i].second;
if (get(1, 1, n, l, r) != q) {
puts("NO");
return 0;
}
}
puts("YES");
for (int i = 1; i <= n; ++i) printf("%d ", b[i]);
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m;
int L[MX], R[MX], Q[MX], A[MX];
bool ok[32][MX << 2];
int cnt[32][MX];
void update(int L, int R, int id, int l, int r, int rt) {
if (L <= l && r <= R) {
ok[id][rt] = 1;
return;
}
int m = (l + r) >> 1;
if (L <= m) update(L, R, id, l, m, rt << 1);
if (R > m) update(L, R, id, m + 1, r, rt << 1 | 1);
}
void build(int l, int r, int rt) {
if (l == r) {
for (int i = 0; i <= 30; i++) {
if (ok[i][rt]) A[l] |= 1 << i;
cnt[i][l] = cnt[i][l - 1] + ok[i][rt];
}
return;
}
int m = (l + r) >> 1;
for (int i = 0; i <= 30; i++) {
if (ok[i][rt]) {
ok[i][rt << 1] = ok[i][rt << 1 | 1] = 1;
}
}
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &L[i], &R[i], &Q[i]);
for (int j = 0; j <= 30; j++) {
if (Q[i] >> j & 1) {
update(L[i], R[i], j, 1, n, 1);
}
}
}
build(1, n, 1);
bool sign = 1;
for (int i = 1; i <= m; i++) {
for (int j = 0; j <= 30; j++) {
if ((Q[i] >> j & 1) == 0) {
int c = cnt[j][R[i]] - cnt[j][L[i] - 1];
if (c == R[i] - L[i] + 1) {
sign = 0;
break;
}
}
}
if (!sign) break;
}
if (!sign)
printf("NO\n");
else {
printf("YES\n");
for (int i = 1; i <= n; i++) {
printf("%d%c", A[i], i == n ? '\n' : ' ');
}
}
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
const long long inf = 998244353;
vector<int> adj[200010], vis(200010, 0), par(200010, 0), dis(200010, 0),
di(200010, 0), fin(200010, 0);
int myrandom(int i) { return std::rand() % i; }
const int MAX = 100010;
int a[MAX], l[MAX], r[MAX], q[MAX], t[4 * MAX];
using namespace std;
inline void build(int v, int l, int r) {
if (l == r) {
t[v] = a[l];
return;
}
int mid = (l + r) / 2;
build(2 * v, l, mid);
build(2 * v + 1, mid + 1, r);
t[v] = t[2 * v] & t[2 * v + 1];
}
inline int query(int v, int l, int r, int tl, int tr) {
if (tl == l && tr == r) {
return t[v];
}
int mid = (l + r) / 2;
if (tr <= mid) {
return query(2 * v, l, mid, tl, tr);
} else if (tl > mid) {
return query(2 * v + 1, mid + 1, r, tl, tr);
} else {
return query(2 * v, l, mid, tl, mid) &
query(2 * v + 1, mid + 1, r, mid + 1, tr);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
memset(a, 0, sizeof(a));
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> q[i];
l[i]--;
}
for (int i = 0; i <= 30; i++) {
int sum[n + 2];
memset(sum, 0, sizeof(sum));
for (int j = 0; j < m; j++) {
if ((1 << i) & q[j]) {
sum[l[j]]++;
sum[r[j]]--;
}
}
for (int j = 0; j < n; j++) {
if (j > 0) {
sum[j] += sum[j - 1];
}
if (sum[j] > 0) {
a[j] |= 1 << i;
}
}
}
build(1, 0, n - 1);
for (int i = 0; i < m; i++) {
if (query(1, 0, n - 1, l[i], r[i] - 1) != q[i]) {
cout << "NO" << '\n';
return 0;
}
}
cout << "YES" << '\n';
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << '\n';
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | // package CodeForces;
import java.io.*;
import java.util.*;
public class Problem_482B {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner();
PrintWriter pw = new PrintWriter(System.out);
int n=sc.nextInt(),q=sc.nextInt();
SegmentTree sgt=new SegmentTree(n);
Triple[]query=new Triple[q];
for(int i=0;i<q;i++)
{
int l=sc.nextInt()-1,r=sc.nextInt()-1,v=sc.nextInt();
query[i]=new Triple(l,r,v);
sgt.rangeupdate(l, r, v);
// System.out.println(Arrays.toString(sgt.lazy));
}
// System.out.println(sgt);
for(int i=0;i<q;i++)
{
if(query[i].v!=sgt.query(query[i].l, query[i].r))
{
System.out.println("NO");
return;
}
}
for(int i=1;i<sgt.sgt.length/2;i++)
{
sgt.propagate(i, 0);
}
System.out.println("YES");
for(int i=sgt.sgt.length/2;i<sgt.sgt.length && n-->0;i++)
System.out.print(sgt.sgt[i]+" ");
pw.close();
}
static class Triple
{
int l,r,v;
public Triple(int left,int right,int val)
{
l=left;r=right;v=val;
// TODO Auto-generated constructor stub
}
}
static class SegmentTree
{
int[]sgt;
int[]a;
int[]lazy;
int n;
int max=(1<<30)-1;
public SegmentTree(int l)
{
int log=(int)(Math.ceil(Math.log(l)/Math.log(2)));
n=1<<log;
sgt=new int[n<<1];//index 0 is unused
lazy=new int[n<<1];
// Arrays.fill(lazy, -1);
// Arrays.fill(sgt, max);
}
public int query(int l,int r)//take zero index parameters
{
return query(1,n,1,l+1,r+1);
}
public int query(int s,int e,int i,int l,int r)
{
if(l<=s && r>=e)
return sgt[i];
if(r<s || l>e)
return -1;
int mid=e+s>>1;
propagate(i,(e-s+1)/2);
int left=query(s,mid,i*2,l,r);
int right=query(mid+1,e,i*2+1,l,r);
return left&right;
}
public void rangeupdate(int l,int r,int val)
{
rangeupdate(l+1,r+1,val,1, n, 1);
}
public void rangeupdate(int l,int r,int val,int s,int e,int i)
{
if(l<=s && r>=e)
{
lazy[i]|=val;
sgt[i]|=val;
return;
}
if(l>e || r<s)
return;
int mid=e+s>>1;
propagate(i,(e-s+1)/2);
if(l<=mid)
rangeupdate(l,r,val,s,mid,i*2);
if(r>mid)
rangeupdate(l,r,val,mid+1,e,i*2+1);
}
public void propagate(int node,int mid)
{
// System.out.println(i);
lazy[node<<1] |= lazy[node];
lazy[node<<1|1] |= lazy[node];
sgt[node<<1] |=lazy[node];
sgt[node<<1|1] |=lazy[node];
lazy[node] = 0;
}
public String toString()
{
String s="";
// int level=1;
for(int i=1;i<sgt.length;i++)
{
s+=sgt[i]+" ";
}
return s;
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
return br.readLine();
}
boolean hasnext() throws IOException {
return br.ready();
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
const long long inf = 998244353;
vector<int> adj[200010], vis(200010, 0), par(200010, 0), dis(200010, 0),
di(200010, 0), fin(200010, 0);
int myrandom(int i) { return std::rand() % i; }
const int MAX = 100010;
int a[MAX], l[MAX], r[MAX], q[MAX], t[4 * MAX];
using namespace std;
inline void build(int v, int l, int r) {
if (l + 1 == r) {
t[v] = a[l];
return;
}
int mid = (l + r) >> 1;
build(v * 2, l, mid);
build(v * 2 + 1, mid, r);
t[v] = t[v * 2] & t[v * 2 + 1];
}
inline int query(int v, int l, int r, int L, int R) {
if (l == L && r == R) {
return t[v];
}
int mid = (L + R) >> 1;
int ans = (1ll << 30) - 1;
if (l < mid) ans &= query(v * 2, l, std::min(r, mid), L, mid);
if (mid < r) ans &= query(v * 2 + 1, std::max(l, mid), r, mid, R);
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
memset(a, 0, sizeof(a));
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> q[i];
l[i]--;
}
for (int i = 0; i <= 30; i++) {
int sum[n + 2];
memset(sum, 0, sizeof(sum));
for (int j = 0; j < m; j++) {
if ((1 << i) & q[j]) {
sum[l[j]]++;
sum[r[j]]--;
}
}
for (int j = 0; j < n; j++) {
if (j > 0) {
sum[j] += sum[j - 1];
}
if (sum[j] > 0) {
a[j] |= 1 << i;
}
}
}
build(1, 0, n);
for (int i = 0; i < m; i++) {
if (query(1, l[i], r[i], 0, n) != q[i]) {
cout << "NO" << '\n';
return 0;
}
}
cout << "YES" << '\n';
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << '\n';
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
int n, m;
int a[100010];
int queryL[100010], queryR[100010], b[100010];
int s[30][100010];
int data[400010];
void build(int p, int L, int R) {
if (L == R) {
data[p] = a[L];
return;
}
int mid = L + R >> 1;
build(p << 1, L, mid);
build(p << 1 | 1, mid + 1, R);
data[p] = data[p << 1] & data[p << 1 | 1];
}
int query(int p, int L, int R, int l, int r) {
if (L == l && R == r) return data[p];
int mid = L + R >> 1;
if (r <= mid)
return query(p << 1, L, mid, l, r);
else if (l > mid)
return query(p << 1 | 1, mid + 1, R, l, r);
return query(p << 1, L, mid, l, mid) &
query(p << 1 | 1, mid + 1, R, mid + 1, r);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d%d", queryL + i, queryR + i, b + i);
for (int bit = 0, value = 1; bit < 30; bit++, value <<= 1) {
if (value & b[i]) {
s[bit][queryL[i]]++;
s[bit][queryR[i] + 1]--;
}
}
}
for (int i = 1; i <= n; ++i) {
for (int bit = 0, value = 1; bit < 30; bit++, value <<= 1) {
s[bit][i] += s[bit][i - 1];
if (s[bit][i]) {
a[i] |= value;
}
}
}
build(1, 1, n);
for (int i = 1; i <= m; ++i) {
if (query(1, 1, n, queryL[i], queryR[i]) != b[i]) {
puts("NO");
return 0;
}
}
puts("YES");
for (int i = 1; i <= n; ++i) printf("%d%s", a[i], i == n ? "\n" : " ");
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
public static final double EPS = 1e-9;
static PrintWriter pr = new PrintWriter(System.out);
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int N=1;
while(N<n)
N<<=1;
int a[] = new int[N];
SegmentTree stree = new SegmentTree(a);
int queries[][] = new int[m][3];
for(int i=0; i<m; i++) {
int l = sc.nextInt();
int r = sc.nextInt();
int q = sc.nextInt();
stree.updateRange(l, r, q);
queries[i][0] = l;
queries[i][1] = r;
queries[i][2] = q;
}
boolean can = true;
for(int i=0; i<m; i++) {
int val = stree.Query(queries[i][0], queries[i][1]);
if(val != queries[i][2]) {
can = false;
break;
}
}
if(!can)
pr.println("NO");
else {
pr.println("YES");
for(int i = 1; i <=n; i++ )
pr.print(stree.Query(i, i)+" ");
}
pr.close();
}
static class SegmentTree{
int Stree[],lazy[];
int N;
SegmentTree(int a[]){
int n = a.length;
N=1;
while(N<n)
N<<=1;
Stree = new int[N<<1];
lazy = new int[N<<1];
// for(int i=0; i<n; ++i)
// Stree[i+n] = a[i];
// for(int i=n-1; i>0; --i)
// Stree[i] = Stree[i<<1]|Stree[i<<1 | 1];
}
void updateRange(int l, int r, int val) {
Update(1,1,N,l,r,val);
}
void Update(int v, int b, int e, int l , int r, int val) {
if(r<b || e<l)
return;
if(l<=b && e<=r) {
Stree[v]|=val;
lazy[v]|=val;
return;
}
push(v);
int mid = (b+e)>>1;
Update(v<<1 , b,mid,l,r,val);
Update(v<<1 | 1,mid+1,e,l,r,val);
Stree[v] = Stree[v<<1]&Stree[v<<1 | 1];
}
int Query(int l ,int r) {
return query(1,1,N,l,r);
}
int query(int v, int b, int e, int l,int r) {
if(r<b || e<l)
return (1<<31)-1;
if(l<=b && e<=r) {
return Stree[v];
}
push(v);
int mid = (b+e)>>1;
int q1 = query(v<<1,b,mid, l,r);
int q2 = query(v<<1 | 1, mid+1,e, l,r);
return q1&q2;
}
void push(int v) {
if(lazy[v]==0)
return;
Stree[v<<1]|=lazy[v];
Stree[v<<1 | 1]|=lazy[v];
lazy[v<<1]|=lazy[v];
lazy[v<<1 | 1] |=lazy[v];
lazy[v] = 0;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
//
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solver {
public static void main(String[] Args) throws NumberFormatException,
IOException {
new Solver().Run();
}
PrintWriter pw;
StringTokenizer Stok;
BufferedReader br;
public String nextToken() throws IOException {
while (Stok == null || !Stok.hasMoreTokens()) {
Stok = new StringTokenizer(br.readLine());
}
return Stok.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
final int dig=31;
int n;
int kolQv;
int[][] result;
int[][] partSum;
int[][] event;
int[] line,l,r,q;
public void Run() throws NumberFormatException, IOException {
//br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter("output.txt");
br=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(new OutputStreamWriter(System.out));
n=nextInt();
kolQv=nextInt();
l=new int[kolQv];
r=new int[kolQv];
q=new int[kolQv];
result=new int[n+10][dig];
partSum=new int[n+10][dig];
event=new int[n+10][dig];
for (int i=0; i<kolQv; i++){
l[i]=nextInt();
r[i]=nextInt();
q[i]=nextInt();
for (int j=0; j<dig; j++){
if ((q[i]&(1<<j))>0){
event[l[i]][j]++;
event[r[i]+1][j]--;
}
}
}
line=new int[dig];
for (int i=1; i<=n; i++){
for (int j=0; j<dig; j++){
line[j]+=event[i][j];
if (line[j]>0)
result[i][j]=1;
partSum[i][j]=partSum[i-1][j]+result[i][j];
}
}
boolean isPossible=true;
for (int i=0; i<kolQv && isPossible; i++){
for (int j=0; j<dig; j++){
if ((q[i]&(1<<j))==0)
if (partSum[r[i]][j]-partSum[l[i]-1][j]==r[i]-l[i]+1)
isPossible=false;
}
}
if (!isPossible){
pw.println("NO");
} else {
pw.println("YES");
for (int i=1; i<=n; i++){
int zn=0;
for (int j=0; j<dig; j++){
zn+=(1<<j)*result[i][j];
}
pw.print(zn);
pw.print(' ');
}
pw.println();
}
pw.flush();
pw.close();
}
} | JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long D = 31;
const long long N = 100010;
struct constraint {
long long l, r, q;
constraint(long long _l, long long _r, long long _q) {
l = _l;
r = _r;
q = _q;
}
};
void err() {
cout << "NO" << '\n';
exit(0);
}
long long n, m;
long long a[N][D];
long long pref[N][D];
vector<constraint> e;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (long long i = 0; i < m; i++) {
long long l, r, q;
cin >> l >> r >> q;
e.push_back({l, r, q});
for (long long bit = 0; bit < D; bit++) {
if (q >> bit & 1) {
a[l][bit]++;
a[r + 1][bit]--;
}
}
}
for (long long i = 1; i <= n; i++) {
for (long long bit = 0; bit < D; bit++) {
a[i][bit] += a[i - 1][bit];
}
}
for (long long i = 1; i <= n; i++) {
for (long long bit = 0; bit < D; bit++) {
pref[i][bit] = (i ? pref[i - 1][bit] : 0LL) + (a[i][bit] > 0);
}
}
for (auto [l, r, q] : e) {
for (long long bit = 0; bit < D; bit++) {
if (q >> bit & 1) {
if (pref[r][bit] - pref[l - 1][bit] != r - l + 1) {
err();
}
} else {
if (pref[r][bit] - pref[l - 1][bit] == r - l + 1) {
err();
}
}
}
}
vector<long long> res(n);
for (long long i = 1; i <= n; i++) {
for (long long bit = 0; bit < D; bit++) {
if (a[i][bit] > 0) {
res[i - 1] |= (1LL << bit);
}
}
}
cout << "YES" << '\n';
for (long long it : res) {
cout << it << " ";
}
cout << '\n';
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m;
int l[100005], r[100005], q[100005];
struct node {
int v, add;
};
node t[4 * 100005];
const int MX = (1LL << 31) - 1;
void Push(int v, int tl, int tm, int tr) {
if (t[v].add != 0) {
t[2 * v].add |= t[v].add;
t[2 * v].v |= t[v].add;
t[2 * v + 1].add |= t[v].add;
t[2 * v + 1].v |= t[v].add;
t[v].add = 0;
}
}
void Update(int v, int tl, int tr, int l, int r, int value) {
if (l > r) return;
if ((tl == l) && (tr == r)) {
t[v].add |= value;
t[v].v |= value;
return;
}
int tm = (tl + tr) / 2;
Push(v, tl, tm, tr);
Update(2 * v, tl, tm, l, min(tm, r), value);
Update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, value);
t[v].v &= t[2 * v].v;
t[v].v &= t[2 * v + 1].v;
}
int Get(int v, int tl, int tr, int l, int r) {
if (l > r) return MX;
if ((tl == l) && (tr == r)) return t[v].v;
int tm = (tl + tr) / 2;
Push(v, tl, tm, tr);
return Get(2 * v, tl, tm, l, min(tm, r)) &
Get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}
int main() {
scanf("%d %d", &n, &m);
for (int(i) = (0); (i) < int(m); ++(i)) {
scanf("%d %d %d", l + i, r + i, q + i);
Update(1, 0, n - 1, l[i] - 1, r[i] - 1, q[i]);
}
bool Ok = 1;
for (int(i) = (0); (i) < int(m); ++(i)) {
int x = Get(1, 0, n - 1, l[i] - 1, r[i] - 1);
if (x != q[i]) {
Ok = 0;
break;
}
}
if (!Ok) {
puts("NO");
cin.sync();
cin.get();
return 0;
} else {
puts("YES");
for (int(i) = (0); (i) < int(n); ++(i)) {
printf("%d ", Get(1, 0, n - 1, i, i));
}
}
cin.sync();
cin.get();
return 0;
}
| CPP |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | import java.util.*;
import java.io.*;
import javafx.util.*;
public class Solution
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HashMap<Pair<Integer,Integer> , Long> map = new HashMap<Pair<Integer,Integer> , Long>();
int q = sc.nextInt();
boolean bl = true;
int sum[][] = new int[n][31];
while(q-- > 0)
{
int l = sc.nextInt();
int r = sc.nextInt();
l--;
r--;
long ans = sc.nextLong();
Pair<Integer,Integer> pp = new Pair<Integer,Integer>(l , r);
if(map.containsKey(pp))
{
if(map.get(pp) != ans)
{
bl = false;
break;
}
continue;
}
map.put(pp,ans);
for(int j = 0 ; j <= 30 ; j++)
{
if((ans&(1L<<j)) > 0)
{
sum[l][j]++;
if(r != n-1)
sum[r+1][j]--;
}
}
}
if(!bl)
System.out.println("NO");
else
{
StringBuffer str = new StringBuffer("");
long asss[] = new long[n];
for(int i = 0 ; i < n ; i++)
{
long curr = 0;
for(int j = 0 ; j <= 30 ; j++)
{
if(i != 0)
sum[i][j] = sum[i-1][j] + sum[i][j];
if(sum[i][j] > 0)
curr += (1L<<j);
}
str.append(curr + " ");
asss[i] = curr;
}
for(int i = 0 ; i < n ; i++)
{
for(int j = 0 ; j <= 30 ; j++)
{
sum[i][j] = 0;
}
}
for(int i = 0 ; i < n ; i++)
{
for(int j = 0 ; j <= 30 ; j++)
{
if(i != 0)
sum[i][j] = sum[i-1][j];
if((asss[i]&(1L<<j)) > 0)
sum[i][j]++;
}
}
for(Map.Entry entry : map.entrySet())
{
Pair pp = (Pair)entry.getKey();
long ans = (long)entry.getValue();
int l = (int)pp.getKey();
int r = (int)pp.getValue();
long ass = 0;
for(int j = 0 ; j <= 30 ; j++)
{
int ss = 0;
if(l != 0)
ss = sum[r][j] - sum[l-1][j];
else
ss = sum[r][j];
if(ss == r-l+1)
ass += 1L<<j;
}
if(ass != ans)
bl = false;
}
if(bl)
{
System.out.println("YES");
System.out.println(str);
}
else
System.out.println("NO");
}
}
}
| JAVA |
482_B. Interesting Array | 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 <image> 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 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.
Examples
Input
3 1
1 3 3
Output
YES
3 3 3
Input
3 2
1 3 3
1 3 2
Output
NO | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m;
int l[112345], r[112345], q[112345];
int prefix[31][112345], s[31][112345], cnt[31][112345];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; ++i) cin >> l[i] >> r[i] >> q[i];
for (int i = 0; i < m; ++i) {
for (int bit = 0; bit < 31; ++bit) {
if (q[i] & (1 << bit)) {
s[bit][l[i]]++;
s[bit][r[i] + 1]--;
}
}
}
for (int bit = 0; bit < 31; ++bit) {
for (int i = 1; i <= n; ++i) {
prefix[bit][i] = prefix[bit][i - 1] + s[bit][i];
cnt[bit][i] = cnt[bit][i - 1];
if (prefix[bit][i] > 0) cnt[bit][i]++;
}
}
for (int i = 0; i < m; ++i) {
for (int bit = 0; bit < 31; ++bit) {
if (!(q[i] & (1 << bit))) {
if (cnt[bit][r[i]] - cnt[bit][l[i] - 1] == r[i] - l[i] + 1) {
cout << "NO\n";
return 0;
}
}
}
}
cout << "YES\n";
for (int i = 1; i <= n; ++i) {
int digit = 0;
for (int bit = 0; bit < 31; ++bit) {
if (prefix[bit][i]) {
digit += (1 << bit);
}
}
if (i > 1) cout << " ";
cout << digit;
}
cout << endl;
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.