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 | #include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int ok, n, m;
struct Seg {
static const int maxn = 1e5 + 10;
struct node {
int v, lazy;
} tree[maxn << 3];
void build_tree() { memset(tree, 0, sizeof(tree)); }
void push_down(int now, int l, int r) {
if (tree[now].lazy) {
tree[(now << 1)].lazy |= tree[now].lazy;
tree[(now << 1)].v |= tree[now].v;
tree[((now << 1) | 1)].lazy |= tree[now].lazy;
tree[((now << 1) | 1)].v |= tree[now].v;
}
return;
}
void push_up(int now) {
tree[now].v = tree[(now << 1)].v & tree[((now << 1) | 1)].v;
}
void insert(int now, int l, int r, int ql, int qr, int tt) {
if (ql <= l && r <= qr) {
tree[now].v |= tt;
tree[now].lazy |= tt;
return;
}
if (tree[now].lazy) push_down(now, l, r);
if (ql <= ((l + r) >> 1)) insert((now << 1), l, ((l + r) >> 1), ql, qr, tt);
if (((l + r) >> 1) + 1 <= qr)
insert(((now << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr, tt);
push_up(now);
}
int query(int now, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
return tree[now].v;
}
if (tree[now].lazy) push_down(now, l, r);
int res = (1 << 30) - 1;
if (ql <= ((l + r) >> 1)) {
res &= query((now << 1), l, ((l + r) >> 1), ql, qr);
}
if (((l + r) >> 1) + 1 <= qr) {
res &= query(((now << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr);
}
return res;
}
} T;
int q1[N], q2[N], q3[N];
int main() {
int x, y, z;
while (scanf("%d%d", &n, &m) != EOF) {
T.build_tree();
ok = 1;
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &x, &y, &z);
q1[i] = x;
q2[i] = y;
q3[i] = z;
T.insert(1, 1, n, x, y, z);
}
int flag = 1;
for (int i = 0; i < m; i++) {
if (T.query(1, 1, n, q1[i], q2[i]) != q3[i]) flag = 0;
}
if (!flag)
printf("NO\n");
else {
printf("YES\n");
for (int i = 1; i <= n; i++) {
printf("%d%c", T.query(1, 1, n, i, 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>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const int MAXN = 1e5 + 5, MAXBITS = 30, MAXBASE = 1 << 18;
int n, m, base = 1, a[MAXN], t[MAXBASE * 2], q[MAXN], l[MAXN], r[MAXN],
sum[MAXN];
void build() {
for (int i = 1; i <= base; ++i)
if (i <= n)
t[base + i - 1] = a[i];
else
t[base + i - 1] = (1 << 30) - 1;
for (int i = base - 1; i >= 1; --i) t[i] = t[2 * i] & t[2 * i + 1];
}
int query(int l, int r, int left, int right, int node) {
if (left > r || right < l) return (1 << 30) - 1;
if (left >= l && right <= r) return t[node];
int mid = (left + right) / 2;
return query(l, r, left, mid, node * 2) &
query(l, r, mid + 1, right, node * 2 + 1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= m; ++i) cin >> l[i] >> r[i] >> q[i];
for (int bit = 0; bit <= 30; ++bit) {
for (int i = 1; i <= n; ++i) sum[i] = 0;
for (int i = 1; i <= m; ++i)
if ((q[i] >> bit) & 1) {
++sum[l[i]];
--sum[r[i] + 1];
}
for (int i = 1; i <= n; ++i) {
if (i > 1) sum[i] += sum[i - 1];
if (sum[i] > 0) a[i] |= (1 << bit);
}
}
while (base < n) base *= 2;
build();
for (int i = 1; i <= m; ++i)
if (query(l[i] - 1, r[i] - 1, 0, base - 1, 1) != q[i]) {
cout << "NO\n";
exit(0);
}
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 | #include <bits/stdc++.h>
using namespace std;
int ma[500000];
vector<pair<pair<int, int>, long long int> > v;
long long int seg[500000];
long long int ans[500000];
long long int all = 0;
long long int qry(int index, int start, int end, int qs, int qe) {
if (start > end || end < qs || start > qe) {
return all;
}
if (start >= qs && end <= qe) {
return seg[index];
}
long long int q1 = qry(2 * index, start, (start + end) / 2, qs, qe);
long long int q2 = qry(2 * index + 1, ((start + end) / 2) + 1, end, qs, qe);
return q1 & q2;
}
void build(int index, int start, int end) {
if (start > end) return;
if (start == end) {
seg[index] = ans[start];
return;
}
build(2 * index, start, (start + end) / 2);
build(2 * index + 1, ((start + end) / 2) + 1, end);
seg[index] = seg[2 * index] & seg[2 * index + 1];
}
int main() {
all = 0;
for (int i = 0; i <= 30; i++) all = all | (1 << i);
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i <= 4 * n; i++) seg[i] = all;
for (int i = 0; i < m; i++) {
int l, r;
long long int va;
scanf("%d %d %lld", &l, &r, &va);
v.push_back(make_pair(make_pair(l, r), va));
}
for (int i = 0; i <= 30; i++) {
for (int k = 0; k <= n; k++) ma[k] = 0;
for (int j = 0; j < m; j++) {
int l = v[j].first.first;
int r = v[j].first.second;
long long int val = v[j].second;
if (val & (1 << i)) {
ma[l]++;
ma[r + 1]--;
}
}
for (int k = 1; k <= n; k++) {
ma[k] += ma[k - 1];
if (ma[k] >= 1) ans[k] = ans[k] | (1 << i);
}
}
build(1, 1, n);
int f = 0;
for (int i = 0; i < m; i++) {
int l, r;
long long int val;
l = v[i].first.first;
r = v[i].first.second;
val = v[i].second;
long long int aa = qry(1, 1, n, l, r);
if (aa != val) {
f = 1;
break;
}
}
if (f == 1) {
cout << "NO" << endl;
return 0;
} else {
cout << "YES" << endl;
for (int i = 1; i <= n; i++) {
printf("%lld ", ans[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 | import java.io.*;
import java.util.*;
public class Main{
final boolean isFileIO = false;
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
String delim = " ";
public static void main(String[] args) throws IOException {
Main m = new Main();
m.initIO();
m.solve();
m.in.close();
m.out.close();
}
public void initIO() throws IOException {
if(!isFileIO) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("B.txt"));
out = new PrintWriter("output.txt");
}
}
String nextToken() throws IOException {
if(!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken(delim);
}
String readLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
class Query {
public int l, r, q;
public Query(int l, int r, int q) {
this.l = l;
this.r = r;
this.q = q;
}
}
public void solve() throws IOException {
int n, m;
n = nextInt(); m = nextInt();
int[] a = new int[n];
Query[] queries = new Query[m];
for(int i = 0; i < m; i++) {
queries[i] = new Query(nextInt() - 1, nextInt() - 1, nextInt());
}
HashMap<Integer, ArrayList<Query>> open = new HashMap<Integer, ArrayList<Query>>();
HashMap<Integer, ArrayList<Query>> close = new HashMap<Integer, ArrayList<Query>>();
for(int i = 0; i < m; i++) {
int l = queries[i].l;
int r = queries[i].r;
ArrayList<Query> listOpen = open.get(l);
ArrayList<Query> listClose = close.get(r);
if(listOpen == null) {
listOpen = new ArrayList<Query>();
}
if(listClose == null) {
listClose = new ArrayList<Query>();
}
listOpen.add(queries[i]);
listClose.add(queries[i]);
open.put(l, listOpen);
close.put(r, listClose);
}
int[] bits = new int[31];
for(int i = 0; i < n; i++) {
ArrayList<Query> listOpen = open.get(i);
ArrayList<Query> listClose = close.get(i);
if(listOpen != null) {
for(int j = 0; j < listOpen.size(); j++) {
int q = listOpen.get(j).q;
for(int z = 0; z < 31; z++) {
if((q & (1 << z)) > 0) {
bits[z]++;
}
}
}
}
for(int j = 0; j < 31; j++) {
if(bits[j] > 0) {
a[i] |= (1 << j);
}
}
if(listClose != null) {
for(int j = 0; j < listClose.size(); j++) {
int q = listClose.get(j).q;
for(int z = 0; z < 31; z++) {
if((q & (1 << z)) > 0) {
bits[z]--;
}
}
}
}
}
int[][] d = new int[n + 1][31];
for(int i = 1; i <= n; i++) {
for(int j = 0; j < 31; j++) {
d[i][j] = d[i - 1][j] + ((a[i - 1] & (1 << j)) > 0 ? 1 : 0);
}
}
for(int i = 0; i < m; i++) {
int l = queries[i].l;
int r = queries[i].r;
int q = queries[i].q;
for(int j = 0; j < 31; j++) {
bits[j] = d[r + 1][j] - d[l][j];
}
for(int j = 0; j < 31; j++) {
if((q & (1 << j)) > 0) {
if(bits[j] != (r - l + 1)) {
out.println("NO");
return;
}
} else {
if(bits[j] == (r - l + 1)) {
out.println("NO");
return;
}
}
}
}
out.println("YES");
for(int i = 0; i < n; i++) {
out.print(a[i] + " ");
}
out.println();
}
}
class Utils {
public static long binpow(long a, long exp, long mod) {
if(exp == 0) {
return 1;
}
if(exp % 2 == 0) {
long temp = binpow(a, exp / 2, mod);
return (temp * temp) % mod;
} else {
return (binpow(a, exp - 1, mod) * a) % mod;
}
}
public static long inv(long a, long mod) {
return binpow(a, mod - 2, mod);
}
public static long addmod(long a, long b, long mod) {
return (a + b + 2 * mod) % mod;
}
public static long gcd(long a, long b) {
if(b == 0)
return a;
return gcd(b, a % b);
}
}
| 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.util.InputMismatchException;
import java.io.PrintStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author karan173
*/
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();
}
}
class TaskB
{
int n, m;
int masks[];
int f[][];
int ll[];
int rr[];
int qq[];
int cf[][];
public void solve(int testNumber, FastReader in, PrintWriter out)
{
n = in.ni ();
m = in.ni ();
f = new int[30][n + 1];
cf = new int[30][n];
ll = new int[m];
rr = new int[m];
qq = new int[m];
for (int i = 0; i < m; i++)
{
int l = in.ni () - 1;
int r = in.ni () - 1;
int q = in.ni ();
add(l,r,q);
ll[i] = l;
rr[i] = r;
qq[i] = q;
}
masks = new int[n];
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < n; j++)
{
if (j > 0)
{
f[i][j] += f[i][j - 1];
cf[i][j] = cf[i][j - 1];
}
if (f[i][j] > 0)
{
masks[j] |= (1 << i);
cf[i][j]++;
}
}
}
for (int i = 0; i < m; i++)
{
int l = ll[i];
int r = rr[i];
int q = qq[i];
if (!test (l, r, q))
{
out.println ("NO");
return;
}
}
out.println ("YES");
for (int i = 0; i < n; i++)
{
out.print (masks[i] + " ");
}
out.println ();
}
private boolean test(int l, int r, int q)
{
int bit = 0;
int ran = r - l + 1;
while (bit<30)
{
int rightCnt = cf[bit][r];
int leftCnt = l == 0 ? 0 : cf[bit][l - 1];
int cnt = rightCnt - leftCnt;
if (q % 2 == 0)
{
if (cnt == ran)
{
return false;
}
}
else
{
if (cnt != ran)
{
return false;
}
}
q >>= 1;
bit++;
}
return true;
}
private void add(int ll, int rr, int q)
{
int bit = 0;
while (q != 0)
{
if (q % 2 == 1)
{
f[bit][ll]++;
f[bit][rr+1]--;
}
q >>= 1;
bit++;
}
}
}
class FastReader
{
public InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastReader(InputStream stream)
{
this.stream = stream;
}
public FastReader()
{
}
public 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++];
}
public int ni()
{
int c = read ();
while (isSpaceChar (c))
c = read ();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read ();
}
int res = 0;
do
{
if (c < '0' || c > '9')
{
throw new InputMismatchException ();
}
res *= 10;
res += c - '0';
c = read ();
} while (!isSpaceChar (c));
return res * sgn;
}
public boolean isSpaceChar(int c)
{
if (filter != null)
{
return filter.isSpaceChar (c);
}
return isWhitespace (c);
}
public static boolean isWhitespace(int c)
{
return c==' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
| 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.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
new Main().run(in, out);
out.close();
}
void run(FastScanner in, PrintWriter out) {
int N = in.nextInt();
int M = in.nextInt();
int[] a = new int[N+1];
Arrays.fill(a, 0x7fffffff);
int[][] queries = new int[M][3];
while (M-- > 0) {
queries[M][0] = in.nextInt();
queries[M][1] = in.nextInt();
queries[M][2] = in.nextInt();
}
SegmentTree st = new SegmentTree(N);
for (int i = 0; i < queries.length; i++) {
st.or(1, queries[i][0], queries[i][1], queries[i][2]);
}
boolean possible = true;
for (int i = 0; i < queries.length; i++) {
int x = st.and(1, queries[i][0], queries[i][1]);
if (x != queries[i][2]) {
possible = false;
break;
}
}
if (!possible) {
out.println("NO");
return;
}
out.println("YES");
for (int i = 1; i <= N; i++) {
out.print(st.and(1, i, i) + " ");
}
out.println();
}
class SegmentTree {
int[] lo;
int[] hi;
int[] st;
int[] delta;
SegmentTree(int N) {
lo = new int[4*N+4];
hi = new int[4*N+4];
st = new int[4*N+4];
delta = new int[4*N+4];
getBounds(1, 0, N);
}
void getBounds(int i, int a, int b) {
lo[i] = a;
hi[i] = b;
if (a == b) return;
int m = (a+b)>>1;
getBounds(2*i, a, m);
getBounds(2*i+1, m+1, b);
}
void or(int i, int a, int b, int x) {
if (b < lo[i] || a > hi[i]) return;
if (a <= lo[i] && hi[i] <= b) {
delta[i] |= x;
st[i] |= x;
return;
}
prop(i);
or(2*i, a, b, x);
or(2*i+1, a, b, x);
}
void prop(int i) {
if (delta[i] == 0) return;
delta[2*i] |= delta[i];
st[2*i] |= delta[i];
delta[2*i+1] |= delta[i];
st[2*i+1] |= delta[i];
delta[i] = 0;
}
int and(int i, int a, int b) {
if (b < lo[i] || a > hi[i]) return 0x7fffffff;
if (a <= lo[i] && hi[i] <= b) return st[i];
prop(i);
return and(2*i, a, b) & and(2*i+1, a, b);
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(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 MAX = 100005, INF = (1 << 30) - 1;
struct segtree {
int a[MAX << 2], o[MAX << 2], ql, qr, qv;
void pushDown(int x) {
if (o[x]) {
a[x << 1] |= o[x];
a[x << 1 | 1] |= o[x];
o[x << 1] |= o[x];
o[x << 1 | 1] |= o[x];
o[x] = 0;
}
}
int query(int x, int l, int r) {
if (l >= ql && r <= qr) return a[x];
pushDown(x);
int mid = (l + r) >> 1, ret = INF;
if (ql <= mid) ret &= query(x << 1, l, mid);
if (qr > mid) ret &= query(x << 1 | 1, mid + 1, r);
return ret;
}
void update(int x, int l, int r) {
if (l >= ql && r <= qr) {
o[x] |= qv;
a[x] |= qv;
return;
}
pushDown(x);
int mid = (l + r) >> 1;
if (ql <= mid) update(x << 1, l, mid);
if (qr > mid) update(x << 1 | 1, mid + 1, r);
a[x] = a[x << 1] & a[x << 1 | 1];
}
} sgt;
struct query {
int l, r, v;
} q[MAX];
int n, m;
int main() {
int i;
scanf("%d%d", &n, &m);
for (i = 1; i <= m; ++i) {
scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].v);
sgt.ql = q[i].l, sgt.qr = q[i].r, sgt.qv = q[i].v;
sgt.update(1, 1, n);
}
for (i = 1; i <= m; ++i) {
sgt.ql = q[i].l, sgt.qr = q[i].r;
if (sgt.query(1, 1, n) != q[i].v) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
for (i = 1; i <= n; ++i) {
sgt.ql = sgt.qr = i;
printf("%d%c", sgt.query(1, 1, n), 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;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
void CI(int &_x) { scanf("%d", &_x); }
void CO(int &_x) { cout << _x; }
template <typename T>
void getarray(T a[], int n) {
for (int i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void printarray(T a[], int n) {
for (int i = 0; i < n - 1; i++) cout << a[i] << " ";
cout << a[n - 1] << endl;
}
const double EPS = 1e-9;
const int INF = 0x7f7f7f7f;
int dr8[8] = {1, -1, 0, 0, 1, -1, -1, 1};
int dc8[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dr4[4] = {0, 0, 1, -1};
int dc4[4] = {-1, 1, 0, 0};
int kn8r[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int kn8c[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int ar[100005];
int l[100000 + 5], r[100000 + 5], q[100000 + 5], sum[100000 + 5];
int a[100000 + 5], tree[3 * 100000 + 5];
void init(int node, int b, int e) {
if (b == e) {
tree[node] = a[b - 1];
return;
}
int left = node * 2;
int right = left + 1;
int mid = (b + e) / 2;
init(left, b, mid);
init(right, mid + 1, e);
tree[node] = tree[left] & tree[right];
}
int query(int node, int b, int e, int x, int y) {
if (x > e || b > y) return (1 << 30) - 1;
if (b >= x && e <= y) return tree[node];
int left = node * 2;
int right = left + 1;
int mid = (b + e) / 2;
int p1 = query(left, b, mid, x, y);
int p2 = query(right, mid + 1, e, x, y);
return p1 & p2;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int ll, rr, qq;
cin >> l[i] >> r[i] >> q[i];
l[i] -= 1;
}
for (int pos = 0; pos <= 30; pos++) {
memset(sum, 0, sizeof sum);
for (int i = 0; i < m; i++)
if (check(q[i], pos)) {
sum[l[i]]++;
sum[r[i]]--;
}
for (int i = 1; i < n; i++) sum[i] += sum[i - 1];
for (int i = 0; i < n; i++)
if (sum[i] > 0) a[i] = Set(a[i], pos);
}
init(1, 1, n);
for (int i = 0; i < m; i++) {
int ans = query(1, 1, n, l[i] + 1, r[i]);
if (ans != q[i]) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
for (int i = 0; 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 = 100005;
int sum[maxn << 2];
vector<int> v;
struct node {
int l, r, q;
} dot[maxn];
void build(int x, int l, int r) {
if (l == r) {
sum[l] = 0;
return;
}
int mid = (l + r) / 2;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
}
void update(int x, int L, int R, int l, int r, int w) {
if (l <= L && r >= R) {
sum[x] |= w;
return;
}
int mid = (L + R) / 2;
if (mid >= r)
update(x << 1, L, mid, l, r, w);
else if (l > mid)
update(x << 1 | 1, mid + 1, R, l, r, w);
else {
update(x << 1, L, mid, l, mid, w);
update(x << 1 | 1, mid + 1, R, mid + 1, r, w);
}
}
int query(int x, int L, int R, int l, int r) {
if (l <= L && R <= r) {
return sum[x];
}
int mid = (L + R) / 2;
if (mid >= r)
return query(x << 1, L, mid, l, r);
else if (l > mid)
return query(x << 1 | 1, mid + 1, R, l, r);
else
return query(x << 1, L, mid, l, mid) &
query(x << 1 | 1, mid + 1, R, mid + 1, r);
}
void find(int x, int l, int r) {
if (x != 1) sum[x] |= sum[x / 2];
if (l == r) {
v.push_back(sum[x]);
return;
}
int mid = (l + r) / 2;
find(x << 1, l, mid);
find(x << 1 | 1, mid + 1, r);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
build(1, 1, n);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &dot[i].l, &dot[i].r, &dot[i].q);
update(1, 1, n, dot[i].l, dot[i].r, dot[i].q);
}
int flag = 0;
for (int i = 1; i <= m; i++) {
if (query(1, 1, n, dot[i].l, dot[i].r) != dot[i].q) {
flag = 1;
break;
}
}
if (flag)
printf("NO\n");
else {
find(1, 1, n);
printf("YES\n");
printf("%d", v[0]);
for (int i = 1; i < v.size(); i++) {
printf(" %d", v[i]);
}
printf("\n");
v.clear();
}
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 A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
return out << "(" << a.first << "," << a.second << ")";
}
template <class A>
ostream &operator<<(ostream &out, const vector<A> &a) {
for (const A &it : a) out << it << " ";
return out;
}
template <class A, class B>
istream &operator>>(istream &in, pair<A, B> &a) {
return in >> a.first >> a.second;
}
template <class A>
istream &operator>>(istream &in, vector<A> &a) {
for (A &i : a) in >> i;
return in;
}
ifstream cinn("in.txt");
ofstream coutt("out.txt");
long long poww(const long long &a, long long b,
const long long &m = 1000000007) {
if (b == 0) return 1;
long long first = poww(a, b / 2, m);
first = first * first % m;
if (b & 1) first = first * a % m;
return first;
}
long long gcd(long long a, long long b) {
if (a > b) swap(a, b);
if (a == 0) return b;
return gcd(b % a, a);
}
long long rint(long long l, long long r) { return rand() % (r - l + 1) + l; }
const long long N = 5e5 + 5;
long long st[N];
vector<long long> ans(N + 1);
void build(long long n, long long l, long long r) {
if (l == r) {
st[n] = ans[l];
return;
}
long long mid = (l + r) / 2;
build(n * 2, l, mid);
build((n * 2) + 1, mid + 1, r);
st[n] = st[n * 2] & st[(n * 2) + 1];
}
long long query(long long n, long long l, long long r, long long a,
long long b) {
if (l > b || r < a) {
return -1;
} else if (l >= a && r <= b) {
return st[n];
}
long long mid = (l + r) / 2;
long long leftQuery = query(n * 2, l, mid, a, b);
long long rightQuery = query(n * 2 + 1, mid + 1, r, a, b);
long long q = 0;
if (rightQuery == -1) {
return leftQuery;
}
if (leftQuery == -1) {
return rightQuery;
}
return leftQuery & rightQuery;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long i, j, first;
long long n, m;
cin >> n >> m;
long long bits = 31;
set<long long> st[bits];
for (i = 0; i < bits; i++) {
for (j = 0; j < n + 1; j++) {
st[i].insert(j);
}
}
vector<pair<pair<long long, long long>, long long>> queries;
for (i = 0; i < m; i++) {
long long left, right, val;
cin >> left >> right >> val;
queries.push_back({{left, right}, val});
for (j = 0; j < bits; j++) {
if ((val & 1 << j)) {
long long cur = left;
auto it = st[j].lower_bound(cur);
while ((it != st[j].end()) && (*it <= right)) {
ans[*it] |= (1 << j);
st[j].erase(it);
it = st[j].lower_bound(cur);
}
}
}
}
build(1, 1, n);
for (i = 0; i < m; i++) {
if (query(1, 1, n, queries[i].first.first, queries[i].first.second) !=
queries[i].second) {
cout << "NO" << '\n';
return 0;
}
}
cout << "YES" << '\n';
for (long long i = 1; i < n + 1; 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 | #include <bits/stdc++.h>
using namespace std;
int n, m;
int t[32][100100];
int l[100001], r[100001], q[100001];
int a[100100];
int st[100001][18];
int Log2[100001];
void buildSparse() {
for (int i = 0; i < n; ++i) st[i][0] = a[i];
for (int j = 1; j < 18; ++j) {
for (int i = 0; i + (1 << j) - 1 < n; ++i) {
st[i][j] = st[i][j - 1] & st[i + (1 << (j - 1))][j - 1];
}
}
}
int f(int l, int r) {
int logarithm = Log2[r - l + 1];
return st[l][logarithm] & st[r - (1 << logarithm) + 1][logarithm];
}
bool check() {
buildSparse();
for (int i = 0; i < m; ++i) {
if (f(l[i] - 1, r[i] - 1) != q[i]) return false;
}
return true;
}
int main() {
Log2[0] = -1;
for (int i = 1; i <= 100000; ++i) Log2[i] = Log2[i >> 1] + 1;
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 j = 0; j < 31; ++j) {
if (q[i] >> j & 1) {
++t[j][l[i] - 1];
--t[j][r[i]];
}
}
}
for (int id = 0; id < 31; ++id) {
for (int i = 0; i < n; ++i) {
if (i) t[id][i] += t[id][i - 1];
if (t[id][i]) a[i] |= 1 << id;
}
}
if (check()) {
cout << "YES\n";
for (int i = 0; i < n; ++i) cout << a[i] << " ";
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Solution {
public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static StringTokenizer tok;
public static class Interval implements Comparable<Interval> {
int l;
int r;
int q;
public Interval(int l, int r, int q) {
this.l = l;
this.r = r;
this.q = q;
}
@Override
public int compareTo(Interval o) {
if (this.l < o.l) {
return -1;
} else if (this.l == o.l) {
return Integer.compare(this.r, this.l);
} else {
return 1;
}
}
}
public static int nextInt() throws IOException {
if (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return Integer.parseInt(tok.nextToken());
}
public static int getBit(int a, int k) {
return ((a >> k) & 1);
}
public static int setBit(int a, int k) {
return (a | (1 << k));
}
public static void main(String[] args) throws IOException {
int n = nextInt();
int m = nextInt();
Interval[] intervals = new Interval[m];
for (int i = 0; i < m; i++) {
intervals[i] = new Interval(nextInt() - 1, nextInt() - 1, nextInt());
}
Arrays.sort(intervals);
int[] result = new int[n];
for (int j = 0; j < 30; j++) {
int lastR = -1;
for (int i = 0; i < m; i++) {
if (getBit(intervals[i].q, j) == 1 && intervals[i].r > lastR) {
for (int k = Math.max(lastR, intervals[i].l); k <= intervals[i].r; k++) {
result[k] = setBit(result[k], j);
}
lastR = intervals[i].r;
}
}
}
boolean possible = true;
for (int j = 0; j < 30 && possible; j++) {
int lastL = -1;
int[] zerosCount = new int[n + 1];
zerosCount[0] = 0;
for (int i = 0; i < n; i++) {
zerosCount[i + 1] = zerosCount[i] + (getBit(result[i], j) == 0 ? 1 : 0);
}
for (int i = 0; i < m && possible; i++) {
if (getBit(intervals[i].q, j) == 0) {
possible = (zerosCount[intervals[i].r + 1] - zerosCount[intervals[i].l] > 0);
}
}
}
if (!possible) {
System.out.println("NO");
} else {
System.out.println("YES");
for (int i = 0; i < n; i++) {
System.out.print(result[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.util.*;
import java.io.*;
public class b {
public static void main(String[] args) throws IOException
{
input.init(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = input.nextInt(), m = input.nextInt();
int[] as = new int[m], bs = new int[m], cs = new int[m];
IT it = new IT(n);
for(int i = 0; i<m; i++)
{
as[i] = input.nextInt()-1;
bs[i] = input.nextInt()-1;
cs[i] = input.nextInt();
it.add(as[i], bs[i], cs[i]);
}
boolean good = true;
for(int i = 0; i<m; i++)
{
good &= it.get(as[i], bs[i]) == cs[i];
}
if(good)
{
out.println("YES");
for(int i = 0; i<n; i++) out.print(it.get(i, i)+" ");
}
else out.println("NO");
out.close();
}
//Minimum Interval Tree
static class IT
{
int[] left,right, val, a, b, prop;
IT(int n)
{
left = new int[4*n];
right = new int[4*n];
val = new int[4*n];
prop = new int[4*n];
a = new int[4*n];
b = new int[4*n];
init(0,0, n-1);
}
int init(int at, int l, int r)
{
a[at] = l;
b[at] = r;
if(l==r)
left[at] = right [at] = -1;
else
{
int mid = (l+r)/2;
left[at] = init(2*at+1,l,mid);
right[at] = init(2*at+2,mid+1,r);
}
return at++;
}
//return the min over [x,y]
int get(int x, int y)
{
return go(x,y, 0);
}
void push(int at)
{
if(prop[at] != 0)
{
go3(a[left[at]], b[left[at]], prop[at], left[at]);
go3(a[right[at]], b[right[at]], prop[at], right[at]);
prop[at] = 0;
}
}
int go(int x,int y, int at)
{
if(at==-1 || y<a[at] || x>b[at]) return (1<<30) - 1;
if(x <= a[at] && y>= b[at]) return val[at];
push(at);
return go(x, y, left[at]) & go(x, y, right[at]);
}
//add v to elements x through y
void add(int x, int y, int v)
{
go3(x, y, v, 0);
}
void go3(int x, int y, int v, int at)
{
if(at==-1) return;
if(y < a[at] || x > b[at]) return;
x = Math.max(x, a[at]);
y = Math.min(y, b[at]);
if(y == b[at] && x == a[at])
{
val[at] |= v;
prop[at] |= v;
return;
}
push(at);
go3(x, y, v, left[at]);
go3(x, y, v, right[at]);
val[at] = val[left[at]] & val[right[at]];
}
}
public static class input {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( 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;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
const int INF = 0x3f3f3f3f, N = 1e5 + 5;
const ll LINF = 1e18 + 5;
int n, m, a[N];
struct query {
int l, r, q;
} Q[N];
int bit[N << 2][30];
bool tag[N << 2][30];
void pushdown(int p, int k, int l, int r) {
bool &x = tag[p][k];
int mid = (l + r) >> 1;
bit[p << 1][k] = mid - l + 1;
bit[p << 1 | 1][k] = r - mid;
tag[p << 1][k] = tag[p << 1 | 1][k] = x;
x = 0;
}
void update(int p, int l, int r, int x, int y, int k) {
if (l >= x && r <= y) {
bit[p][k] = r - l + 1;
tag[p][k] = 1;
return;
}
if (tag[p][k]) pushdown(p, k, l, r);
int mid = (l + r) >> 1;
if (x <= mid) update(p << 1, l, mid, x, y, k);
if (y > mid) update(p << 1 | 1, mid + 1, r, x, y, k);
}
bool query(int p, int l, int r, int x, int y, int k) {
if (l >= x && r <= y) return bit[p][k] != (r - l + 1);
if (tag[p][k]) pushdown(p, k, l, r);
int mid = (l + r) >> 1;
if (x <= mid)
if (query(p << 1, l, mid, x, y, k)) return 1;
if (y > mid)
if (query(p << 1 | 1, mid + 1, r, x, y, k)) return 1;
return 0;
}
void find(int p, int l, int r) {
if (l == r) {
for (int k = 0; k < 30; k++) a[l] |= (bit[p][k] << k);
return;
}
for (int k = 0; k < 30; k++)
if (tag[p][k]) pushdown(p, k, l, r);
int mid = (l + r) >> 1;
find(p << 1, l, mid);
find(p << 1 | 1, mid + 1, r);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
bool ok = 1;
for (int i = 1; i <= m; i++) {
cin >> Q[i].l >> Q[i].r >> Q[i].q;
for (int j = 0; j < 30; j++) {
if ((Q[i].q >> j) & 1) update(1, 1, n, Q[i].l, Q[i].r, j);
}
}
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 30; j++) {
if (!((Q[i].q >> j) & 1)) {
ok = query(1, 1, n, Q[i].l, Q[i].r, j);
if (!ok) break;
}
}
if (!ok) break;
}
if (ok) {
cout << "YES" << '\n';
find(1, 1, n);
for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == 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 | //package round275;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class B {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni(), m = ni();
int[][] cs = new int[m][];
for(int i = 0;i < m;i++){
cs[i] = new int[]{ni()-1, ni()-1, ni()};
}
int[] ret = new int[n];
for(int d = 0;d < 30;d++){
int[] imos = new int[n+1];
for(int i = 0;i < m;i++){
if(cs[i][2]<<~d<0){
imos[cs[i][0]]++;
imos[cs[i][1]+1]--;
}
}
for(int i = 1;i < n;i++){
imos[i] += imos[i-1];
}
for(int i = 0;i < n;i++){
if(imos[i] > 0)imos[i] = 1;
}
int[] cum = new int[n+1];
for(int i = 1;i <= n;i++){
cum[i] = cum[i-1] + imos[i-1];
}
for(int i = 0;i < m;i++){
if(cs[i][2]<<~d >= 0){
if(cum[cs[i][1]+1]-cum[cs[i][0]] == cs[i][1]-cs[i][0]+1){
out.println("NO");
return;
}
}
}
for(int i = 0;i < n;i++){
ret[i] |= imos[i]<<d;
}
}
out.println("YES");
for(int i = 0;i < n;i++){
out.print(ret[i] + " ");
}
out.println();
// for(int i = 0;i < m;i++){
// int a = -1;
// for(int j = cs[i][0];j <= cs[i][1];j++){
// a &= ret[j];
// }
// if(a != cs[i][2])throw new RuntimeException();
// }
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new B().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private 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++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(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[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
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 * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
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 * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| 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;
long long getMid(long long start, long long end) {
return start + (end - start) / 2;
}
long long getFin_helper(vector<long long> &seg_tree, long long index,
long long val) {
if (2 * index + 1 < seg_tree.size() && 2 * index + 2 < seg_tree.size()) {
seg_tree[index] =
getFin_helper(seg_tree, 2 * index + 1, val | seg_tree[index]) &
getFin_helper(seg_tree, 2 * index + 2, val | seg_tree[index]);
} else {
seg_tree[index] |= val;
}
return seg_tree[index];
}
void updateValue_helper(vector<long long> &seg_tree, long long segment_start,
long long segment_end, long long query_start,
long long query_end, long long diff, long long index) {
if (query_start <= segment_start && query_end >= segment_end) {
seg_tree[index] |= diff;
return;
}
if (segment_end < query_start || segment_start > query_end) return;
long long mid = getMid(segment_start, segment_end);
if (query_end <= mid) {
updateValue_helper(seg_tree, segment_start, mid, query_start, query_end,
diff, 2 * index + 1);
} else if (query_start > mid) {
updateValue_helper(seg_tree, mid + 1, segment_end, query_start, query_end,
diff, 2 * index + 2);
} else {
updateValue_helper(seg_tree, segment_start, mid, query_start, mid, diff,
2 * index + 1);
updateValue_helper(seg_tree, mid + 1, segment_end, mid + 1, query_end, diff,
2 * index + 2);
}
}
long long getAnd_helper(vector<long long> &seg_tree, long long segment_start,
long long segment_end, long long query_start,
long long query_end, long long index) {
if (query_start <= segment_start && query_end >= segment_end) {
return seg_tree[index];
}
if (segment_end < query_start || segment_start > query_end) return 0;
long long mid = getMid(segment_start, segment_end);
if (query_end <= mid) {
return getAnd_helper(seg_tree, segment_start, mid, query_start, query_end,
2 * index + 1);
} else if (query_start > mid) {
return getAnd_helper(seg_tree, mid + 1, segment_end, query_start, query_end,
2 * index + 2);
} else {
return getAnd_helper(seg_tree, segment_start, mid, query_start, mid,
2 * index + 1) &
getAnd_helper(seg_tree, mid + 1, segment_end, mid + 1, query_end,
2 * index + 2);
}
}
void updateValue(vector<long long> &seg_tree, long long query_start,
long long query_end, long long diff) {
long long l = seg_tree.size();
updateValue_helper(seg_tree, 0, l / 2, query_start, query_end, diff, 0);
}
long long getFin(vector<long long> &seg_tree) {
long long l = seg_tree.size();
return getFin_helper(seg_tree, 0, 0);
}
long long getAnd(vector<long long> &seg_tree, long long query_start,
long long query_end) {
long long l = seg_tree.size();
return getAnd_helper(seg_tree, 0, l / 2, query_start, query_end, 0);
}
long long make_seg_tree(long long n) {
long long x = (long long)(ceil(log2(n)));
long long max_size = 2 * (long long)pow(2, x) - 1;
return max_size;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<long long> seg_tree(make_seg_tree(n));
int m;
cin >> m;
vector<long long> query_p;
vector<long long> query_q;
vector<long long> query_v;
for (int i = 0; i < m; i++) {
long long p, q, v;
cin >> p >> q >> v;
query_p.push_back(p);
query_q.push_back(q);
query_v.push_back(v);
updateValue(seg_tree, p - 1, q - 1, v);
}
getFin(seg_tree);
for (int i = 0; i < m; i++) {
if (getAnd(seg_tree, query_p[i] - 1, query_q[i] - 1) != query_v[i]) {
cout << "NO";
return 0;
}
}
cout << "YES\n";
for (int i = 0; i < n; i++) {
cout << seg_tree[seg_tree.size() / 2 + 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.BufferedReader;
import java.io.File;
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.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class Solution {
static class FastScanner{
BufferedReader s;
StringTokenizer st;
public FastScanner(){
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(File f) throws FileNotFoundException{
st = new StringTokenizer("");
s = new BufferedReader (new FileReader(f));
}
public int nextInt() throws IOException{
if(st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public double nextDouble() throws IOException{
if(st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException{
if(st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String nextString() throws IOException{
if(st.hasMoreTokens())
return st.nextToken();
else{
st = new StringTokenizer(s.readLine());
return nextString();
}
}
public String readLine() throws IOException{
return s.readLine();
}
public void close() throws IOException{
s.close();
}
}
//FastScanner s = new FastScanner(new File("input.txt")); copy inside void solve
//PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
static FastScanner s = new FastScanner();
static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String args[])throws IOException{
//Main ob=new Main();
Solution ob=new Solution();
ob.solve();
ww.close();
}
/////////////////////////////////////////////
int tree[];
int dp[];
int arr[];
void build(int node,int l,int r){
if(l+1==r){
tree[node]=arr[l];
return;
}
int mid = (l+r)/2;
build(node*2,l,mid);
build(node*2+1,mid,r);
tree[node]=tree[node*2]&tree[node*2+1];
}
/*
int query(int i,int j,int node,int l,int h){
if (i == l && j == h) {
return tree[node];
}
int mid = (l + h) >> 1;
int ans = (1 << 30) - 1;
if (l < mid)
ans &= query(i, Math.min(j, mid),node*2, l, mid);
if (mid < h)
ans &= query(Math.max(i, mid),j, node*2+1, mid, h);
return ans;
}
*/
/* void build(int v, int l, int r) {
if (l + 1 == r) {
tree[v] = arr[l];
return;
}
int mid = (l + r) >> 1;
build(v * 2, l, mid);
build(v * 2 + 1, mid, r);
tree[v] = tree[v * 2] & tree[v * 2 + 1];
}
*/
int query(int v, int l, int r, int L, int R) {
if (l == L && r == R) {
return tree[v];
}
int mid = (L + R) >> 1;
int ans = (1<< 30) - 1
;
if (l < mid)
ans &= query(v * 2, l, Math.min(r, mid), L, mid);
if (mid < r)
ans &= query(v * 2 + 1, Math.max(l, mid), r, mid, R);
return ans;
}
/////////////////////////////////////////////
void solve() throws IOException {
int n = s.nextInt();
int m = s.nextInt();
int N=1000000;
int l[] = new int[N];
int r[] = new int[N];
int q[] = new int[N];
for(int i = 0;i < m;i++){
l[i] = s.nextInt()-1;
r[i] = s.nextInt();
q[i] = s.nextInt();
}
dp = new int[N];
arr = new int[N];
int power = (int) Math.floor(Math.log(N) / Math.log(2)) + 1;
power = (int) ((int) 2 * Math.pow(2, power));
tree = new int[power];
for(int bit = 0;bit <= 30;bit++){
for(int i=0;i<n;i++)
dp[i]=0;
for(int i=0;i<m;i++){
if (((q[i] >> bit) & 1)==1) {
dp[l[i]]++;
dp[r[i]]--;
}
}
for(int i=0;i<n;i++){
if(i>0)
dp[i]+= dp[i-1];
if(dp[i]>0)
arr[i]|=(1<<bit);
}
}
build(1,0,n);
//for(int i=0;i<n;i++)
//System.out.println(arr[i]+" ");
for(int i=0;i<m;i++){
if (query(1, l[i], r[i], 0, n) != q[i]){
ww.println("NO");
return;
}
}
ww.println("YES");
for(int i=0;i<n;i++)
ww.print(arr[i]+" ");
}//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 |
import java.io.*;
import java.util.*;
public class D {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
public void run() {
int n = in.nextInt(), m = in.nextInt();
int[][] cs = new int[m][];
for (int i = 0; i < m; i++)
cs[i] = new int[]{in.nextInt() - 1, in.nextInt() - 1, in.nextInt()};
int[] ret = new int[n];
for (int d = 0; d < 30; d++) {
int[] imos = new int[n+1];
for (int i = 0; i < m; i++) {
if (cs[i][2]<<~d < 0) {
imos[cs[i][0]]++;
imos[cs[i][1]+1]--;
}
}
for (int i = 1; i < n; i++)
imos[i] += imos[i-1];
for (int i = 0; i < n; i++)
if (imos[i] > 0) imos[i] = 1;
int[] cum = new int[n+1];
for (int i = 1; i <= n; i++) {
cum[i] = cum[i-1] + imos[i-1];
}
for (int i = 0; i < m; i++) {
if (cs[i][2]<<~d >= 0) {
if (cum[cs[i][1]+1] - cum[cs[i][0]] == cs[i][1] - cs[i][0] + 1) {
System.out.println("NO");
return;
}
}
}
for (int i = 0; i < n; i++) {
ret[i] |= imos[i]<<d;
}
}
out.println("YES");
for (int i = 0; i < n; i++)
out.print(ret[i] + " ");
out.println();
out.close();
}
/*
boolean exist = true;
class SegmentTree {
int N;
int[] dat;
public SegmentTree(int n) {
N = Integer.highestOneBit(n) << 1;
int size = (N << 1) - 1;
dat = new int[size];
Arrays.fill(dat, -1);
}
int query(int k) {
int res = 0;
k += N - 1;
while (k != 0) {
if (dat[k] != -1) res |= dat[k];
k = (k - 1) / 2;
}
return res;
}
boolean isCovered(int a, int b) {
while (b > 0) {
if ((b & 1) == 0 && (a & 1) == 1) return false;
b /= 2;
a /= 2;
}
return true;
}
private void update(int bit, int a, int b, int k, int l, int r) {
// System.out.println("query : " + a + " " + b + " " + k + " " + l + " " + r);
if (r < a || b < l) return;
if (a <= l && r <= b) {
if (dat[k] == -1) {
dat[k] = bit;
} else if (isCovered(bit, dat[k])) {
dat[k] |= bit;
} else {
exist = false;
}
} else {
update(bit, a, b, k * 2 + 1, l, (l + r) / 2);
update(bit, a, b, k * 2 + 2, (l + r) / 2 + 1, r);
}
}
void updateQuery(int a, int b, int bit) {
update(bit, a, b, 0, 0, N - 1);
// printTree(dat);
}
}
void printTree(int[] dat) {
int n = 1;
for (int i = 0, cnt = 1; i < dat.length; i++, cnt++) {
System.out.print(dat[i] + " ");
if (n == cnt) {
System.out.println();
n *= 2;
cnt = 0;
}
}
}
public void run() {
int n = in.nextInt(), m = in.nextInt();
SegmentTree st = new SegmentTree(n);
for (int i = 0; i < m; i++) {
int l = in.nextInt(), r = in.nextInt(), q = in.nextInt();
st.updateQuery(l, r, q);
}
if (exist) {
out.println("YES");
for (int i = 1; i <= n; i++) {
out.print(st.query(i) + " ");
}
out.println();
} else {
out.println("NO");
}
out.close();
}
*/
public static void main(String[] args) {
new D().run();
}
public void mapDebug(int[][] a) {
System.out.println("--------map display---------");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.println();
}
System.out.println("----------------------------");
System.out.println();
}
public void debug(Object... obj) {
System.out.println(Arrays.deepToString(obj));
}
class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
//stream = new FileInputStream(new File("dec.in"));
}
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;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
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();
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++)
array[i] = next();
return array;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
| 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.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
*/
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 {
private static final int MAXBIT = 30;
private int seg[], l[], r[], q[], sum[], ans[];
public void solve(int testNumber, InputReader in, PrintWriter out) {
int N = in.nextInt();
int M = in.nextInt();
l = new int[M];
r = new int[M];
q = new int[M];
sum = new int[N+1];
ans = new int[N];
seg = new int[4 * N];
for (int i = 0; i < M; i++) {
l[i] = in.nextInt() - 1;
r[i] = in.nextInt();
q[i] = in.nextInt();
}
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) == 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) {
ans[i] |= (1 << bit);
}
}
}
build(1, 0, N-1);
for (int i = 0; i < M; i++) {
if (query(1, l[i], r[i]-1, 0, N-1) != q[i]) {
out.println("NO");
return;
}
}
out.println("YES");
for (int i = 0; i < N; i++) {
out.print(ans[i] + " ");
}
out.println();
}
private void build(int idx, int l, int r) {
if (l == r) {
seg[idx] = ans[l];
return;
}
int mid = (l + r) >> 1;
build(2 * idx, l, mid);
build(2 * idx + 1, mid+1, r);
seg[idx] = seg[idx * 2] & seg[idx * 2 + 1];
}
private int query(int idx, int l, int r, int L, int R) {
if (l == L && r == R) {
return seg[idx];
}
int mid = (L + R) >> 1;
int ans = (1 << MAXBIT) - 1;
if (l <= mid) {
ans &= query(idx * 2, l, Math.min(r, mid), L, mid);
}
if (mid+1 <= r) {
ans &= query(idx * 2 + 1, Math.max(l, mid+1), r, mid+1, R);
}
return ans;
}
}
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>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const double PI = 4 * atan(1);
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAX_N = 2e6 + 10;
int n, c;
int L[MAX_N], R[MAX_N], Q[MAX_N];
ll sum[MAX_N];
ll a[MAX_N], t[4 * MAX_N];
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[2 * v] & t[2 * v + 1];
}
int query(int v, int l, int r, int A, int B) {
if (l == A and r == B) return t[v];
int mid = (A + B) / 2;
int ans = (1LL << 30) - 1;
if (l < mid) ans &= query(v * 2, l, min(r, mid), A, mid);
if (mid < r) ans &= query(v * 2 + 1, max(l, mid), r, mid, B);
return ans;
}
void solve() {
for (int bit = 0; bit <= 30; ++bit) {
memset(sum, 0, sizeof(sum));
for (int i = 0; i < c; ++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] > 0) {
a[i] |= (1LL << bit);
}
}
}
build(1, 0, n);
for (int i = 0; i < c; i++) {
if (query(1, L[i], R[i], 0, n) != Q[i]) {
cout << "NO\n";
return;
}
}
cout << "YES\n";
for (int i = 0; i < n; ++i) {
cout << a[i] << ' ';
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> c;
for (int i = 0; i < c; ++i) {
cin >> L[i] >> R[i] >> Q[i];
L[i]--;
}
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;
struct nod {
int l, r;
long long int q;
} nods[100000 + 10];
bool compare(nod x, nod y) {
if (x.l == y.l) return x.r < y.r;
return x.l < y.l;
}
long long int ans[100000 + 10];
int bits[35];
int last[35];
long long int p2[35];
long long int segs[5 * 100000 + 10];
void build(int nod, int b, int e) {
if (b == e) {
segs[nod] = ans[b];
} else {
int mid = (b + e) / 2;
build(2 * nod + 1, b, mid);
build(2 * nod + 2, mid + 1, e);
segs[nod] = segs[2 * nod + 1] & segs[2 * nod + 2];
}
}
long long int query(int nod, int b, int e, int l, int r) {
if (b >= l && e <= r) {
long long int u = segs[nod];
return u;
}
int mid = (b + e) / 2;
if (r <= mid) {
long long int u = query(2 * nod + 1, b, mid, l, r);
return u;
}
if (l > mid) {
long long int u = query(2 * nod + 2, mid + 1, e, l, r);
return u;
}
long long int v1 = query(2 * nod + 1, b, mid, l, r);
long long int v2 = query(2 * nod + 2, mid + 1, e, l, r);
long long int v = v1 & v2;
return v;
}
int main() {
int i, n, m;
scanf("%d %d", &n, &m);
p2[0] = 1;
for (i = 1; i <= 30; i++) {
last[i] = 0;
bits[i] = 0;
p2[i] = p2[i - 1] + p2[i - 1];
}
for (i = 1; i <= m; i++)
scanf("%d %d %lld", &nods[i].l, &nods[i].r, &nods[i].q);
sort(nods + 1, nods + m + 1, compare);
int j = 1;
for (i = 1; i <= n; i++) {
while (j <= m && nods[j].l <= i) {
long long int temp = nods[j].q;
for (int k = 1; k <= 30; k++) {
if (temp % 2 == 1) last[k] = max(last[k], nods[j].r);
temp = temp / 2;
}
j++;
}
long long int val = 0;
for (int k = 1; k <= 30; k++) {
if (last[k] < i)
bits[k] = 0;
else
bits[k] = 1;
val = val + p2[k - 1] * bits[k];
}
ans[i] = val;
}
int fl = 1;
build(0, 1, n);
for (int i = 1; i <= m; i++) {
long long int res = query(0, 1, n, nods[i].l, nods[i].r);
if (res != nods[i].q) {
fl = 0;
printf("NO\n");
return 0;
}
}
printf("YES\n");
for (i = 1; i <= n; i++) {
printf("%lld ", 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;
const long long N = 1e5 + 10, allone = ((long long)1 << 31) - 1;
long long segment[3 * N], segmentand[3 * N], n, q, l[N], r[N], k[N], a[N];
void addSEG(int L, int R, int num, int s, int f, int id) {
if (R <= s || L >= f || s == f) return;
if (L <= s && R >= f) {
segment[id] = (segment[id] | num);
return;
}
long long mid = (s + f) / 2;
addSEG(L, R, num, s, mid, id * 2);
addSEG(L, R, num, mid, f, id * 2 + 1);
}
void findval(int L, int R, int num, int s, int f, int id) {
if (R <= s || L >= f || s == f) return;
num = (num | segment[id]);
if (s == f - 1) {
segmentand[id] = num;
a[L] = num;
return;
}
long long mid = (s + f) / 2;
findval(L, R, num, s, mid, id * 2);
findval(L, R, num, mid, f, id * 2 + 1);
segmentand[id] = (segmentand[id * 2] & segmentand[id * 2 + 1]);
}
long long findand(int L, int R, int s, int f, int id) {
if (L >= f || R <= s || s == f) return allone;
if (L <= s && R >= f) return segmentand[id];
int mid = (s + f) / 2;
return (findand(L, R, s, mid, id * 2) & findand(L, R, mid, f, id * 2 + 1));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 0; i < q; i++) {
cin >> l[i] >> r[i] >> k[i];
addSEG(l[i] - 1, r[i], k[i], 0, n, 1);
}
for (int i = 0; i < n; i++) findval(i, i + 1, 0, 0, n, 1);
for (int i = 0; i < q; i++)
if (findand(l[i] - 1, r[i], 0, n, 1) != k[i]) return (cout << "NO"), 0;
cout << "YES" << endl;
for (int 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 | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import org.omg.CORBA.REBIND;
public class solver {
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
static final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;
void init() throws FileNotFoundException{
if (OJ){
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException{
while (tok == null || !tok.hasMoreTokens()){
tok = new StringTokenizer(in.readLine()," :");
}
return tok.nextToken();
}
int readInt() throws NumberFormatException, IOException{
return Integer.parseInt(readString());
}
double readDouble() throws NumberFormatException, IOException{
return Double.parseDouble(readString());
}
long readLong() throws NumberFormatException, IOException{
return Long.parseLong(readString());
}
long time;
void run(){
try{
time = System.currentTimeMillis();
init();
solve();
if (!OJ) System.err.println(System.currentTimeMillis() - time);
out.close();
in.close();
}catch (Exception e){
e.printStackTrace();
}
}
long gcd(long a, long b){
if (a == 0) return b;
return gcd(b % a, a);
}
class SegmentTree{
int[] a;
int[] b;
int[] flag;
int n;
int size;
int stand = (1 << 31) - 1;
int def = (1 << 30) - 1;
SegmentTree(int n){
this.n = n;
size = 4 * n;
a = new int[size];
Arrays.fill(a, stand);
flag = new int[size];
Arrays.fill(flag, stand);
b = new int[size];
Arrays.fill(b, stand);
}
void push(int v){
if (v >= size) return;
if (flag[v] == stand) return;
if (a[v] == stand)
a[v] = 0;
a[v] |= flag[v];
if (v*2 < size){
if (flag[v*2] == stand)
flag[v*2] = flag[v];
else
flag[v*2] |= flag[v];
}
if (v*2+1 < size){
if (flag[v*2+1] == stand)
flag[v*2+1] = flag[v];
else
flag[v*2+1] |= flag[v];
}
flag[v] = stand;
}
void set(int v,int tl,int tr,int left,int right, int q){
push(v);
if (tl == left && tr == right){
flag[v] = q;
push(v);
return;
}
int mid = (tl + tr) / 2;
if (right <= mid){
set(v*2,tl,mid,left,right,q);
}else{
if (left > mid){
set(v*2+1,mid+1,tr,left,right,q);
}else{
set(v*2,tl,mid,left,mid,q);
set(v*2+1,mid+1,tr,mid+1,right,q);
}
}
push(v*2);
push(v*2+1);
a[v] = a[v*2] | a[v*2+1];
}
int get(int v, int tl, int tr, int left, int right){
if (tl == left && tr == right){
return b[v];
}
int mid = (tl + tr) / 2;
if (right <= mid){
return get(v*2,tl,mid,left,right);
}else{
if (left > mid){
return get(v*2+1, mid+1,tr,left,right);
}else{
return get(v*2,tl,mid,left,mid) & get(v*2+1,mid+1,tr,mid+1,right);
}
}
}
void rebuild(int v, int tl, int tr){
push(v);
if (tl == tr){
b[v] = a[v];
return;
}
int mid = (tl + tr) / 2;
rebuild(v*2,tl,mid);
rebuild(v*2+1,mid+1,tr);
b[v] = b[v*2] & b[v*2+1];
}
void rebuild(){
rebuild(1,1,n);
}
void set(int l,int r,int q){
set(1,1,n,l,r,q);
}
int get(int l,int r){
int res = get(1,1,n,l,r);
return res == stand ? def : res;
}
}
void solve() throws NumberFormatException, IOException{
int n = readInt();
int m = readInt();
SegmentTree st = new SegmentTree(n);
int[] l = new int[m];
int[] r = new int[m];
int[] q = new int[m];
for (int i=0;i<m;i++){
l[i] = readInt();
r[i] = readInt();
q[i] = readInt();
st.set(l[i], r[i], q[i]);
}
st.rebuild();
boolean ok = true;
for (int i=0;i<m;i++){
int p = (st.get(l[i], r[i]));
if (p != q[i]) ok = false;
}
if (!ok){
out.println("NO");
}else{
out.println("YES");
for (int i=1;i<=n;i++){
out.print(st.get(i, i)+" ");
}
}
}
public static void main(String[] args) {
new solver().run();
}
} | 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.util.*;
import java.io.*;
public class D_483 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt(), m = sc.nextInt();
int N = 1;
while(N < n)
N <<= 1;
int[] array = new int[N + 1];
SegmentTree tree = new SegmentTree(array);
int[][] cond = new int[m][3];
for(int i = 0; i < m; i++)
cond[i] = sc.nextIntArray(3);
for(int i = 0; i < m; i++)
tree.update_range(cond[i][0], cond[i][1], cond[i][2]);
boolean flag = true;
for(int i = 0; i < m; i++)
if(tree.query(cond[i][0], cond[i][1]) != cond[i][2])
flag = false;
if(flag) {
pw.println("YES");
for(int i = 0; i < n; i++)
pw.print(tree.query(i + 1, i + 1) + (i == n - 1 ? "\n" : " "));
} else {
pw.println("NO");
}
pw.flush();
}
public static class SegmentTree {
int N;
int[] in, sTree, lazy;
public SegmentTree(int[] array) {
in = array;
N = array.length - 1;
sTree = new int[N<<1];
lazy = new int[N<<1];
build(1, 1, N);
}
public void build(int node, int b, int e) {
if(b == e) {
sTree[node] = in[b];
return;
}
int left = node<<1, right = node<<1|1;
int mid = (b + e)>>1;
build(left, b, mid);
build(right, mid + 1, e);
sTree[node] = sTree[left] & sTree[right];
}
public int query(int l, int r) {
return query(1, 1, N, l, r);
}
public int query(int node, int b, int e, int l, int r) {
if(r < b || l > e)
return -1;
if(b >= l && e <= r)
return sTree[node];
int left = node<<1, right = node<<1|1;
int mid = (b + e)>>1;
propagate(node, b, e, left, right, mid);
int x = query(left, b, mid, l, r), y = query(right, mid + 1, e, l, r);
if(x == -1)
return y;
if(y == -1)
return x;
return x & y;
}
public void update_range(int l, int r, int v) {
update_range(1, 1, N, l, r, v);
}
public void update_range(int node, int b, int e, int l, int r, int v) {
if(l > e || r < b)
return;
if(b >= l && e <= r) {
sTree[node] |= v;
lazy[node] |= v;
return;
}
int left = node<<1, right = node<<1|1;
int mid = (b + e)>>1;
propagate(node, b, e, left, right, mid);
update_range(left, b, mid, l, r, v);
update_range(right, mid + 1, e, l, r, v);
sTree[node] = sTree[left] & sTree[right];
}
public void propagate(int node, int b, int e, int left, int right, int mid) {
lazy[left] |= lazy[node];
lazy[right] |= lazy[node];
sTree[left] |= lazy[node];
sTree[right] |= lazy[node];
lazy[node] = 0;
}
}
public static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
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 int[] nextIntArray(int n) throws IOException {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] array = new Integer[n];
for (int i = 0; i < n; i++)
array[i] = new Integer(nextInt());
return array;
}
public long[] nextLongArray(int n) throws IOException {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
public static int[] shuffle(int[] a) {
int n = a.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
int tmpIdx = rand.nextInt(n);
int tmp = a[i];
a[i] = a[tmpIdx];
a[tmpIdx] = tmp;
}
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
| 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.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
public class Main {
static class Reader {
final private int BUFFER_SIZE = 1 << 17;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader(InputStream in) {
din = new DataInputStream(in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public byte next() {
try {
return read();
} catch (Exception e) {
throw new InputMismatchException();
}
}
public String nextString() {
StringBuilder sb = new StringBuilder("");
byte c = next();
while (c <= ' ') {
c = next();
}
do {
sb.append((char) c);
c = next();
} while (c > ' ');
return sb.toString();
}
public int nextInt() {
int ret = 0;
byte c = next();
while (c <= ' ') {
c = next();
}
boolean neg = c == '-';
if (neg) {
c = next();
}
do {
ret = (ret << 3) + (ret << 1) + c - '0';
c = next();
} while (c > ' ');
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() {
try {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
} catch (IOException e) {
throw new InputMismatchException();
}
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
}
public static void main(String args[]) throws IOException{
Reader r=new Reader(System.in);
PrintWriter pr=new PrintWriter(System.out,false);
int n=r.nextInt();
int m=r.nextInt();
int a[]=new int[n];
int l[]=new int[m];
int R[]=new int[m];
int k[]=new int[m];
int temp[]=new int[m];
int tree[][]=new int[4*n][2];
for(int i=0;i<m;i++)
{
l[i]=r.nextInt()-1;R[i]=r.nextInt()-1;temp[i]=k[i]=r.nextInt();
}
int max=20;
for(int i=0;i<m;i++)
{
int count;
update(tree,1,0,n-1,l[i],R[i],k[i]);
}
int t[]=new int[4*n];
traverse(a,tree,1,0,n-1);
// construct(a,t,1,0,n-1);
for(int i=0;i<m;i++)
{
if(query(tree,1,0,n-1,l[i],R[i])!=k[i])
{
pr.println("NO");
pr.flush();
pr.close();
return;
}
}
pr.println("YES");
for(int i=0;i<n;i++)
{
pr.print(a[i]+" ");
}
pr.println();
pr.flush();
pr.close();
}
public static void traverse(int a[],int tree[][],int root,int l,int r)
{
if(tree[root][1]!=0)
{
tree[root][0]|=tree[root][1];
if(l!=r)
{
tree[root<<1][1]|=tree[root][1];
tree[(root<<1)+1][1]|=tree[root][1];
}
tree[root][1]=0;
}
if(l==r)
{
a[l]=tree[root][0];
return;
}
traverse(a,tree,root<<1,l,(l+r)>>1);
traverse(a,tree,(root<<1)+1,((l+r)>>1)+1,r);
tree[root][0]=tree[root<<1][0]&tree[(root<<1)+1][0];
}
public static void construct(int a[],int tree[], int root,int l ,int r)
{
if(l==r)
{
tree[root]=a[l];
return;
}
construct(a,tree,root<<1,l,(l+r)>>1);
construct(a,tree,(root<<1)+1,((l+r)>>1)+1,r);
tree[root]=tree[root<<1]&tree[(root<<1)+1];
}
public static void update(int tree[][],int root,int l,int r,int begin,int end,int value)
{
if(tree[root][1]!=0)
{
tree[root][0]|=tree[root][1];
if(l!=r)
{
tree[root<<1][1]|=tree[root][1];
tree[(root<<1)+1][1]|=tree[root][1];
}
tree[root][1]=0;
}
if(begin>r||end<l)
return;
if(begin<=l&&r<=end)
{
tree[root][0]|=value;
if(l!=r)
{
tree[root<<1][1]|=value;
tree[(root<<1)+1][1]|=value;
}
return;
}
update(tree,root<<1,l,(l+r)>>1,begin,end,value);
update(tree,(root<<1)+1,((l+r)>>1)+1,r,begin,end,value);
}
static int query(int tree[][],int root,int l,int r,int begin,int end)
{
if(begin>r||end<l)
return Integer.MAX_VALUE;
if(begin<=l&&r<=end)
{
return tree[root][0];
}
return query(tree,root<<1,l,(l+r)>>1,begin,end)&query(tree,(root<<1)+1,((l+r)>>1)+1,r,begin,end);
}
}
| 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.util.*;
import java.io.*;
public class Main {
static long finArray[];
static long tree[];
static long ret = (1 << 31) - 1;
public static void main(String [] args)throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int array[][] = new int[n + 2][31];
int start [] = new int[m];
int end [] = new int[m];
int value [] = new int[m];
for(int t = 0;t < m;t++){
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
start[t] = l;
end[t] = r;
value[t] = q;
int cnt = -1;
while(q > 0){
cnt++;
if((q & 1) == 0){
q /= 2;
continue;
}
array[l][cnt]++;
array[r + 1][cnt]--;
q /= 2;
}
}
for(int i = 0;i < array[0].length;i++){
int cnt = 0;
for(int j = 1;j <= n;j++){
cnt += array[j][i];
if(cnt > 0)array[j][i] = 1;
else array[j][i] = 0;
}
}
finArray = new long [n + 1];
tree = new long [4*n + 1];
int pow [] = new int [32];
pow[0] = 1;
for(int i = 1;i <= 31;i++)
pow[i] = pow[i - 1]*2;
for(int i = 1;i <= n;i++){
long cnt = 0;
for(int j = 0;j < array[0].length;j++){
cnt += pow[j]*array[i][j];
}
finArray[i] = cnt;
}
//for(int i = 1;i <= n;i++)System.out.println(finArray[i]);
buildtree(1,1,n);
for(int i = 0;i < m;i++){
if(value[i] != querytree(1,1,n,start[i],end[i])){
System.out.print("NO");
return;
}
}
StringBuilder sb = new StringBuilder();
System.out.println("YES");
for(int i = 1;i <= n;i++)
sb.append(finArray[i] + " ");
System.out.print(sb);
}
static void buildtree(int node,int start,int end){
if(start > end)return;
if(start == end){
tree[node] = finArray[start];
return;
}
buildtree(2*node,start,(start + end)/2);
buildtree(2*node + 1,(start + end)/2 + 1,end);
tree[node] = (tree[2*node] & tree[2*node + 1]);
}
static long querytree(int node,int start,int end,int i,int j){
if(start > end || start > j || end < i)return ret;
if(start >= i && end <= j)return tree[node];
long q1 = querytree(2*node,start,(start + end)/2,i,j);
long q2 = querytree(2*node + 1,(start + end)/2 + 1,end,i,j);
long res = (q1 & q2);
return res;
}
} | 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 | //package codeforces;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* @author muhossain
* @since 2020-02-20
*/
public class B482 {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int n = fs.nextInt();
int m = fs.nextInt();
int[] l = new int[m + 1];
int[] r = new int[m + 1];
int[] q = new int[m + 1];
for (int i = 0; i < m; i++) {
l[i] = fs.nextInt();
r[i] = fs.nextInt();
q[i] = fs.nextInt();
}
int[] numbers = new int[n];
int[][] segments = new int[n + 1][32];
for (int i = 0; i < m; i++) {
int cQ = q[i];
int left = l[i] - 1;
int right = r[i] - 1;
for (int j = 1; j <= 31; j++) {
int tmp = 1 << j - 1;
if ((tmp & cQ) > 0) {
segments[left][j]++;
segments[right + 1][j]--;
}
}
}
for (int j = 1; j <= 31; j++) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += segments[i][j];
if (sum > 0) {
int tmp = 1 << j - 1;
numbers[i] |= tmp;
}
}
}
int[] segmentTree = buildSegmentTree(numbers);
boolean valid = true;
for (int i = 0; i < m; i++) {
int result = query(l[i], r[i], numbers.length, segmentTree);
if (result != q[i]) {
valid = false;
break;
}
}
StringBuilder output;
if (!valid) {
output = new StringBuilder("NO");
} else {
output = new StringBuilder("YES\n");
for (int i = 0; i < numbers.length; i++) {
int item = numbers[i];
output.append(item);
if (i < numbers.length - 1) {
output.append(" ");
}
}
}
System.out.println(output);
}
private static int query(int l, int r, int dataLength, int[] segmentTree) {
return queryUtil(1, l - 1, r - 1, 0, dataLength - 1, segmentTree);
}
private static int queryUtil(int node, int l, int r, int dataStart, int dataEnd, int[] segmentTree) {
if (l == dataStart && r == dataEnd) {
return segmentTree[node];
}
int mid = (dataStart + dataEnd) / 2;
if (l >= dataStart && r <= mid) {
return queryUtil(2 * node, l, r, dataStart, mid, segmentTree);
}
if (l > mid && r <= dataEnd) {
return queryUtil(2 * node + 1, l, r, mid + 1, dataEnd, segmentTree);
}
return queryUtil(2 * node, l, mid, dataStart, mid, segmentTree) &
queryUtil(2 * node + 1, mid + 1, r, mid + 1, dataEnd, segmentTree);
}
public static int[] buildSegmentTree(int[] data) {
int segmentTreeSize = (int) (2 * Math.pow(2, Math.ceil((Math.log10(data.length) / Math.log10(2)))));
int[] segmentTree = new int[segmentTreeSize];
segmentTreeBuildUtil(1, 0, data.length - 1, data, segmentTree);
return segmentTree;
}
public static void segmentTreeBuildUtil(int node, int start, int end, int[] data, int[] segmentTree) {
if (start == end) {
segmentTree[node] = data[start];
return;
}
int mid = (start + end) / 2;
segmentTreeBuildUtil(2 * node, start, mid, data, segmentTree);
segmentTreeBuildUtil(2 * node + 1, mid + 1, end, data, segmentTree);
segmentTree[node] = segmentTree[2 * node] & segmentTree[2 * node + 1];
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new FileReader("cases.in"));
st = new StringTokenizer("");
} catch (Exception e) {
e.printStackTrace();
}
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String line = "";
try {
line = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
public Integer[] nextIntegerArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public char[] nextCharArray() {
return nextLine().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.*;
import java.util.*;
public class D {
class IntervalTree {
int[] d;
HashSet<Integer> badId;
int neutral;
boolean and;
int cnt = 0;
int n;
public IntervalTree(int n, boolean and) {
this.and = and;
neutral = and ? Integer.MAX_VALUE : 0;
d = new int[n * 4];
this.n = n;
Arrays.fill(d, neutral);
badId = new HashSet<Integer>();
}
void add(int x) {
set(x, cnt++, 0, n - 1, 0);
}
void remove(int id) {
set(neutral, id, 0, n - 1, 0);
badId.add(id);
}
int set(int x, int ind, int L, int R, int i) {
if (L > ind || R < ind)
return d[i];
if (L == R) {
d[i] = badId.contains(ind) ? neutral : x;
return d[i];
}
int M = (R + L) / 2;
int res = set(x, ind, L, M, (i * 2) + 1);
if (and)
res &= set(x, ind, M + 1, R, (i * 2) + 2);
else
res |= set(x, ind, M + 1, R, (i * 2) + 2);
d[i] = res;
return d[i];
}
int get(int l, int r, int L, int R, int i) {
if (R < l || L > r)
return neutral;
if (l <= L && r >= R)
return d[i];
if (L == R) {
if (badId.contains(L))
d[i] = neutral;
return d[i];
}
int M = (R + L) / 2;
if (and)
return get(l, r, L, M, (i * 2) + 1) & get(l, r, M + 1, R, (i * 2) + 2);
else
return get(l, r, L, M, (i * 2) + 1) | get(l, r, M + 1, R, (i * 2) + 2);
}
}
class Interval {
int l, r, q;
Interval(int l, int r, int q) {
this.l = l;
this.r = r;
this.q = q;
}
}
class Event implements Comparable<Event> {
int coord, q, itid;
Event pair;
Event(int coord, int id, boolean begin, int q) {
this.coord = coord;
this.id = id;
this.begin = begin;
this.q = q;
}
int id;
boolean begin;
@Override
public int compareTo(Event o) {
if (this.coord != o.coord)
return this.coord - o.coord;
if (!this.begin && o.begin)
return 1;
if (begin && !o.begin)
return -1;
if (this.id != o.id)
return id - o.id;
return 0;
}
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static void main(String[] args) throws IOException {
new D().run();
}
public void solve() throws IOException {
int n = nextInt();
int m = nextInt();
Event[] events = new Event[m << 1];
Interval[] intervals = new Interval[m];
for (int i = 0; i < m; ++i) {
int l = nextInt() - 1;
int r = nextInt() - 1;
int q = nextInt();
intervals[i] = new Interval(l, r, q);
events[i * 2] = new Event(l, i, true, q);
events[(i * 2) + 1] = new Event(r, i, false, q);
events[i * 2].pair = events[(i * 2) + 1];
events[(i * 2) + 1].pair = events[i * 2];
}
Arrays.sort(events);
int[] a = new int[n];
int cnt = 0;
IntervalTree it = new IntervalTree(m, false);
TreeSet<Event> ts = new TreeSet<Event>(new Comparator<Event>() {
@Override
public int compare(Event o1, Event o2) {
if (o1.q != o2.q)
return o1.q - o2.q;
return o1.id - o2.id;
}
});
for (int i = 0; i < n; ++i) {
while (cnt < (m * 2) && events[cnt].coord == i && events[cnt].begin) {
it.add(events[cnt].q);
ts.add(events[cnt]);
events[cnt].itid = it.cnt - 1;
++cnt;
}
a[i] = ts.size() == 0 ? 239 : it.get(0, m - 1, 0, m - 1, 0);
while (cnt < (m * 2) && events[cnt].coord == i) {
it.remove(events[cnt].pair.itid);
ts.remove(events[cnt].pair);
++cnt;
}
}
IntervalTree it1 = new IntervalTree(n, true);
for (int i = 0; i < n; ++i)
it1.add(a[i]);
for (int i = 0; i < m; ++i)
if (intervals[i].q != it1.get(intervals[i].l, intervals[i].r, 0, n - 1, 0)) {
out.print("NO");
return;
}
out.println("YES");
for (int i : a) {
out.print(i + " ");
}
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(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>
using namespace std;
const long long base = 1331;
const long long N = 1e5 + 1;
template <typename T>
inline void Cin(T& x) {
char c = getchar();
x = 0;
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
}
template <typename T, typename... Args>
inline void Cin(T& a, Args&... args) {
Cin(a);
Cin(args...);
}
long long s[N][31], n, m, a[N], l[N], r[N], q[N], res, ST[4 * N];
void build(long long id, long long l, long long r) {
if (l == r) {
ST[id] = a[l];
return;
}
long long mid = (l + r) / 2;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
ST[id] = ST[id * 2] & ST[id * 2 + 1];
}
void Get(long long id, long long l, long long r, long long u, long long v) {
if (l > v || r < u) return;
if (l >= u && r <= v) {
res = res & ST[id];
return;
}
long long mid = (l + r) / 2;
Get(id * 2, l, mid, u, v);
Get(id * 2 + 1, mid + 1, r, u, v);
}
void read(void) {
cin >> n >> m;
for (long long i = 1; i <= (long long)(m); ++i) {
cin >> l[i] >> r[i] >> q[i];
for (long long j = 0; j < (long long)(31); ++j) {
if (q[i] >> j & 1 == 1) {
s[l[i]][j]++;
s[r[i] + 1][j]--;
}
}
}
for (long long j = 0; j < (long long)(31); ++j) {
for (long long i = 1; i <= (long long)(n); ++i) {
s[i][j] += s[i - 1][j];
if (s[i][j] > 0) a[i] += (1 << j);
}
}
build(1, 1, n);
for (long long i = 1; i <= (long long)(m); ++i) {
res = a[l[i]];
Get(1, 1, n, l[i], r[i]);
if (res != q[i]) {
cout << "NO";
return;
}
}
cout << "YES" << '\n';
for (long long i = 1; i <= (long long)(n); ++i) cout << a[i] << ' ';
}
signed main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
}
| 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 + 19;
int a[maxn][30], ff[maxn], l[maxn], r[maxn], q[maxn];
struct Tree {
static const int LOW = (1 << 30) - 1;
int f(int a, int b) { return (a & b); }
vector<int> s;
int n;
Tree(int n = 0, int def = 0) : s(2 * n, def), n(n) {}
void update(int pos, int val) {
for (s[pos += n] = val; pos > 1; pos /= 2)
s[pos / 2] = f(s[pos & ~1], s[pos | 1]);
}
int query(int b, int e) {
int ra = LOW, rb = LOW;
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b % 2) ra = f(ra, s[b++]);
if (e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
};
Tree tr(maxn);
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> q[i];
l[i]--;
r[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 j = 0; j < 30; j++) {
int counter = 0;
int toadd = (1 << j);
for (int i = 0; i < n; i++) {
counter += a[i][j];
if (counter > 0) ff[i] += toadd;
}
}
for (int i = 0; i < n; i++) {
tr.update(i, ff[i]);
}
bool pos = true;
for (int i = 0; i < m; i++) {
if (tr.query(l[i], r[i] + 1) != q[i]) pos = false;
}
if (pos) {
cout << "YES" << endl;
for (int i = 0; i < n; i++) cout << ff[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 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class InterestingArray
{
static class C implements Comparator<Con>
{
@Override
public int compare(Con arg0, Con arg1)
{
int a = arg0.l - arg1.l;
if(a == 0) a = arg1.r - arg0.r; //big one first
return a;
}
}
static class C2 implements Comparator<Con>
{
@Override
public int compare(Con arg0, Con arg1)
{
int a = arg0.r - arg1.r;
if(a == 0) a = arg1.l - arg0.l;
return a;
}
}
static class Con
{
int l;
int r;
long q;
boolean[] bits = new boolean[31];
public Con(int l1, int r1, long q1)
{
l = l1;
r = r1;
q = q1;
long q0 = q;
int bit = 0;
while(q0 != 0)
{
bits[bit] = q0%2 == 1;
bit++;
if(q0 == 1)break;
q0 /= 2;
}
}
boolean includes(Con con)
{
return (l <= con.l && r >= con.r);
}
}
static void checkbits(long q0)
{
boolean[] bits = new boolean[31];
int bit = 0;
while(true)
{
bits[bit] = q0%2 != 0;
bit++;
if(q0 == 1)break;
q0 /= 2;
}
System.out.println(Arrays.toString(bits));
}
public static void main(String[] args)
{
FastReader sc = new FastReader();
int n = sc.nextInt();
int m = sc.nextInt();
Con[] con = new Con[m];
Con[] conend = new Con[m];
C c = new C();
C2 c2 = new C2();
for(int i = 0; i < m; i++)
{
int l = sc.nextInt()-1;
int r = sc.nextInt()-1;
long q = sc.nextLong();
con[i] = new Con(l, r, q);
conend[i] = new Con(l, r, q);
}
Arrays.sort(con, c);
Arrays.sort(conend, c2);
int[] bitin = new int[31];
for(int i = 1; i < m; i++)
{
if(con[i-1].includes(con[i]))
{
for(int j = 0; j < 31; j++)
{
if(con[i-1].bits[j] && !con[i].bits[j])
{
System.out.println("NO");
return;
}
}
}
if(con[i].includes(con[i-1]))
{
for(int j = 0; j < 31; j++)
{
if(con[i].bits[j] && !con[i-1].bits[j])
{
System.out.println("NO");
return;
}
}
}
}
System.out.println("YES");
StringBuilder sb = new StringBuilder();
int nexcon = 0;
int nexconend = 0;
for(int i = 0; i < n; i++)
{
while(nexcon < m && con[nexcon].l == i)
{
for(int k = 0; k < 31; k++)
{
if(con[nexcon].bits[k])
{
bitin[k]++;
}
}
nexcon++;
}
int cur = 0;
int curadd = 1;
for(int k = 0; k < 31; k++)
{
if(bitin[k] > 0)
{
cur += curadd;
}
curadd *= 2;
}
if(i > 0)sb.append(" ");
sb.append(cur);
while(nexconend < m && conend[nexconend].r == i)
{
for(int k = 0; k < 31; k++)
{
if(conend[nexconend].bits[k])
{
bitin[k]--;
}
}
nexconend++;
}
}
System.out.println(sb);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
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;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
ProblemBInterestingArray solver = new ProblemBInterestingArray();
solver.solve(1, in, out);
out.close();
}
static class ProblemBInterestingArray {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
Query[] queries = new Query[m];
int[][] bits = new int[30][n + 1];
for (int i = 0; i < m; ++i) {
int l = in.nextInt() - 1;
int r = in.nextInt() - 1;
int q = in.nextInt();
queries[i] = new Query(l, r, q);
for (int bit = 0; bit < 30; ++bit) {
if ((q & (1 << bit)) > 0) {
bits[bit][l]++;
bits[bit][r + 1]--;
}
}
}
int[] a = new int[n];
for (int bit = 0; bit < 30; ++bit) {
for (int i = 0; i < n; ++i) {
if (i > 0) bits[bit][i] += bits[bit][i - 1];
if (bits[bit][i] > 0) {
a[i] |= 1 << bit;
}
}
}
ProblemBInterestingArray.RMQ rmq = new ProblemBInterestingArray.RMQ(a);
boolean possible = true;
for (Query q : queries) {
if (!possible) break;
if (rmq.query(q.l, q.r) != q.q) {
possible = false;
break;
}
}
if (!possible) {
out.println("NO");
return;
}
out.println("YES");
for (int i : a) out.print(i + " ");
}
class Query {
int l;
int r;
int q;
Query(int ll, int rr, int qq) {
l = ll;
r = rr;
q = qq;
}
}
static class RMQ {
int[] vs;
int[][] lift;
public RMQ(int[] vs) {
this.vs = vs;
int n = vs.length;
int maxlog = Integer.numberOfTrailingZeros(Integer.highestOneBit(n)) + 2;
lift = new int[maxlog][n];
for (int i = 0; i < n; i++)
lift[0][i] = vs[i];
int lastRange = 1;
for (int lg = 1; lg < maxlog; lg++) {
for (int i = 0; i < n; i++) {
lift[lg][i] = lift[lg - 1][i] & lift[lg - 1][Math.min(i + lastRange, n - 1)];
}
lastRange *= 2;
}
}
public int query(int low, int hi) {
int range = hi - low + 1;
int exp = Integer.highestOneBit(range);
int lg = Integer.numberOfTrailingZeros(exp);
return lift[lg][low] & lift[lg][hi - exp + 1];
}
}
}
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());
}
}
}
| 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;
int n, m, l, r, p, cnt;
int a[maxn + 10], st[(maxn + 10) << 2], tag[(maxn + 10) << 2];
struct node {
int l, r, p;
node(int a = 0, int b = 0, int c = 0) : l(a), r(b), p(c) {}
} cc[maxn + 10];
void pushdown(int o, int l, int r) {
if (tag[o]) {
int lc = o * 2, rc = o * 2 + 1;
tag[lc] |= tag[o];
tag[rc] |= tag[o];
st[lc] |= tag[o];
st[rc] |= tag[o];
tag[o] = 0;
}
}
void add(int o, int l, int r, int ll, int rr, int p) {
if (l >= ll && r <= rr) {
st[o] |= p;
tag[o] |= p;
return;
}
int lc = o * 2, rc = o * 2 + 1, mid = (l + r) / 2;
pushdown(o, l, r);
if (ll <= mid) add(lc, l, mid, ll, rr, p);
if (rr > mid) add(rc, mid + 1, r, ll, rr, p);
st[o] = st[lc] & st[rc];
}
int query(int o, int l, int r, int ll, int rr) {
if (l >= ll && r <= rr) {
return st[o];
}
int lc = o * 2, rc = o * 2 + 1, mid = (l + r) / 2, p1 = (1 << 30) - 1,
p2 = (1 << 30) - 1;
pushdown(o, l, r);
if (ll <= mid) p1 = query(lc, l, mid, ll, rr);
if (rr > mid) p2 = query(rc, mid + 1, r, ll, rr);
pushdown(o, l, r);
return p1 & p2;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d%d", &l, &r, &p);
add(1, 1, n, l, r, p);
cc[++cnt] = node(l, r, p);
}
for (int i = 1; i <= cnt; ++i) {
if (query(1, 1, n, cc[i].l, cc[i].r) != cc[i].p) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
for (int i = 1; i <= n; ++i) {
printf("%d%c", query(1, 1, n, i, i), " \n"[i == 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.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;
public class Main implements Runnable
{
private class Query {
private int id;
private int l;
private int r;
private int q;
Query(int l,int r,int q,int id)
{
this.l=l;
this.r=r;
this.q=q;
this.id=id;
}
}
public static void main(String args[])
{
new Main().run();
}
public void run()
{
try {
solve();
} catch (IOException e) {
e.printStackTrace();
}
out.flush();
}
private void solve() throws IOException
{
int n=in.nextInt();
int m=in.nextInt();
Query q[]=new Query[m];
for(int i=0;i<m;i++)
q[i]=new Query(in.nextInt()-1, in.nextInt()-1, in.nextInt(), i);
int arr[]=new int[n];
Arrays.fill(arr, 0);
int inc[]=new int[n+1];
for(int i=0;i<30;i++)
{
Arrays.fill(inc, 0);
for(int j=0;j<m;j++)
{
if(testbit(q[j].q,i))
{
inc[q[j].l]++;
inc[q[j].r+1]--;
}
}
int val=0;
for(int j=0;j<n;j++)
{
val+=inc[j];
if(val!=0)
arr[j]|=(1<<i);
}
}
int ds=getds(n);
int v[]=new int[2*ds];
boolean ok=true;
Arrays.fill(v, 0);
for(int i=0;i<n;i++)
v[i+ds]=arr[i];
for(int i=ds-1;i>0;i--)
v[i]=v[2*i]&v[2*i+1];
for(int i=0;i<m;i++)
ok&=(accumulate(q[i].l,q[i].r,v,ds)==q[i].q);
if(ok)
{
out.println("YES");
for(int i:arr)
{
out.printf("%d ",i);
}
out.println();
}
else
out.println("NO");
}
private int accumulate(int l, int r, int[] v, int ds) {
l+=ds;
r+=ds;
int res=(1<<30)-1;
while(l<=r)
{
if((l&1)==1) res=res&v[l++];
if((r&1)==0) res=res&v[r--];
l>>=1;
r>>=1;
}
return res;
}
private int getds(int n) {
n--;
n|=n>>1;
n|=n>>2;
n|=n>>4;
n|=n>>8;
n|=n>>16;
return n+1;
}
private boolean testbit(int q, int i) {
return ((q>>i)&1)==1;
}
class Scanner
{
BufferedReader in;
StringTokenizer token;
String delim;
Scanner(Reader reader)
{
in=new BufferedReader(reader);
token=new StringTokenizer("");
this.delim=" \n";
}
Scanner(Reader reader, String delimeters)
{
in=new BufferedReader(reader);
token=new StringTokenizer("");
this.delim=delimeters;
}
public boolean hasNext() throws IOException
{
while(!token.hasMoreTokens())
{
String line=in.readLine();
if(line==null) return false;
token=new StringTokenizer(line,delim);
}
return true;
}
String next() throws IOException
{
return hasNext()?token.nextToken():null;
}
int nextInt() throws IOException
{
return Integer.parseInt(next());
}
long nextLong() throws IOException
{
return Long.parseLong(next());
}
double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
int[] nextInts(int n) throws IOException
{
int[] res=new int[n];
for(int i=0;i<n;i++)
res[i]=nextInt();
return res;
}
}
private Scanner in=new Scanner(new InputStreamReader(System.in),"\n\r\t ");
private PrintWriter out=new PrintWriter(new BufferedWriter(
new OutputStreamWriter(System.out)));
} | 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.*;
public class Main {
static Scanner scanner = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static final long INF = (long) 1e18;
public static void main(String[] args) throws Exception {
//============================================//
//answer here:
int n = scanner.nextInt(), m = scanner.nextInt();
int[] l = new int[m], r = new int[m], q = new int[m];
int N = 1;
while (N < n) N <<= 1;
int[] ans = new int[N + 1];
for (int i = 0; i < m; i++) {
l[i] = scanner.nextInt();
r[i] = scanner.nextInt();
q[i] = scanner.nextInt();
}
for (int bit = 0; bit <= 30; bit++) {
int[] sum = new int[n];
for (int i = 0; i < m; i++) {
if ((q[i] & (1 << bit)) > 0) {
//out.println("bit is "+bit);
sum[l[i] - 1]++;
if (r[i] < n) sum[r[i]]--;
}
}
for (int i = 0; i < n; i++) {
if (i > 0) sum[i] += sum[i - 1];
if (sum[i] > 0) ans[i + 1] |= 1 << bit;
}
}
boolean flag = true;
SegmentTree stree = new SegmentTree(ans);
for (int i = 0; i < m; i++) {
flag &= (q[i] == stree.query(l[i], r[i]));
}
if (!flag) out.println("NO");
else {
out.println("YES");
for (int i = 1; i <= n; i++) {
out.print(ans[i] + " ");
}
}
//============================================//
out.close();
}
}
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];
}
}
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 << 31) - 1);
if (b >= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
int q1 = query(node << 1, b, mid, i, j);
int q2 = query(node << 1 | 1, mid + 1, e, i, j);
return q1 & q2;
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
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() throws InterruptedException {
Thread.sleep(3000);
}
int[] readArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
class pair implements Comparable<pair> {
int t;
int c;
public pair(int t, int c) {
this.t = t;
this.c = c;
}
@Override
public String toString() {
return t + "" + c;
}
@Override
public int compareTo(pair o) {
return o.t - t;
}
}
| 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.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
private void run() throws IOException {
if (new File("input.txt").exists())
in = new BufferedReader(new FileReader("input.txt"));
else
in = new BufferedReader(new InputStreamReader(System.in));
if (new File("output.txt").exists())
out = new PrintWriter("output.txt");
else
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
final int maxn = 100 * 1000 + 10;
final int maxLog = 31;
int n, m;
int d[][] = new int[maxLog][maxn];
int s[][] = new int[maxLog][maxn];
int mask[] = new int[maxn];
int l[] = new int[maxn];
int r[] = new int[maxn];
int q[] = new int[maxn];
PriorityQueue<Event> events = new PriorityQueue<>();
void print() {
for (int i = 0; i < n; i++) {
int a = 0;
for (int j = 0; j < maxLog; j++) {
a |= (d[j][i] << j);
}
out.print(a + " ");
}
out.println();
}
void solve() throws IOException {
n = nextInt();
m = nextInt();
for (int i = 0; i < m; i++) {
l[i] = nextInt() - 1;
r[i] = nextInt() - 1;
q[i] = nextInt();
events.add(new Event(l[i], q[i], true));
events.add(new Event(r[i] + 1, q[i], false));
}
fill(mask, 0);
for (int i = 0; i < n; i++) {
while (!events.isEmpty() && events.element().index == i) {
final Event event = events.remove();
final int sg = event.isOpen ? 1 : -1;
final int q = event.q;
for (int j = 0; j < maxLog; j++)
mask[j] += sg * ((q >> j) & 1);
}
for (int j = 0; j < maxLog; j++)
d[j][i] = mask[j] > 0 ? 1 : 0;
}
for (int j = 0; j < maxLog; j++)
s[j][0] = d[j][0];
for (int i = 1; i < n; i++)
for (int j = 0; j < maxLog; j++)
s[j][i] = s[j][i - 1] + d[j][i];
boolean isAns = true;
for (int i = 0; i < m && isAns; i++) {
for (int j = 0; j < maxLog && isAns; j++) {
final int count = s[j][r[i]] - (l[i] > 0 ? s[j][l[i] - 1] : 0);
if (((q[i] >> j) & 1) > 0)
isAns = (count == r[i] - l[i] + 1);
else
isAns = (count < r[i] - l[i] + 1);
}
}
out.println(isAns ? "YES" : "NO");
if (isAns) {
print();
}
}
class Event implements Comparable<Event> {
int index;
int q;
boolean isOpen;
public Event(int index, int q, boolean isOpen) {
super();
this.index = index;
this.q = q;
this.isOpen = isOpen;
}
@Override
public int compareTo(Event o) {
return Integer.compare(index, o.index);
}
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
String nextToken() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String str = in.readLine();
if (str == null)
return true;
st = new StringTokenizer(str);
}
return false;
}
}
| 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.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int l[] = new int[m + 10];
int r[] = new int[m + 10];
int v[] = new int[m + 10];
for (int i = 1; i <= m; i++)
{
l[i] = sc.nextInt();
r[i] = sc.nextInt();
v[i] = sc.nextInt();
updaterange(1, 1, n, l[i], r[i], v[i]);
}
boolean f = true;
for (int i = 1; i <= m; i++)
{
if (query(1, 1, n, l[i], r[i]) != v[i])
{
f = false;
break;
}
}
if (!f)
{
System.out.println("NO");
} else
{
System.out.println("YES");
for (int i = 1; i <= n; i++)
{
System.out.print(query(1, 1, n, i, i) + " ");
}
System.out.println();
}
}
static int N = 100000;
static long tree[] = new long[4 * N + 10];
static long lazy[] = new long[4 * N + 10];
static long query(int node, int start, int end, int left, int right)
{
if (left <= start && end <= right)
{
return tree[node];
}
pushdown(node, start, end);
int mid = (start + end) >> 1;
long ans = (2 << 30) - 1;
if (left <= mid)
{
ans &= query(node << 1, start, mid, left, right);
}
if (right > mid)
{
ans &= query((node << 1) | 1, mid + 1, end, left, right);
}
return ans;
}
static void updaterange(int node, int start, int end, int left, int right, int val)
{
if (left <= start && end <= right)
{
tree[node] |= val;
lazy[node] |= val;
return;
}
pushdown(node, start, end);
int mid = (start + end) >> 1;
if (left <= mid)
{
updaterange(node << 1, start, mid, left, right, val);
}
if (right > mid)
{
updaterange((node << 1) | 1, mid + 1, end, left, right, val);
}
tree[node] = tree[node << 1] & tree[(node << 1) | 1];
}
static void pushdown(int node, int start, int end)
{
if (lazy[node] != 0)
{
tree[node << 1] |= lazy[node];
tree[(node << 1) | 1] |= lazy[node];
lazy[node << 1] |= lazy[node];
lazy[(node << 1) | 1] |= lazy[node];
lazy[node] = 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;
long long int tree[4 * 100001];
vector<long long int> A;
void build(long long int i, long long int l, long long int r) {
if (l == r)
tree[i] = A[l];
else {
long long int mid = (l + r) / 2;
build(2 * i, l, mid);
build(2 * i + 1, mid + 1, r);
tree[i] = tree[2 * i] & tree[2 * i + 1];
}
}
long long int query(long long int i, long long int l, long long int r,
long long int ql, long long int qr) {
if (ql > r || qr < l) return 1073741823;
long long int mid = (l + r) / 2;
if (ql <= l && qr >= r) return tree[i];
return query(2 * i, l, mid, ql, qr) & query(2 * i + 1, mid + 1, r, ql, qr);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n, m;
cin >> n >> m;
long long int q[m][3];
for (int i = 0; i < m; i++)
for (auto &x : q[i]) cin >> x;
vector<vector<long long int> > prefix(30, vector<long long int>(n + 1, 0));
for (int i = 0; i < m; i++) {
long long int l = q[i][0];
l--;
long long int r = q[i][1], val = q[i][2];
for (int j = 0; j < 30; j++) {
if ((val >> j) & 1) prefix[j][l]++, prefix[j][r]--;
}
}
for (int i = 0; i < 30; i++) {
for (int j = 1; j <= n; j++) prefix[i][j] += prefix[i][j - 1];
}
for (int i = 0; i < n; i++) {
long long int ans = 0;
long long int pow = 1;
for (int j = 0; j < 30; j++) {
if (prefix[j][i] > 0) ans += pow;
pow *= 2;
}
A.push_back(ans);
}
bool ispos = true;
build(1, 0, n - 1);
for (int i = 0; i < m; i++) {
if (!ispos) break;
long long int l = q[i][0];
l--;
long long int r = q[i][1], val = q[i][2];
r--;
long long int pres = query(1, 0, n - 1, l, r);
if (pres != val) ispos = false;
}
if (!ispos)
cout << "NO";
else {
cout << "YES\n";
for (auto x : A) cout << x << " ";
}
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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import org.omg.CORBA.REBIND;
public class solver {
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
static final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;
void init() throws FileNotFoundException{
if (OJ){
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException{
while (tok == null || !tok.hasMoreTokens()){
tok = new StringTokenizer(in.readLine()," :");
}
return tok.nextToken();
}
int readInt() throws NumberFormatException, IOException{
return Integer.parseInt(readString());
}
double readDouble() throws NumberFormatException, IOException{
return Double.parseDouble(readString());
}
long readLong() throws NumberFormatException, IOException{
return Long.parseLong(readString());
}
long time;
void run(){
try{
time = System.currentTimeMillis();
init();
solve();
if (!OJ) System.err.println(System.currentTimeMillis() - time);
out.close();
in.close();
}catch (Exception e){
e.printStackTrace();
}
}
long gcd(long a, long b){
if (a == 0) return b;
return gcd(b % a, a);
}
class SegmentTree{
int[] a;
int[] b;
int[] flag;
int n;
int size;
int stand = (1 << 31) - 1;
int def = 0;
SegmentTree(int n){
this.n = n;
size = 4 * n;
a = new int[size];
Arrays.fill(a, stand);
flag = new int[size];
Arrays.fill(flag, stand);
b = new int[size];
Arrays.fill(b, stand);
}
void push(int v){
if (v >= size) return;
if (flag[v] == stand) return;
if (a[v] == stand)
a[v] = 0;
a[v] |= flag[v];
if (v*2 < size){
if (flag[v*2] == stand)
flag[v*2] = flag[v];
else
flag[v*2] |= flag[v];
}
if (v*2+1 < size){
if (flag[v*2+1] == stand)
flag[v*2+1] = flag[v];
else
flag[v*2+1] |= flag[v];
}
flag[v] = stand;
}
void set(int v,int tl,int tr,int left,int right, int q){
push(v);
if (tl == left && tr == right){
flag[v] = q;
push(v);
return;
}
int mid = (tl + tr) / 2;
if (right <= mid){
set(v*2,tl,mid,left,right,q);
}else{
if (left > mid){
set(v*2+1,mid+1,tr,left,right,q);
}else{
set(v*2,tl,mid,left,mid,q);
set(v*2+1,mid+1,tr,mid+1,right,q);
}
}
push(v*2);
push(v*2+1);
a[v] = a[v*2] | a[v*2+1];
}
int get(int v, int tl, int tr, int left, int right){
if (tl == left && tr == right){
return b[v];
}
int mid = (tl + tr) / 2;
if (right <= mid){
return get(v*2,tl,mid,left,right);
}else{
if (left > mid){
return get(v*2+1, mid+1,tr,left,right);
}else{
return get(v*2,tl,mid,left,mid) & get(v*2+1,mid+1,tr,mid+1,right);
}
}
}
void rebuild(int v, int tl, int tr){
push(v);
if (tl == tr){
b[v] = a[v];
return;
}
int mid = (tl + tr) / 2;
rebuild(v*2,tl,mid);
rebuild(v*2+1,mid+1,tr);
b[v] = b[v*2] & b[v*2+1];
}
void rebuild(){
rebuild(1,1,n);
}
void set(int l,int r,int q){
set(1,1,n,l,r,q);
}
int get(int l,int r){
int res = get(1,1,n,l,r);
return res == stand ? def : res;
}
}
void solve() throws NumberFormatException, IOException{
int n = readInt();
int m = readInt();
SegmentTree st = new SegmentTree(n);
int[] l = new int[m];
int[] r = new int[m];
int[] q = new int[m];
for (int i=0;i<m;i++){
l[i] = readInt();
r[i] = readInt();
q[i] = readInt();
st.set(l[i], r[i], q[i]);
}
st.rebuild();
boolean ok = true;
for (int i=0;i<m;i++){
int p = (st.get(l[i], r[i]));
if (p != q[i]) ok = false;
}
if (!ok){
out.println("NO");
}else{
out.println("YES");
for (int i=1;i<=n;i++){
out.print(st.get(i, i)+" ");
}
}
}
public static void main(String[] args) {
new solver().run();
}
} | 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Alex
*/
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();
}
static class TaskB {
int MAXBITS = 30;
int[] create(int n, int[] ls, int[] rs, int[] qs) {
int[] res = new int[n];
ArrayList<Constraint> al = new ArrayList<>();
for(int i = 0; i < ls.length; i++) {
int l = ls[i], r = rs[i], q = qs[i];
al.add(new Constraint(l, 1, q));
al.add(new Constraint(r + 1, -1, q));
}
Collections.sort(al);
int cp = 0;
int[] count = new int[MAXBITS];
for(int i = 0; i < n; i++) {
while(cp < al.size() && al.get(cp).index == i) {
Constraint c = al.get(cp);
for(int bit = 0; bit < MAXBITS; bit++) {
if(((c.val >> bit) & 1) == 1) {
count[bit] += c.kind;
}
}
cp++;
}
for(int bit = 0; bit < MAXBITS; bit++) {
if(count[bit] > 0) {
res[i] |= (1 << bit);
}
}
}
return res;
}
boolean check(int[] res, int[] ls, int[] rs, int[] qs) {
for(int bit = 0; bit < MAXBITS; bit++) {
int[] count = new int[res.length + 1];
for(int i = 1; i < count.length; i++)
count[i] = count[i - 1] + (((res[i - 1] >> bit) & 1) == 1 ? 1 : 0);
for(int query = 0; query < ls.length; query++) {
int l = ls[query], r = rs[query], q = qs[query];
int here = count[r + 1] - count[l];
if(((q >> bit) & 1) == 1) {
if(here != r - l + 1) return false;
} else {
if(here >= r - l + 1) return false;
}
}
}
return true;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt(), constraints = in.readInt();
int[] ls = new int[constraints], rs = new int[constraints], qs = new int[constraints];
IOUtils.readIntArrays(in, ls, rs, qs);
MiscUtils.decreaseByOne(ls, rs);
int[] res = create(n, ls, rs, qs);
boolean ok = check(res, ls, rs, qs);
if(ok) {
out.printLine("YES");
out.printLine(res);
} else {
out.printLine("NO");
}
}
class Constraint implements Comparable<Constraint> {
int index;
int kind;
int val;
public Constraint(int index, int kind, int val) {
this.index = index;
this.kind = kind;
this.val = val;
}
public int compareTo(Constraint o) {
if(index != o.index) return Integer.compare(index, o.index);
if(kind != o.kind) return -Integer.compare(kind, o.kind);
return 0;
}
}
}
static class MiscUtils {
public static void decreaseByOne(int[]... arrays) {
for(int[] array : arrays) {
for(int i = 0; i < array.length; i++)
array[i]--;
}
}
}
static class IOUtils {
public static void readIntArrays(InputReader in, int[]... arrays) {
for(int i = 0; i < arrays[0].length; i++) {
for(int j = 0; j < arrays.length; j++)
arrays[j][i] = in.readInt();
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public 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++];
}
public int readInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if(c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while(!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if(filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for(int i = 0; i < objects.length; i++) {
if(i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void print(int[] array) {
for(int i = 0; i < array.length; i++) {
if(i != 0)
writer.print(' ');
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* 482B
*
* @author artyom
*/
public class InterestingArray implements Runnable {
private static final int MAX_BIT = 30;
private BufferedReader in;
private PrintStream out;
private StringTokenizer tok;
private void solve() throws IOException {
int n = nextInt(), m = nextInt();
int[] ary = new int[n], sum = new int[n + 1];
int[] l = new int[m], r = new int[m], q = new int[m];
for (int i = 0; i < m; i++) {
l[i] = nextInt() - 1;
r[i] = nextInt();
q[i] = nextInt();
}
for (int bit = 0; bit <= MAX_BIT; bit++) {
Arrays.fill(sum, 0);
for (int i = 0; i < m; i++) {
if (((q[i] >> bit) & 1) == 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) {
ary[i] |= (1 << bit);
}
}
}
SegmentTree st = new SegmentTree(ary);
for (int i = 0; i < m; i++) {
if (st.evaluate(l[i], r[i] - 1) != q[i]) {
out.println("NO");
return;
}
}
out.println("YES");
StringBuilder sb = new StringBuilder();
for (int x : ary) {
sb.append(x).append(' ');
}
out.println(sb.toString());
}
//--------------------------------------------------------------
public static void main(String[] args) {
new InterestingArray().run();
}
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = System.out;
tok = null;
solve();
in.close();
} catch (IOException e) {
System.exit(0);
}
}
private String nextToken() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static final class SegmentTree {
private final int[] arr;
private final int[] st;
SegmentTree(int[] arr) {
this.arr = arr;
st = new int[getMaxSize(arr.length)];
build(0, arr.length - 1, 0);
}
private static int getMaxSize(int arrSize) {
int x = (int) Math.ceil(Math.log10(arrSize) / Math.log10(2));
return 2 * (int) Math.pow(2, x) - 1;
}
private int build(int ss, int se, int si) {
if (ss == se) {
return st[si] = arr[ss];
}
int mid = getMid(ss, se);
return st[si] = build(ss, mid, si * 2 + 1) & build(mid + 1, se, si * 2 + 2);
}
private static int getMid(int s, int e) {
return s + (e - s) / 2;
}
private int evaluate(int qs, int qe) {
return evaluate(0, arr.length - 1, qs, qe, 0);
}
private int evaluate(int ss, int se, int qs, int qe, int index) {
if (qs <= ss && qe >= se) {
return st[index];
}
if (se < qs || ss > qe) {
return Integer.MAX_VALUE;
}
int mid = getMid(ss, se);
return evaluate(ss, mid, qs, qe, 2 * index + 1) &
evaluate(mid + 1, se, qs, qe, 2 * index + 2);
}
}
} | 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.Arrays;
import java.util.StringTokenizer;
public class D275 {
public static void main(String[] args) {
MyScanner scan = new MyScanner();
PrintWriter out = new PrintWriter(System.out);
int N = scan.nextInt(), M = scan.nextInt();
int[] l = new int[M], r = new int[M], q = new int[M];
ST st = new ST(N);
for(int i=0;i<M;i++){
l[i] = scan.nextInt()-1;
r[i] = scan.nextInt()-1;
q[i] = scan.nextInt();
st.update(l[i], r[i], q[i]);
}
boolean valid = true;
for(int i=0;i<M;i++){
if(st.query(l[i], r[i])!=q[i])valid=false;
if(!valid)break;
}
if(!valid){out.println("NO");}
else{
out.println("YES");
for(int i=0;i<N;i++){
out.print(st.query(i, i)+" ");
}out.println();
}
out.close();
}
private static class ST {
int K;
int[] val, delta;
public ST(int N) {
K = (int)Math.ceil(Math.log(N)/Math.log(2));
val = new int[4*N];
delta = new int[4*N];
Arrays.fill(delta, -1);
}
public void update(int L, int R, int V){update(L,R,0,(1<<K)-1, 1, V);}
public int query(int L, int R){return query(L,R,0,(1<<K)-1,1);}
private int query(int L, int R, int tL, int tR, int idx) {
if(tL<L&&tR<L||tL>R&&tR>R)return (1<<30)-1;
if(tL>=L&&tR<=R){
return val[idx];
}
prop(tL,tR,idx);
int mid = (tL+tR)/2;
return (query(L,R,tL,mid,idx*2))&(query(L,R,mid+1,tR,idx*2+1));
}
private void update(int L, int R, int tL, int tR, int idx, int V) {
if(tL<L&&tR<L||tL>R&&tR>R)return;
if(tL>=L&&tR<=R){
val[idx] |= V;
delta[idx] = (delta[idx]==-1)?V:delta[idx]|V;
return;
}
prop(tL,tR,idx);
int mid = (tL+tR)/2;
update(L,R,tL,mid,idx*2,V);
update(L,R,mid+1,tR,idx*2+1,V);
}
private void prop(int L, int R, int idx){
if(delta[idx] == -1)return;
int mid = (L+R)/2;
update(L,R,L,mid,idx*2,delta[idx]);
update(L,R,mid+1,R,idx*2+1,delta[idx]);
delta[idx] = -1;
}
}
private static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {br = new BufferedReader(new InputStreamReader(System.in));}
String next(){
while(st==null||!st.hasMoreElements()){
try{st = new StringTokenizer(br.readLine());}
catch(IOException e){e.printStackTrace();}
}return st.nextToken();
}
int nextInt() {return Integer.parseInt(next());}
long nextLong(){return Long.parseLong(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;
const int N = 110000;
struct node {
int val, lazy;
} tree[N * 4];
int n, m, ql, qr, qa;
int ans[N], ll[N], rr[N], aa[N];
void pushup(int x) { tree[x].val = tree[x << 1].val & tree[x << 1 | 1].val; }
void pushdown(int x) {
int &d = tree[x].lazy;
if (d) {
tree[x << 1].lazy |= d;
tree[x << 1 | 1].lazy |= d;
tree[x << 1].val |= d;
tree[x << 1 | 1].val |= d;
d = 0;
tree[x].val = tree[x << 1].val & tree[x << 1 | 1].val;
}
}
void build(int x, int l, int r) {
if (l == r) {
tree[x].lazy = tree[x].val = 0;
return;
}
int mid = (l + r) >> 1;
tree[x].lazy = 0;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
pushup(x);
}
void update(int x, int l, int r) {
if (ql <= l && qr >= r) {
tree[x].val |= qa;
tree[x].lazy |= qa;
return;
}
int mid = (l + r) >> 1;
pushdown(x);
if (ql <= mid) update(x << 1, l, mid);
if (qr > mid) update(x << 1 | 1, mid + 1, r);
pushup(x);
}
int query(int x, int l, int r) {
if (ql <= l && qr >= r) {
return tree[x].val;
}
int ans = (1 << 30) - 1, mid = (l + r) >> 1;
pushdown(x);
if (ql <= mid) ans &= query(x << 1, l, mid);
if (qr > mid) ans &= query(x << 1 | 1, mid + 1, r);
return ans;
}
int main() {
scanf("%d%d", &n, &m);
build(1, 1, n);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", ll + i, rr + i, aa + i);
ql = ll[i];
qr = rr[i];
qa = aa[i];
update(1, 1, n);
}
for (int i = 1; i <= m; i++) {
ql = ll[i];
qr = rr[i];
qa = aa[i];
int ans = query(1, 1, n);
if (ans != qa) {
puts("NO");
return 0;
}
}
puts("YES");
for (int i = 1; i <= n; i++) {
ql = qr = i;
int ans = query(1, 1, n);
printf("%d%c", ans, 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;
int s[31][100003];
int bit[31][100003];
int countzero[31][100003];
int main() {
int n, m;
cin >> n >> m;
int l[m], r[m], q[m];
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> q[i];
for (int j = 0; j < 30; j++) {
if (q[i] & (1 << j)) {
s[j][l[i]]++;
s[j][r[i] + 1]--;
}
}
}
for (int i = 0; i < 30; i++) {
int count = 0;
countzero[i][0] = 0;
for (int j = 1; j <= n; j++) {
count += s[i][j];
if (count > 0) {
bit[i][j] = 1;
} else {
bit[i][j] = 0;
}
countzero[i][j] = countzero[i][j - 1] + (bit[i][j] == 0);
}
}
bool res = true;
for (int i = 0; i < m; i++) {
for (int j = 0; j < 30; j++) {
if (!(q[i] & (1 << j))) {
if (countzero[j][r[i]] - countzero[j][l[i] - 1] <= 0) {
res = false;
goto df;
}
}
}
}
df:
if (res) {
cout << "YES\n";
for (int i = 1; i <= n; i++) {
int res = 0;
for (int j = 29; j >= 0; j--) {
res *= 2;
res += bit[j][i];
}
cout << res << " ";
}
} else
cout << "NO";
}
| 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.InputStreamReader;
import java.io.PrintWriter;
import java.util.Random;
import java.util.StringTokenizer;
public class Main implements Runnable {
int INF = (int) 1e9 + 7;
static class Pair {
long a;
long b;
public Pair(long a, long b) {
this.a = a;
this.b = b;
}
}
private void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int q[][] = new int[m][];
for (int i = 0; i < m; ++i) {
q[i] = new int[] { nextInt() - 1, nextInt() - 1, nextInt() };
}
int ans[] = new int[n];
for (int pos = 0; pos < 30; ++pos) {
int pre[] = new int[n];
int cs[] = new int[n + 1];
for (int i = 0; i < m; ++i) {
int l = q[i][0];
int r = q[i][1];
if ((q[i][2] >> pos & 1) == 1) {
pre[l]++;
if (r + 1 < n)
pre[r + 1]--;
}
}
for (int i = 0; i < n; ++i) {
if (i > 0) {
pre[i] += pre[i - 1];
}
}
for (int i = 0; i < n; ++i) {
if (pre[i] > 0)
pre[i] = 1;
}
for (int i = 1; i <= n; ++i) {
cs[i] += cs[i - 1] + pre[i - 1];
}
for (int i = 0; i < m; ++i) {
if ((q[i][2] >> pos & 1) == 0)
if (cs[q[i][1] + 1] - cs[q[i][0]] == q[i][1] - q[i][0] + 1) {
pw.println("NO");
return;
}
}
for (int i = 0; i < n; ++i) {
ans[i] |= (pre[i] << pos);
}
}
pw.println("YES");
for (int i = 0; i < n; ++i) {
pw.print(ans[i] + " ");
}
pw.println();
}
void test() throws IOException {
Random rnd = new Random();
for (int i = 0; i < 5; ++i) {
int n = rnd.nextInt(5) + 1;
int m = rnd.nextInt(5) + 1;
System.err.println(n + " " + m);
for (int j = 0; j < m; ++j) {
int l = rnd.nextInt(n) + 1;
int r = l + rnd.nextInt(n - l + 1);
int q = rnd.nextInt(10);
System.err.println(l + " " + r + " " + q);
}
// solve(n, a);
System.err.println();
}
}
BufferedReader br;
StringTokenizer st;
PrintWriter pw;
public static void main(String args[]) {
new Main().run();
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in), 32768);
pw = new PrintWriter(System.out);
st = null;
solve();
pw.flush();
pw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
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 next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
| 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;
void make_unique(vector<int> &a) {
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
}
void printDouble(double f, int p) {
cout << fixed;
cout << setprecision(p) << f << endl;
}
int Set(int N, int cur) { return N = N | (1 << cur); }
int Reset(int N, int cur) { return N = N & ~(1 << cur); }
bool Check(int N, int cur) { return (bool)(N & (1 << cur)); }
long long GCD(long long a, long long b) {
if (b == 0) return a;
return GCD(b, a % b);
}
long long LCM(long long a, long long b) { return a * b / GCD(a, b); }
long long POW(long long a, long long p) {
long long res = 1, x = a;
while (p) {
if (p & 1) res = (res * x);
x = (x * x);
p >>= 1;
}
return res;
}
struct qry {
int u;
int v;
int q;
};
int N, M;
int cnt[33][100005];
qry Q[100005];
int A[100005];
int tree[100005 * 4];
void build_tree(int cur, int Start, int End) {
if (Start == End) {
tree[cur] = A[Start];
return;
}
int left = cur * 2;
int right = left + 1;
int mid = (Start + End) / 2;
build_tree(left, Start, mid);
build_tree(right, mid + 1, End);
tree[cur] = (tree[left] & tree[right]);
}
int tree_query(int cur, int Start, int End, int u, int v) {
if (End < u || Start > v) {
return -1;
}
if (Start >= u && End <= v) {
return tree[cur];
}
int left = cur * 2;
int right = left + 1;
int mid = (Start + End) / 2;
int v1 = tree_query(left, Start, mid, u, v);
int v2 = tree_query(right, mid + 1, End, u, v);
if (v1 == -1) return v2;
if (v2 == -1) return v1;
return (v1 & v2);
}
bool solve() {
for (int pos = 0; pos < 31; pos++) {
int sum = 0;
for (int val = 1; val <= N; val++) {
sum += cnt[pos][val];
if (sum > 0) {
cnt[pos][val] = 1;
}
}
}
for (int i = 1; i <= N; i++) {
int val = 0;
for (int pos = 0; pos < 31; pos++) {
if (cnt[pos][i] == 1) {
val = Set(val, pos);
}
}
A[i] = val;
}
memset(tree, 0, sizeof(tree));
build_tree(1, 1, N);
for (int i = 0; i < M; i++) {
int res = tree_query(1, 1, N, Q[i].u, Q[i].v);
if (res != Q[i].q) return false;
}
return true;
}
int main() {
memset(cnt, 0, sizeof(cnt));
scanf("%d %d", &N, &M);
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &Q[i].u, &Q[i].v, &Q[i].q);
for (int pos = 0; pos < 31; pos++) {
if (Check(Q[i].q, pos) == true) {
cnt[pos][Q[i].u] += 1;
cnt[pos][Q[i].v + 1] -= 1;
}
}
}
bool rs = solve();
if (rs == false) {
printf("NO\n");
} else {
printf("YES\n");
for (int i = 1; i <= N; i++) {
if (i == 1)
printf("%d", A[i]);
else
printf(" %d", A[i]);
}
printf("\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.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt() , m = sc.nextInt();
SegmentTree tree = new SegmentTree(n);
Queue<Query> qu = new LinkedList<>();
while(m-->0)
{
int l = sc.nextInt() - 1, r = sc.nextInt() -1 , q = sc.nextInt();
qu.add(new Query(l, r, q));
tree.update(l, r, q);
}
while(!qu.isEmpty())
{
Query q = qu.poll();
if(tree.query(q.l, q.r)!=q.v)
{
System.out.println("NO");
return;
}
}
System.out.println("YES");
System.out.println(tree.printArray());
}
static class Query{
int l,r,v;
public Query(int lq,int rq,int val) {l=lq;r=rq;v=val;}
}
static class SegmentTree
{
int N,len;
int[] tree ;
int[] lazy;
StringBuilder sb;
public SegmentTree(int n)
{
N = 1;
len = n;
while(N<len)N<<=1;
N<<=1;
tree = new int[N];
lazy = new int[N];
sb = new StringBuilder();
}
void propagate(int n,int l,int r)
{
if(l==r)return;
lazy[n<<1] |= lazy[n];
lazy[n<<1|1] |= lazy[n];
tree[n<<1] |= lazy[n];
tree[n<<1|1] |= lazy[n];
lazy[n] = 0;
}
void update(int l,int r,int v) {update(1, 0, len-1, l, r, v);}
void update(int n,int l,int r,int lq,int rq,int v)
{
if(l>rq||r<lq)return;
propagate(n, l, r);
if(l>=lq&&r<=rq)
{
lazy[n] |= v;
tree[n] |= v;
}
else
{
int mid = (l+r)>>1;
update(n<<1, l, mid, lq, rq, v);
update(n<<1|1, mid+1, r, lq, rq, v);
tree[n] = tree[n<<1] & tree[n<<1|1];
}
}
String printArray()
{
printArray(1,0,len-1);
return sb.toString();
}
void printArray(int n , int l,int r)
{
propagate(n, l, r);
if(l==r)
sb.append(tree[n]+" ");
else
{
int mid = (l+r)>>1;
printArray(n<<1, l, mid);
printArray(n<<1|1, mid+1, r);
}
}
int query(int l,int r) {return query(1, 0, len-1, l, r);}
int query(int n,int l,int r,int lq,int rq)
{
if(l>rq||r<lq)return Integer.MAX_VALUE;
propagate(n, l, r);
if(l>=lq&&r<=rq)
return tree[n];
else
{
int mid = (l+r)>>1;
return query(n<<1, l, mid, lq, rq) & query(n<<1|1, mid+1, r, lq, rq);
}
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public Scanner(String s) throws FileNotFoundException {br = new BufferedReader(new FileReader(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 String nextLine() throws IOException {return br.readLine();}
public long nextLong() throws IOException {return Long.parseLong(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public char nextChar() throws IOException{return next().charAt(0);}
public boolean ready() throws IOException {return br.ready();}
public int[] nextIntArr() throws IOException{
st = new StringTokenizer(br.readLine());
int[] res = new int[st.countTokens()];
for (int i = 0; i < res.length; i++)
res[i] = nextInt();
return res;
}
public char[] nextCharArr() throws IOException{
st = new StringTokenizer(br.readLine());
char[] res = new char[st.countTokens()];
for (int i = 0; i < res.length; i++)
res[i] = nextChar();
return res;
}
}
} | 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 mod = (long long)1e9 + 7;
struct node {
long long l, r, q;
};
void test_case() {
long long n, m;
cin >> n >> m;
vector<long long> arr(n);
vector<node> query(m);
vector<vector<long long> > bit(n + 1, vector<long long>(31));
for (long long i = 0; i < m; i++) {
long long a, b, c;
cin >> a >> b >> c;
--a, --b;
query[i] = {a, b, c};
for (long long bi = 0; bi < 31; bi++) {
if (c & (1 << bi)) {
bit[a][bi] += 1, bit[b + 1][bi] -= 1;
}
}
}
for (long long i = 0; i < 31; i++) {
for (long long j = 0; j < n; j++) {
bit[j + 1][i] += bit[j][i];
if (bit[j][i]) {
arr[j] = arr[j] | (1 << i);
}
}
}
vector<long long> seg(2 * n + 5);
for (long long i = 0; i < n; i++) seg[i + n] = arr[i];
for (long long i = n - 1; i > 0; i--) seg[i] = (seg[2 * i] & seg[2 * i + 1]);
for (long long i = 0; i < m; i++) {
long long l = query[i].l + n, r = query[i].r + n;
long long val = (1 << 30) - 1;
while (l <= r) {
if (l & 1) val = val & seg[l++];
if (!(r & 1)) val = val & seg[r--];
l /= 2, r /= 2;
}
if (val != query[i].q) {
cout << "NO";
return;
}
}
cout << "YES\n";
for (long long i = 0; i < n; i++) cout << arr[i] << " ";
}
signed main() {
long long t = 1;
while (t--) test_case();
}
| 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 lt[100005], rt[100005], q[100005];
int a[100005], lg[100005];
int s[100005][31];
int Ask(int x, int y) {
int tmp = lg[y - x + 1];
return s[x][tmp] & s[y - (1 << tmp) + 1][tmp];
}
void End(int x) {
printf("%s\n", x ? "YES" : "NO");
if (x)
for (int i = 1; i <= N; i++) printf("%d ", a[i]);
exit(0);
}
int main() {
int i, j;
scanf("%d %d", &N, &M);
for (i = 1; i <= M; i++) {
scanf("%d %d %d", <[i], &rt[i], &q[i]);
for (j = 0; j < 30; j++)
if ((q[i] >> j) & 1) s[lt[i]][j]++, s[rt[i] + 1][j]--;
}
for (i = 1; i <= N; i++)
for (j = 0; j < 30; j++) {
s[i][j] += s[i - 1][j];
if (s[i][j]) a[i] += (1 << j);
}
for (i = 1; i <= N; i++) lg[i] = lg[i - 1] + (i - 1 == (1 << lg[i - 1] + 1));
for (i = 1; i <= N; i++) s[i][0] = a[i];
for (j = 1; (1 << j) <= N; j++)
for (i = 1; i + (1 << j - 1) <= N; i++)
s[i][j] = (s[i][j - 1] & s[i + (1 << j - 1)][j - 1]);
for (i = 1; i <= M; i++)
if (Ask(lt[i], rt[i]) != q[i]) End(0);
End(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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Stack;
public class Main {
static final int SIZE = (int) Math.pow(2, 17);
static final int MAX_VAL_LOG = 32;
static int[] l = new int[SIZE];
static int[] r = new int[SIZE];
static int[] q = new int[SIZE];
static int[][] seq = new int[MAX_VAL_LOG][SIZE];
static int[] t = new int[SIZE*2];
public static void insert(int val, int index, int l, int r, int pos) {
if (l == r) {
t[index] = val;
return;
}
int mid = (l+r)/2;
if (mid >= pos) insert(val, index*2, l, mid, pos);
else insert(val, index*2+1, mid+1, r, pos);
t[index] = t[index*2] & t[index*2+1];
}
public static int query(int index, int L, int R, int l, int r) {
if (l==L && R==r) {
return t[index];
}
int mid = (l+r)/2;
int result = (1<<31) - 1;
if (mid >= L)
result &= query(index*2, L, Math.min(mid, R), l, mid);
if (R > mid)
result &= query(index*2+1, Math.max(mid+1, L), R, mid+1,r);
return result;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] values = line.split(" ");
int n = Integer.parseInt(values[0]);
int m = Integer.parseInt(values[1]);
for (int i = 0; i < m; i++) {
line = br.readLine();
values = line.split(" ");
l[i] = Integer.parseInt(values[0]);
r[i] = Integer.parseInt(values[1]);
q[i] = Integer.parseInt(values[2]);
for (int j = 0; j < MAX_VAL_LOG; j++) {
if ((q[i] >> j & 1) == 1) {
seq[j][l[i]]++; seq[j][r[i]+1]--;
}
}
}
int[] sum = new int[MAX_VAL_LOG];
for (int j = 0; j < SIZE; j++) {
int val = 0;
for (int i = MAX_VAL_LOG-1; i >= 0; i--) {
val <<= 1;
sum[i] += seq[i][j];
val += (sum[i] > 0) ? 1 : 0;
}
if (val > 0)
insert(val, 1, 1, SIZE,j);
}
for (int i = 0; i < m; i++) {
if (query(1, l[i], r[i], 1, SIZE) != q[i]) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
int index = SIZE;
for (int i = 0; i < n; i++) {
System.out.print(t[index] + " ");
index++;
}
}
}
| 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>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
const int N = 2e5 + 5;
const long long int mod = 1e9 + 7;
const long long int Mod = 998244353;
const long double Pi = acos(-1);
const long long int Inf = 4e18;
using namespace std;
vector<int> a;
struct SegmentTree {
vector<int> t;
int n;
void Initialize(int n) {
this->n = 2 * n;
t.resize(2 * n);
for (int i = 0; i < n; i++) Update(i, 0, 0, n - 1);
}
void Update(int idx, int v, int tl, int tr) {
if (tl == tr) {
t[v] = a[idx];
} else {
int tm = (tl + tr) >> 1;
if (idx <= tm)
Update(idx, 2 * v + 1, tl, tm);
else
Update(idx, 2 * v + 2, tm + 1, tr);
t[v] = (t[2 * v + 1] & t[2 * v + 2]);
}
}
int And(int v, int tl, int tr, int l, int r) {
if (tl > tr) return 0;
if (tl == l && tr == r) return t[v];
int tm = (tl + tr) >> 1;
if (l > tm)
return And(2 * v + 2, tm + 1, tr, l, r);
else if (r <= tm)
return And(2 * v + 1, tl, tm, l, r);
else
return (And(2 * v + 1, tl, tm, l, tm) &
And(2 * v + 2, tm + 1, tr, tm + 1, r));
}
};
void TestCase() {
int n, m, k;
cin >> n >> m;
vector<vector<int> > g(n + 1, vector<int>(35, 0));
vector<pair<pair<int, int>, int> > c(m);
bitset<32> Bit(n);
int msb = 0;
for (int i = 0; i < 32; i++) {
if (Bit[i]) msb = max(msb, i);
}
if (n & (n - 1))
k = (1 << (msb + 1));
else
k = n;
a.assign(k, 0);
for (int i = 0, l, r, q; i < m; i++) {
cin >> l >> r >> q;
l--;
r--;
c[i] = {{l, r}, q};
bitset<32> Bit(q);
for (int j = 31; j >= 0; j--) {
if (Bit[j]) {
g[l][j]++;
g[r + 1][j]--;
}
}
}
for (int i = 0; i <= n; i++) {
if (i - 1 >= 0) {
for (int j = 0; j < 31; j++) g[i][j] += g[i - 1][j];
}
for (int j = 0; j < 31; j++) {
if (g[i][j]) a[i] = (a[i] | (1 << j));
}
}
bool ok = true;
struct SegmentTree sgmt;
sgmt.Initialize(k);
for (int i = 0; i < m; i++) {
if (sgmt.And(0, 0, k - 1, c[i].first.first, c[i].first.second) !=
c[i].second)
ok = false;
}
if (ok) {
cout << "YES\n";
for (int i = 0; i < n; i++) cout << a[i] << " ";
} else
cout << "NO";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int T = 1;
while (T--) {
TestCase();
}
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 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);
}
for (int i = 0; i < q; i++) {
int ans = sgt.query(query[i].l, query[i].r);
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;
}
}
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];
lazy = new int[n << 1];
}
public int query(int l, int r)
{
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) {
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;
int arr[1000000];
int tree[1000000];
int lazy[1000000];
void buildtree(int node, int a, int b) {
if (a > b) return;
if (a == b) {
tree[node] = arr[a];
return;
}
buildtree(2 * node, a, (a + b) / 2);
buildtree(2 * node + 1, (a + b) / 2 + 1, b);
tree[node] = (tree[2 * node]) & (tree[2 * node + 1]);
}
void update(int node, int a, int b, int i, int j, int value) {
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 || j < a || b < i) return;
if (i <= a && b <= j) {
tree[node] |= value;
if (a != b) {
lazy[2 * node] |= value;
lazy[2 * node + 1] |= value;
}
return;
}
update(2 * node, a, (a + b) / 2, i, j, value);
update(2 * node + 1, 1 + (a + b) / 2, b, i, j, value);
tree[node] = (tree[2 * node]) & (tree[2 * node + 1]);
}
int query(int node, int a, int b, int i, int j) {
if (a > b || j < a || b < i) return (((long long)1) << 32) - 1;
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 (i <= a && b <= j) return tree[node];
int q1 = query(2 * node, a, (a + b) / 2, i, j);
int q2 = query(2 * node + 1, 1 + (a + b) / 2, b, i, j);
return q1 & q2;
}
typedef struct $ {
int l, r, q;
} node;
int main() {
int i, j, n, m, ans;
cin >> n >> m;
std::vector<node> v(m);
buildtree(1, 0, n - 1);
for (i = 0; i < m; i++) {
cin >> v[i].l >> v[i].r >> v[i].q;
v[i].l--;
v[i].r--;
update(1, 0, n - 1, v[i].l, v[i].r, v[i].q);
}
for (i = 0; i < m; i++) {
ans = query(1, 0, n - 1, v[i].l, v[i].r);
if (ans != v[i].q) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
for (i = 0; i < n; i++) {
printf("%d ", query(1, 0, n - 1, i, 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 D {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
/*
public void run() {
int n = in.nextInt(), m = in.nextInt();
int[][] cs = new int[m][];
for (int i = 0; i < m; i++)
cs[i] = new int[]{in.nextInt() - 1, in.nextInt() - 1, in.nextInt()};
int[] ret = new int[n];
for (int d = 0; d < 30; d++) {
int[] imos = new int[n+1];
for (int i = 0; i < m; i++) {
if (cs[i][2]<<~d < 0) {
imos[cs[i][0]]++;
imos[cs[i][1]+1]--;
}
}
for (int i = 1; i < n; i++)
imos[i] += imos[i-1];
for (int i = 0; i < n; i++)
if (imos[i] > 0) imos[i] = 1;
int[] cum = new int[n+1];
for (int i = 1; i <= n; i++) {
cum[i] = cum[i-1] + imos[i-1];
}
for (int i = 0; i < m; i++) {
if (cs[i][2]<<~d >= 0) {
if (cum[cs[i][1]+1] - cum[cs[i][0]] == cs[i][1] - cs[i][0] + 1) {
System.out.println("NO");
return;
}
}
}
for (int i = 0; i < n; i++) {
ret[i] |= imos[i]<<d;
}
}
out.println("YES");
for (int i = 0; i < n; i++)
out.print(ret[i] + " ");
out.println();
out.close();
}
*/
class SegmentTree {
int N;
int[] dat;
int INF = 0x7fffffff;
public SegmentTree(int n) {
N = Integer.highestOneBit(n) << 1;
int size = (N << 1) - 1;
dat = new int[size];
}
int query(int k) {
int res = 0;
k += N - 1;
while (k != 0) {
if (dat[k] != -1) res |= dat[k];
k = (k - 1) / 2;
}
return res;
}
private void update(int bit, int a, int b, int k, int l, int r) {
// System.out.println("query : " + a + " " + b + " " + k + " " + l + " " + r);
if (r < a || b < l) return;
if (a <= l && r <= b) {
dat[k] |= bit;
} else {
update(bit, a, b, k * 2 + 1, l, (l + r) / 2);
update(bit, a, b, k * 2 + 2, (l + r) / 2 + 1, r);
}
}
int allAnd(int a, int b, int k, int l, int r) {
if (r < a || b < l) return INF;
if (a <= l && r <= b) {
return dat[k];
} else {
int res = INF;
res &= allAnd(a, b, k * 2 + 1, l, (l + r) / 2);
res &= allAnd(a, b, k * 2 + 2, (l + r) / 2 + 1, r);
return res;
}
}
boolean andQuery(int a, int b, int bit) {
return allAnd(a, b, 0, 0, N - 1) == bit;
}
void updateQuery(int a, int b, int bit) {
update(bit, a, b, 0, 0, N - 1);
// printTree(dat);
}
}
void printTree(int[] dat) {
int n = 1;
for (int i = 0, cnt = 1; i < dat.length; i++, cnt++) {
System.out.print(dat[i] + " ");
if (n == cnt) {
System.out.println();
n *= 2;
cnt = 0;
}
}
}
public void run() {
int n = in.nextInt(), m = in.nextInt();
SegmentTree st = new SegmentTree(n);
int[][] order = new int[m][];
for (int i = 0; i < m; i++) {
order[i] = new int[]{in.nextInt(), in.nextInt(), in.nextInt()};
st.updateQuery(order[i][0], order[i][1], order[i][2]);
}
boolean exist = true;
for (int i = 0; i < m; i++) {
if (!st.andQuery(order[i][0], order[i][1], order[i][2]))
exist = false;
}
if (exist) {
out.println("YES");
for (int i = 1; i <= n; i++) {
out.print(st.query(i) + " ");
}
out.println();
} else {
out.println("NO");
}
out.close();
}
public static void main(String[] args) {
new D().run();
}
public void mapDebug(int[][] a) {
System.out.println("--------map display---------");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.println();
}
System.out.println("----------------------------");
System.out.println();
}
public void debug(Object... obj) {
System.out.println(Arrays.deepToString(obj));
}
class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
//stream = new FileInputStream(new File("dec.in"));
}
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;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
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();
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++)
array[i] = next();
return array;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
| 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 N = 1e5 + 10, inf = 2e9 + 10;
int seg[4 * N], flag[4 * N], l[N], r[N], q[N];
inline void relax(int ind) {
int x = flag[ind];
if (!x) return;
flag[ind] = 0;
seg[2 * ind] |= x;
flag[2 * ind] |= x;
seg[2 * ind + 1] |= x;
flag[2 * ind + 1] |= x;
}
int query(int s, int e, int ind, int l, int r) {
if (s >= l && e <= r) return seg[ind];
relax(ind);
int mid = (s + e) / 2;
int ret = (1 << 30) - 1;
if (mid > l) ret &= query(s, mid, 2 * ind, l, r);
if (mid < r) ret &= query(mid, e, 2 * ind + 1, l, r);
return ret;
}
void add(int s, int e, int ind, int l, int r, int val) {
if (s >= l && e <= r) {
seg[ind] |= val;
flag[ind] |= val;
return;
}
relax(ind);
int mid = (s + e) / 2;
if (mid > l) add(s, mid, 2 * ind, l, r, val);
if (mid < r) add(mid, e, 2 * ind + 1, l, r, val);
seg[ind] = seg[2 * ind] & seg[2 * ind + 1];
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> q[i];
l[i]--;
add(0, n, 1, l[i], r[i], q[i]);
}
for (int i = 0; i < m; i++) {
if (query(0, n, 1, l[i], r[i]) != q[i]) return cout << "NO", 0;
}
cout << "YES\n";
for (int i = 0; i < n; i++) cout << query(0, n, 1, i, i + 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 std::cin;
using std::cout;
using std::vector;
const int MAXBITS = 30;
void buildTree(vector<unsigned int>* T, int n,
const vector<unsigned int>& result) {
for (int i = n; i < T->size(); ++i) {
(*T)[i] = result[i - n];
}
for (int i = n - 1; i > 0; --i) {
(*T)[i] = (*T)[i << 1] & (*T)[(i << 1) | 1];
}
}
unsigned int query(vector<unsigned int>& T, int n, int left, int right) {
unsigned int result = 0xffffffff;
left += n;
right += n;
while (left < right) {
if (left & 1) {
result &= T[left];
left += 1;
}
if (right & 1) {
right -= 1;
result &= T[right];
}
left >>= 1;
right >>= 1;
}
return result;
}
int main() {
int n, m;
cin >> n >> m;
vector<unsigned int> T(n * 2, 0);
vector<int> L, R;
vector<unsigned int> Q;
vector<unsigned int> result(n, 0);
vector<int> sums(n + 1, 0);
for (int i = 0; i < m; ++i) {
int left, right;
unsigned int q;
cin >> left >> right >> q;
L.push_back(left - 1);
R.push_back(right);
Q.push_back(q);
}
for (int bit = 0; bit <= MAXBITS; bit += 1) {
for (int i = 0; i < n; ++i) {
sums[i] = 0;
}
for (int i = 0; i < m; ++i) {
int left = L[i];
int right = R[i];
int q = Q[i];
if ((q >> bit) & 1) {
sums[left] += 1;
sums[right] -= 1;
}
}
for (int pos = 0; pos < n; ++pos) {
if (pos > 0) {
sums[pos] += sums[pos - 1];
}
if (sums[pos] > 0) {
result[pos] |= (1 << bit);
}
}
}
buildTree(&T, n, result);
for (int i = 0; i < m; ++i) {
int left = L[i];
int right = R[i];
unsigned int q = Q[i];
unsigned int qand = query(T, n, left, right);
if (qand != q) {
cout << "NO";
return 0;
}
}
cout << "YES\n";
for (int i = 0; i < result.size(); ++i) {
cout << result[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.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputReader in = new InputReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
new Task().go(in, out);
out.close();
}
}
class Task {
public int maxn = 100010;
int[][] c = new int[33][maxn];
int n, m;
int[] a = new int[maxn];
int[] l = new int[maxn], r = new int[maxn], q = new int[maxn];
int[] and = new int[maxn<<2];
public void go(InputReader in, PrintWriter out) {
n = in.nextInt();
m = in.nextInt();
for(int i=0; i<m; i++) {
l[i] = in.nextInt();
r[i] = in.nextInt();
q[i] = in.nextInt();
for(int j=0; j<=30; j++) if(((q[i]>>j)&1)!=0) {
add(j, l[i], 1);
add(j, r[i]+1, -1);
}
}
for(int i=1; i<=n; i++) {
for(int j=0; j<=30; j++) if(ask(j, i)!=0) {
a[i] |= (1<<j);
}
}
build(1, n, 1);
boolean flag = true;
for(int i=0; i<m; i++) {
if(query(l[i], r[i], 1, n, 1) != q[i]) {
flag = false;
break;
}
}
if(!flag) out.println("NO");
else {
out.println("YES");
for(int i=1; i<=n; i++) out.print(a[i]+" ");
out.println();
}
}
public void add(int p, int x, int v) {
for(int i=x; i<=n; i+=(i&-i)) c[p][i] += v;
}
public int ask(int p, int x) {
int res = 0;
for(int i=x; i>0; i-=(i&-i)) res += c[p][i];
return res;
}
public void build(int l, int r, int rt) {
if(l==r) and[rt] = a[l];
else {
int m = (l + r) >> 1;
build(l, m, rt<<1);
build(m+1, r, rt<<1|1);
push_up(rt);
}
}
public int query(int x, int y, int l, int r, int rt) {
if(x<=l && r<=y) return and[rt];
else {
int m = (l+r)>>1, res = (1<<30)-1;
if(x<=m) res &= query(x, y, l, m, rt<<1);
if(y>m) res &= query(x, y, m+1, r, rt<<1|1);
return res;
}
}
public void push_up(int rt) {
and[rt] = and[rt<<1] & and[rt<<1|1];
}
}
class InputReader {
BufferedReader bf;
StringTokenizer st;
InputReader(InputStreamReader is) {
bf = new BufferedReader(is);
}
public String next() {
if (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(bf.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public 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.awt.image.ReplicateScaleFilter;
import java.io.*;
import java.math.*;
import java.util.*;
import javax.swing.RepaintManager;
public class Main {
static long mod = (long) 1e9 + 7;
public static void main(String[] args) {
FasterScanner s = new FasterScanner();
PrintWriter out = new PrintWriter(System.out);
int test = 1;
testloop: while (test-- > 0) {
int n = s.nextInt();
int m = s.nextInt();
int[][] x = new int[30][n + 1];
int[] qL = new int[m];
int[] qR = new int[m];
int[] qY = new int[m];
int j = 0;
while (m-- > 0) {
int l = qL[j] = s.nextInt() - 1;
int r = qR[j] = s.nextInt() - 1;
int y = qY[j++] = s.nextInt();
for (int i = 0; i < 30; i++) {
if ((y & (1 << i)) != 0) {
x[i][l]++;
x[i][r + 1]--;
}
}
}
int[] ans = new int[n];
for (int i = 0; i < 30; i++) {
for (j = 0; j < n; j++) {
if (j > 0)
x[i][j] += x[i][j - 1];
if (x[i][j] > 0) {
ans[j] |= (1 << i);
}
}
}
int[] segTree = new int[2 * n];
for (int i = 0; i < n; i++)
add(segTree, i, ans[i]);
for (int i = 0; i < qL.length; i++) {
if (max(segTree, qL[i], qR[i]) != qY[i]) {
out.println("NO");
continue testloop;
}
}
out.println("YES");
printArray(ans, out);
}
out.close();
}
public static void add(int[] t, int i, int value) {
i += t.length / 2;
t[i] += value;
for (; i > 1; i >>= 1)
t[i >> 1] = t[i] & t[i ^ 1];
}
// max[a, b]
public static int max(int[] t, int a, int b) {
int res = (1<<30) - 1;
for (a += t.length / 2, b += t.length / 2; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) {
if ((a & 1) != 0)
res = res & t[a];
if ((b & 1) == 0)
res = res & t[b];
}
return res;
}
public static class Node implements Comparable<Node> {
int id, val;
Node(int id1, int val1) {
id = id1;
val = val1;
}
public int compareTo(Node o) {
if (this.val != o.val)
return Integer.compare(this.val, o.val);
return this.id - o.id;
}
}
public static void printArray(int[] a, PrintWriter out) {
for (int i = 0; i < a.length; i++) {
out.print(a[i]);
out.print(' ');
}
out.println();
}
public static long pow(long x, long n, long mod) {
long res = 1;
for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) {
if ((n & 1) != 0) {
res = (res * p % mod);
}
}
return res;
}
static long gcd(long n1, long n2) {
long r;
while (n2 != 0) {
r = n1 % n2;
n1 = n2;
n2 = r;
}
return n1;
}
static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int snumChars;
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || 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>
using namespace std;
int n, m, l, r, q;
int d[2 * 131072 + 3];
int t[1000];
vector<int> st[100007], kon[100007];
pair<pair<int, int>, int> kraw[100007];
void insert(int poz, int val) {
poz += 131072;
d[poz] = val;
poz /= 2;
while (poz) {
d[poz] = d[poz * 2] & d[poz * 2 + 1];
poz /= 2;
}
}
int query(int a, int b) {
a += 131072;
b += 131072;
int res = d[a] & d[b];
while (a / 2 != b / 2) {
if (!(a & 1)) res &= d[a + 1];
if (b & 1) res &= d[b - 1];
a /= 2;
b /= 2;
}
return res;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < (m); i++) {
scanf("%d%d%d", &l, &r, &q);
st[l].push_back(q);
kon[r].push_back(q);
kraw[i] = make_pair(make_pair(l, r), q);
}
int akt_wyn = 0;
for (int i = 1; i <= (n); i++) {
for (__typeof((st[i]).begin()) it = ((st[i]).begin()); it != (st[i]).end();
it++) {
int x = *it;
int poz = 0;
while (x) {
t[poz++] += (x & 1);
x /= 2;
}
}
akt_wyn = 0;
for (int j = 29; j >= (0); j--) {
akt_wyn *= 2;
if (t[j]) ++akt_wyn;
}
insert(i, akt_wyn);
for (__typeof((kon[i]).begin()) it = ((kon[i]).begin());
it != (kon[i]).end(); it++) {
int x = *it;
int poz = 0;
while (x) {
t[poz++] -= (x & 1);
x /= 2;
}
}
}
for (int i = 0; i < (m); i++) {
if (query(kraw[i].first.first, kraw[i].first.second) != kraw[i].second) {
puts("NO");
return 0;
}
}
puts("YES");
for (int i = 1; i <= (n); i++) printf("%d ", d[i + 131072]);
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;
const int N = (int)1e5;
int n, m, x[N + 10], y[N + 10], z[N + 10];
bool tag[N * 4 + 10][31], bit[N * 4 + 10][31];
inline void down(int k, int i) {
if (tag[k][i]) {
tag[k << 1][i] = bit[k << 1][i] = 1;
tag[k << 1 | 1][i] = bit[k << 1 | 1][i] = 1;
tag[k][i] = 0;
}
}
inline void down(int k) {
for (int i = 0; i <= 30; ++i)
if (tag[k][i]) {
tag[k << 1][i] = bit[k << 1][i] = 1;
tag[k << 1 | 1][i] = bit[k << 1 | 1][i] = 1;
tag[k][i] = 0;
}
}
inline void update(int k, int i) {
bit[k][i] = bit[k << 1][i] && bit[k << 1 | 1][i];
}
inline void update(int k) {
for (int i = 0; i <= 30; ++i)
bit[k][i] = bit[k << 1][i] && bit[k << 1 | 1][i];
}
void cover(int k, int lc, int rc, int l, int r, int d) {
if (l == lc && r == rc) {
if (bit[k][d]) return;
bit[k][d] = tag[k][d] = 1;
return;
}
down(k, d);
int mid = lc + rc >> 1;
if (r <= mid)
cover(k << 1, lc, mid, l, r, d);
else if (l > mid)
cover(k << 1 | 1, mid + 1, rc, l, r, d);
else
cover(k << 1, lc, mid, l, mid, d),
cover(k << 1 | 1, mid + 1, rc, mid + 1, r, d);
update(k, d);
}
bool query(int k, int lc, int rc, int l, int r, int d) {
if (l == lc && r == rc) return bit[k][d];
down(k, d);
int mid = lc + rc >> 1;
if (r <= mid) return query(k << 1, lc, mid, l, r, d);
if (l > mid) return query(k << 1 | 1, mid + 1, rc, l, r, d);
return query(k << 1, lc, mid, l, mid, d) &&
query(k << 1 | 1, mid + 1, rc, mid + 1, r, d);
}
int query(int k, int lc, int rc, int p) {
if (lc == rc) {
int t = 0;
for (int i = 30; i >= 0; --i) t = t * 2 + bit[k][i];
return t;
}
down(k);
int mid = lc + rc >> 1;
if (p <= mid)
return query(k << 1, lc, mid, p);
else
return query(k << 1 | 1, mid + 1, rc, p);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d%d", &x[i], &y[i], &z[i]);
for (int j = 0, k = z[i]; k; k /= 2, ++j)
if (k & 1) cover(1, 1, n, x[i], y[i], j);
}
for (int i = 1; i <= m; ++i)
for (int j = 0; j <= 30; ++j)
if ((z[i] & 1 << j) == 0 && query(1, 1, n, x[i], y[i], j))
puts("NO"), exit(0);
puts("YES");
for (int i = 1; i <= n; ++i)
printf("%d%c", query(1, 1, n, 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;
long long x[100005], y[100005], q[100005], N = ((long long)1 << 40) - 1;
struct node {
long long r, l, v, sum;
} tree[400005];
void buildtree(int i, int l, int r) {
tree[i].l = l;
tree[i].r = r;
tree[i].v = 0;
if (tree[i].r == tree[i].l) {
tree[i].sum = 0;
return;
}
buildtree(i * 2, l, (r + l) / 2);
buildtree(i * 2 + 1, (l + r) / 2 + 1, r);
tree[i].sum = tree[i * 2].sum & tree[i * 2 + 1].sum;
}
void downadd(long long v) {
tree[v * 2].v |= tree[v].v;
tree[v * 2 + 1].v |= tree[v].v;
tree[v * 2].sum |= tree[v].v;
tree[v * 2 + 1].sum |= tree[v].v;
tree[v].v = 0;
}
void update(long long i, long long l, long long r, long long k) {
if (tree[i].l >= l && tree[i].r <= r) {
tree[i].sum |= k;
tree[i].v |= k;
return;
}
downadd(i);
long long m = (tree[i].l + tree[i].r) / 2;
if (l <= m) update(i * 2, l, r, k);
if (r > m) update(i * 2 + 1, l, r, k);
tree[i].sum = tree[i * 2].sum & tree[i * 2 + 1].sum;
}
long long query(long long i, long long l, long long r) {
if (tree[i].l >= l && tree[i].r <= r) {
return tree[i].sum;
}
downadd(i);
long long ans = N, m = (tree[i].r + tree[i].l) / 2;
if (l <= m) ans &= query(i * 2, l, r);
if (r > m) ans &= query(i * 2 + 1, l, r);
return ans;
}
int main() {
int n, m;
cin >> n >> m;
buildtree(1, 1, n);
for (int i = 1; i <= m; i++) {
cin >> x[i] >> y[i] >> q[i];
update(1, x[i], y[i], q[i]);
}
for (int i = 1; i <= m; i++) {
long long v = query(1, x[i], y[i]);
if (v != q[i]) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
for (int i = 1; i <= n; i++) {
cout << query(1, i, i) << " ";
}
cout << endl;
}
| 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.*;
public class CF482B {
class ST {
int n, and[], lo[], hi[];
public ST(int nn, int[] a) {
n = nn;
and = new int[n << 2];
lo = new int[n << 2];
hi = new int[n << 2];
build(1, 0, n - 1, a);
}
void build(int i, int a, int b, int[] arr) {
lo[i] = a; hi[i] = b;
if(a == b) {
and[i] = arr[a];
return;
}
int m = (a + b) / 2;
build(2 * i, a, m, arr);
build(2 * i + 1, m + 1, b, arr);
and[i] = and[2 * i] & and[2 * i + 1];
}
int raq(int l, int r) { return raq(1, l, r); }
int raq(int i, int a, int b) {
if(b < lo[i] || hi[i] < a) return 0x3FFFFFFF;
if(a <= lo[i] && hi[i] <= b) return and[i];
return raq(2 * i, a, b) & raq(2 * i + 1, a, b);
}
}
public CF482B() {
FS scan = new FS();
PrintWriter out = new PrintWriter(System.out);
int n = scan.nextInt(), c = scan.nextInt();
int[][] bits = new int[30][n + 1];
int[][] q = new int[3][c];
for(int i = 0 ; i < c ; i++) {
int l = scan.nextInt() - 1, r = scan.nextInt() - 1, v = scan.nextInt();
q[0][i] = l; q[1][i] = r; q[2][i] = v;
while(v > 0) {
int bit = Integer.numberOfTrailingZeros(v & -v);
v -= v & -v;
bits[bit][l]++;
bits[bit][r + 1]--;
}
}
int[] res = new int[n];
for(int bit = 0 ; bit < 30 ; bit++) {
int cnt = 0;
for(int i = 0 ; i < n ; i++) {
cnt += bits[bit][i];
if(cnt > 0) res[i] |= 1 << bit;
}
}
boolean valid = true;
ST st = new ST(n, res);
for(int i = 0 ; i < c ; i++) {
int raq = st.raq(q[0][i], q[1][i]);
if(raq != q[2][i]) valid = false;
}
if(valid) {
out.println("YES");
for(int i = 0 ; i < n ; i++)
out.print(res[i] + " ");
} else {
out.println("NO");
}
out.close();
}
class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while(!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine()); }
catch(Exception e) { e.printStackTrace(); }
}
return st.nextToken();
}
public int nextInt() { return Integer.parseInt(next()); }
}
public static void main(String[] args) { new CF482B(); }
} | 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.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class D {
public static int[] seg;
public static int[] initial;
public static int[] cum;
public static int[] ans;
public static boolean[] lazy;
public static void buildtree(int node, int i, int j) {
if(i == j) {
seg[node] = ans[i];
return;
}
buildtree(2*node, i, (i+j)/2);
buildtree(2*node+1, (i+j)/2+1, j);
seg[node] = seg[2*node] & seg[2*node+1];
}
/*
public static void update(int node, int i, int j, int b, int e) {
if(lazy[node]) {
seg[node] = 1;
if(i != j) {
lazy[2*node] = true;
lazy[2*node+1] = true;
}
lazy[node] = false;
}
if(e < i || b > j || i > j) return;
if(i >= b && j <= e) {
seg[node] = 1;
if(i != j) {
lazy[2*node] = true;
lazy[2*node+1] = true;
}
return;
}
update(2*node, i, (i+j)/2, b, e);
update(2*node+1, (i+j)/2+1, j, b, e);
seg[node] = seg[2*node] & seg[2*node+1];
}
*/
public static int query(int node, int i, int j, int b, int e) {
if(e < i || b > j || i > j) return -1;
if(i >= b && j <= e) return seg[node];
int first = query(2*node, i, (i+j)/2, b, e);
int second = query(2*node+1, (i+j)/2+1, j, b, e);
if(first == -1) return second;
if(second == -1) return first;
return first & second;
}
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] a = new int[m][3];
for(int i=0; i<m; i++) {
st = new StringTokenizer(bf.readLine());
a[i][0] = Integer.parseInt(st.nextToken());
a[i][1] = Integer.parseInt(st.nextToken());
a[i][2] = Integer.parseInt(st.nextToken());
}
ans = new int[n]; seg = new int[4*n];
for(int i=0; i<30; i++) {
cum = new int[n]; initial = new int[n+1];
for(int j=0; j<m; j++) {
if((a[j][2] & (1 << i)) != 0) {
initial[a[j][0]-1]++;
initial[a[j][1]]--;
}
}
cum[0] = initial[0];
for(int j=1; j<n; j++)
cum[j] = cum[j-1] + initial[j];
for(int j=0; j<n; j++)
if(cum[j] > 0) cum[j] = 1;
for(int j=0; j<n; j++) ans[j] += (cum[j] << i);
}
buildtree(1, 0, n-1);
boolean possible = true;
for(int j=0; j<m; j++) {
if(query(1, 0, n-1, a[j][0]-1, a[j][1]-1) != a[j][2]) {
//if(n == 1000) System.out.println((a[j][0] - 1) + " " + (a[j][1] - 1) + " " + " " + query(1, 0, n-1, a[j][0]-1, a[j][1]-1) + " " + (a[j][2]));
possible = false;
break;
}
}
//if(n == 100000) System.out.println(i);
if(!possible) {
//for(int j=0; j<n; j++) System.out.print(query(1, 0, n-1, j, j) + " ");
out.println("NO");
out.close();
System.exit(0);
}
StringBuilder sb = new StringBuilder(); sb.append("YES\n");
for(int j=0; j<n; j++) sb.append(ans[j]).append(" ");
out.println(sb.toString());
out.close(); System.exit(0);
}
/*
*
* 0011
* 0110
* 1001
* 1100
* 1111
*
* 1 1 0 0 0 0 1 1 0 0 0 0 1 1 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 | import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Tifuera
*/
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 {
public static final int BITS = 29;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] L = new int[m];
int[] R = new int[m];
int[] Q = new int[m];
List<Condition> conditions = new ArrayList<>(m);
for (int i = 0; i < m; i++) {
L[i] = in.nextInt() - 1;
R[i] = in.nextInt() - 1;
Q[i] = in.nextInt();
conditions.add(new Condition(L[i], R[i], Q[i]));
}
int[] answer = new int[n];
int[] bits = new int[n];
for (int i = 0; i <= BITS; i++) {
Arrays.fill(bits, 0);
constructBits(conditions, i, bits);
for (int j = 0; j < n; j++) {
if (bits[j] > 0) {
answer[j] += (bits[j] << i);
}
}
}
if (isSolutionCorrect(conditions, answer)) {
out.println("YES");
for (int a : answer) {
out.print(a + " ");
}
} else {
out.println("NO");
}
}
//TODO use segment tree to verify solution
private boolean isSolutionCorrect(List<Condition> conditions, int[] answer) {
SimpleSegmentTree segmentTree = new SimpleSegmentTree(answer);
for (Condition cond : conditions) {
if (segmentTree.logicalAnd(cond.l, cond.r) != cond.q) {
return false;
}
}
return true;
}
private void constructBits(List<Condition> conditions, int pos, int[] bits) {
int[] arr = new int[bits.length + 1];
for (Condition cond : conditions) {
int cur = (cond.q >> pos) & 1;
if (cur == 1) {
arr[cond.l]++;
arr[cond.r + 1]--;
}
}
int s = 0;
for (int i = 0; i < bits.length; i++) {
s += arr[i];
if (s > 0) {
bits[i] = 1;
}
}
}
private static class Condition {
int l;
int r;
int q;
private Condition(int l, int r, int q) {
this.l = l;
this.r = r;
this.q = q;
}
}
private static class SimpleSegmentTree {
private int n;
private int[] tree;
private int[] initialValues;
private SimpleSegmentTree(int[] initialValues) {
this.initialValues = initialValues;
this.n = initialValues.length;
tree = new int[4 * n];
init(0, n - 1, 1);
}
private void init(int left, int right, int v) {
if (left == right) {
tree[v] = initialValues[left];
} else {
int mid = (left + right) / 2;
init(left, mid, 2 * v);
init(mid + 1, right, 2 * v + 1);
tree[v] = tree[2 * v] & tree[2 * v + 1];
}
}
private int logicalAnd(int left, int right) {
return logicalAnd(1, 0, n - 1, left, right);
}
private int logicalAnd(int v, int vLeft, int vRight, int left, int right) {
if (left > right) {
return (2 << BITS) - 1;
} else {
if (vLeft == left && vRight == right) {
return tree[v];
}
int vMid = (vLeft + vRight) / 2;
int leftAns = logicalAnd(2 * v, vLeft, vMid, left, Math.min(right, vMid));
int rightAns = logicalAnd(2 * v + 1, vMid + 1, vRight, Math.max(left, vMid + 1), right);
return leftAns & rightAns;
}
}
}
}
class InputReader {
private BufferedReader reader;
private String[] currentArray;
private int curPointer;
public InputReader(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
public int nextInt() {
if ((currentArray == null) || (curPointer >= currentArray.length)) {
try {
currentArray = reader.readLine().split(" ");
} catch (IOException e) {
throw new RuntimeException(e);
}
curPointer = 0;
}
return Integer.parseInt(currentArray[curPointer++]);
}
}
| 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;
long long int power(long long int a, long long int b) {
long long int res = 1;
a = a % 1000000007;
while (b > 0) {
if (b & 1) {
res = (res * a) % 1000000007;
b--;
}
a = (a * a) % 1000000007;
b >>= 1;
}
return res;
}
long long int gcd(long long int a, long long int b) {
return (b == 0) ? a : gcd(b, a % b);
}
vector<long long int> a(100005, 0);
vector<long long int> tree(500005, 0);
long long int n, m;
void build(long long int l, long long int r, long long int v) {
if (l == r)
tree[v] = a[l];
else {
long long int mid = l + (r - l) / 2;
build(l, mid, 2 * v);
build(mid + 1, r, 2 * v + 1);
tree[v] = (tree[2 * v] & tree[2 * v + 1]);
}
}
long long int x = 0;
long long int sum(long long int tl, long long int tr, long long int l,
long long int r, long long int v) {
if (l > r) return x;
long long int mid = tl + (tr - tl) / 2;
if (tl == l && tr == r) return tree[v];
return (sum(tl, mid, l, min(mid, r), 2 * v) &
sum(mid + 1, tr, max(mid + 1, l), r, 2 * v + 1));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (long long int i = 0; i <= 30; i++) x += (1 << i);
vector<pair<pair<long long int, long long int>, long long int>> v(m);
for (long long int i = 0; i < m; i++) {
cin >> v[i].first.first >> v[i].first.second >> v[i].second;
}
for (long long int i = 0; i <= 30; i++) {
vector<long long int> sum(n + 2, 0);
for (auto j : v) {
if ((j.second & (1 << i)) == 0) continue;
sum[j.first.first] += 1;
sum[j.first.second + 1] -= 1;
}
for (long long int j = 1; j <= n; j++) sum[j] += sum[j - 1];
for (long long int j = 1; j <= n; j++) {
if (sum[j]) a[j] |= (1 << i);
}
}
build(1, n, 1);
for (auto i : v) {
if (sum(1, n, i.first.first, i.first.second, 1) != i.second) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
for (long long 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;
long long tree[3 * 100005];
bool lazy[3 * 100005];
void update(long long node, long long b, long long e, long long i, long long j,
long long v) {
if (i > e || j < b) {
return;
}
if (b >= i && e <= j) {
lazy[node] = true;
{ tree[node] = tree[node] | v; }
return;
}
if (lazy[node]) {
tree[node * 2] |= tree[node];
tree[node * 2 + 1] |= tree[node];
lazy[node * 2] = 1;
lazy[node * 2 + 1] = 1;
lazy[node] = 0;
}
update(node * 2, b, (b + e) / 2, i, j, v);
update(node * 2 + 1, (b + e) / 2 + 1, e, i, j, v);
tree[node] = tree[node * 2] & tree[node * 2 + 1];
}
long long query(long long node, long long b, long long e, long long i,
long long j) {
if (i > e || j < b) {
return (1LL << 33) - 1;
}
if (b >= i && e <= j) {
return tree[node];
}
if (lazy[node]) {
tree[node * 2] |= tree[node];
tree[node * 2 + 1] |= tree[node];
lazy[node * 2] = 1;
lazy[node * 2 + 1] = 1;
lazy[node] = 0;
}
return query(node * 2, b, (b + e) / 2, i, j) &
query(node * 2 + 1, (b + e) / 2 + 1, e, i, j);
}
int main() {
long long n, m;
cin >> n >> m;
pair<long long, long long> p[m + 1];
long long a[m + 1];
memset(tree, 0, sizeof tree);
memset(lazy, 0, sizeof lazy);
for (long long i = 1; i <= m; i++) {
long long x, y, z;
cin >> x >> y >> z;
update(1, 1, n, x, y, z);
p[i].first = x;
p[i].second = y;
a[i] = z;
}
int f = 1;
for (long long i = 1; i <= m; i++) {
long long x = query(1, 1, n, p[i].first, p[i].second);
if (x != a[i]) {
f = 0;
}
}
if (f == 1) {
cout << "YES\n";
for (long long i = 1; i <= n; i++) {
cout << query(1, 1, n, i, i) << " ";
}
cout << endl;
} else {
cout << "NO\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 | from sys import stdin
input=stdin.readline
def funcand(a,b):
return a&b
class SegmentTree:
def __init__(self, data, default=(1<<31)-1, func=max):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
"""func of data[start, stop)"""
start += self._size
stop += self._size
res_left = res_right = self._default
while start < stop:
if start & 1:
res_left = self._func(res_left, self.data[start])
start += 1
if stop & 1:
stop -= 1
res_right = self._func(self.data[stop], res_right)
start >>= 1
stop >>= 1
return self._func(res_left, res_right)
def f(n,q):
a=[0]*(n)
for bit in range(30):
s=[0]*(n+1)
for l,r,nm in q:
if((nm>>bit)&1):
s[l]+=1
s[r]-=1
for i in range(n):
if i>0:
s[i]+=s[i-1] # bich ke bhar raha hai
if s[i]>0:
a[i]|=(1<<bit)
st=SegmentTree(a,func=funcand,default=(1<<31)-1)
for l,r,nm in q:
# print(st.query(l,r))
if st.query(l,r)!=nm:
return "NO"
print("YES")
print(*a)
return ''
q=[]
n,m=map(int,input().strip().split())
for _ in range(m):
l,r,nm=map(int,input().strip().split())
q.append((l-1,r,nm))
print(f(n,q)) | 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 |
import java.io.BufferedReader;
import java.io.File;
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.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class Solution {
static class FastScanner{
BufferedReader s;
StringTokenizer st;
public FastScanner(){
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(File f) throws FileNotFoundException{
st = new StringTokenizer("");
s = new BufferedReader (new FileReader(f));
}
public int nextInt() throws IOException{
if(st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public double nextDouble() throws IOException{
if(st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException{
if(st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String nextString() throws IOException{
if(st.hasMoreTokens())
return st.nextToken();
else{
st = new StringTokenizer(s.readLine());
return nextString();
}
}
public String readLine() throws IOException{
return s.readLine();
}
public void close() throws IOException{
s.close();
}
}
//FastScanner s = new FastScanner(new File("input.txt")); copy inside void solve
//PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
static FastScanner s = new FastScanner();
static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String args[])throws IOException{
//Main ob=new Main();
Solution ob=new Solution();
ob.solve();
ww.close();
}
/////////////////////////////////////////////
int tree[];
int dp[];
int arr[];
void build(int node,int l,int r){
if(l+1==r){
tree[node]=arr[l];
return;
}
int mid = (l+r)/2;
build(node*2,l,mid);
build(node*2+1,mid,r);
tree[node]=tree[node*2]&tree[node*2+1];
}
int query(int node,int l,int r,int L,int R){
if (l <= L && R <= r) {
return tree[node];
}
int mid = (L + R) >> 1;
int ans = (1 << 30) - 1;
if (l < mid)
ans &= query(node*2,l, Math.min(r, mid), L, mid);
if (mid < r)
ans &= query(node*2+1,Math.max(l, mid),r, mid, R);
return ans;
}
/* int query(int v, int l, int r, int L, int R) {
if (l == L && r == R) {
return tree[v];
}
int mid = (L + R) >> 1;
int ans = (1<< 30) - 1
;
if (l < mid)
ans &= query(v * 2, l, Math.min(r, mid), L, mid);
if (mid < r)
ans &= query(v * 2 + 1, Math.max(l, mid), r, mid, R);
return ans;
}
*/
/////////////////////////////////////////////
void solve() throws IOException {
int n = s.nextInt();
int m = s.nextInt();
int N=1000000;
int l[] = new int[N];
int r[] = new int[N];
int q[] = new int[N];
for(int i = 0;i < m;i++){
l[i] = s.nextInt()-1;
r[i] = s.nextInt();
q[i] = s.nextInt();
}
dp = new int[N];
arr = new int[N];
int power = (int) Math.floor(Math.log(N) / Math.log(2)) + 1;
power = (int) ((int) 2 * Math.pow(2, power));
tree = new int[power];
for(int bit = 0;bit <= 30;bit++){
for(int i=0;i<n;i++)
dp[i]=0;
for(int i=0;i<m;i++){
if (((q[i] >> bit) & 1)==1) {
dp[l[i]]++;
dp[r[i]]--;
}
}
for(int i=0;i<n;i++){
if(i>0)
dp[i]+= dp[i-1];
if(dp[i]>0)
arr[i]|=(1<<bit);
}
}
build(1,0,n);
//for(int i=0;i<n;i++)
//System.out.println(arr[i]+" ");
for(int i=0;i<m;i++){
if (query(1, l[i], r[i], 0, n) != q[i]){
ww.println("NO");
return;
}
}
ww.println("YES");
for(int i=0;i<n;i++)
ww.print(arr[i]+" ");
}//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;
long long bigmod(long long b, long long p) {
if (p == 0) return 1;
long long my = bigmod(b, p / 2);
my *= my;
my %= 1000000007;
if (p & 1) my *= b, my %= 1000000007;
return my;
}
int setb(int n, int pos) { return n = n | (1 << pos); }
int resb(int n, int pos) { return n = n & ~(1 << pos); }
bool checkb(int n, int pos) { return (bool)(n & (1 << pos)); }
struct node {
long long x, y, val;
};
node ara[100005];
long long tree[4 * 100005], n, m, lol[100005];
void update(long long ind, long long b, long long e, long long i, long long j,
long long val) {
if (i > j) return;
if (b == i && e == j) {
tree[ind] |= val;
return;
}
long long mid = (b + e) / 2, l = 2 * ind, r = l + 1;
update(l, b, mid, i, min(j, mid), val);
update(r, mid + 1, e, max(i, mid + 1), j, val);
}
long long q(long long ind, long long b, long long e, long long i,
long long car) {
if (b == e) {
return tree[ind] | car;
}
long long mid = (b + e) / 2, l = 2 * ind, r = l + 1;
if (i <= mid)
return q(l, b, mid, i, car | tree[ind]);
else
return q(r, mid + 1, e, i, car | tree[ind]);
}
void init(long long ind, long long b, long long e) {
if (b == e) {
tree[ind] = lol[b];
return;
}
long long mid = (b + e) / 2, l = 2 * ind, r = l + 1;
init(l, b, mid);
init(r, mid + 1, e);
tree[ind] = tree[l] & tree[r];
}
long long fun(long long ind, long long b, long long e, long long i,
long long j) {
if (i > j) return 1073741823ll;
if (b == i && e == j) return tree[ind];
long long mid = (b + e) / 2, l = 2 * ind, r = l + 1;
return fun(l, b, mid, i, min(j, mid)) &
fun(r, mid + 1, e, max(mid + 1, i), j);
}
int main() {
long long i;
scanf("%lld %lld", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%lld %lld %lld", &ara[i].x, &ara[i].y, &ara[i].val);
update(1, 1, n, ara[i].x, ara[i].y, ara[i].val);
}
for (i = 1; i <= n; i++) lol[i] = q(1, 1, n, i, 0);
memset(tree, 0, sizeof(tree));
init(1, 1, n);
bool flag = 1;
for (i = 1; i <= m; i++) {
long long my = fun(1, 1, n, ara[i].x, ara[i].y);
if (my != ara[i].val) {
flag = 0;
break;
}
}
if (flag == 0)
puts("NO");
else {
puts("YES");
for (i = 1; i <= n; i++) {
if (i > 1) printf(" ");
printf("%d", lol[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;
void update(vector<int> &BIT, int value, int index) {
int n = BIT.size() - 1;
while (index <= n) {
BIT[index] += value;
index += index & (-index);
}
}
int query(vector<int> &BIT, int index) {
int sum = 0;
while (index > 0) {
sum += BIT[index];
index -= index & (-index);
}
return sum;
}
int query(vector<int> &BIT, int l, int r) {
return query(BIT, r) - query(BIT, l - 1);
}
const int N = 30;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int> > arr(n + 1, vector<int>(N, 0));
bool ok = true;
int l, r, q;
vector<pair<pair<int, int>, int> > ops(m);
for (int i = 0; i < m; i++) {
cin >> l >> r >> q;
l--, r--;
ops[i] = make_pair(make_pair(l, r), q);
for (int j = 0; j < N; j++) {
if (q >> j & 1) {
arr[l][j] += 1;
arr[r + 1][j] -= 1;
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < n; j++) {
arr[j + 1][i] += arr[j][i];
arr[j][i] = !!arr[j][i];
}
}
vector<vector<int> > BITS(N, vector<int>(n + 1));
for (int i = 0; i < N; i++) {
for (int j = 0; j < n; j++) {
update(BITS[i], arr[j][i], j + 1);
}
}
for (int i = 0; i < m; i++) {
l = ops[i].first.first, r = ops[i].first.second, q = ops[i].second;
for (int j = 0; j < N; j++) {
if (!(q >> j & 1)) {
ok &= (query(BITS[j], l + 1, r + 1) < (r - l + 1));
}
}
}
if (!ok)
cout << "NO\n";
else {
cout << "YES\n";
for (int i = 0; i < n; i++) {
int val = 0;
for (int j = 0; j < N; j++) {
if (arr[i][j]) val |= 1 << j;
}
cout << val << ' ';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
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;
int a[112345], l[112345], r[112345], q[112345], sum[112345], tree[4 * 112345];
int maxx = (1ll << 30) - 1;
void build_tree(int node, int s, int e) {
if (s == e) {
tree[node] = a[s];
return;
}
int mid = (s + e) / 2;
int c1 = 2 * node + 1;
int c2 = c1 + 1;
build_tree(c1, s, mid);
build_tree(c2, mid + 1, e);
tree[node] = (tree[c1] & tree[c2]);
}
int query_tree(int node, int s, int e, int p, int q) {
if (q < s || p > e) return maxx;
if (s >= p && e <= q) return tree[node];
int mid = (s + e) / 2;
int c1 = 2 * node + 1;
int c2 = c1 + 1;
return (query_tree(c1, s, mid, p, q) & query_tree(c2, mid + 1, e, p, q));
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i] >> q[i];
l[i]--;
}
for (int bit = 0; bit <= 30; 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_tree(0, 0, n - 1);
for (int i = 0; i < m; i++) {
if (query_tree(0, 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 << 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.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Tifuera
*/
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 {
public static final int BITS = 30;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] L = new int[m];
int[] R = new int[m];
int[] Q = new int[m];
List<Condition> conditions = new ArrayList<>(m);
for (int i = 0; i < m; i++) {
L[i] = in.nextInt() - 1;
R[i] = in.nextInt() - 1;
Q[i] = in.nextInt();
conditions.add(new Condition(L[i], R[i], Q[i]));
}
int[] answer = new int[n];
int[] bits = new int[n];
for (int i = 0; i <= BITS; i++) {
Arrays.fill(bits, 0);
constructBits(conditions, i, bits);
for (int j = 0; j < n; j++) {
if (bits[j] > 0) {
answer[j] += (bits[j] << i);
}
}
}
if (isSolutionCorrect(conditions, answer)) {
out.println("YES");
for (int a : answer) {
out.print(a + " ");
}
} else {
out.println("NO");
}
}
private boolean isSolutionCorrect(List<Condition> conditions, int[] answer) {
int[][] partialSums = new int[BITS + 1][answer.length + 1];
for (int i = 0; i <= BITS; i++) {
for (int j = 0; j < answer.length; j++) {
partialSums[i][j + 1] = partialSums[i][j] + ((answer[j] >> i) & 1);
}
}
for (Condition cond : conditions) {
for (int i = 0; i <= BITS; i++) {
int numberOfOneOnSegment = partialSums[i][cond.r + 1] - partialSums[i][cond.l];
boolean isRealBitOne = ((cond.q >> i) & 1) == 1;
boolean isOne = numberOfOneOnSegment == (cond.r - cond.l + 1);
if (isRealBitOne != isOne) {
return false;
}
}
}
return true;
}
private void constructBits(List<Condition> conditions, int pos, int[] bits) {
int[] arr = new int[bits.length + 1];
for (Condition cond : conditions) {
int cur = (cond.q >> pos) & 1;
if (cur == 1) {
arr[cond.l]++;
arr[cond.r + 1]--;
}
}
int s = 0;
for (int i = 0; i < bits.length; i++) {
s += arr[i];
if (s > 0) {
bits[i] = 1;
}
}
}
private static class Condition {
int l;
int r;
int q;
private Condition(int l, int r, int q) {
this.l = l;
this.r = r;
this.q = q;
}
}
}
class InputReader {
private BufferedReader reader;
private String[] currentArray;
private int curPointer;
public InputReader(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
public int nextInt() {
if ((currentArray == null) || (curPointer >= currentArray.length)) {
try {
currentArray = reader.readLine().split(" ");
} catch (IOException e) {
throw new RuntimeException(e);
}
curPointer = 0;
}
return Integer.parseInt(currentArray[curPointer++]);
}
}
| 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, cum[31][100100], a[100100], seg[4 * 100100];
pair<int, pair<int, int> > pr[100100];
void build(int p, int l, int r) {
if (l == r) {
seg[p] = a[l];
return;
}
int md = (l + r) >> 1;
build(p * 2, l, md);
build(p * 2 + 1, md + 1, r);
seg[p] = seg[p * 2] & seg[p * 2 + 1];
}
int get(int p, int l, int r, int a, int b) {
if (a <= l and r <= b) return seg[p];
if (r < a or l > b) return (1 << 30) - 1;
int md = (l + r) >> 1;
int r1 = get(p * 2, l, md, a, b);
int r2 = get(p * 2 + 1, md + 1, r, a, b);
return r1 & r2;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> pr[i].first >> pr[i].second.first >> pr[i].second.second;
for (int j = 0; j < 31; ++j) {
if ((pr[i].second.second & (1 << j)) != 0) {
++cum[j][pr[i].first];
--cum[j][pr[i].second.first + 1];
}
}
}
for (int i = 1; i <= n; ++i)
for (int j = 0; j < 31; ++j) cum[j][i] += cum[j][i - 1];
for (int i = 1; i <= n; ++i)
for (int j = 0; j < 31; ++j)
if (cum[j][i] > 0) a[i] |= (1 << j);
build(1, 1, n);
for (int i = 0; i < m; ++i) {
if (get(1, 1, n, pr[i].first, pr[i].second.first) != pr[i].second.second)
return cout << "NO\n", 0;
}
cout << "YES\n";
for (int i = 1; i <= n; ++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.BufferedWriter;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.math.BigInteger;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
/**
* 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;
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 in, OutputWriter out) {
int n=in.readInt(), m=in.readInt();
int[] from=new int[m], to=new int[m], and=new int[m];
long[] ret=new long[n];
IOUtils.readIntArrays(in, from, to, and);
MiscUtils.decreaseByOne(from, to);
IntervalTree andTree=new LongIntervalTree(n) {
protected long neutralDelta() {
return Long.MAX_VALUE;
}
protected long neutralValue() {
return Long.MAX_VALUE;
}
protected long accumulate(long value, long delta, int length) {
return valueδ
}
protected long joinDelta(long was, long delta) {
return wasδ
}
protected long joinValue(long left, long right) {
return left&right;
}
}, orTree=new LongIntervalTree(n) {
protected long neutralDelta() {
return 0;
}
protected long neutralValue() {
return 0;
}
protected long accumulate(long value, long delta, int length) {
return value|delta;
}
protected long joinDelta(long was, long delta) {
return was|delta;
}
protected long joinValue(long left, long right) {
return left|right;
}
};
for (int i=0; i<m; i++) orTree.update(from[i], to[i], and[i]);
for (int i=0; i<n; i++) andTree.update(i, i, ret[i]=orTree.query(i, i));
for (int i=0; i<m; i++) if (andTree.query(from[i], to[i])!=and[i]) {
out.printLine("NO");
return;
}
out.printLine("YES");
out.printLine(ret);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public 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++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void print(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(array[i]);
}
}
public void printLine(long[] array) {
print(array);
writer.println();
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
class IOUtils {
public static void readIntArrays(InputReader in, int[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++)
arrays[j][i] = in.readInt();
}
}
}
class MiscUtils {
public static void decreaseByOne(int[]...arrays) {
for (int[] array : arrays) {
for (int i = 0; i < array.length; i++)
array[i]--;
}
}
}
abstract class IntervalTree {
protected int size;
public IntervalTree(int size, boolean shouldInit) {
this.size = size;
int nodeCount = Math.max(1, Integer.highestOneBit(size) << 2);
initData(size, nodeCount);
if (shouldInit)
init();
}
protected abstract void initData(int size, int nodeCount);
protected abstract void initAfter(int root, int left, int right, int middle);
protected abstract void initBefore(int root, int left, int right, int middle);
protected abstract void initLeaf(int root, int index);
protected abstract void updatePostProcess(int root, int left, int right, int from, int to, long delta, int middle);
protected abstract void updatePreProcess(int root, int left, int right, int from, int to, long delta, int middle);
protected abstract void updateFull(int root, int left, int right, int from, int to, long delta);
protected abstract long queryPostProcess(int root, int left, int right, int from, int to, int middle, long leftResult, long rightResult);
protected abstract void queryPreProcess(int root, int left, int right, int from, int to, int middle);
protected abstract long queryFull(int root, int left, int right, int from, int to);
protected abstract long emptySegmentResult();
public void init() {
if (size == 0)
return;
init(0, 0, size - 1);
}
private void init(int root, int left, int right) {
if (left == right) {
initLeaf(root, left);
} else {
int middle = (left + right) >> 1;
initBefore(root, left, right, middle);
init(2 * root + 1, left, middle);
init(2 * root + 2, middle + 1, right);
initAfter(root, left, right, middle);
}
}
public void update(int from, int to, long delta) {
update(0, 0, size - 1, from, to, delta);
}
protected void update(int root, int left, int right, int from, int to, long delta) {
if (left > to || right < from)
return;
if (left >= from && right <= to) {
updateFull(root, left, right, from, to, delta);
return;
}
int middle = (left + right) >> 1;
updatePreProcess(root, left, right, from, to, delta, middle);
update(2 * root + 1, left, middle, from, to, delta);
update(2 * root + 2, middle + 1, right, from, to, delta);
updatePostProcess(root, left, right, from, to, delta, middle);
}
public long query(int from, int to) {
return query(0, 0, size - 1, from, to);
}
protected long query(int root, int left, int right, int from, int to) {
if (left > to || right < from)
return emptySegmentResult();
if (left >= from && right <= to)
return queryFull(root, left, right, from, to);
int middle = (left + right) >> 1;
queryPreProcess(root, left, right, from, to, middle);
long leftResult = query(2 * root + 1, left, middle, from, to);
long rightResult = query(2 * root + 2, middle + 1, right, from, to);
return queryPostProcess(root, left, right, from, to, middle, leftResult, rightResult);
}
}
abstract class LongIntervalTree extends IntervalTree {
protected long[] value;
protected long[] delta;
protected LongIntervalTree(int size) {
this(size, true);
}
public LongIntervalTree(int size, boolean shouldInit) {
super(size, shouldInit);
}
protected void initData(int size, int nodeCount) {
value = new long[nodeCount];
delta = new long[nodeCount];
}
protected abstract long joinValue(long left, long right);
protected abstract long joinDelta(long was, long delta);
protected abstract long accumulate(long value, long delta, int length);
protected abstract long neutralValue();
protected abstract long neutralDelta();
protected long initValue(int index) {
return neutralValue();
}
protected void initAfter(int root, int left, int right, int middle) {
value[root] = joinValue(value[2 * root + 1], value[2 * root + 2]);
delta[root] = neutralDelta();
}
protected void initBefore(int root, int left, int right, int middle) {
}
protected void initLeaf(int root, int index) {
value[root] = initValue(index);
delta[root] = neutralDelta();
}
protected void updatePostProcess(int root, int left, int right, int from, int to, long delta, int middle) {
value[root] = joinValue(value[2 * root + 1], value[2 * root + 2]);
}
protected void updatePreProcess(int root, int left, int right, int from, int to, long delta, int middle) {
pushDown(root, left, middle, right);
}
protected void pushDown(int root, int left, int middle, int right) {
value[2 * root + 1] = accumulate(value[2 * root + 1], delta[root], middle - left + 1);
value[2 * root + 2] = accumulate(value[2 * root + 2], delta[root], right - middle);
delta[2 * root + 1] = joinDelta(delta[2 * root + 1], delta[root]);
delta[2 * root + 2] = joinDelta(delta[2 * root + 2], delta[root]);
delta[root] = neutralDelta();
}
protected void updateFull(int root, int left, int right, int from, int to, long delta) {
value[root] = accumulate(value[root], delta, right - left + 1);
this.delta[root] = joinDelta(this.delta[root], delta);
}
protected long queryPostProcess(int root, int left, int right, int from, int to, int middle, long leftResult, long rightResult) {
return joinValue(leftResult, rightResult);
}
protected void queryPreProcess(int root, int left, int right, int from, int to, int middle) {
pushDown(root, left, middle, right);
}
protected long queryFull(int root, int left, int right, int from, int to) {
return value[root];
}
protected long emptySegmentResult() {
return neutralValue();
}
} | 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 node {
int l, r, val;
} q[150000];
int INF;
int a[150000];
int sum[32][150000];
int now[32];
int tree[150000 * 4];
void pushup(int rt) { tree[rt] = tree[rt * 2] & tree[rt * 2 + 1]; }
void build(int l, int r, int rt) {
if (l == r) {
tree[rt] = a[l];
return;
}
int m = (l + r) / 2;
build(l, m, rt * 2);
build(m + 1, r, rt * 2 + 1);
pushup(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (l >= L && r <= R) {
return tree[rt];
}
pushup(rt);
int m = (l + r) / 2;
int ans = INF;
if (L <= m) ans = (ans & (query(L, R, l, m, rt * 2)));
if (R > m) ans = (ans & (query(L, R, m + 1, r, rt * 2 + 1)));
pushup(rt);
return ans;
}
int main() {
int n, m;
for (int i = 0; i <= 30; i++) INF += (1 << i);
while (~scanf("%d%d", &n, &m)) {
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= m; i++) {
int L, R, val;
scanf("%d%d%d", &L, &R, &val);
q[i].l = L;
q[i].r = R, q[i].val = val;
for (int j = 0; j <= 30; j++) {
if ((val & ((1 << j))) > 0) {
sum[j][L]++;
sum[j][R + 1]--;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 30; j++) {
now[j] += sum[j][i];
}
int num = 0;
for (int j = 0; j <= 30; j++) {
if (now[j] > 0) num += (1 << j);
}
a[i] = num;
}
build(1, n, 1);
int flag = 1;
for (int i = 1; i <= m; i++) {
if (query(q[i].l, q[i].r, 1, n, 1) != q[i].val) flag = 0;
}
if (flag == 0)
printf("NO\n");
else {
printf("YES\n");
for (int i = 1; i <= n; i++) {
printf("%d ", a[i]);
}
printf("\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 N = 2 * 1e5 + 10, LG = 30, Non_And = ((1 << 30)) - 1;
int ps[LG][N], n;
pair<pair<int, int>, int> quer[N];
void upd(int l, int r, int bit) {
ps[bit][r]--;
ps[bit][l]++;
}
void mdfy(int bit) {
for (int i = 1; i <= n; i++) ps[bit][i] += ps[bit][i - 1];
for (int i = 0; i <= n; i++) ps[bit][i] = min(ps[bit][i], 1);
for (int i = 1; i <= n; i++) ps[bit][i] += ps[bit][i - 1];
}
int get(int l, int r, int bit) {
if (l == 0) return ps[bit][r];
return ps[bit][r] - ps[bit][l - 1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int l, r, q;
cin >> l >> r >> q;
l--;
for (int j = 0; j < LG; j++) {
if (q & ((long long)1 << j)) {
upd(l, r, j);
}
}
quer[i].first.first = l;
quer[i].first.second = r;
quer[i].second = q;
}
for (int j = 0; j < LG; j++) mdfy(j);
for (int i = 0; i < m; i++) {
int l, r, k;
l = quer[i].first.first;
r = quer[i].first.second;
k = quer[i].second;
for (int j = 0; j < LG; j++) {
if ((k & ((long long)1 << j)) == 0) {
if (get(l, r - 1, j) == r - l) {
cout << "NO\n";
return 0;
}
}
}
}
cout << "YES\n";
for (int i = 0; i < n; i++) {
int ansi = 0;
for (int j = 0; j < LG; j++) {
if (get(i, i, j)) ansi += ((long long)1 << j);
}
cout << ansi << " ";
}
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.util.*;
public class InterestingArray
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int[][] pre = new int[31][n+1];
int[][] queries = new int[3][m];
for(int q = 1; q <= m; q++) {
int l = scan.nextInt()-1;
int r = scan.nextInt()-1;
int x = scan.nextInt();
queries[0][q-1] = l;
queries[1][q-1] = r;
queries[2][q-1] = x;
for(int i = 30; i >= 0; i--) {
int j = (1 << i);
if((x & j) > 0) {
pre[i][l]++;
pre[i][r+1]--;
}
}
}
long[] ans = new long[n];
int sum = 0;
for(int j = 0; j < 31; j++) {
sum = 0;
for(int i = 0; i < n; i++) {
sum += pre[j][i];
if(sum > 0) ans[i] |= (1 << j);
}
}
ST seg = new ST(n, ans);
for(int i = 0; i < m; i++) {
int l = queries[0][i];
int r = queries[1][i];
long x = queries[2][i];
long a = seg.and(l, r);
if(a != x) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
for(int i = 0; i < n; i++) System.out.print(ans[i]+" ");
}
static class ST { // 0-based indexed queries
int n, lo[], hi[];
long[] d, t;
public ST(int nn, long[] a) {
n = nn;
d = new long[n << 2];
t = new long[n << 2];
lo = new int[n << 2];
hi = new int[n << 2];
build(a, 1, 0, n - 1);
}
void build(long[] arr, int i, int a, int b) {
lo[i] = a; hi[i] = b;
if(a == b) {
t[i] = arr[a];
return;
}
int m = (a + b) / 2;
build(arr, i << 1, a, m);
build(arr, i << 1 | 1, m + 1, b);
update(i);
}
void prop(int i) {
d[i << 1] += d[i];
d[i << 1 | 1] += d[i];
d[i] = 0;
}
void update(int i) {
t[i] = (t[i << 1] + d[i << 1]) & (t[i << 1 | 1] + d[i << 1 | 1]);
}
// adds value v over range [a, b]
void inc(int a, int b, long v) { inc(1, a, b, v); }
private void inc(int i, int a, int b, long v) {
if(b < lo[i] || a > hi[i]) return;
if(a <= lo[i] && b >= hi[i]) {
d[i] += v;
return;
}
prop(i);
inc(i << 1, a, b, v);
inc(i << 1 | 1, a, b, v);
update(i);
}
// finds minimum value over range [a, b]
long and(int a, int b) { return and(1, a, b); }
private long 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 t[i] + d[i];
prop(i);
long res = and(i << 1, a, b) & and(i << 1 | 1, a, b);
update(i);
return res;
}
}
}
| 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
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;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
int[] t;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] l = new int[m];
int[] r = new int[m];
int[] q = new int[m];
int[][] der = new int[30][n + 1];
for (int i = 0; i < m; i++) {
l[i] = in.nextInt();
r[i] = in.nextInt();
q[i] = in.nextInt();
for (int j = 0; j < 30; j++) {
if (((q[i] >> j) & 1) == 1) {
der[j][l[i] - 1]++;
der[j][r[i]]--;
}
}
}
int[] ans = new int[n];
int[] bits = new int[30];
for (int i = 0; i < n; i++) {
int cur = 0;
for (int j = 0; j < 30; j++) {
bits[j] += der[j][i];
if (bits[j] > 0) cur |= 1 << j;
}
ans[i] = cur;
}
t = new int[4 * n];
build(ans, 1, 0, n - 1);
for (int i = 0; i < m; i++) {
if (and(1, 0, n - 1, l[i] - 1, r[i] - 1) != q[i]) {
out.println("NO");
return;
}
}
out.println("YES");
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
}
void build(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 and(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 and(v * 2, tl, tm, l, Math.min(r, tm)) & and(v * 2 + 1, tm + 1, tr, Math.max(l, tm + 1), r);
}
}
static class InputReader {
private StringTokenizer tokenizer;
private BufferedReader reader;
public InputReader(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
private void fillTokenizer() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public String next() {
fillTokenizer();
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>
using namespace std;
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& _p) {
return os << "(" << _p.first << "," << _p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& _V) {
bool f = true;
os << "[";
for (auto v : _V) {
os << (f ? "" : ",") << v;
f = false;
}
return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& _S) {
bool f = true;
os << "(";
for (auto s : _S) {
os << (f ? "" : ",") << s;
f = false;
}
return os << ")";
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& _M) {
return os << set<pair<T, U>>(_M.begin(), _M.end());
}
const signed long long INF = 1000000100;
const long double EPS = 1e-9;
struct tree {
int M;
int* T;
tree(int n) : M(1 << (32 - __builtin_clz(n))), T(new int[2 * M]) {
for (int(i) = (0); (i) < (2 * M); (i)++) T[i] = 0;
}
void insert(int a, int b, int t) {
a += M;
b += M;
T[a] |= t;
T[b] |= t;
while (a / 2 != b / 2) {
if (a % 2 == 0) T[a + 1] |= t;
if (b % 2 == 1) T[b - 1] |= t;
a /= 2;
b /= 2;
}
}
int query(int a) {
a += M;
int ret = 0;
while (a != 0) {
ret |= T[a];
a /= 2;
}
return ret;
}
void insert(int a, int t) {
a += M;
T[a] = t;
a /= 2;
while (a != 0) {
T[a] = (T[2 * a] & T[2 * a + 1]);
a /= 2;
}
}
int query(int a, int b) {
a += M;
b += M;
int ret = (T[a] & T[b]);
while (a / 2 != b / 2) {
if (a % 2 == 0) ret &= T[a + 1];
if (b % 2 == 1) ret &= T[b - 1];
a /= 2;
b /= 2;
}
return ret;
}
};
const int MAXN = 300300;
int N, Q;
int A[MAXN];
int B[MAXN];
int first[MAXN];
void read_data() {
scanf("%d %d", &N, &Q);
for (int(i) = (1); (i) <= (Q); (i)++)
scanf("%d %d %d", A + i, B + i, first + i);
}
void solve() {
tree T1(N);
for (int(i) = (1); (i) <= (Q); (i)++) T1.insert(A[i], B[i], first[i]);
tree T2(N);
for (int(i) = (1); (i) <= (N); (i)++) T2.insert(i, T1.query(i));
bool ok = true;
for (int(i) = (1); (i) <= (Q); (i)++)
ok &= (T2.query(A[i], B[i]) == first[i]);
printf(ok ? "YES\n" : "NO\n");
if (ok) {
for (int(i) = (1); (i) <= (N); (i)++) printf("%d ", T1.query(i));
printf("\n");
}
}
int main() {
read_data();
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 | import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.regex.*;
public class Myclass{
// public static ArrayList adj[]=new ArrayList[300001];
static long cal=0L;
static void pre() {
for(int i=0;i<32;i++) {
cal+=(1<<i);
}
}
public static long arr[]=new long[1000001];
static long[] seg=new long[4000000];
static long h[];
static char s[];
static void build(int node,int start,int end) {
if(start==end) {
seg[node]=arr[start];
return ;
}
int mid=(start+end)/2;
build(2*node+1,start,mid);
build(2*node+2,mid+1,end);
seg[node]=(seg[2*node+1]&seg[2*node+2]);
}
/*static void update(int node,int start,int end,int idx,double value) {
if(start==end) {
seg[node]=value;
return ;
}
int mid=(start+end)/2;
if(idx>=start && idx<=mid) {
update(2*node+1,start,mid,idx, value);
}
else
update(2*node+2,mid+1,end,idx,value);
seg[node]=Math.max(seg[2*node+1],seg[2*node+2]);
}*/
static Long query(int node,int start,int end,int l,int r) {
if(r<start || end<l)
return cal;
if(start>=l && end<=r)
return seg[node];
int mid=(start+end)/2;
return (query(2*node+1,start,mid,l,r)&query(2*node+2,mid+1,end,l,r));
}
static int dp[][]=new int[100005][32];
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
pre();
int n=in.nextInt();
int q=in.nextInt();
query[]a=new query[q];
for(int i=0;i<q;i++) {
a[i]=new query(in.nextInt()-1,in.nextInt()-1,in.nextLong());
for(int j=0;j<32;j++) {
if(((a[i].z>>j)&1)==1) {
dp[a[i].x][j]++;
dp[a[i].y+1][j]--;
}
}
}
for(int i=0;i<32;i++) {
for(int j=1;j<=n;j++) {
dp[j][i]+=dp[j-1][i];
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<32;j++) {
if(dp[i][j]>0) {
arr[i]+=(1<<j);
}
}
}
build(0,0,n-1);
boolean chk=true;
for(int i=0;i<q;i++) {
long ans=query(0,0,n-1,a[i].x,a[i].y);
if(ans!=a[i].z)
chk=false;
}
if(!chk)
pw.println("NO");
else
{
pw.println("YES");
for(int i=0;i<n;i++) {
pw.print(arr[i]+" ");
}
}
pw.flush();
pw.close();
}
private static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
static class tri implements Comparable<tri> {
int p, c, l;
tri(int p, int c, int l) {
this.p = p;
this.c = c;
this.l = l;
}
public int compareTo(tri o) {
return Integer.compare(l, o.l);
}
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
/* public static long primeFactorization(long n)
{
HashSet<Integer> a =new HashSet<Integer>();
long cnt=0;
for(int i=2;i*i<=n;i++)
{
while(a%i==0)
{
a.add(i);
a/=i;
}
}
if(a!=1)
cnt++;
//a.add(n);
return cnt;
}*/
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x%M*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result%M * x%M)%M;
x=(x%M * x%M)%M;
n=n/2;
}
return result;
}
public static long modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
{
if (n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
public static long[] shuffle(long[] a, Random gen){
for(int i = 0, n = a.length;i < n;i++){
int ind = gen.nextInt(n-i)+i;
long d = a[i];
a[i] = a[ind];
a[ind] = d;
}
return a;
}
static class pair implements Comparable<pair>
{
Integer x;
Integer y;
pair(int a,int sum)
{
this.x=a;
this.y=sum;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return p.x == x && p.y == y ;
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
}
class query {
Integer x,y;
Long z;
query(int x,int y,long z){
this.x=x;
this.y=y;
this.z=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 | #include <bits/stdc++.h>
long double sqr(long double a) { return a * a; };
using namespace std;
int n, m, l[100010], r[100010], t[4 * 100010], q[100010];
int ans[100010];
int s[35][100010];
void build(int v, int L, int R) {
if (L == R)
t[v] = ans[L];
else {
int M = (L + R) >> 1;
build(v + v, L, M);
build(v + v + 1, M + 1, R);
t[v] = t[v + v] & t[v + v + 1];
}
}
int get(int v, int L, int R, int l, int r) {
if (L == l && R == r) return t[v];
int M = (L + R) >> 1;
if (r <= M) return get(v + v, L, M, l, r);
if (l > M) return get(v + v + 1, M + 1, R, l, r);
return get(v + v, L, M, l, M) & get(v + v + 1, M + 1, R, M + 1, r);
}
void check() {
build(1, 1, n);
for (int i = 0; i < m; i++) {
if (get(1, 1, n, l[i], r[i]) != q[i]) {
puts("NO");
exit(0);
}
}
}
int main() {
cin >> 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)) {
s[j][l[i] - 1] += 1;
s[j][r[i]] -= 1;
}
}
}
for (int i = 0; i < 30; i++)
for (int j = 0; j < n; j++) {
s[i][j] += (j > 0 ? s[i][j - 1] : 0);
if (s[i][j] > 0) ans[j + 1] += (1 << i);
}
check();
puts("YES");
for (int i = 0; i < n; i++) cout << ans[i + 1] << " ";
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 int n, m;
static long[] st, arr, l, r, q;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
n = sc.nextInt();
m = sc.nextInt();
flage = true;
arr = new long[n];
st = new long[2 * (int) (Math.pow(2, Math.ceil(Math.log(n) / Math.log(2))))];
l = new long[m];
r = new long[m];
q = new long[m];
for (int i = 0; i < m; i++) {
l[i] = sc.nextLong() - 1;
r[i] = sc.nextLong() - 1;
q[i] = sc.nextLong();
}
for (int i = 0; i < 31; i++) {
long[]sum=new long[n];
for (int j = 0; j < m; j++) {
if ((q[j] & (1 << i)) != 0) {
sum[(int)l[j]]=Math.max(sum[(int)l[j]],(r[j]-l[j]+1));
// pw.println(maxr+" "+lessl);
}
}
long s=0;
for(int j=0;j<n;j++) {
s=Math.max(s,sum[j]);
if(s>0) {
arr[j]|=(1<<i);
s--;
}
}
}
//if(n==1916)pw.println(Arrays.toString(arr));
complete(0, n - 1, 0);
for (int i = 0; i < m; i++) {
if (check(0, n - 1, l[i], r[i], 0) != q[i]) {
pw.println("NO");
pw.close();
return;
}
}
pw.println("YES");
for (long i : arr)
pw.print(i + " ");
pw.close();
}
public static long complete(int i, int j, int node) {
if (i == j) {
return st[node] = arr[i];
}
int mid = i + j >> 1;
return st[node] = complete(i, mid, 2 * node + 1) & complete(mid + 1, j, 2 * node + 2);
}
public static long check(int l, int r, long i, long j, int node) {
if (r < i || j < l)
return (1 << 31) - 1;
if (l >= i && j >= r)
return st[node];
int mid = l + r >> 1;
return check(l, mid, i, j, 2 * node + 1) & check(mid + 1, r, i, j, 2 * node + 2);
}
static int max = (1 << 31) - 1;
static boolean flage;
public static boolean prime(int x) {
int[] arr = new int[10000000];
int index = 0;
int num = 3;
int i = 0;
arr[0] = 2;
while (num <= x) {
for (i = 0; i <= index; i++) {
if (num % arr[i] == 0) {
break;
}
}
if (i == index + 1) {
arr[++index] = num;
}
num++;
}
if (arr[index] == x)
return true;
return false;
}
public static void conquer(int[] arr, int b, int m, int e) {
int len1 = m - b + 1;
int len2 = e - m;
int[] l = new int[len1];
int[] r = new int[len2];
for (int i = 0; i < len1; i++) {
l[i] = arr[b + i];
}
for (int j = 0; j < len2; j++) {
r[j] = arr[m + 1 + j];
}
int i = 0, j = 0, k = b;
while ((i < len1) && (j < len2)) {
if (r[j] >= l[i]) {
arr[k] = l[i];
k++;
i++;
} else {
arr[k] = r[j];
k++;
j++;
}
}
while (i < len1) {
arr[k++] = l[i++];
}
while (j < len2) {
arr[k++] = r[j++];
}
}
static int mid;
public static void sort(int[] arr, int b, int e) {
if (b < e) {
int mid = (e + b) / 2;
sort(arr, b, mid);
sort(arr, mid + 1, e);
conquer(arr, b, mid, e);
}
}
public static void Sort(int[] arr) {
sort(arr, 0, arr.length - 1);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
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 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;
int n, m, ST[1 << 18], Query[100000][3], RAQ(int, int, int, int, int);
void Update(int, int, int, int, int, int);
int main() {
scanf("%d%d", &n, &m);
for (int q = 0, l, r, x; q < m; q++) {
scanf("%d%d%d", &l, &r, &x);
l--, r--;
Query[q][0] = l, Query[q][1] = r, Query[q][2] = x;
Update(0, 0, n - 1, l, r, x);
}
for (int i = 0, l, r, x; i < m; i++) {
l = Query[i][0], r = Query[i][1], x = Query[i][2];
if (RAQ(0, 0, n - 1, l, r) != x) {
puts("NO");
return 0;
}
}
puts("YES");
for (int i = 0; i < n; i++) printf("%d ", RAQ(0, 0, n - 1, i, i));
}
void Update(int pos, int rl, int rr, int ql, int qr, int v) {
if (rr < ql || qr < rl) return;
if (ql <= rl && rr <= qr) {
ST[pos] |= v;
return;
}
int mid = (rl + rr) >> 1;
Update((1 + (pos << 1)), rl, mid, ql, qr, v),
Update((2 + (pos << 1)), mid + 1, rr, ql, qr, v);
}
int RAQ(int pos, int rl, int rr, int ql, int qr) {
if (rr < ql || qr < rl) return INT_MAX;
if (ql <= rl && rr <= qr) return ST[pos];
int mid = (rl + rr) >> 1;
ST[(1 + (pos << 1))] |= ST[pos], ST[(2 + (pos << 1))] |= ST[pos];
return RAQ((1 + (pos << 1)), rl, mid, ql, qr) &
RAQ((2 + (pos << 1)), mid + 1, rr, ql, qr);
}
| 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 tree[400005] = {0}, a[100005], b[100005], q[100005];
void add(int l, int r, int ll, int rr, int data, int node) {
if (l <= ll && r >= rr)
tree[node] |= data;
else if (l > rr || r < ll)
return;
else {
add(l, r, ll, (ll + rr) / 2, data, node * 2);
add(l, r, (ll + rr) / 2 + 1, rr, data, node * 2 + 1);
}
}
void update(int node, int ll, int rr) {
if (ll != rr) {
tree[node * 2] |= tree[node];
tree[node * 2 + 1] |= tree[node];
update(node * 2, ll, (ll + rr) / 2);
update(node * 2 + 1, (ll + rr) / 2 + 1, rr);
}
}
int f(int l, int r, int ll, int rr, int node) {
if (l <= ll && r >= rr)
return tree[node];
else if (l > rr || r < ll)
return -1;
else {
int k1 = f(l, r, ll, (ll + rr) / 2, node * 2),
k2 = f(l, r, (ll + rr) / 2 + 1, rr, node * 2 + 1);
if (k1 == -1)
return k2;
else if (k2 == -1)
return k1;
else
return k1 & k2;
}
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < k; i++) {
scanf("%d%d%d", &a[i], &b[i], &q[i]);
add(a[i], b[i], 1, n, q[i], 1);
}
update(1, 1, n);
int flag = 0;
for (int i = 0; i < k; i++)
if (f(a[i], b[i], 1, n, 1) != q[i]) flag = 1;
if (flag)
printf("NO\n");
else {
printf("YES\n");
for (int i = 1; i <= n; i++)
printf("%d%c", f(i, i, 1, n, 1), 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;
int a[100005][31] = {0};
int L[100001];
int R[100001];
int Q[100001];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int z = 0; z < m; z++) {
int l, r, q;
scanf("%d %d %d", &l, &r, &q);
L[z] = l;
R[z] = r;
Q[z] = q;
for (int i = 0; i < 31; i++) {
if (q & (1 << i)) {
a[l][i]++;
a[r + 1][i]--;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 31; j++) {
a[i][j] += a[i - 1][j];
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < 31; j++) {
if (a[i][j]) a[i][j] = 1;
if (i) a[i][j] += a[i - 1][j];
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < 31; j++) {
if ((Q[i] & (1 << j)) == 0) {
if (a[R[i]][j] - a[L[i] - 1][j] == R[i] - L[i] + 1) {
cout << "NO" << endl;
return 0;
}
}
}
}
printf("YES\n");
for (int i = 1; i <= n; i++) {
int num = 0;
for (int j = 0; j < 31; j++) {
int val = a[i][j] - a[i - 1][j];
if (val) num |= (1 << j);
}
printf("%d ", num);
}
printf("\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;
int ans[1000005], tree[1000005], n, l[1000005], r[1000005], x[1000005];
int an[1000005][35];
int anded(int a, int b) {
a += n, b += n;
int s = (1 << 30) - 1;
while (a <= b) {
if (a % 2 == 1) s &= tree[a++];
if (b % 2 == 0) s &= tree[b--];
a /= 2, b /= 2;
}
return s;
}
void add(int k, int x1) {
k += n;
tree[k] += x1;
for (k /= 2; k >= 1; k /= 2) tree[k] = tree[2 * k] & tree[2 * k + 1];
}
int main() {
int m;
scanf("%d%d", &n, &m);
for (int i = 0; i < int(m); ++i) {
cin >> l[i] >> r[i] >> x[i];
int j = 0, val = x[i];
while (val) {
if (val % 2 == 1) an[l[i]][j]++, an[r[i] + 1][j]--;
val /= 2, j++;
}
}
for (int i = 0; i < int(30); ++i)
for (int j = 0; j < int(n + 1); ++j) an[j + 1][i] += an[j][i];
for (int i = 0; i < int(n); ++i) {
int t = 1;
for (int j = 0; j < int(30); ++j)
ans[i + 1] += min(an[i + 1][j], 1) * t, t *= 2;
add(i + 1, ans[i + 1]);
}
for (int i = 0; i < int(m); ++i)
if (anded(l[i], r[i]) != x[i]) return cout << "NO", 0;
cout << "YES" << endl;
for (int i = 0; i < int(n); ++i) printf("%d ", ans[i + 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 | import os
import sys
from io import BytesIO, IOBase
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")
MAX = 1000001
bitscount = 30
prefix_count = [[0]*(10**5+1) for i in range(30)]
def findPrefixCount(arr, n):
for i in range(0, bitscount):
prefix_count[i][0] = ((arr[0] >> i) & 1)
for j in range(1, n):
prefix_count[i][j] = ((arr[j] >> i) & 1)
prefix_count[i][j] += prefix_count[i][j - 1]
def rangeOr(l, r):
ans = 0
for i in range(bitscount):
x = 0
if (l == 0):
x = prefix_count[i][r]
else:
x = prefix_count[i][r] - prefix_count[i][l - 1]
# Condition for ith bit
# of answer to be set
if (x == r - l + 1):
ans = (ans | (1 << i))
return ans
n, m = map(int, input().split())
a = [[0] * n for i in range(30)]
query = []
for i in range(m):
l, r, q = map(int, input().split())
query.append([l-1, r-1, q])
c = bin(q)[2:][::-1]
b = []
for j in c:
b.append(int(j))
j = 0
while (j < len(b)):
if b[j] == 1:
a[j][l - 1] += 1
if r != n:
a[j][r] -= 1
j += 1
for i in range(30):
j = 1
while (j < n):
a[i][j] += a[i][j - 1]
j += 1
j = 0
while (j < n):
if a[i][j] > 0:
a[i][j] = 1
j += 1
res=[]
for i in range(n):
s = ""
j=29
while(j>=0):
s += str(a[j][i])
j+=-1
res.append(int(s,2))
findPrefixCount(res, n)
f=0
for j in query:
if rangeOr(j[0],j[1])!=j[2]:
f=1
break
if f==1:
print("NO")
else:
print("YES")
print(*res)
| 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 | // practice with rainboy
import java.io.*;
import java.util.*;
public class CF482B extends PrintWriter {
CF482B() { super(System.out, true); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF482B o = new CF482B(); o.main(); o.flush();
}
int[] tt;
int n;
void update(int l, int r, int q) {
for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
tt[l++] |= q;
if ((r & 1) == 0)
tt[r--] |= q;
}
}
void sweep() {
for (int i = 1; i < n; i++) {
tt[i << 1] |= tt[i];
tt[i << 1 | 1] |= tt[i];
}
for (int i = n - 1; i >= 1; i--)
tt[i] = tt[i << 1] & tt[i << 1 | 1];
}
int query(int l, int r) {
int x = 0x7fffffff;
for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
x &= tt[l++];
if ((r & 1) == 0)
x &= tt[r--];
}
return x;
}
void main() {
n = sc.nextInt();
int m = sc.nextInt();
tt = new int[n + n];
int[] ll = new int[m];
int[] rr = new int[m];
int[] qq = new int[m];
for (int h = 0; h < m; h++) {
ll[h] = sc.nextInt() - 1;
rr[h] = sc.nextInt() - 1;
qq[h] = sc.nextInt();
update(ll[h], rr[h], qq[h]);
}
sweep();
for (int h = 0; h < m; h++)
if (query(ll[h], rr[h]) != qq[h]) {
println("NO");
return;
}
println("YES");
for (int i = n; i < n + n; i++)
print(tt[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;
const int N = 1e5 + 100;
int a[N], b[N];
struct node {
int a, b, c;
};
vector<node> v;
int T[4 * N];
void inti(int n, int b, int e) {
if (b == e) {
T[n] = a[b];
return;
}
inti(n * 2, b, (b + e) / 2);
inti(n * 2 + 1, (b + e) / 2 + 1, e);
T[n] = T[n * 2] & T[n * 2 + 1];
}
int query1(int n, int b, int e, int i, int j) {
if (b > j || e < i) return 2147483647;
if (b >= i && e <= j) return T[n];
return query1(n * 2, b, (b + e) / 2, i, j) &
query1(n * 2 + 1, (b + e) / 2 + 1, e, i, j);
}
int main() {
int n, q;
cin >> n >> q;
while (q--) {
node nd;
scanf("%d%d%d", &nd.a, &nd.b, &nd.c);
v.push_back(nd);
}
for (int i = 0; i <= 30; i++) {
memset(b, 0, sizeof b);
for (int j = 0; j < v.size(); j++) {
int val = v[j].c;
if (val & (1 << i)) {
b[v[j].a]++;
b[v[j].b + 1]--;
}
}
for (int j = 1; j <= n; j++) {
b[j] += b[j - 1];
if (b[j]) a[j] |= (1 << i);
}
}
memset(T, 0, sizeof T);
inti(1, 1, n);
for (int i = 0; i < v.size(); i++) {
if (query1(1, 1, n, v[i].a, v[i].b) != v[i].c) {
puts("NO");
return 0;
}
}
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 | import java.io.*;
import java.util.*;
public class Main implements Runnable
{
private class Query {
private int id;
private int l;
private int r;
private int q;
Query(int l,int r,int q,int id)
{
this.l=l;
this.r=r;
this.q=q;
this.id=id;
}
}
public static void main(String args[])
{
new Main().run();
}
public void run()
{
try {
solve();
} catch (IOException e) {
e.printStackTrace();
}
out.flush();
}
private void solve() throws IOException
{
int n=in.nextInt();
int m=in.nextInt();
int ds=getds(n);
int v[]=new int[2*ds];
// Query q[]=new Query[m];
int l[]=new int[m];
int r[]=new int[m];
int q[]=new int[m];
for(int i=0;i<m;i++)
{
l[i]=in.nextInt()-1;
r[i]=in.nextInt()-1;
q[i]=in.nextInt();
}
for(int i=0;i<m;i++)
inc(l[i],r[i],q[i], v, ds);
push(v,ds);
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=v[i+ds];
boolean ok=true;
for(int i=ds-1;i>0;i--)
v[i]=v[2*i]&v[2*i+1];
for(int i=0;i<m;i++)
ok&=(accumulate(l[i],r[i],v,ds)==q[i]);
if(ok)
{
out.println("YES");
for(int i:arr)
{
out.print(i);
out.print(' ');
}
out.println();
}
else
out.println("NO");
}
private void push(int [] v, int ds)
{
for(int i=1;i<ds;i++)
{
v[2*i]|=v[i];
v[2*i+1]|=v[i];
}
}
private void inc(int l, int r, int val, int[] v, int ds)
{
l+=ds;
r+=ds;
while(l<=r)
{
if((l&1)==1) v[l++]|=val;
if((r&1)==0) v[r--]|=val;
l>>=1;
r>>=1;
}
}
private int accumulate(int l, int r, int[] v, int ds) {
l+=ds;
r+=ds;
int res=(1<<30)-1;
while(l<=r)
{
if((l&1)==1) res=res&v[l++];
if((r&1)==0) res=res&v[r--];
l>>=1;
r>>=1;
}
return res;
}
private int getds(int n) {
n--;
n|=n>>1;
n|=n>>2;
n|=n>>4;
n|=n>>8;
n|=n>>16;
return n+1;
}
private boolean testbit(int q, int i) {
return ((q>>i)&1)==1;
}
class Scanner
{
BufferedReader in;
StringTokenizer token;
String delim;
Scanner(Reader reader)
{
in=new BufferedReader(reader);
token=new StringTokenizer("");
this.delim=" \n";
}
Scanner(Reader reader, String delimeters)
{
in=new BufferedReader(reader);
token=new StringTokenizer("");
this.delim=delimeters;
}
public boolean hasNext() throws IOException
{
while(!token.hasMoreTokens())
{
String line=in.readLine();
if(line==null) return false;
token=new StringTokenizer(line);
}
return true;
}
String next() throws IOException
{
return hasNext()?token.nextToken():null;
}
int nextInt() throws IOException
{
return Integer.parseInt(next());
}
long nextLong() throws IOException
{
return Long.parseLong(next());
}
double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
int[] nextInts(int n) throws IOException
{
int[] res=new int[n];
for(int i=0;i<n;i++)
res[i]=nextInt();
return res;
}
}
private Scanner in=new Scanner(new InputStreamReader(System.in),"\n\r\t ");
private PrintWriter out=new PrintWriter(new BufferedWriter(
new OutputStreamWriter(System.out)));
} | 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 N = 1e5 + 10, MOD = 1e9 + 7, INF = 1e9 + 10, SQ = 550, LOG = 25;
long long n, m, x1, x2, d, z, t, y2, a[N], dp[N << 2], lazy[N << 2], mini, ptr,
x, y, ptr1, ptr2, ans;
pair<pair<long long, long long>, long long> pr[N];
void shift(int pos, int lch, int rch) {
dp[lch] |= lazy[pos];
dp[rch] |= lazy[pos];
lazy[lch] |= lazy[pos];
lazy[rch] |= lazy[pos];
lazy[pos] = 0;
}
void upd(int b, int e, int pos, int l, int r, int val) {
int mid = e + b >> 1, lch = pos << 1, rch = lch + 1;
if (b >= r || e <= l) return;
if (l <= b && e <= r) {
dp[pos] |= val;
lazy[pos] |= val;
return;
}
shift(pos, lch, rch);
upd(b, mid, lch, l, r, val);
upd(mid, e, rch, l, r, val);
}
void gogo(int b, int e, int pos) {
int mid = e + b >> 1, lch = pos << 1, rch = lch + 1;
if (e - b == 1) {
a[b] = dp[pos];
return;
}
shift(pos, lch, rch);
gogo(b, mid, lch);
gogo(mid, e, rch);
}
void bld(int b, int e, int pos) {
int mid = e + b >> 1, lch = pos << 1, rch = lch + 1;
if (e - b == 1) {
dp[pos] = a[b];
return;
}
bld(b, mid, lch);
bld(mid, e, rch);
dp[pos] = dp[lch] & dp[rch];
}
long long give_and(int b, int e, int pos, int l, int r) {
int mid = e + b >> 1, lch = pos << 1, rch = lch + 1;
if (b >= r || e <= l) return d;
if (l <= b && e <= r) return dp[pos];
return give_and(b, mid, lch, l, r) & give_and(mid, e, rch, l, r);
}
int main() {
d = (1 << 30) - 1;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> pr[i].first.first >> pr[i].first.second >> pr[i].second;
pr[i].first.first--;
upd(0, n, 1, pr[i].first.first, pr[i].first.second, pr[i].second);
}
gogo(0, n, 1);
memset(dp, sizeof(dp), 0);
bld(0, n, 1);
for (int i = 0; i < m; i++) {
if (give_and(0, n, 1, pr[i].first.first, pr[i].first.second) !=
pr[i].second)
return cout << "NO", 0;
}
cout << "YES\n";
for (int i = 0; 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 |
// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;
public class Main {
static int [] num,seg;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// (new FileReader("input.in"));
StringBuilder out = new StringBuilder();
StringTokenizer tk;
tk = new StringTokenizer(in.readLine());
int n = parseInt(tk.nextToken()),m = parseInt(tk.nextToken());
num = new int[n];
seg = new int[4*n+1];
int [][] dp = new int[31][n];
int l,r,q;
query [] qq = new query[m];
for(int i=0; i<m; i++) {
tk = new StringTokenizer(in.readLine());
l = parseInt(tk.nextToken())-1;
r = parseInt(tk.nextToken())-1;
q = parseInt(tk.nextToken());
qq[i] = new query(l,r,q);
for(int j=0; j<31; j++) {
if((q & (1<<j))!=0) {
dp[j][l]++;
if(r+1 < n) dp[j][r+1]--;
}
}
}
for(int j=0; j<31; j++)
for(int i=1; i<n; i++)
dp[j][i] += dp[j][i-1];
for(int i=0; i<n; i++) {
for(int j=0; j<31; j++)
if(dp[j][i] > 0) num[i] |= (1<<j);
}
build(1,0,n-1);
for(int i=0; i<m; i++) {
if(get(1,0,n-1,qq[i].l,qq[i].r) != qq[i].q) {
//System.out.println(qq[i].l+" , "+qq[i].r);
//System.out.println(get(1,0,n-1,qq[i].l,qq[i].r)+" "+qq[i].q);
System.out.println("NO");
return;
}
}
out.append("YES\n").append(num[0]);
for(int i=1; i<n; i++)
out.append(" ").append(num[i]);
System.out.println(out);
}
static void build(int p,int s,int e) {
if(s==e) {
seg[p] = num[s];
return;
}
build(2*p,s,(s+e)/2);
build(2*p+1,(s+e)/2+1,e);
seg[p] = seg[2*p] & seg[2*p+1];
}
static int get(int p,int s,int e,int a,int b) {
if(s>=a && e<=b)
return seg[p];
if(s>b || e<a)
return (int)((1l<<31l)-1);
return get(2*p,s,(s+e)/2,a,b) & get(2*p+1,(s+e)/2+1,e,a,b);
}
}
class query {
int l,r,q;
public query(int l,int r,int q) {
this.l = l;
this.r = r;
this.q = q;
}
} | 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.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
public class a {
public static void main(String[] Args) throws Exception {
FS sc = new FS(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = sc.nextInt();
int m = sc.nextInt();
int[] ls = new int[m];
int[] rs = new int[m];
int[] qs = new int[m];
for (int i = 0; i < m; i++){
ls[i] = sc.nextInt() - 1;
rs[i] = sc.nextInt() - 1;
qs[i] = sc.nextInt();
}
int[] ans = new int[n];
int[] tmp = new int[n];
int[] sum = new int[n + 1];
for (int k = 0; k < 30; k++) {
for (int i = 0; i < n; i++)
sum[i] = 0;
sum[n] = 0;
for (int i = 0; i < m; i++) {
if ((qs[i] & (1<<k)) != 0) {
sum[ls[i]]++;
sum[rs[i] + 1]--;
}
}
int cc = sum[0];
for (int i = 0; i < n; i++) {
tmp[i] = (cc == 0) ? 0 : 1;
cc += sum[i + 1];
}
sum[0] = 0;
for (int i = 0; i < n; i++) {
sum[i + 1] = sum[i] + tmp[i];
if (tmp[i] == 1)
ans[i] |= (1<<k);
}
for (int i = 0; i < m; i++){
if ((qs[i] & (1<<k))==0 && sum[rs[i] + 1] - sum[ls[i]] == rs[i] + 1 - ls[i]) {
out.println("NO");
out.close();
return;
}
}
}
out.println("YES");
for (int i = 0; i < n; i++){
out.print(ans[i] + " ");
}
out.close();
}
public static class Pair implements Comparable<Pair> {
int ind, type;
Pair(int a, int b) {
ind = a;
type = b;
}
public int compareTo(Pair o) {
if (ind == o.ind)
return o.type - type;
return ind - o.ind;
}
}
public static class FS {
BufferedReader br;
StringTokenizer st;
FS(InputStream in) throws Exception {
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer(br.readLine());
}
FS(File f) throws Exception {
br = new BufferedReader(new FileReader(f));
st = new StringTokenizer(br.readLine());
}
String next() throws Exception {
if (st.hasMoreTokens())
return st.nextToken();
st = new StringTokenizer(br.readLine());
return next();
}
int nextInt() throws Exception {
return Integer.parseInt(next());
}
long nextLong() throws Exception {
return Long.parseLong(next());
}
double nextDouble() throws Exception {
return Double.parseDouble(next());
}
}
}
/*
*
* 19 2 2 2 2 2 2 2 2 1 1 1 2 1 1 2 1 1 1 2
*
*
*/ | 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[200005], tree[400005];
void build(int node, int l, int r) {
if (l == r) {
tree[node] = a[l - 1];
return;
}
int mid = (l + r) >> 1;
build(node * 2, l, mid);
build(node * 2 + 1, mid + 1, r);
tree[node] = tree[node * 2] & tree[node * 2 + 1];
}
int query(int node, int l, int r, int x, int y) {
if (l > y || r < x) return (1 << 30) - 1;
if (l >= x && r <= y) return tree[node];
int mid = (l + r) >> 1;
int left = query(node * 2, l, mid, x, y);
int right = query(node * 2 + 1, mid + 1, r, x, y);
return left & right;
}
int main() {
int n, m;
cin >> n >> m;
int l[m], r[m], q[m], sum[n + 1];
for (int i = 0; i < m; i++) cin >> l[i] >> r[i] >> q[i];
for (int bit = 0; bit <= 30; 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] - 1]++;
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, 1, n);
for (int i = 0; i < m; i++) {
if (query(1, 1, n, l[i], r[i]) != q[i]) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
for (int i = 0; i < n; 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 | #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000 + 10;
struct Node {
int l, r;
int val;
} t[MAX_N * 4];
vector<int> ans;
int a[MAX_N], b[MAX_N], c[MAX_N], n, m;
void build(int o, int l, int r) {
t[o].l = l;
t[o].r = r;
t[o].val = 0;
if (l == r) {
return;
}
int mid = (l + r) >> 1;
build(o << 1, l, mid);
build(o << 1 | 1, mid + 1, r);
}
void update(int o, int l, int r, int x) {
if (t[o].l == l && t[o].r == r) {
t[o].val |= x;
return;
}
int mid = (t[o].l + t[o].r) >> 1;
if (r <= mid)
update(o << 1, l, r, x);
else if (l > mid)
update(o << 1 | 1, l, r, x);
else {
update(o << 1, l, mid, x);
update(o << 1 | 1, mid + 1, r, x);
}
}
int query(int o, int l, int r) {
if (t[o].l == l && t[o].r == r) {
return t[o].val;
}
int mid = (t[o].l + t[o].r) >> 1;
if (r <= mid)
return query(o << 1, l, r);
else if (l > mid)
return query(o << 1 | 1, l, r);
else
return query(o << 1, l, mid) & query(o << 1 | 1, mid + 1, r);
}
void solve(int o) {
if (o != 1) {
t[o].val |= t[o >> 1].val;
}
if (t[o].l == t[o].r) {
ans.push_back(t[o].val);
return;
}
solve(o << 1);
solve(o << 1 | 1);
}
int main() {
scanf("%d%d", &n, &m);
build(1, 1, n);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a[i], &b[i], &c[i]);
update(1, a[i], b[i], c[i]);
}
bool ok = true;
for (int i = 0; i < m; i++) {
if (query(1, a[i], b[i]) != c[i]) {
ok = false;
break;
}
}
if (ok) {
solve(1);
printf("YES\n");
for (int i = 0; i < (int)ans.size() - 1; i++) {
printf("%d ", ans[i]);
}
printf("%d\n", ans[(int)ans.size() - 1]);
} 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;
struct node {
int l, r, val;
} a[1000000 + 10];
int num[1000000 + 10], laze[1000000 + 10];
void pushup(int l, int r, int rt) { num[rt] = num[rt * 2] & num[rt * 2 + 1]; }
void pushdown(int l, int r, int rt) {
if (laze[rt]) {
num[rt * 2] |= laze[rt];
num[rt * 2 + 1] |= laze[rt];
laze[rt * 2] |= laze[rt];
laze[rt * 2 + 1] |= laze[rt];
laze[rt] = 0;
}
}
void build(int l, int r, int rt) {
if (l == r) {
return;
}
int mid = (l + r) / 2;
build(l, mid, rt * 2);
build(mid + 1, r, rt * 2 + 1);
pushup(l, r, rt);
}
void updata(int l, int r, int ql, int qr, int val, int rt) {
if (ql <= l && qr >= r) {
num[rt] |= val;
laze[rt] |= val;
return;
}
pushdown(l, r, rt);
int mid = (l + r) / 2;
if (ql <= mid) updata(l, mid, ql, qr, val, rt * 2);
if (qr > mid) updata(mid + 1, r, ql, qr, val, rt * 2 + 1);
pushup(l, r, rt);
}
int qry(int l, int r, int ql, int qr, int rt) {
if (ql <= l && qr >= r) {
return num[rt];
}
pushdown(l, r, rt);
int mid = (l + r) / 2;
if (ql <= mid && qr > mid)
return qry(l, mid, ql, qr, rt * 2) & qry(mid + 1, r, ql, qr, rt * 2 + 1);
else if (qr > mid)
return qry(mid + 1, r, ql, qr, rt * 2 + 1);
else if (ql <= mid)
return qry(l, mid, ql, qr, rt * 2);
pushup(l, r, rt);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
build(1, n, 1);
for (int i = (0); i < (q); i++) {
cin >> a[i].l >> a[i].r >> a[i].val;
updata(1, n, a[i].l, a[i].r, a[i].val, 1);
}
int flag = 0;
for (int i = (0); i < (q); i++) {
if (a[i].val != qry(1, n, a[i].l, a[i].r, 1)) flag = 1;
}
if (flag) {
cout << "NO";
} else {
cout << "YES"
<< "\n";
for (int i = (1); i < (n + 1); i++) {
cout << qry(1, n, i, i, 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;
long long dp[100005][35];
long long a[100005];
long long segtree[400005];
void build(long long node, long long start, long long end) {
if (start == end) {
segtree[node] = a[start];
return;
}
long long mid = (start + end) / 2;
build(2 * node + 1, start, mid);
build(2 * node + 2, mid + 1, end);
segtree[node] = segtree[2 * node + 1] & segtree[2 * node + 2];
}
long long query(long long node, long long start, long long end, long long l,
long long r) {
if (start > r || end < l) return -1;
if (l <= start && end <= r) {
return segtree[node];
}
long long mid = (start + end) / 2;
long long p1 = query(2 * node + 1, start, mid, l, r);
long long p2 = query(2 * node + 2, mid + 1, end, l, r);
if (p1 == -1) return p2;
if (p2 == -1) return p1;
return p1 & p2;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
pair<pair<long long, long long>, long long> p[m];
for (long long i = 0; i < m; i++) {
cin >> p[i].first.first >> p[i].first.second >> p[i].second;
p[i].first.first--;
p[i].first.second--;
long long temp = p[i].second;
long long idx = 0;
while (temp > 0) {
if (temp % 2 == 1) {
dp[p[i].first.first][idx] += 1;
dp[p[i].first.second + 1][idx] -= 1;
}
temp = temp / 2;
idx++;
}
}
for (long long j = 0; j < 35; j++) {
for (long long i = 1; i < n; i++) {
dp[i][j] = dp[i][j] + dp[i - 1][j];
}
}
long long pow[35];
pow[0] = 1;
for (long long i = 1; i < 35; i++) {
pow[i] = 2 * pow[i - 1];
}
for (long long i = 0; i < n; i++) {
long long temp = 0;
for (long long j = 0; j < 35; j++) {
if (dp[i][j] >= 1) {
temp += pow[j];
}
}
a[i] = temp;
}
build(0, 0, n - 1);
for (long long i = 0; i < m; i++) {
if (query(0, 0, n - 1, p[i].first.first, p[i].first.second) !=
p[i].second) {
cout << "NO";
return 0;
}
}
cout << "YES" << endl;
for (long long i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}
long long choose(long long n, long long k) {
if (k == 0) return 1;
return (n * choose(n - 1, k - 1)) / k;
}
bool isprime(long long n) {
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
void setprime(bool isprime[], long long n) {
for (long long i = 0; i < n; i++) isprime[i] = true;
isprime[0] = false;
isprime[1] = false;
for (long long i = 2; i < n; i++) {
for (long long j = 2; i * j < n; j++) isprime[i * j] = false;
}
}
| 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 treeor[400000];
int treeand[400000];
int a[100001];
void modifyor(int v, int tl, int tr, int l, int r, int val) {
if (l == tl && r == tr)
treeor[v] |= val;
else {
int x = (tl + tr) / 2;
if (l <= x) modifyor(v * 2, tl, x, l, min(x, r), val);
if (r >= x + 1) modifyor(v * 2 + 1, x + 1, tr, max(l, x + 1), r, val);
}
}
int get(int v, int tl, int tr, int num) {
if (tl == tr)
return treeor[v];
else {
int x = (tl + tr) / 2;
if (num <= x) {
treeor[v * 2] |= treeor[v];
return get(v * 2, tl, x, num);
} else {
treeor[v * 2 + 1] |= treeor[v];
return get(v * 2 + 1, x + 1, tr, num);
}
}
}
void init(int v, int tl, int tr) {
if (tl == tr)
treeand[v] = a[tl];
else {
int x = (tl + tr) / 2;
init(v * 2, tl, x);
init(v * 2 + 1, x + 1, tr);
treeand[v] = treeand[v * 2] & treeand[v * 2 + 1];
}
}
int getand(int v, int tl, int tr, int l, int r) {
if (tl == l && tr == r)
return treeand[v];
else {
int x = (tl + tr) / 2;
if (l <= x && r >= x + 1) {
int sonl, sonr;
sonl = getand(v * 2, tl, x, l, x);
sonr = getand(v * 2 + 1, x + 1, tr, x + 1, r);
return sonl & sonr;
} else if (l <= x)
return getand(v * 2, tl, x, l, r);
else
return getand(v * 2 + 1, x + 1, tr, l, r);
}
}
struct Query {
int l, r, q;
} q[100000];
int main() {
int n, m;
scanf("%d%d", &n, &m);
memset(treeor, 0, 1600000);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].q);
modifyor(1, 1, n, q[i].l, q[i].r, q[i].q);
}
for (int i = 1; i <= n; i++) a[i] = get(1, 1, n, i);
init(1, 1, n);
bool corr = 1;
for (int i = 0; i < m; i++)
if (getand(1, 1, n, q[i].l, q[i].r) != q[i].q) {
corr = 0;
break;
}
if (corr) {
printf("YES\n");
for (int 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 | #include <bits/stdc++.h>
using namespace std;
int value[101000 * 4], f[101000], l[101000], r[101000], q[101000];
void insert(int p, int L, int R, int l, int r, int q) {
if (L == l && R == r) {
value[p] |= q;
return;
}
int mid = (L + R) >> 1;
if (r <= mid)
insert(p << 1, L, mid, l, r, q);
else if (l > mid)
insert(p * 2 + 1, mid + 1, R, l, r, q);
else {
insert(p << 1, L, mid, l, mid, q);
insert(p * 2 + 1, mid + 1, R, mid + 1, r, q);
}
}
int find(int p, int L, int R, int l, int r) {
if (L == l && R == r) return value[p];
int mid = (L + R) >> 1;
if (r <= mid)
return find(p << 1, L, mid, l, r) | value[p];
else if (l > mid)
return find(p * 2 + 1, mid + 1, R, l, r) | value[p];
else
return (find(p << 1, L, mid, l, mid) &
find(p * 2 + 1, mid + 1, R, mid + 1, r)) |
value[p];
}
int get(int p, int L, int R, int l, int r) {
if (L == l && R == r) return value[p];
int mid = (L + R) >> 1;
if (r <= mid)
return get(p << 1, L, mid, l, r) | value[p];
else
return get(p * 2 + 1, mid + 1, R, l, r) | value[p];
}
inline void read(int &x) {
x = 0;
char ch;
while (ch = getchar(), ch > '9' || ch < '0')
;
while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
}
int main() {
int n, m;
read(n), read(m);
for (int i = 1; i <= m; i++) {
read(l[i]), read(r[i]), read(q[i]);
insert(1, 1, n, l[i], r[i], q[i]);
}
int i;
for (i = 1; i <= m; i++)
if (find(1, 1, n, l[i], r[i]) != q[i]) break;
if (i <= m)
printf("NO\n");
else {
printf("YES\n");
printf("%d", get(1, 1, n, 1, 1));
for (int i = 2; i <= n; i++) printf(" %d", get(1, 1, n, i, 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;
const int maxn = 100005;
int a[maxn << 2], n, m;
int OR[maxn << 2];
struct IA {
int l, r, q;
} ia[maxn];
void push_up(int rt) { a[rt] = a[rt << 1] & a[rt << 1 | 1]; }
void push_down(int rt) {
if (OR[rt]) {
a[rt << 1] |= OR[rt];
a[rt << 1 | 1] |= OR[rt];
OR[rt << 1] |= OR[rt];
OR[rt << 1 | 1] |= OR[rt];
OR[rt] = 0;
}
}
void update(int L, int R, int C, int l, int r, int rt) {
if (L <= l && r <= R) {
OR[rt] |= C;
a[rt] |= C;
return;
}
push_down(rt);
int m = (l + r) / 2;
if (L <= m) update(L, R, C, l, m, rt << 1);
if (R > m) update(L, R, C, m + 1, r, rt << 1 | 1);
push_up(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
return a[rt];
}
int m = (l + r) / 2;
push_down(rt);
int part1 = -1, part2 = -1;
if (L <= m) part1 = query(L, R, l, m, rt << 1);
if (R > m) part2 = query(L, R, m + 1, r, rt << 1 | 1);
if (part1 == -1) return part2;
if (part2 == -1) return part1;
return part1 & part2;
}
vector<int> ans;
void showit(int l, int r, int rt) {
if (l == r) {
ans.push_back(a[rt]);
return;
}
int m = (l + r) / 2;
push_down(rt);
showit(l, m, rt << 1);
showit(m + 1, r, rt << 1 | 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &ia[i].l, &ia[i].r, &ia[i].q);
update(ia[i].l, ia[i].r, ia[i].q, 1, n, 1);
}
bool flag = true;
for (int i = 0; i < m && flag; i++) {
int Q = query(ia[i].l, ia[i].r, 1, n, 1);
if (Q != ia[i].q) flag = false;
}
if (!flag)
puts("NO");
else {
puts("YES");
ans.clear();
showit(1, n, 1);
for (int i = 0, sz = ans.size(); i < sz; i++) printf("%d ", ans[i]);
putchar(10);
}
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.