Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class InterestingArray { public static int tree[]; public static int lazy[]; public static void create(int s, int e, int p, int l, int r, int val) { if (lazy[p] != 0) { tree[p] |= lazy[p]; if (s != e) { lazy[p * 2] |= lazy[p]; lazy[p * 2 + 1] |= lazy[p]; } lazy[p] = 0; } if (s > r || e < l) return; if (s >= l && e <= r) { tree[p] |= val; if (s != e) { lazy[p * 2] |= val; lazy[p * 2 + 1] |= val; } return; } int mid = (s + e) / 2; create(s, mid, p * 2, l, r, val); create(mid + 1, e, p * 2 + 1, l, r, val); tree[p] = tree[p * 2] & tree[p * 2 + 1]; } public static long get(int s, int e, int p, int l, int r) { if (lazy[p] != 0) { tree[p] |= lazy[p]; if (s != e) { lazy[p * 2] |= lazy[p]; lazy[p * 2 + 1] |= lazy[p]; } lazy[p] = 0; } if (s > r || e < l) return (1L << 31)-1; if (s >= l && e <= r) { //System.out.println(s + " " + e + " " + tree[p]); return tree[p]; } int mid = (s + e) / 2; long a = get(s, mid, p * 2, l, r); long b = get(mid + 1, e, p * 2 + 1, l, r); return (a & b); } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringBuilder qq = new StringBuilder(); String y[] = in.readLine().split(" "); int n = Integer.parseInt(y[0]); int m = Integer.parseInt(y[1]); tree = new int[n * 4]; lazy = new int[n * 4]; int q[][] = new int[m][3]; for (int i = 0; i < m; i++) { y = in.readLine().split(" "); int l = Integer.parseInt(y[0]); int r = Integer.parseInt(y[1]); int val = Integer.parseInt(y[2]); q[i][0] = l; q[i][1] = r; q[i][2] = val; create(1, n, 1, l, r, val); //System.out.println(Arrays.toString(tree)); //System.out.println(Arrays.toString(lazy)); } for (int i = 0; i < m; i++) { if (get(1, n, 1, q[i][0], q[i][1]) != q[i][2]) { // System.out.println(get(1, n, 1, q[i][0], q[i][1])); System.out.println("NO"); return; } } qq.append("YES\n"); for (int i = 1; i <= n; i++) { qq.append(get(1, n, 1, i, i)+" "); } qq.append("\n"); System.out.print(qq); } }
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.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class InterestingArray { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int N = (int) Math.pow(2, Math.ceil(Math.log(n)/Math.log(2))); int m = sc.nextInt(); int a[] = new int[N+1]; SegmentTree tree = new SegmentTree(a,false); int l[] = new int[m]; int r[] = new int[m]; int q[] = new int[m]; for(int i=0; i<m; i++) { l[i] = sc.nextInt(); r[i] = sc.nextInt(); q[i] = sc.nextInt(); tree.update_range(l[i], r[i], q[i]); // } for(int i=1; i<=N; i++) tree.query(i, i); int a2[] = new int[N+1]; Arrays.fill(a2, -1); for(int i=1; i<=n; i++) a2[i] = tree.query(i, i); // System.out.println(Arrays.toString(a2)); SegmentTree tree2 = new SegmentTree(a2,true); // System.out.println(Arrays.toString(tree2.sTree)); // System.out.println(tree2.query2(1, 4)); // System.out.println(Arrays.toString(tree2.sTree)); for(int i=0; i<m; i++) { int res = tree2.query2(l[i], r[i]); if(res != q[i]) { // System.out.println(res+" "+q[i]); System.out.println("NO"); return; } } PrintWriter out = new PrintWriter(System.out); out.println("YES"); for(int i=1; i<=n; i++) { out.print(tree.query(i, i)+" "); } out.flush(); } static class SegmentTree { // 1-based DS, OOP int N; //the number of elements in the array as a multipls of 2 (i.e. after padding) int[] array, sTree, lazy; SegmentTree(int[] in,boolean and) { 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 // if(and) // Arrays.fill(sTree, -1); lazy = new int[N<<1]; if(and) Arrays.fill(lazy, -1); if(and) build2(1,1,N); else build(1,1,N); } void build(int node, int b, int e) // O(n) { if(b == e) sTree[node] = array[b]; else { build(node<<1,b,(b+e)/2); build((node<<1)+1,(b+e)/2+1,e); sTree[node] = sTree[node<<1] | sTree[(node<<1)+1]; } } void build2(int node, int b, int e) // O(n) { if(b == e) sTree[node] = array[b]; else { build2(node<<1,b,(b+e)/2); build2((node<<1)+1,(b+e)/2+1,e); sTree[node] = sTree[node<<1] & sTree[(node<<1)+1]; } } void update_point(int index, int val) // O(log n) { index += N - 1; sTree[index] += val; while(index>1) { index >>= 1; sTree[index] = sTree[index<<1] + sTree[(index<<1) + 1]; } } void update_range(int i, int j, int val) // O(log n) { update_range(1,1,N,i,j,val); } void update_range2(int i, int j, int val) // O(log n) { update_range2(1,1,N,i,j,val); } void update_range(int node, int b, int e, int i, int j, int val) { if(i > e || j < b) return; if(b >= i && e <= j) { sTree[node] |= val; lazy[node] |= val; } else { propagate(node, b, e); update_range(node<<1,b,(b+e)/2,i,j,val); update_range((node<<1)+1,(b+e)/2+1,e,i,j,val); sTree[node] = sTree[node<<1] | sTree[(node<<1)+1]; } } void update_range2(int node, int b, int e, int i, int j, int val) { if(i > e || j < b) return; if(b >= i && e <= j) { sTree[node] &= val; lazy[node] &= val; } else { propagate2(node, b, e); update_range2(node<<1,b,(b+e)/2,i,j,val); update_range2((node<<1)+1,(b+e)/2+1,e,i,j,val); sTree[node] = sTree[node<<1] & sTree[(node<<1)+1]; } } void propagate(int node, int b, int e) { int mid = (b+e)/2; lazy[node<<1] |= lazy[node]; lazy[(node<<1)+1] |= lazy[node]; sTree[node<<1] |= lazy[node]; sTree[(node<<1)+1] |= lazy[node]; lazy[node] = 0; } void propagate2(int node, int b, int e) { lazy[node<<1] &= lazy[node]; lazy[(node<<1)+1] &= lazy[node]; sTree[node<<1] &= lazy[node]; sTree[(node<<1)+1] &= lazy[node]; lazy[node] = -1; } int query(int i, int j) { return query(1,1,N,i,j); } int query2(int i, int j) { return query2(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 0; if(b>= i && e <= j) return sTree[node]; propagate(node, b, e); return query(node<<1,b,(b+e)/2,i,j) | query((node<<1)+1,(b+e)/2+1,e,i,j); } int query2(int node, int b, int e, int i, int j) // O(log n) { if(i>e || j <b) return -1; if(b>= i && e <= j) return sTree[node]; return query2(node<<1,b,(b+e)/2,i,j) & query2((node<<1)+1,(b+e)/2+1,e,i,j); } } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(FileReader f) { br = new BufferedReader(f); } public boolean ready() throws IOException { return br.ready(); } Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, 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; int t, n, m, q, k; int ql[111111], qr[111111], qq[111111], sm[111111], ans[111111]; int T[111111 << 3]; void bt(int x, int l, int r) { if (l == r) { T[x] = ans[l]; return; } int md = (l + r) >> 1; bt((x << 1), l, md); bt(((x << 1) | 1), md + 1, r); T[x] = T[(x << 1)] & T[((x << 1) | 1)]; } int qry(int x, int l, int r, int L, int R) { if (L <= l && r <= R) return T[x]; int md = (l + r) >> 1; if (R <= md) return qry((x << 1), l, md, L, R); else if (L > md) return qry(((x << 1) | 1), md + 1, r, L, R); else return (qry((x << 1), l, md, L, md) & qry(((x << 1) | 1), md + 1, r, md + 1, R)); } int main() { while (~scanf("%d%d", &n, &m)) { memset(ans, 0, sizeof(ans)); bool ok = 1; for (int i = (1); i <= (m); ++i) scanf("%d%d%d", &ql[i], &qr[i], &qq[i]); for (int p = (0); p <= (30); ++p) { memset(sm, 0, sizeof(sm)); for (int i = (1); i <= (m); ++i) { if ((qq[i] & (1 << p))) { ++sm[ql[i]], --sm[qr[i] + 1]; } } int cursm = 0; for (int i = (1); i <= (n); ++i) { cursm += sm[i]; if (cursm > 0) ans[i] |= (1 << p); } } bt(1, 1, n); for (int i = (1); i <= (m); ++i) { int aa = qry(1, 1, n, ql[i], qr[i]); if (aa != qq[i]) { ok = 0; puts("NO"); break; } } if (ok) { puts("YES"); for (int i = 1; i <= n; ++i) printf("%d%c", ans[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; const int bits = 30; struct rest { int l, r, q; }; int n, m, i; int ans[100011]; rest R[100011]; int sum[100011]; int cnt[100011]; void solve(int bit) { int i; memset(sum, 0, sizeof(sum)); for (i = 1; i <= m; i++) { if ((R[i].q >> bit) & 1) { sum[R[i].l]++; sum[R[i].r + 1]--; } } for (i = 1; i <= n; i++) { sum[i] += sum[i - 1]; if (sum[i]) ans[i] |= 1 << bit; cnt[i] = cnt[i - 1] + (sum[i] == 0 ? 1 : 0); } for (i = 1; i <= m; i++) { if ((R[i].q >> bit) & 1) continue; int act = cnt[R[i].r] - cnt[R[i].l - 1]; if (act == 0) { printf("NO"); exit(0); ; } } } int main() { scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) scanf("%d%d%d", &R[i].l, &R[i].r, &R[i].q); for (int bit = 0; bit < bits; bit++) solve(bit); printf("YES\n"); for (i = 1; i <= n; i++) printf("%d ", 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 main() { ios::sync_with_stdio(0); int n, m; cin >> n >> m; vector<vector<int> > A(n + 2, vector<int>(32)); vector<int> q(m), l(m), r(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] >> j) & 1) { A[l[i]][j]++; A[r[i] + 1][j]--; } } } vector<int> a(n); for (int i = 1; i <= n; i++) { for (int j = 0; j < 30; j++) { A[i][j] += A[i - 1][j]; if (A[i][j]) a[i - 1] |= 1 << j; } } vector<vector<int> > rmq(19, vector<int>(n + 1)); vector<int> lg(n + 1); rmq[0][1] = a[0]; for (int i = 2; i <= n; i++) { lg[i] = lg[i >> 1] + 1; rmq[0][i] = a[i - 1]; } for (int i = 1; (1 << i) <= n; i++) { for (int j = 1; j <= n - (1 << (i - 1)) + 1; j++) { rmq[i][j] = rmq[i - 1][j] & rmq[i - 1][j + (1 << (i - 1))]; } } auto query = [&](int a, int b) -> int { int l = lg[b - a + 1]; return rmq[l][a] & rmq[l][b - (1 << l) + 1]; }; for (int i = 0; i < m; i++) { if (query(l[i], r[i]) != q[i]) { cout << "NO"; return 0; } } cout << "YES\n"; for (int& 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
#include <bits/stdc++.h> const int N = 1e5 + 20; using namespace std; int n, m; int l[N], r[N], q[N], a[N], it[4 * N], s[N][30]; void build(int ind, int b, int e) { if (b == e) { it[ind] = a[b]; return; } int mid = (b + e) / 2; build(2 * ind, b, mid); build(2 * ind + 1, mid + 1, e); it[ind] = it[2 * ind] & it[2 * ind + 1]; } int query(int i, int l, int r, int x, int y) { if (r < x || l > y || l > r) return (1 << 30) - 1; if (x <= l && r <= y) return it[i]; int m = (l + r) / 2; return query(i * 2, l, m, x, y) & query(i * 2 + 1, m + 1, r, x, y); } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i <= n; i++) for (int j = 0; j < 30; j++) s[i][j] = 0; for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> q[i]; int k = q[i]; for (int j = 0; j < 30; j++, k /= 2) { if (k % 2) { s[l[i]][j]++; s[r[i] + 1][j]--; } } } for (int i = 1; i <= n; i++) { a[i] = 0; int k = 1; for (int j = 0; j < 30; j++, k *= 2) { s[i][j] += s[i - 1][j]; if (s[i][j] > 0) a[i] += k; } } build(1, 1, n); while (m--) { if (query(1, 1, n, l[m], r[m]) != q[m]) { cout << "NO\n"; return 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
//package Graphs; import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class main482B { public static PrintWriter out = new PrintWriter(System.out); public static FastScanner enter = new FastScanner(System.in); public static long mod=(1 << 30) - 1; public static void main(String[] args) throws IOException { int n = enter.nextInt(); int m = enter.nextInt(); int[] l = new int[m]; int[] r = new int[m]; long[] q = new long[m]; long[][] ans = new long[n + 2][30]; for (int i = 0; i < m; i++) { l[i] = enter.nextInt(); r[i] = enter.nextInt(); q[i] = enter.nextLong(); long a = 1; for (int j = 0; j < 30; j++, a = a << 1) { if ((a & q[i]) != 0) { ans[l[i]][j]+= 1; ans[r[i] + 1][j]-=1; } } } for (int j = 0; j < 30; j++) { for (int i = 1; i < n + 2; i++) { ans[i][j] += ans[i - 1][j]; } } for (int j = 0; j < 30; j++) { for (int i = 1; i < n + 1; i++) { ans[i][j] = (ans[i][j] > 0) ? 1 : 0; } } long a[] = new long[n + 1]; for (int i = 1; i < n + 1; i++) { long g=1; for (int j = 0; j < 30; j++,g=g<<1) { a[i] += ans[i][j] * g; } } //build segtree on mass a[] /*System.out.println(536870912&1073741823); System.out.println(536870911&1073741823); System.out.println(536870911&536870912); System.out.println(Arrays.toString(a));*/ long t[] = new long[4 * n + 1]; build(t, a, 1, 1, n); /*System.out.println(Arrays.toString(t)); System.out.println(sum(t,1,1,n,5,5)); System.out.println(sum(t,1,1,n,2,2)); System.out.println(sum(t,1,1,n,3,3));*/ for (int i = 0; i <m ; i++) { if(sum(t,1,1, n, l[i], r[i])!=q[i]){ System.out.println("NO"); return; } } System.out.println("YES"); for (int i = 1; i < n + 1; i++) { System.out.print(a[i] + " "); } /*long f[] ={-7,1,2,3,4}; long t[]=new long[20]; build(t,f,1,1,4); System.out.println(sum(t,1,1,4, 1,4)); System.out.println(sum(t,1,1,4, 1,1)); System.out.println(sum(t,1,1,4, 2,2)); System.out.println(sum(t,1,1,4, 3,3)); System.out.println(sum(t,1,1,4, 1,3));*/ } public static void build(long t[], long arr[], int v, int tl, int tr) { //По массиву arr строим if (tl == tr) { t[v] = arr[tl]; return; } int tmp = (tl + tr) / 2; build(t, arr, 2 * v, tl, tmp); build(t, arr, 2 * v + 1, tmp + 1, tr); t[v] = t[2 * v] & t[2 * v + 1]; //К примеру это построили сумму на отрехке l,r - можно кучу всего другого }//o(n) public static long sum(long t[], int v, int tl, int tr, int l, int r) { if (l > r) return mod; if (l == tl && r == tr) { return t[v]; } int tmp = (tl + tr) / 2; /*System.out.println(2*v+" "+tl+" "+ tmp); System.out.println((2*v+1)+" "+ (tmp+1)+" "+ tr); System.out.println(sum(t,2*v, tl,tmp, l, tmp)); System.out.println(sum(t,2*v+1, tmp+1, tr, tmp+1,r));*/ //return sum(t,2*v, tl,tmp, l, tmp)&sum(t,2*v+1, tmp+1, tr, tmp+1,r); return sum(t, 2 * v, tl, tmp, l, Math.min(r, tmp)) & sum(t, 2 * v + 1, tmp + 1, tr, Math.max(l, tmp + 1), r); //Так в e-maxx }//o(logn) static class FastScanner { BufferedReader br; StringTokenizer stok; FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.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()); } char nextChar() throws IOException { return (char) (br.read()); } String nextLine() throws IOException { return br.readLine(); } } }
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 = 5 + 100000; int b[MAXN][32]; struct Query { int l, r, q; } arr[MAXN]; int go(int l, int r, int bit) { return b[r][bit] - (l ? b[l - 1][bit] : 0); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = (int)0; i < (int)m; ++i) { int l, r, q; cin >> l >> r >> q; --l; --r; for (int k = (int)0; k < (int)32; ++k) if (q & (1 << k)) { ++b[l][k]; --b[1 + r][k]; } arr[i].l = l; arr[i].r = r; arr[i].q = q; } for (int i = (int)0; i < (int)32; ++i) { int c = 0; for (int j = (int)0; j < (int)MAXN; ++j) { c += b[j][i]; b[j][i] = (c >= 1); b[j][i] += (j ? b[j - 1][i] : 0); } } for (int i = (int)0; i < (int)m; ++i) { int l = arr[i].l, r = arr[i].r, q = arr[i].q; for (int k = (int)0; k < (int)32; ++k) if ((q & (1 << k)) == 0) { if (go(l, r, k) == 1 + r - l) { cout << "NO"; return 0; } } } cout << "YES\n"; for (int i = (int)0; i < (int)n; ++i) { int num = 0; for (int k = (int)0; k < (int)32; ++k) if (go(i, i, k)) num |= (1 << k); cout << num << ' '; } 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 a[100005], n, i, l[100005], r[100005], value, qu, q[100005], lazy[400005]; long long st[400005]; void update(long long node, long long start, long long end, long long l, long long r, long long value) { if (lazy[node] != 0) { st[node] |= lazy[node]; if (start != end) { lazy[2 * node + 1] |= lazy[node]; lazy[2 * node + 2] |= lazy[node]; } lazy[node] = 0; } if (l > end || r < start) { return; } if (l <= start && r >= end) { st[node] |= value; if (start != end) { lazy[2 * node + 1] |= value; lazy[2 * node + 2] |= value; } return; } long long mid = (start + end) / 2; update(2 * node + 1, start, mid, l, r, value); update(2 * node + 2, mid + 1, end, l, r, value); st[node] = st[2 * node + 1] & st[2 * node + 2]; } long long rmq(long long node, long long start, long long end, long long l, long long r) { if (lazy[node] != 0) { st[node] |= lazy[node]; if (start != end) { lazy[2 * node + 1] |= lazy[node]; lazy[2 * node + 2] |= lazy[node]; } lazy[node] = 0; } long long mid = (start + end) / 2; if (l <= start && r >= end) { return st[node]; } if (r <= mid) { return rmq(2 * node + 1, start, mid, l, r); } if (l > mid) { return rmq(2 * node + 2, mid + 1, end, l, r); } long long left = rmq(2 * node + 1, start, mid, l, r); long long right = rmq(2 * node + 2, mid + 1, end, l, r); return left & right; } int main() { scanf("%lld%lld", &n, &qu); for (i = 0; i < qu; i++) { scanf("%lld%lld%lld", &l[i], &r[i], &q[i]); l[i]--; r[i]--; update(0, 0, n - 1, l[i], r[i], q[i]); } for (i = 0; i < qu; i++) { if (rmq(0, 0, n - 1, l[i], r[i]) != q[i]) { cout << "NO" << endl; exit(0); } } cout << "YES" << endl; for (i = 0; i < n; i++) { cout << rmq(0, 0, n - 1, i, 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
/* ID: govind.3, GhpS, govindpatel LANG: JAVA TASK: Main */ import java.io.*; import java.util.*; public class Main { private final int BIT = 30; private final int MAX = 1000 * 1000;//10^6 private int[] tree = new int[4 * MAX]; private int[] values = new int[MAX]; private void build(int v, int tl, int tr) {//v=1 => root, tl=0 => values.sIndex, tr=N-1 => values.length-1 if (tl + 1 == tr) { tree[v] = values[tl]; } else { int tm = (tl + tr) / 2; build(2 * v, tl, tm);//left build(2 * v + 1, tm, tr);//right tree[v] = tree[v * 2] & tree[v * 2 + 1]; } } private long AND(int v, int tl, int tr, int l, int r) { long ans = (1L << BIT) - 1; if (l == tl && r == tr) { return tree[v]; } int tm = (tl + tr) / 2; if (l < tm) { ans = ans & AND(2 * v, tl, tm, l, Math.min(r, tm)); } if (tm < r) { ans = ans & AND(2 * v + 1, tm, tr, Math.max(l, tm), r); } return ans; } private void solve() throws IOException { StringBuilder sb = new StringBuilder(""); int N = nextInt(); int M = nextInt(); int[][] query = new int[M][]; int[] sum = new int[N + 1]; for (int i = 0; i < M; i++) { query[i] = new int[]{nextInt(), nextInt(), nextInt()};//(l,r) - q --query[i][0]; } for (int bit = 0; bit <= BIT; bit++) { Arrays.fill(sum, 0); for (int i = 0; i < M; i++) { if (((query[i][2] >> bit) & 1) != 0) { sum[query[i][0]]++; sum[query[i][1]]--; } } for (int i = 0; i < N; i++) { if (i > 0) { sum[i] += sum[i - 1]; } if (sum[i] > 0) {//set this bit; values[i] = values[i] | (1 << bit); } } } build(1, 0, N); for (int i = 0; i < M; i++) { if (AND(1, 0, N, query[i][0], query[i][1]) != query[i][2]) { System.out.println("NO"); return; } } sb.append("YES\n"); for (int i = 0; i < N; i++) { sb.append(values[i]); if (i != N - 1) { sb.append(" "); } } System.out.println(sb); } public static void main(String[] args) { new Main().run(); } public void run() { try { in = new BufferedReader(new FileReader("Main.in")); out = new PrintWriter(new BufferedWriter(new FileWriter("Main.out"))); tok = null; solve(); in.close(); out.close(); System.exit(0); } catch (IOException e) {//(FileNotFoundException e) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); tok = null; solve(); in.close(); out.close(); System.exit(0); } catch (IOException ex) { System.out.println(ex.getMessage()); 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()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BufferedReader in; StringTokenizer tok; PrintWriter 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int m = Integer.parseInt(tokenizer.nextToken()); Segment[] segments = new Segment[m]; for(int i = 0;i < m;i++) { tokenizer = new StringTokenizer(reader.readLine()); int a = Integer.parseInt(tokenizer.nextToken())-1; int b = Integer.parseInt(tokenizer.nextToken())-1; int c = Integer.parseInt(tokenizer.nextToken()); segments[i] = new Segment(a, b, c); } boolean[][] bits = new boolean[n][30]; boolean bad = false; for(int bit = 0;bit < 30;bit++) { int[] start = new int[n]; int[] end = new int[n+1]; int[] prev = new int[n]; for(Segment segment : segments) if((segment.val&(1<<bit)) != 0) { start[segment.start]++; end[segment.end+1]++; } int sum = 0; int x = 0; for(int i = 0;i < n;i++) { sum += start[i]; sum -= end[i]; if(sum > 0) { bits[i][bit] = true; prev[i] = x; } else { prev[i] = -1; x = i+1; } } for(Segment segment : segments) if((segment.val&(1<<bit)) == 0) if(prev[segment.end] != -1 && prev[segment.end] <= segment.start) bad = true; } PrintWriter writer = new PrintWriter(System.out); if(bad) { writer.println("NO"); } else { writer.println("YES"); for(int i = 0; i < n; i++) { int num = 0; for(int bit = 0; bit < 30; bit++) if(bits[i][bit]) num |= 1 << bit; writer.print(num + " "); } } writer.flush(); writer.close(); } static class Segment { int start; int end; int val; public Segment(int start, int end, int val) { this.start = start; this.end = end; this.val = val; } } }
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.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; /* br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); */ public class Main { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //int qq = 1; int qq = Integer.MAX_VALUE; //int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { int n = readInt(); int m = readInt(); int[] l = new int[m]; int[] r = new int[m]; int[] q = new int[m]; int[][] bits = new int[30][n]; for(int i = 0; i < m; i++) { l[i] = readInt()-1; r[i] = readInt()-1; q[i] = readInt(); for(int a = 0; a < 30; a++) { if((q[i]&(1<<a)) == 0) continue; bits[a][l[i]]++; if(r[i] != n-1) { bits[a][r[i]+1]--; } } } int[][] prefix = new int[30][n+1]; for(int a = 0; a < prefix.length; a++) { int curr = 0; for(int i = 0; i < bits[a].length; i++) { curr += bits[a][i]; prefix[a][i+1] = prefix[a][i]; if(curr > 0) { prefix[a][i+1]++; } } } boolean good = true; for(int i = 0; good && i < m; i++) { for(int a = 0; a < 30; a++) { if((q[i]&(1<<a)) != 0) continue; if(prefix[a][r[i]+1] - prefix[a][l[i]] == r[i] - l[i] + 1) { good = false; break; } } } if(!good) { pw.println("NO"); } else { pw.println("YES"); for(int i = 0; i < n; i++) { int num = 0; for(int a = 0; a < 30; a++) { if(prefix[a][i+1] != prefix[a][i]) { num |= 1 << a; } } pw.print(num + " "); } pw.println(); } } exitImmediately(); } private static void exitImmediately() { pw.close(); System.exit(0); } private static long readLong() throws IOException { return Long.parseLong(nextToken()); } private static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } private static int readInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextLine() throws IOException { if(!br.ready()) { exitImmediately(); } st = null; return br.readLine(); } private static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { if(!br.ready()) { exitImmediately(); } st = new StringTokenizer(br.readLine().trim()); } 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; int n, l[100005], r[100005], q[100005], m, t[300000], sum[100005], arr[100005]; void buildTree(int root, int l, int r) { if (l == r) { t[root] = arr[l]; return; } int mid = (l + r) >> 1; buildTree(root * 2, l, mid); buildTree(root * 2 + 1, mid + 1, r); t[root] = t[root * 2] & t[root * 2 + 1]; } int query(int root, int l, int r, int ql, int qr) { if (l >= ql && r <= qr) { return t[root]; } int mid = (l + r) >> 1; if (ql > mid) { return query(root * 2 + 1, mid + 1, r, ql, qr); } else if (qr <= mid) { return query(root * 2, l, mid, ql, qr); } else { return query(root * 2, l, mid, ql, mid) & query(root * 2 + 1, mid + 1, r, mid + 1, qr); } } int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> l[i] >> r[i] >> q[i]; } for (int i = 0; i <= 29; i++) { memset(sum, 0, sizeof sum); for (int j = 1; j <= m; j++) { if ((q[j] >> i) & 1) { sum[l[j]]++; sum[r[j] + 1]--; } } for (int j = 1; j <= n; j++) { sum[j] += sum[j - 1]; if (sum[j]) { arr[j] |= 1 << i; } } } buildTree(1, 1, n); bool valid = true; for (int i = 1; i <= m; i++) { if (query(1, 1, n, l[i], r[i]) != q[i]) { cout << "NO" << endl; valid = false; break; } } if (valid) { cout << "YES" << endl; for (int i = 1; i <= n; i++) { cout << arr[i] << " "; } cout << endl; } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; import java.math.*; import java.util.*; public class Main { public static class pair implements Comparable<pair> { int a; int b; public pair(int pa, int pb) { a = pa; b= pb; } @Override public int compareTo(pair o) { if(this.a < o.a) return -1; if(this.a > o.a) return 1; return Integer.compare(o.b, this.b); } } //int n = Integer.parseInt(in.readLine()); //int n = Integer.parseInt(spl[0]); //String[] spl = in.readLine().split(" "); public static class lim implements Comparable<lim> { int num; int q; boolean isL; public lim(int pnum, boolean pisL,int pq) { num = pnum; isL = pisL; q = pq; } public int compareTo(lim o) { if(this.num == o.num) { if(this.isL && !o.isL) return -1; else if(o.isL && !this.isL) return 1; return 0; } return Integer.compare(this.num, o.num); } } public static long[] data; public static long[] st; public static void init(int r, int rl, int rr) { if(rr == rl) { st[r] = data[rl]; return; } int mid = (rl+rr)/2; init(2*r+1, rl, mid); init(2*r+2, mid+1, rr); st[r] = (st[2*r+1] & st[2*r+2]); } public static long query(int r, int rl, int rr, int L, int R) { if(L>R) return Integer.MAX_VALUE; if(rl==L && rr==R) return st[r]; int mid = (rl+rr)/2; long m1 = query(2*r+1, rl, mid, L, Math.min(R, mid)); long m2 = query(2*r+2, mid+1, rr, Math.max(L, mid+1), R); return (m1 & m2); } public static void main (String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] spl = in.readLine().split(" "); int n = Integer.parseInt(spl[0]); int m = Integer.parseInt(spl[1]); int[] l = new int[m]; int[] r = new int[m]; int[] q = new int[m]; lim[] lims = new lim[2*m]; for (int i = 0; i < m; i++) { spl = in.readLine().split(" "); int cl = Integer.parseInt(spl[0])-1; int cr = Integer.parseInt(spl[1])-1; int cq = Integer.parseInt(spl[2]); l[i] = cl; r[i] = cr; q[i] = cq; lims[2*i] = new lim(cl, true,cq); lims[2*i+1] = new lim(cr, false,cq); } Random rnd = new Random(); for (int i = 2*m - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); lim tmp = lims[index]; lims[index] = lims[i]; lims[i] = tmp; } Arrays.sort(lims); /*for (int i = 0; i < 2*m; i++) { System.out.println((lims[i].num+1)+" "+lims[i].isL+" "+lims[i].q); }*/ int[] mat1 = new int[40]; int idx = 0; data = new long[n]; for (int i = 0; i < n; i++) { while(idx < 2*m && i == lims[idx].num && lims[idx].isL) { int curq = lims[idx].q; String toB = Integer.toBinaryString(curq); for (int j = toB.length()-1; j >=0; j--) { if(toB.charAt(j)=='1') mat1[toB.length()-1-j]++; } idx++; } data[i] = 0; for (int j = 0; j < 40; j++) { if(mat1[j] > 0) data[i] |= (1<<j); } //System.out.println(i+" "+data[i]); while(idx < 2*m && i == lims[idx].num) { int curq = lims[idx].q; String toB = Integer.toBinaryString(curq); for (int j = toB.length()-1; j >=0; j--) { if(toB.charAt(j)=='1') mat1[toB.length()-1-j]--; } idx++; } } st = new long[4*n]; init(0,0,n-1); boolean perd = false; for (int i = 0; i < m; i++) { if( query(0,0,n-1, l[i], r[i]) != q[i]) { perd = true; } } if(!perd) { System.out.println("YES"); StringBuffer sb = new StringBuffer(); for (int i = 0; i < n; i++) { sb.append(data[i]); sb.append(" "); } System.out.println(sb.toString().trim()); } else System.out.println("NO"); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; int n, m; int arr[100005]; int pf[32][100005]; int st[4 * 100005]; void init() { memset(arr, 0, sizeof(arr)); memset(pf, 0, sizeof(pf)); return; } void build(int l = 0, int r = n - 1, int idx = 0) { if (l == r) { st[idx] = arr[l]; return; } int mid = l + (r - l) / 2; build(l, mid, 2 * idx + 1); build(mid + 1, r, 2 * idx + 2); st[idx] = st[2 * idx + 1] & st[2 * idx + 2]; return; } int query(int x, int y, int l = 0, int r = n - 1, int idx = 0) { if (y < l || r < x) { return (1 << 31) - 1; } if (x <= l && r <= y) { return st[idx]; } int mid = l + (r - l) / 2; return query(x, y, l, mid, 2 * idx + 1) & query(x, y, mid + 1, r, 2 * idx + 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); init(); cin >> n >> m; int l[m], r[m], q[m]; for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> q[i]; l[i]--; r[i]--; for (int j = 0; j < 32; j++) { if ((q[i] >> j) & 1) { pf[j][l[i]] += 1; if (r[i] < n - 1) pf[j][r[i] + 1] -= 1; } } } for (int i = 0; i < 32; i++) { for (int j = 1; j < n; j++) { pf[i][j] += pf[i][j - 1]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < 32; j++) { if (pf[j][i] > 0) arr[i] += (1 << j); } } build(); for (int i = 0; i < m; i++) { if (query(l[i], r[i]) != q[i]) { cout << "NO" << endl; exit(0); } } cout << "YES" << endl; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; struct item { int l, r, q; }; item a[100007]; int t[100007 * 4]; int t_add[100007 * 4]; void add(int v, int l, int r, int pl, int pr, int delta) { if (pl > pr) return; if (pr < l || r < pl) return; if (pl <= l && r <= pr) { t_add[v] |= delta; t[v] |= delta; return; } int lr = (l + r) >> 1; add(v + v, l, lr, pl, pr, delta); add(v + v + 1, lr + 1, r, pl, pr, delta); t[v] = (t[v + v] & t[v + v + 1]) | t_add[v]; } int get(int v, int l, int r, int pl, int pr) { if (pl > pr) return (1 << 30) - 1; if (pr < l || r < pl) return (1 << 30) - 1; if (pl <= l && r <= pr) return t[v]; int lr = (l + r) >> 1; return (get(v + v, l, lr, pl, pr) & get(v + v + 1, lr + 1, r, pl, pr)) | t_add[v]; } int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) cin >> a[i].l >> a[i].r >> a[i].q; for (int i = 0; i < m; i++) add(1, 1, n, a[i].l, a[i].r, a[i].q); for (int i = 0; i < m; i++) { int val = get(1, 1, n, a[i].l, a[i].r); if (val != a[i].q) { cout << "NO"; return 0; } } cout << "YES" << endl; for (int i = 1; i <= n; i++) cout << get(1, 1, n, i, 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
//package HackerEarthA; import java.io.*; import java.util.*; import java.util.Map.Entry; import java.text.*; import java.math.*; import java.util.regex.*; import java.awt.Point; /** * * @author prabhat */ public class easy18{ public static long[] BIT; public static long[] tree; public static long[] sum; public static int mod=1000000007; public static int[][] dp; public static boolean[][] isPalin; public static int max1=1000005; public static int[][] g; public static LinkedList<Pair> l[]; public static int n,m,q,k,t,arr[],b[],cnt[],chosen[],pos[],ans,val[]; public static ArrayList<Integer> adj[],al; public static TreeSet<Integer> ts; public static char[][] s; public static int depth,mini,maxi; public static long V[],low,high,min=Long.MAX_VALUE; public static boolean visit[][],isPrime[],used[]; public static int[][] dist; public static ArrayList<Integer>prime; public static int[] x={0,1}; public static int[] y={-1,0}; public static ArrayList<Integer> divisor[]=new ArrayList[1500005]; public static int[][] subtree,parent,mat; public static TreeMap<Integer,Integer> tm; public static LongPoint[] p; public static int[][] grid; public static ArrayList<Pair> list; public static TrieNode root; static LinkedHashMap<String,Integer> map; public static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { InputReader in = new InputReader(System.in); PrintWriter pw=new PrintWriter(System.out); n=in.ii(); m=in.ii(); arr=new int[n]; 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]=in.ii()-1; r[i]=in.ii()-1; q[i]=in.ii(); } int[] sum=new int[n+1]; 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)>0) { sum[l[i]]++; sum[r[i]+1]--; } } for(int i=0;i<n;i++) { if(i>0) sum[i]+=sum[i-1]; if(sum[i]>0) arr[i]|=1<<bit; } } SegmentTree st=new SegmentTree(n); boolean bb=true; for(int i=0;i<m;i++) { if(st.query(1, 0, n-1, l[i], r[i])!=q[i]) { bb=false; break; } } if(!bb)pw.println("NO"); else{ pw.println("YES"); for(int i:arr) pw.print(i+" "); } pw.close(); } static boolean matche(String s,String p) { if (s == null || p == null) { return false; } boolean[][] dp = new boolean[s.length()+1][p.length()+1]; dp[0][0] = true; for (int i = 0; i < p.length(); i++) { if (p.charAt(i) == '*' && dp[0][i-1]) { dp[0][i+1] = true; } } for (int i = 0 ; i < s.length(); i++) { for (int j = 0; j < p.length(); j++) { if (p.charAt(j) == '.'||(s.charAt(i)==p.charAt(j))) { dp[i+1][j+1] = dp[i][j]; } if (p.charAt(j) == '*') { if (p.charAt(j-1) != s.charAt(i) && p.charAt(j-1) != '.') { dp[i+1][j+1] = dp[i+1][j-1]; } else { dp[i+1][j+1] = (dp[i+1][j] || dp[i][j+1] || dp[i+1][j-1]); } } } } return dp[s.length()][p.length()]; } static int gcd(int a, int b) { int res=0; while(a!=0) { res=a; a=b%a; b=res; } return b; } static slope getSlope(Point p, Point q) { int dx = q.x - p.x, dy = q.y - p.y; int g = gcd(dx, dy); return new slope(dy / g, dx / g); } static class slope implements Comparable<slope> { int x,y; public slope(int y,int x) { this.x=x; this.y=y; } public int compareTo(slope s) { if(s.y!=y)return y-s.y; return x-s.x; } } static void update(int indx,long val) { while(indx<max1) { BIT[indx]=Math.max(BIT[indx],val); indx+=(indx&(-indx)); } } static long query1(int indx) { long sum=0; while(indx>0) { sum=Math.max(BIT[indx],sum); // System.out.println(indx); indx-=(indx&(-indx)); } return sum; } static class Ball implements Comparable { int r; int occ; public Ball(int r,int occ) { this.r = r; this.occ = occ; } @Override public int compareTo(Object o) { Ball b = (Ball) o; return b.occ-this.occ; } } static int bs(ArrayList<Long> al,long x) { int s=0; int e=al.size(); while(s<e) { int mid=(s+e)/2; if(al.get(mid)==x)return mid; else if(al.get(mid)<x) { s=mid+1; } else e=mid; } return -1; } static class E implements Comparable<E>{ int x,y; char c; E(int x,int y,char c) { this.x=x; this.y=y; this.c=c; } public int compareTo(E o) { if(x!=o.x)return o.x-x; else return y-o.y; } } static ArrayList<String> dfs(String s,String[] sarr,HashMap<String, ArrayList<String>> map) { if(map.containsKey(s))return map.get(s); ArrayList<String> res=new ArrayList<>(); if(s.length()==0) { res.add(""); return res; } for(String word:sarr) { if(s.startsWith(word)) { ArrayList<String > sub=dfs(s.substring(word.length()),sarr,map); for(String s2:sub) { res.add(word+ (s2.isEmpty() ? "" : " ")+s2); } } } map.put(s,res); return res; } static class SegmentTree{ int n; int max_bound; public SegmentTree(int n) { this.n=n; tree=new long[4*n+1]; build(1,0,n-1); } void build(int c,int s,int e) { if(s==e) { tree[c]=arr[s]; return ; } int mid=(s+e)>>1; build(2*c,s,mid); build(2*c+1,mid+1,e); tree[c]=(tree[2*c]&tree[2*c+1]); } void put(int c,int s, int e,int l,int r,int v) { if(l>e||r<s||s>e)return ; if (s == e) { tree[c] = arr[s]^v; return; } int mid = (s + e) >> 1; if (l>mid) put(2*c+1,m+1, e , l,r, v); else if(r<=mid)put(2*c,s,m,l,r , v); else{ } } long query(int c,int s,int e,int l,int r) { if(e<l||s>r)return 0L; if(s>=l&&e<=r) { return tree[c]; } int mid=(s+e)>>1; long ans=(1l<<30)-1; if(l>mid)return query(2*c+1,mid+1,e,l,r); else if(r<=mid)return query(2*c,s,mid,l,r); else{ return query(2*c,s,mid,l,r)&query(2*c+1,mid+1,e,l,r); } } } public static ListNode removeNthFromEnd(ListNode a, int b) { int cnt=0; ListNode ra1=a; ListNode ra2=a; while(ra1!=null){ra1=ra1.next; cnt++;} if(b>cnt)return a.next; else if(b==1&&cnt==1)return null; else{ int y=cnt-b+1; int u=0; ListNode prev=null; while(a!=null) { u++; if(u==y) { if(a.next==null)prev.next=null; else { if(prev==null)return ra2.next; prev.next=a.next; a.next=null; } break; } prev=a; a=a.next; } } return ra2; } static ListNode rev(ListNode a) { ListNode prev=null; ListNode cur=a; ListNode next=null; while(cur!=null) { next=cur.next; cur.next=prev; prev=cur; cur=next; } return prev; } static class ListNode { public int val; public ListNode next; ListNode(int x) { val = x; next = null; } } static class TrieNode { int value; // Only used in leaf nodes TrieNode[] arr = new TrieNode[2]; public TrieNode() { value = 0; arr[0] = null; arr[1] = null; } } static void insert(int pre_xor) { TrieNode temp = root; // Start from the msb, insert all bits of // pre_xor into Trie for (int i=31; i>=0; i--) { // Find current bit in given prefix int val = (pre_xor & (1<<i)) >=1 ? 1 : 0; // Create a new node if needed if (temp.arr[val] == null) temp.arr[val] = new TrieNode(); temp = temp.arr[val]; } // Store value at leaf node temp.value = pre_xor; } static int query(int pre_xor) { TrieNode temp = root; for (int i=31; i>=0; i--) { // Find current bit in given prefix int val = (pre_xor & (1<<i)) >= 1 ? 1 : 0; // Traverse Trie, first look for a // prefix that has opposite bit if (temp.arr[val] != null) temp = temp.arr[val]; // If there is no prefix with opposite // bit, then look for same bit. else if (temp.arr[1-val] != null) temp = temp.arr[1-val]; } return pre_xor^(temp.value); } public static String add(String s1,String s2) { int n=s1.length()-1; int m=s2.length()-1; int rem=0; int carry=0; int i=n; int j=m; String ans=""; while(i>=0&&j>=0) { int c1=(int)(s1.charAt(i)-'0'); int c2=(int)(s2.charAt(j)-'0'); int sum=c1+c2; sum+=carry; rem=sum%10; carry=sum/10; ans=rem+ans; i--; j--; } while(i>=0) { int c1=(int)(s1.charAt(i)-'0'); int sum=c1; sum+=carry; rem=sum%10; carry=sum/10; ans=rem+ans; i--; } while(j>=0) { int c2=(int)(s2.charAt(j)-'0'); int sum=c2; sum+=carry; rem=sum%10; carry=sum/10; ans=rem+ans; j--; } if(carry>0)ans=carry+ans; return ans; } public static String[] multiply(String A, String B) { int lb=B.length(); char[] a=A.toCharArray(); char[] b=B.toCharArray(); String[] s=new String[lb]; int cnt=0; int y=0; String s1=""; q=0; for(int i=b.length-1;i>=0;i--) { int rem=0; int carry=0; for(int j=a.length-1;j>=0;j--) { int mul=(int)(b[i]-'0')*(int)(a[j]-'0'); mul+=carry; rem=mul%10; carry=mul/10; s1=rem+s1; } s1=carry+s1; s[y++]=s1; q=Math.max(q,s1.length()); s1=""; for(int i1=0;i1<y;i1++)s1+='0'; } return s; } public static long nCr(long total, long choose) { if(total < choose) return 0; if(choose == 0 || choose == total) return 1; return nCr(total-1,choose-1)+nCr(total-1,choose); } static int get(long s) { int ans=0; for(int i=0;i<n;i++) { if(p[i].x<=s&&s<=p[i].y)ans++; } return ans; } static class LongPoint { public long x; public long y; public LongPoint(long x, long y) { this.x = x; this.y = y; } public LongPoint subtract(LongPoint o) { return new LongPoint(x - o.x, y - o.y); } public long cross(LongPoint o) { return x * o.y - y * o.x; } } static int CountPs(String s,int n) { boolean b=false; char[] S=s.toCharArray(); int[][] dp=new int[n][n]; boolean[][] p=new boolean[n][n]; for(int i=0;i<n;i++)p[i][i]=true; for(int i=0;i<n-1;i++) { if(S[i]==S[i+1]) { p[i][i+1]=true; dp[i][i+1]=0; b=true; } } for(int gap=2;gap<n;gap++) { for(int i=0;i<n-gap;i++) { int j=gap+i; if(S[i]==S[j]&&p[i+1][j-1]){p[i][j]=true;} if(p[i][j]) dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1]; else if(!p[i][j]) dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]; //if(dp[i][j]>=1) // return true; } } // return b; return dp[0][n-1]; } static void Seive() { isPrime=new boolean[1500005]; Arrays.fill(isPrime,true); for(int i=0;i<1500005;i++){divisor[i]=new ArrayList<>();} int u=0; prime=new ArrayList<>(); for(int i=2;i<=(1500000);i++) { if(isPrime[i]) { prime.add(i); for(int j=i;j<1500000;j+=i) { divisor[j].add(i); isPrime[j]=false; } /* for(long x=(divCeil(low,i))*i;x<=high;x+=i) { divisor[(int)(x-low)].add(i*1L); }*/ } } } static long divCeil(long a,long b) { return (a+b-1)/b; } static long root(int pow, long x) { long candidate = (long)Math.exp(Math.log(x)/pow); candidate = Math.max(1, candidate); while (pow(candidate, pow) <= x) { candidate++; } return candidate-1; } static long pow(long x, int pow) { long result = 1; long p = x; while (pow > 0) { if ((pow&1) == 1) { if (result > Long.MAX_VALUE/p) return Long.MAX_VALUE; result *= p; } pow >>>= 1; if (pow > 0 && p >= 4294967296L) return Long.MAX_VALUE; p *= p; } return result; } static boolean valid(int i,int j) { if(i>=0&&i<n&&j>=0&&j<m)return true; return false; } private static class DSU{ int[] parent; int[] rank; int cnt; public DSU(int n){ parent=new int[n]; rank=new int[n]; for(int i=0;i<n;i++){ parent[i]=i; rank[i]=1; } cnt=n; } int find(int i){ while(parent[i] !=i){ parent[i]=parent[parent[i]]; i=parent[i]; } return i; } int Union(int x, int y){ int xset = find(x); int yset = find(y); if(xset!=yset) cnt--; if(rank[xset]<rank[yset]){ parent[xset] = yset; rank[yset]+=rank[xset]; rank[xset]=0; return yset; }else{ parent[yset]=xset; rank[xset]+=rank[yset]; rank[yset]=0; return xset; } } } public static int[][] packU(int n, int[] from, int[] to, int max) { int[][] g = new int[n][]; int[] p = new int[n]; for (int i = 0; i < max; i++) p[from[i]]++; for (int i = 0; i < max; i++) p[to[i]]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < max; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } public static int[][] parents3(int[][] g, int root) { int n = g.length; int[] par = new int[n]; Arrays.fill(par, -1); int[] depth = new int[n]; depth[0] = 0; int[] q = new int[n]; q[0] = root; for (int p = 0, r = 1; p < r; p++) { int cur = q[p]; for (int nex : g[cur]) { if (par[cur] != nex) { q[r++] = nex; par[nex] = cur; depth[nex] = depth[cur] + 1; } } } return new int[][]{par, q, depth}; } public static int lower_bound(int[] nums, int target) { int low = 0, high = nums.length - 1; while (low < high) { int mid = low + (high - low) / 2; if (nums[mid] < target) low = mid + 1; else high = mid; } return nums[low] == target ? low : -1; } public static int upper_bound(int[] nums, int target) { int low = 0, high = nums.length - 1; while (low < high) { int mid = low + (high + 1 - low) / 2; if (nums[mid] > target) high = mid - 1; else low = mid; } return nums[low] == target ? low : -1; } public static boolean palin(String s) { int i=0; int j=s.length()-1; while(i<j) { if(s.charAt(i)==s.charAt(j)) { i++; j--; } else return false; } return true; } static int lcm(int a,int b) { return (a*b)/(gcd(a,b)); } public static long pow(long n,long p,long m) { long result = 1; if(p==0) return 1; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } /** * * @param n * @param p * @return */ public static long pow(long n,long p) { long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; p >>=1; n*=n; } return result; } static class Edge implements Comparator<Edge> { private int u; private int v; private long w; public Edge() { } public Edge(int u, int v, long w) { this.u=u; this.v=v; this.w=w; } public int getU() { return u; } public void setU(int u) { this.u = u; } public int getV() { return v; } public void setV(int v) { this.v = v; } public long getW() { return w; } public void setW(int w) { this.w = w; } public long compareTo(Edge e) { return (this.getW() - e.getW()); } @Override public int compare(Edge o1, Edge o2) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } } static class Pair implements Comparable<Pair> { int x,y; Pair (int a,int b) { this.x=a; this.y=b; } public int compareTo(Pair o) { // TODO Auto-generated method stub if(this.x!=o.x) return -Integer.compare(this.x,o.x); else return -Integer.compare(this.y, o.y); //return 0; } 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 Integer(x).hashCode() * 31 + new Integer(y).hashCode(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private SpaceCharFilter filter; byte inbuffer[] = new byte[1024]; int lenbuffer = 0, ptrbuffer = 0; final int M = (int) 1e9 + 7; int md=(int)(1e7+1); int[] SMP=new int[md]; final double eps = 1e-6; final double pi = Math.PI; PrintWriter out; String check = ""; InputStream obj = check.isEmpty() ? System.in : new ByteArrayInputStream(check.getBytes()); public InputReader(InputStream stream) { this.stream = stream; } int readByte() { if (lenbuffer == -1) throw new InputMismatchException(); if (ptrbuffer >= lenbuffer) { ptrbuffer = 0; try { lenbuffer = obj.read(inbuffer); } catch (IOException e) { throw new InputMismatchException(); } } if (lenbuffer <= 0) return -1; return inbuffer[ptrbuffer++]; } public int read() { 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; } String is() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) // when nextLine, (isSpaceChar(b) && b!=' ') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int ii() { 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(); } } public long il() { 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(); } } boolean isSpaceChar(int c) { return (!(c >= 33 && c <= 126)); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } float nf() { return Float.parseFloat(is()); } double id() { return Double.parseDouble(is()); } char ic() { return (char) skip(); } int[] iia(int n) { int a[] = new int[n]; for (int i = 0; i<n; i++) a[i] = ii(); return a; } long[] ila(int n) { long a[] = new long[n]; for (int i = 0; i <n; i++) a[i] = il(); return a; } String[] isa(int n) { String a[] = new String[n]; for (int i = 0; i < n; i++) a[i] = is(); return a; } long mul(long a, long b) { return a * b % M; } long div(long a, long b) { return mul(a, pow(b, M - 2)); } double[][] idm(int n, int m) { double a[][] = new double[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = id(); return a; } int[][] iim(int n, int m) { int a[][] = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j <m; j++) a[i][j] = ii(); return a; } public String readLine() { 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public 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; const int MAXN = 100005; vector<int> op[MAXN], cl[MAXN]; int l[MAXN], r[MAXN]; int a[MAXN]; int val[MAXN]; int n, m; int t[MAXN * 4]; int tsize; int getAnd(int l, int r) { l += tsize; r += tsize; int res = (1 << 30) - 1; while (l <= r) { if (l & 1) res &= t[l++]; if (!(r & 1)) res &= t[r--]; l >>= 1; r >>= 1; } return res; } int main() { cin >> n >> m; for (int i = 0; i < (int)(m); i++) { scanf("%d %d %d", &l[i], &r[i], &val[i]); l[i]--, r[i]--; op[l[i]].push_back(i); cl[r[i]].push_back(i); } vector<int> cnt(30); for (int i = 0; i < n; i++) { for (int j = 0; j < (int)(op[i].size()); j++) { for (int b = 0; b < (int)(30); b++) { if (val[op[i][j]] & (1 << b)) cnt[b]++; } } for (int b = 0; b < (int)(30); b++) if (cnt[b]) a[i] |= (1 << b); for (int j = 0; j < (int)(cl[i].size()); j++) { for (int b = 0; b < (int)(30); b++) { if (val[cl[i][j]] & (1 << b)) cnt[b]--; } } } tsize = 1; while (tsize < n) tsize *= 2; for (int i = 0; i < (int)(2 * tsize); i++) t[i] = (1 << 30) - 1; for (int i = 0; i < (int)(n); i++) t[tsize + i] = a[i]; for (int i = tsize - 1; i > 0; i--) { t[i] = t[i * 2] & t[i * 2 + 1]; } for (int i = 0; i < (int)(m); i++) { if (getAnd(l[i], r[i]) != val[i]) { puts("NO"); return 0; } } puts("YES"); for (int i = 0; i < (int)(n); i++) cout << a[i] << " "; return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class InterestingArray { public static void main(String[] args) throws IOException { Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); SegmentTree st = new SegmentTree(n); Query[] qs = new Query[m]; for (int i = 0; i < qs.length; i++) { int x = s.nextInt(); int y = s.nextInt(); int v = s.nextInt(); st.update(x, y, v); qs[i] = new Query(x, y, v); } for (int i = 0; i < qs.length; i++) { Query q = qs[i]; if(st.get(q.i, q.j)!= q.val){ System.out.println("NO"); return; } } System.out.println("YES"); for (int i = 1; i < st.n; i++) { st.propagate(i); } int i = st.n; while(n-- >0) { System.out.print(st.stree[i]+" "); i++; } System.out.println(); } static class Query{ int i,j,val; public Query(int x, int y, int v){ i =x; j = y; val =v; } } static class SegmentTree{ int n; int[] stree; int[] lazy; public SegmentTree(int s){ n = 1; while(n <s) n<<=1; stree = new int[n<<1]; lazy = new int[n<<1]; //Arrays.fill(lazy, 1); } public void update(int i, int j, int val){ update(1,1,n,i,j,val); } public void update(int node, int b, int e, int i, int j, int val){ if(i > e || j < b) return; if(b >= i && e <= j) { stree[node] |= val; lazy[node] |= val; } else{ int mid = b + e >>1; propagate(node, b, mid, e); update(node<<1,b,mid,i,j,val); update(node<<1|1,mid+1,e,i,j,val); stree[node] = stree[node<<1] & stree[node<<1|1]; } } void propagate(int node, int b, int mid, int e) { lazy[node<<1] |= lazy[node]; lazy[node<<1|1] |= lazy[node]; stree[node<<1] |= lazy[node]; stree[node<<1|1] |= lazy[node]; lazy[node] = 0; } void propagate(int node) { lazy[node<<1] |= lazy[node]; lazy[node<<1|1] |= lazy[node]; stree[node<<1] |= lazy[node]; stree[node<<1|1] |= lazy[node]; lazy[node] = 0; } public int get(int i, int j){ return get(1,1,n,i,j); } public int get(int node, int b, int e,int i, int j){ if(e < i || b > j) return -1; if(b>= i && e <= j) return stree[node]; int mid = b + e >> 1; propagate(node, b, mid, e); int q1 = get(node<<1,b,mid,i,j); int q2 = get(node<<1|1,mid+1,e,i,j); return q1 & q2; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) {br = new BufferedReader(new InputStreamReader(system));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine()throws IOException{return br.readLine();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public char nextChar()throws IOException{return next().charAt(0);} public Long nextLong()throws IOException{return Long.parseLong(next());} public boolean ready() throws IOException{return br.ready();} public void waitForInput() throws InterruptedException {Thread.sleep(4000);} } }
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 comment(linker, "/stack:32000000") using namespace std; const int INF = 2147483647; const long long LLINF = 9223372036854775807LL; const int maxn = 4 * 100010; int T[maxn]; int N; int query(int l, int r) { int ans = ~0; for (l += N, r += N; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1) ans &= T[l] & T[r]; return ans; } int t[maxn]; int tor[maxn]; void push(int v, int L, int R) { t[v] |= tor[v]; if (L < R) tor[v + v + 1] |= tor[v], tor[v + v + 2] |= tor[v]; tor[v] = 0; } void modify(int v, int L, int R, int l, int r, int val) { if (l > R || L > r) return; if (l <= L && R <= r) tor[v] |= val; else { push(v, L, R); int m = (L + R) >> 1; modify(v + v + 1, L, m, l, r, val); modify(v + v + 2, m + 1, R, l, r, val); } } int a[maxn]; void build(int v, int L, int R) { push(v, L, R); if (L == R) a[L] = t[v]; else { int m = (L + R) >> 1; build(v + v + 1, L, m); build(v + v + 2, m + 1, R); } } int main() { int n, m; scanf("%d%d", &n, &m); vector<pair<pair<int, int>, int> > q; for (int i = 0; i < m; ++i) { int ql, qr, qval; scanf("%d%d%d", &ql, &qr, &qval), --ql, --qr; modify(0, 0, n - 1, ql, qr, qval); q.push_back(make_pair(make_pair(ql, qr), qval)); } build(0, 0, n - 1); N = n; for (int i = 0; i < N; ++i) T[i + N] = a[i]; for (int i = N - 1; i; --i) T[i] = T[i + i] & T[i + i + 1]; for (int i = 0; i < m; ++i) { if (query(q[i].first.first, q[i].first.second) != q[i].second) { printf("NO\n"); return 0; } } printf("YES\n"); for (int i = 0; i < n; ++i) printf("%d ", a[i]); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
/** * Date: 20 Nov, 2018 * Link: * * @author Prasad-Chaudhari * @linkedIn: https://www.linkedin.com/in/prasad-chaudhari-841655a6/ * @git: https://github.com/Prasad-Chaudhari */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; public class newProgram1 { public static void main(String[] args) throws IOException { // TODO code application logic here FastIO in = new FastIO(); int n = in.ni(); int m = in.ni(); Data l[] = new Data[m]; Data r[] = new Data[m]; Data d[] = new Data[m]; int i = 0; while (i < m) { int x = in.ni(); int y = in.ni(); int q = in.ni(); d[i] = new Data(x, y, q); l[i] = new Data(x, i, q); r[i] = new Data(y, i, q); ++i; } Arrays.sort(l); Arrays.sort(r); OrSegmentTree or = new OrSegmentTree(); or.getArray(new int[m]); or.build(0, m - 1, 1); int l_ind = 0; int r_ind = 0; int ans[] = new int[n]; for (i = 1; i <= n; i++) { while (l_ind < m && l[l_ind].l <= i) { or.update(0, m - 1, 1, l[l_ind].r - 1, l[l_ind].q); l_ind++; } ans[i - 1] = or.query(0, m - 1, 1, 0, m - 1); while (r_ind < m && r[r_ind].l <= i) { or.update(0, m - 1, 1, r[r_ind].r - 1, 0); r_ind++; } } AnsSegmentTree and = new AnsSegmentTree(); and.getArray(ans); and.build(0, n - 1, 1); for (i = 0; i < m; i++) { if (and.query(0, n - 1, 1, d[i].l - 1, d[i].r - 1) != d[i].q) { System.out.println("NO"); return; } } in.println("YES"); for (int j : ans) { in.println(j + ""); } in.bw.flush(); } static class AnsSegmentTree extends SegmentTree { public AnsSegmentTree() { DEFAULT = Integer.MAX_VALUE; } @Override public int keyFunc(int a, int b) { return a & b; } } static class OrSegmentTree extends SegmentTree { public OrSegmentTree() { DEFAULT = 0; } @Override public int keyFunc(int a, int b) { return a | b; } } static class SegmentTree { private int[] tree, array; int DEFAULT = 0; public void getArray(int[] a) { array = a; int si = a.length; double x = Math.log(si) / Math.log(2); int n = (int) (Math.pow(2, Math.ceil(x) + 1)) + 1; tree = new int[n]; } public void build(int start, int end, int pos) { if (start == end) { tree[pos] = array[start]; } else { int mid = (start + end) / 2; build(start, mid, 2 * pos); build(mid + 1, end, 2 * pos + 1); tree[pos] = keyFunc(tree[2 * pos], tree[2 * pos + 1]); } } public void update(int start, int end, int pos, int idx, int x) { if (start == end) { array[start] = x; tree[pos] = x; } else { int mid = (start + end) / 2; if (start <= idx && idx <= mid) { update(start, mid, 2 * pos, idx, x); } else { update(mid + 1, end, 2 * pos + 1, idx, x); } tree[pos] = keyFunc(tree[2 * pos], tree[2 * pos + 1]); } } public int query(int start, int end, int pos, int l, int r) { if (start > r || end < l) { return DEFAULT; } if (l <= start && end <= r) { return tree[pos]; } else { int mid = (start + end) / 2; int a = query(start, mid, 2 * pos, l, r); int b = query(mid + 1, end, 2 * pos + 1, l, r); return keyFunc(a, b); } } public void printTree() { for (int i = 0; i <= 2 * array.length; i++) { System.out.println(i + " " + tree[i]); } System.out.println(); } public int keyFunc(int a, int b) { return a + b; } } static class SegmentTree_LazyPropagation { static int DEFAULT = 1; int tree[]; int[] lazy_value; public void flush() { for (int i = 0; i < tree.length; i++) { tree[i] = 0; lazy_value[i] = 0; } } public SegmentTree_LazyPropagation(int si) { tree = new int[4 * si]; lazy_value = new int[4 * si]; } public int query(int start, int end, int pos, int l, int r) { if (end < l || r < start) { return DEFAULT; } if (toBeUpdated(pos)) { updateFunction(pos, lazy_value[pos]); if (start != end) { lazyUpdate(2 * pos, lazy_value[pos]); lazyUpdate(2 * pos + 1, lazy_value[pos]); } lazy_value[pos] = 0; } if (end < l || r < start) { return DEFAULT; } else if (l <= start && end <= r) { return tree[pos]; } else { int mid = (start + end) / 2; int a = query(start, mid, 2 * pos, l, r); int b = query(mid + 1, end, 2 * pos + 1, l, r); return keyFunction(a, b); } } public void update(int start, int end, int pos, int l, int r, int d) { if (start > r || end < l) { return; } if (toBeUpdated(pos)) { updateFunction(pos, lazy_value[pos]); if (start != end) { lazyUpdate(2 * pos, lazy_value[pos]); lazyUpdate(2 * pos + 1, lazy_value[pos]); } lazy_value[pos] = 0; } if (l <= start && end <= r) { updateFunction(pos, d); if (start != end) { lazyUpdate(2 * pos, d); lazyUpdate(2 * pos + 1, d); } } else { int mid = (start + end) / 2; update(start, mid, 2 * pos, l, r, d); update(mid + 1, end, 2 * pos + 1, l, r, d); tree[pos] = keyFunction(tree[2 * pos], tree[2 * pos + 1]); } } private int keyFunction(int a, int b) { return a & b; } private void lazyUpdate(int pos, int d) { lazy_value[pos] = d; } private void updateFunction(int pos, int d) { tree[pos] = d; } private boolean toBeUpdated(int pos) { return lazy_value[pos] != 0; } } static class FastIO { private final BufferedReader br; private final BufferedWriter bw; private String s[]; private int index; private StringBuilder sb; public FastIO() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8")); s = br.readLine().split(" "); sb = new StringBuilder(); index = 0; } public int ni() throws IOException { return Integer.parseInt(nextToken()); } public double nd() throws IOException { return Double.parseDouble(nextToken()); } public long nl() throws IOException { return Long.parseLong(nextToken()); } public String next() throws IOException { return nextToken(); } public String nli() throws IOException { try { return br.readLine(); } catch (IOException ex) { } return null; } public int[] gia(int n) throws IOException { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public int[] gia(int n, int start, int end) throws IOException { validate(n, start, end); int a[] = new int[n]; for (int i = start; i < end; i++) { a[i] = ni(); } return a; } public double[] gda(int n) throws IOException { double a[] = new double[n]; for (int i = 0; i < n; i++) { a[i] = nd(); } return a; } public double[] gda(int n, int start, int end) throws IOException { validate(n, start, end); double a[] = new double[n]; for (int i = start; i < end; i++) { a[i] = nd(); } return a; } public long[] gla(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public long[] gla(int n, int start, int end) throws IOException { validate(n, start, end); long a[] = new long[n]; for (int i = start; i < end; i++) { a[i] = nl(); } return a; } public int[][] gg(int n, int m) throws IOException { int adja[][] = new int[n + 1][]; int from[] = new int[m]; int to[] = new int[m]; int count[] = new int[n + 1]; for (int i = 0; i < m; i++) { from[i] = ni(); to[i] = ni(); count[from[i]]++; count[to[i]]++; } for (int i = 0; i <= n; i++) { adja[i] = new int[count[i]]; } for (int i = 0; i < m; i++) { adja[from[i]][--count[from[i]]] = to[i]; adja[to[i]][--count[to[i]]] = from[i]; } return adja; } public void print(String s) throws IOException { bw.write(s); } public void println(String s) throws IOException { bw.write(s); bw.newLine(); } private String nextToken() throws IndexOutOfBoundsException, IOException { if (index == s.length) { s = br.readLine().split(" "); index = 0; } return s[index++]; } private void validate(int n, int start, int end) { if (start < 0 || end >= n) { throw new IllegalArgumentException(); } } } static class Data implements Comparable<Data> { int l, r, q; public Data(int l, int r, int q) { this.l = l; this.r = r; this.q = q; } @Override public int compareTo(Data o) { if (l == o.l) { return Integer.compare(r, o.r); } return Integer.compare(l, o.l); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const int B = 317; const int N = 1e5 + 7; const long long mod = 998244353; const long long inf = 1e18; long long n, k; long long a[N]; long long l[N]; long long q[N]; long long r[N]; long long sum[N]; long long t[4 * N]; void build(int x, int l, int r) { if (l == r) { t[x] = a[l]; return; } int m = (l + r) / 2; build(x * 2, l, m); build(x * 2 + 1, m + 1, r); t[x] = t[x * 2] & t[x * 2 + 1]; } long long get(int x, int l, int r, int tl, int tr) { if (tl > tr) return (1LL << 30) - 1; if (l == tl && tr == r) return t[x]; int m = (l + r) / 2; return (get(x * 2, l, m, tl, min(tr, m)) & get(x * 2 + 1, m + 1, r, max(m + 1, tl), tr)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= k; i++) { cin >> l[i] >> r[i] >> q[i]; } for (long long i = 0; i < 31; i++) { for (int j = 1; j <= n; j++) sum[j] = 0; for (int j = 1; j <= k; j++) { if ((q[j] >> i) & 1) { sum[l[j]]++; sum[r[j] + 1]--; } } for (int j = 1; j <= n; j++) { sum[j] += sum[j - 1]; if (sum[j] > 0) a[j] |= (1LL << i); } } build(1, 1, n); for (int i = 1; i <= k; i++) { if (get(1, 1, n, l[i], r[i]) != q[i]) { cout << "NO"; return 0; } } cout << "YES\n"; for (int i = 1; i <= n; i++) { cout << a[i] << " "; } }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; // atharva washimkar // Oct 18, 2018 public class CODEFORCES_482_B { static int[] tree; public static void build (int i, int l, int r, int[] arr) { if (l == r) { tree[i] = arr[l]; } else { int mid = (l + r) / 2; build (i * 2 + 1, l, mid, arr); build (i * 2 + 2, mid + 1, r, arr); tree[i] = tree[i * 2 + 1] & tree[i * 2 + 2]; } } public static int query (int i, int l, int r, int ql, int qr) { if (ql == l && qr == r) return tree[i]; int mid = (l + r) / 2; if (qr <= mid) return query (i * 2 + 1, l, mid, ql, qr); else if (ql > mid) return query (i * 2 + 2, mid + 1, r, ql, qr); else return query (i * 2 + 1, l, mid, ql, mid) & query (i * 2 + 2, mid + 1, r, mid + 1, qr); } public static void main (String[] t) throws IOException { INPUT in = new INPUT (System.in); PrintWriter out = new PrintWriter (System.out); int N = in.iscan (), M = in.iscan (); final int BITS = 30; // diff array of bits that MUST be set to 1 int[][] ones = new int[BITS][N + 1]; int[] l = new int[M], r = new int[M], q = new int[M]; for (int m = 0; m < M; ++m) { l[m] = in.iscan () - 1; r[m] = in.iscan () - 1; q[m] = in.iscan (); for (int i = 0; i < BITS; ++i) { if (((q[m] >> i) & 1) == 1) { ++ones[i][l[m]]; --ones[i][r[m] + 1]; } } } // get real array from diff for (int i = 0; i < BITS; ++i) for (int n = 1; n < N + 1; ++n) ones[i][n] += ones[i][n - 1]; // generate answer int[] ans = new int[N]; for (int i = 0; i < BITS; ++i) for (int n = 0; n < N; ++n) if (ones[i][n] != 0) ans[n] |= (1 << i); // verify answer with segment tree tree = new int[4 * N]; build (0, 0, N - 1, ans); boolean good = true; for (int m = 0; m < M && good; ++m) good &= (query (0, 0, N - 1, l[m], r[m]) == q[m]); if (!good) out.print ("NO"); else { out.println ("YES"); for (int n = 0; n < N; ++n) out.print (ans[n] + " "); } out.close (); } private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static int gcd (int a, int b) { return b == 0 ? a : gcd (b, a % b); } public static int lcm (int a, int b) { return a * b / gcd (a, b); } public static int fast_pow_mod (int b, int x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static int fast_pow (int b, int x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const char InFile[] = "input.in"; const char OutFile[] = "output.out"; const int MaxN = 100111; int N, M; int T[32][MaxN], L[MaxN], R[MaxN], Q[MaxN]; int main() { cin >> N >> M; for (int i = 1; i <= M; ++i) { cin >> L[i] >> R[i] >> Q[i]; int X = Q[i]; for (int b = 0; b <= 30; ++b, X >>= 1) { if (X & 1) { T[b][L[i]] += 1; T[b][R[i] + 1] -= 1; } } } for (int b = 0; b <= 30; ++b) { int cnt = 0; bool bitSet = false; for (int i = 1; i <= N; ++i) { cnt += T[b][i]; if (cnt > 0) { bitSet = true; } else { bitSet = false; } if (bitSet) { T[b][i] = 1; } else { T[b][i] = 0; } T[b][i] += T[b][i - 1]; } } for (int i = 1; i <= M; ++i) { int X = Q[i]; for (int b = 0; b <= 30; ++b, X >>= 1) { if (X & 1) { continue; } if (T[b][R[i]] - T[b][L[i] - 1] == R[i] - L[i] + 1) { cout << "NO"; return 0; } } } cout << "YES\n"; for (int i = 1; i <= N; ++i) { int val = 0; for (int b = 30; b >= 0; --b) { val <<= 1; val += T[b][i] - T[b][i - 1]; } cout << val << " "; } 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 B { public static void main(String[] args) throws Exception { new B().solve(); } void solve() throws IOException { FastScanner in = new FastScanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[][] d = new int[n + 1][32]; int[] l = new int[m]; int[] r = new int[m]; int[] q = new int[m]; for (int mi = 0; mi < m; mi++) { l[mi] = in.nextInt() - 1; r[mi] = in.nextInt() - 1; q[mi] = in.nextInt(); for (int b = 0; (q[mi] >> b) > 0; b++) { if ((q[mi] >> b & 1) == 1) { d[l[mi]][b]++; d[r[mi] + 1][b]--; } } } for (int i = 1; i < d.length; i++) { for (int b = 0; b < d[0].length; b++) { d[i][b] += d[i - 1][b]; } } for (int i = 0; i < d.length; i++) { for (int b = 0; b < d[0].length; b++) { d[i][b] = d[i][b] > 0 ? 1 : 0; } } int[][] cnt = new int[n + 1][d[0].length]; for (int i = 1; i < d.length; i++) { for (int b = 0; b < d[0].length; b++) { cnt[i][b] = cnt[i - 1][b] + d[i - 1][b]; } } for (int mi = 0; mi < m; mi++) { int and = 0; for (int b = 0; b < cnt[0].length; b++) { if (cnt[r[mi] + 1][b] - cnt[l[mi]][b] == r[mi] + 1 - l[mi]) { and |= 1 << b; } } if (and != q[mi]) { // NG System.out.println("NO"); return; } } int[] v = new int[n]; for (int i = 0; i < n; i++) { for (int b = 0; b < d[0].length; b++) { v[i] |= d[i][b] << b; } } System.out.println("YES"); StringBuilder sb = new StringBuilder(); sb.append(v[0]); for (int i = 1; i < v.length; i++) { sb.append(" " + v[i]); } System.out.print(sb); } class FastScanner { private InputStream _stream; private byte[] _buf = new byte[1024]; private int _curChar; private int _numChars; private StringBuilder _sb = new StringBuilder(); FastScanner(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 String next() { int c = read(); while (isWhitespace(c)) { c = read(); } _sb.setLength(0); do { _sb.appendCodePoint(c); c = read(); } while (!isWhitespace(c)); return _sb.toString(); } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isWhitespace(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 (!isWhitespace(c)); return res * sgn; } public boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } } // // // // // // // // // // // // // // // // // // // // // // // // // //
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
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; const int base = 1331; const int 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...); } int s[N][31], n, m, f[N][31], a[N], sum[N][31], l[N], r[N], q[N], res, ST[4 * N]; void build(int id, int l, int r) { if (l == r) { ST[id] = a[l]; return; } int 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(int id, int l, int r, int u, int v) { if (l > v || r < u) return; if (l >= u && r <= v) { res = res & ST[id]; return; } int mid = (l + r) / 2; Get(id * 2, l, mid, u, v); Get(id * 2 + 1, mid + 1, r, u, v); } void read(void) {} signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> l[i] >> r[i] >> q[i]; for (int j = 0; j <= 30; j++) { if (q[i] >> j & 1 == 1) { s[l[i]][j]++; s[r[i] + 1][j]--; } } } for (int j = 0; j <= 30; j++) { for (int i = 1; i <= n; i++) { sum[i][j] = sum[i - 1][j] + s[i][j]; if (sum[i][j] > 0) f[i][j] = 1; else f[i][j] = 0; } } for (int i = 1; i <= n; i++) { for (int j = 0; j <= 30; j++) { if (f[i][j] == 1) a[i] += (1 << j); } } build(1, 1, n); for (int i = 1; i <= m; i++) { res = a[l[i]]; Get(1, 1, n, l[i], r[i]); if (res != q[i]) { cout << "NO"; return 0; } } cout << "YES" << '\n'; for (int i = 1; i <= n; i++) cout << a[i] << ' '; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; import java.math.*; import java.util.*; import static java.util.Arrays.*; public class B { private static final int mod = (int)1e9+7; final Random random = new Random(0); final IOFast io = new IOFast(); /// MAIN CODE public void run() throws IOException { // int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim()); int TEST_CASE = 1; while(TEST_CASE-- != 0) { int n = io.nextInt(); int m = io.nextInt(); Seg seg = new Seg(n); int[][] q = new int[m][]; for(int i = 0; i < m; i++) { int l = io.nextInt() - 1; int r = io.nextInt(); int v = io.nextInt(); q[i] = new int[]{l,r,v}; seg.or(l, r, v); } for(int i = 0; i < m; i++) { if(seg.and(q[i][0], q[i][1]) != q[i][2]) { io.out.println("NO"); return; } } io.out.println("YES"); for(int i = 0; i < n; i++) { io.out.print(seg.and(i, i + 1) + (i==n-1?"\n":" ")); } } } static class Seg { final int n; final int[] seg; final int[] add; public Seg(final int n) { this.n = Integer.highestOneBit(n) << 1; seg = new int[this.n << 1]; add = new int[this.n << 1]; } void propagate(int curL, int curR, int k) { if(add[k] != 0) { final int curM = (curL + curR) / 2; or(curL, curR, 2 * k + 1, add[k], curL, curM); or(curL, curR, 2 * k + 2, add[k], curM, curR); add[k] = 0; } } int and(int l, int r) { return and(l, r, 0, 0, n); } int and(int l, int r, int k, int curL, int curR) { if(curR <= l || curL >= r) return ~0; if(l <= curL && curR <= r) { return seg[k]; } propagate(curL, curR, k); final int curM = (curL + curR) / 2; return and(l, r, 2 * k + 1, curL, curM) & and(l, r, 2 * k + 2, curM, curR); } void or(int l, int r, int v) { or(l, r, 0, v, 0, n); } void or(int l, int r, int k, int v, int curL, int curR) { if(curR <= l || curL >= r) return; if(l <= curL && curR <= r) { seg[k] |= v; add[k] |= v; return; } final int curM = (curL + curR) / 2; propagate(curL, curR, k); or(l, r, 2 * k + 1, v, curL, curM); or(l, r, 2 * k + 2, v, curM, curR); seg[k] = seg[2*k+1] & seg[2*k+2]; } } /// TEMPLATE static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); } static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); } static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; } static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; } void main() throws IOException { // IOFast.setFileIO("rle-size.in", "rle-size.out"); try { run(); } catch (EndOfFileRuntimeException e) { } io.out.flush(); } public static void main(String[] args) throws IOException { new B().main(); } static class EndOfFileRuntimeException extends RuntimeException { private static final long serialVersionUID = -8565341110209207657L; } static public class IOFast { private BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); private PrintWriter out = new PrintWriter(System.out); void setFileIn(String ins) throws IOException { in.close(); in = new BufferedReader(new FileReader(ins)); } void setFileOut(String outs) throws IOException { out.flush(); out.close(); out = new PrintWriter(new FileWriter(outs)); } void setFileIO(String ins, String outs) throws IOException { setFileIn(ins); setFileOut(outs); } private static int pos, readLen; private static final char[] buffer = new char[1024 * 8]; private static char[] str = new char[500*8*2]; private static boolean[] isDigit = new boolean[256]; private static boolean[] isSpace = new boolean[256]; private static boolean[] isLineSep = new boolean[256]; static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; } public int read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; } public int nextInt() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; } public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; } public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } } int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; } int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; } public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); } public String nextString() throws IOException { return new String(next()); } public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); } public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; } public double nextDouble() throws IOException { return Double.parseDouble(nextString()); } public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; } public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; } public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } 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; int x[100005], y[100005], q[100005], ans[100005]; struct segtree { private: bool tree[100005 << 2]; public: void Clear(int l, int r, int rt) { tree[rt] = false; if (l == r) return; int m = (l + r) >> 1; Clear(l, m, rt << 1); Clear(m + 1, r, rt << 1 | 1); } void Pushdown(int rt) { if (tree[rt]) tree[rt << 1 | 1] = tree[rt << 1] = true; } void Pushup(int rt) { if (tree[rt << 1 | 1] && tree[rt << 1]) tree[rt] = true; } void Updata(int L, int R, int l, int r, int rt) { if (L <= l && r <= R) { tree[rt] = true; return; } Pushdown(rt); int m = (l + r) >> 1; if (L <= m) Updata(L, R, l, m, rt << 1); if (R > m) Updata(L, R, m + 1, r, rt << 1 | 1); Pushup(rt); } void Query(int L, int R, bool &ok, int l, int r, int rt) { if (ok || tree[rt]) return; if (L <= l && r <= R) { if (!tree[rt]) ok = true; return; } Pushdown(rt); int m = (l + r) >> 1; if (L <= m) Query(L, R, ok, l, m, rt << 1); if (R > m) Query(L, R, ok, m + 1, r, rt << 1 | 1); } void solve(int w, int l, int r, int rt) { if (l == r) { if (tree[rt]) ans[l] += (1 << w); return; } Pushdown(rt); int m = (l + r) >> 1; solve(w, l, m, rt << 1); solve(w, m + 1, r, rt << 1 | 1); } void Get(int v, bool &f, int l, int r, int rt) { if (l == r) { f = tree[v]; return; } Pushdown(rt); int m = (l + r) >> 1; if (v <= m) Get(v, f, l, m, rt << 1); else Get(v, f, m + 1, r, rt << 1 | 1); } void debug(int l, int r, int rt) { printf("[%d %d]:%d\n", l, r, (tree[rt] ? 1 : 0)); if (l == r) return; int m = (l + r) >> 1; debug(l, m, rt << 1); debug(m + 1, r, rt << 1 | 1); } } T[32]; int main() { int n, m, i, j, k, w, value; bool ok, f; scanf("%d%d", &n, &m); for (i = 0; i < 32; i++) T[i].Clear(1, n, 1); for (i = 1; i <= m; i++) { scanf("%d%d%d", &x[i], &y[i], &q[i]); for (j = 0; j < 31; j++) if (q[i] & (1 << j)) T[j].Updata(x[i], y[i], 1, n, 1); } for (i = 1; i <= m; i++) { for (j = 0; j < 31; j++) { if (!(q[i] & (1 << j))) { ok = false; T[j].Query(x[i], y[i], ok, 1, n, 1); if (!ok) { printf("NO\n"); return 0; } } } } printf("YES\n"); for (i = 0; i < 31; i++) T[i].solve(i, 1, n, 1); for (i = 1; i <= n; i++) printf("%d ", 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; struct Tree { int sz; vector<int> t; vector<int> d; Tree(int sz) : sz(sz), t(sz * 4), d(sz * 4) {} void push(int v) { d[2 * v + 1] |= d[v]; t[2 * v + 1] |= d[v]; d[2 * v + 2] |= d[v]; t[2 * v + 2] |= d[v]; d[v] = 0; } int get(int v) { return t[v] & d[v]; } void OrImpl(int v, int tl, int tr, int l, int r, int value) { if (l >= r) return; if (l == tl && r == tr) { t[v] |= value; d[v] |= value; return; } push(v); int tm = (tl + tr) / 2; OrImpl(2 * v + 1, tl, tm, l, min(r, tm), value); OrImpl(2 * v + 2, tm, tr, max(l, tm), r, value); t[v] = t[2 * v + 1] & t[2 * v + 2]; } void Or(int l, int r, int value) { OrImpl(0, 0, sz, l, r, value); } int GetAndImpl(int v, int tl, int tr, int l, int r) { if (l >= r) return -1; if (l == tl && r == tr) { return t[v]; } push(v); int tm = (tl + tr) / 2; return GetAndImpl(2 * v + 1, tl, tm, l, min(r, tm)) & GetAndImpl(2 * v + 2, tm, tr, max(l, tm), r); } int GetAnd(int l, int r) { return GetAndImpl(0, 0, sz, l, r); } }; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(m); vector<int> b(m); vector<int> c(m); Tree t(n); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i] >> c[i]; a[i]--; } for (int i = 0; i < m; i++) { t.Or(a[i], b[i], c[i]); } for (int i = 0; i < m; i++) { int x = t.GetAnd(a[i], b[i]); if (x != c[i]) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; for (int i = 0; i < n; i++) { cout << t.GetAnd(i, 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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn Agrawal coderbond007 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[][] q = new int[m][]; for (int i = 0; i < m; i++) { q[i] = new int[]{ in.nextInt() - 1, in.nextInt() - 1, in.nextInt() }; } int[] ret = new int[n]; for (int d = 0; d < 30; d++) { int[] count = new int[n + 1]; for (int i = 0; i < m; i++) { if ((q[i][2] & 1) == 1) { count[q[i][0]]++; count[q[i][1] + 1]--; } } for (int i = 1; i <= n; i++) { count[i] += count[i - 1]; } for (int i = 0; i <= n; i++) { count[i] = Math.min(count[i], 1); } int[] cum = new int[n + 1]; cum[0] = count[0]; for (int i = 1; i <= n; i++) { cum[i] = cum[i - 1] + count[i]; } for (int i = 0; i < m; i++) { if ((q[i][2] & 1) == 0) { int sum = cum[q[i][1]]; if (q[i][0] > 0) { sum -= cum[q[i][0] - 1]; } if (sum == q[i][1] - q[i][0] + 1) { out.println("NO"); return; } } } for (int i = 0; i < n; i++) { ret[i] |= count[i] << d; } for (int i = 0; i < m; i++) { q[i][2] >>= 1; } } out.println("YES"); for (int i = 0; i < n; i++) { if (i != 0) out.print(" "); out.print(ret[i]); } out.println(); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c == ',') { c = read(); } 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
#include <bits/stdc++.h> using namespace std; const int N = 100010, M = 35; long long prefix[M][N], bit[M][N]; int get_sum(int p, int left, int right) { int res = prefix[p][right]; if (left > 0) res -= prefix[p][left - 1]; return res; } int main() { int n, m; scanf("%d %d", &n, &m); vector<vector<int>> arr; arr.reserve(m); for (int i = 0; i < m; i++) { int left, right, q; scanf("%d %d %d", &left, &right, &q); for (long long j = 0; (1LL << j) <= q; j++) { if (q & (1 << j)) { bit[j][left - 1] += 1; bit[j][right] += -1; } } arr.push_back({left, right, q}); } for (int j = 0; j < M; j++) { int balance = 0; for (int i = 0; i < n; i++) { balance += bit[j][i]; prefix[j][i] = (balance > 0); if (i > 0) prefix[j][i] += prefix[j][i - 1]; } } for (auto it : arr) { for (int j = 0; j < M; j++) { if (((1LL << j) & it[2]) == 0 && get_sum(j, it[0] - 1, it[1] - 1) == it[1] - it[0] + 1) { printf("NO\n"); return 0; } } } printf("YES\n"); for (int i = 0; i < n; i++) { long long elem = 0; for (int j = 0; j < M; j++) { if (get_sum(j, i, i)) elem |= (1LL << j); } printf("%lld ", elem); } 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 lazy[5000001]; int it[5000001]; int l[1000001], r[1000001], u[1000001]; void push_down(int k, int l, int r) { if (l == r) return; it[2 * k] |= lazy[k]; it[2 * k + 1] |= lazy[k]; lazy[2 * k] |= lazy[k]; lazy[2 * k + 1] |= lazy[k]; } void update(int k, int l, int r, int p, int q, int x) { if (l > q || r < p) return; if (l >= p && r <= q) { it[k] |= x; lazy[k] |= x; return; } push_down(k, l, r); update(2 * k, l, (l + r) / 2, p, q, x); update(2 * k + 1, (l + r) / 2 + 1, r, p, q, x); it[k] = it[2 * k] & it[2 * k + 1]; } int get(int k, int l, int r, int p, int q) { if (l > q || r < p) return (1 << 30) - 1; push_down(k, l, r); if (l >= p && r <= q) return it[k]; return get(2 * k, l, (l + r) / 2, p, q) & get(2 * k + 1, (l + r) / 2 + 1, r, p, q); } int main() { int m, n, i; scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) { scanf("%d%d%d", &l[i], &r[i], &u[i]); update(1, 1, n, l[i], r[i], u[i]); } for (i = 1; i <= m; i++) { if (get(1, 1, n, l[i], r[i]) != u[i]) { printf("NO"); return 0; } } printf("YES\n"); for (i = 1; i <= n; i++) printf("%d ", get(1, 1, n, i, 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; template <class T> void read(T &x) { int f = 0; x = 0; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) f |= (ch == '-'); for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; if (f) x = -x; } const int N = 100005; int s[N][30], t[N][30], l[N], r[N], v[N]; int n, m; int main() { read(n), read(m); for (int i = (1); i <= (m); i++) { read(l[i]), read(r[i]), read(v[i]); for (int k = 0; k < (30); k++) { if (!(v[i] >> k & 1)) continue; s[l[i]][k]++, s[r[i] + 1][k]--; } } for (int i = (1); i <= (n); i++) { for (int k = 0; k < (30); k++) { s[i][k] += s[i - 1][k]; t[i][k] = t[i - 1][k] + (s[i][k] > 0); } } for (int i = (1); i <= (m); i++) { for (int k = 0; k < (30); k++) { int now = (t[r[i]][k] - t[l[i] - 1][k] == (r[i] - l[i] + 1)); if (now != (v[i] >> k & 1)) { puts("NO"); return 0; } } } puts("YES"); for (int i = (1); i <= (n); i++) { int val = 0; for (int k = 0; k < (30); k++) { if (s[i][k] > 0) val |= 1 << k; } printf("%d%c", val, " \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.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { // write your code here InputReader reader = new InputReader(System.in); PrintWriter writer = new PrintWriter(System.out,true); try { Task task = new Task(); task.solve(reader, writer); } finally { reader.close(); writer.close(); } } } class Task { static int MAX_BITS = 30; public int[] buildTree(int l, int r, int[] ans) { int[] t = new int[4*ans.length]; buildTree(t, 1, l, r, ans); return t; } public void buildTree(int[] t, int deep, int l, int r, int[] ans) { if (l+1 == r) { t[deep] = ans[l]; return; } int mid = (l+r) >> 1; buildTree(t, deep * 2, l, mid, ans); buildTree(t, deep * 2 + 1, mid, r, ans); t[deep] = (t[deep*2] & t[deep*2+1]); } public int query(int[] t, int deep, int l, int r, int bigL, int bigR) { if (l == bigL && r == bigR) { return t[deep]; } int mid = (bigL + bigR) >> 1; int ans = ((1 << MAX_BITS) - 1); if (l < mid) { ans &= query(t, 2*deep, l, Math.min(mid, r), bigL, mid); } if (mid < r) { ans &= query(t, 2* deep + 1, Math.max(mid, l), r, mid, bigR); } return ans; } public void solve(InputReader reader, PrintWriter writer) throws IOException { int n = reader.nextInt(); int m = reader.nextInt(); int[] xs = new int[n]; int[] qs = new int[m]; int[] ls = new int[m]; int[] rs = new int[m]; for (int i = 0 ; i < m ; i++){ ls[i] = reader.nextInt() - 1; rs[i] = reader.nextInt(); qs[i] = reader.nextInt(); } int[] sum = new int[n+10]; int[] ans = new int[n]; for (int bit = 0; bit < MAX_BITS; bit++) { for (int i=0; i < n; i++) sum[i] = 0; for (int i = 0; i < m; i ++) { if (0 < ((1 << bit) & qs[i])) { sum[ls[i]]++; sum[rs[i]]--; } } for (int i = 0; i < n; i++) { if (0 != i) sum[i] += sum[i-1]; if (0 < sum[i]) { ans[i] |= (1 << bit); } } } int[] tree = buildTree(0, n, ans); for (int i =0; i < m; i++) { if (qs[i] != query(tree,1, ls[i], rs[i], 0, n)) { writer.println("NO"); return; } } writer.println("YES"); for (int i =0; i < n; i++) { writer.print(ans[i]); writer.print(" "); } } } class InputReader { BufferedReader reader = null; StringTokenizer tokenizer = null; public InputReader(InputStream stream) { this.reader = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() throws IOException { while (null == tokenizer || !tokenizer.hasMoreTokens()) { String line = this.reader.readLine(); tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } public Integer nextInt() throws IOException { return Integer.parseInt(nextToken()); } public void close() throws IOException { this.reader.close(); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 1 << 17; const int MAXB = 30; int a[MAXN][MAXB]; int l[MAXN], r[MAXN], q[MAXN]; int T[2 * MAXN]; int offset = 1 << 17; int query(int i, int lo, int hi, int a, int b) { if (lo >= b || hi <= a) return (1 << MAXB) - 1; if (lo >= a && hi <= b) return T[i]; return query(i * 2, lo, (lo + hi) / 2, a, b) & query(i * 2 + 1, (lo + hi) / 2, hi, a, b); } int main(void) { int n, m; scanf("%d %d", &n, &m); for (int i = (0); i < (m); ++i) { scanf("%d %d %d", l + i, r + i, q + i); --l[i], --r[i]; for (int j = (0); j < (MAXB); ++j) if (q[i] & (1 << j)) a[l[i]][j]++, a[r[i] + 1][j]--; } int cur[MAXB]; for (int i = (0); i < (MAXB); ++i) cur[i] = 0; for (int i = (0); i < (n); ++i) { int x = 0; for (int j = (0); j < (MAXB); ++j) { cur[j] += a[i][j]; if (cur[j]) x |= 1 << j; } T[offset + i] = x; } for (int i = offset - 1; i > 0; --i) T[i] = T[i * 2] & T[i * 2 + 1]; for (int i = (0); i < (m); ++i) if (query(1, 0, offset, l[i], r[i] + 1) != q[i]) { puts("NO"); return 0; } puts("YES"); for (int i = (0); i < (n); ++i) printf("%d ", T[offset + i]); printf("\n"); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; int n, m, l, r, q, gl = 0; int ans[200000], seg[3000000], hl[200000], hr[200000], hq[200000]; void func(int pos, int l, int r, int ql, int qr, int x) { if (r < ql || l > qr) return; if (l >= ql && r <= qr) { seg[pos] = seg[pos] | x; return; } int mid = (l + r) / 2; func(2 * pos, l, mid, ql, qr, x); func(2 * pos + 1, mid + 1, r, ql, qr, x); seg[pos] = seg[pos] | (seg[2 * pos] & seg[2 * pos + 1]); } int get(int pos, int l, int r, int ql, int qr) { if (r < ql || l > qr) { return (1 << 30) - 1; } if (l >= ql && r <= qr) { return seg[pos]; } int mid = (l + r) / 2; int left = get(2 * pos, l, mid, ql, qr); int right = get(2 * pos + 1, mid + 1, r, ql, qr); return (left & right) | seg[pos]; } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; int m1 = m; for (int i = 0; i < m; i++) { cin >> l >> r >> q; l--, r--; hl[i] = l, hr[i] = r, hq[i] = q; func(1, 0, n - 1, l, r, q); } for (int i = 0; i < m; i++) { if (hq[i] != get(1, 0, n - 1, hl[i], hr[i])) { cout << "NO"; return 0; } } cout << "YES\n"; for (int i = 0; i < n; i++) { cout << get(1, 0, n - 1, i, i) << " "; } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 123; const int mod = 1e9 + 7; const int INF = 1e9 + 1; const long long INFL = 3e18 + 1; const double pi = acos(-1.0); int n, m; struct ramen { int l, r, val; } a[N]; int sum[N][50], ans[N][50]; int f[N], T[4 * N]; void build(int v = 1, int tl = 1, int tr = n) { if (tl == tr) { T[v] = f[tl]; return; } int tm = (tl + tr) / 2; build(v + v, tl, tm); build(v + v + 1, tm + 1, tr); T[v] = (T[v + v] & T[v + v + 1]); } int get(int l, int r, int v = 1, int tl = 1, int tr = n) { if (tl > r || tr < l) return (1 << 30) - 1; if (l <= tl && tr <= r) return T[v]; int tm = (tl + tr) / 2; int ans = (1 << 30) - 1; ans &= get(l, r, v + v, tl, tm); ans &= get(l, r, v + v + 1, tm + 1, tr); return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a[i].l >> a[i].r >> a[i].val; for (int j = 0; j < 30; j++) { if ((a[i].val & (1 << j))) { sum[a[i].l][j]++; sum[a[i].r + 1][j]--; } } } for (int j = 0; j < 30; j++) { int cnt = 0; for (int i = 1; i <= n; i++) { cnt += sum[i][j]; if (cnt >= 1) ans[i][j] = 1; } } for (int i = 1; i <= n; i++) { int cnt = 0; for (int j = 0; j < 30; j++) { if (ans[i][j]) cnt += (1 << j); } f[i] = cnt; } build(); for (int i = 1; i <= m; i++) { if (get(a[i].l, a[i].r) != a[i].val) { cout << "NO"; return 0; } } cout << "YES\n"; for (int i = 1; i <= n; i++) cout << f[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.*; import java.util.*; public class segTree { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(), m = sc.nextInt(); Triple[] queries = new Triple[m]; for(int i=0;i<m;i++) queries[i] = new Triple(sc.nextInt(), sc.nextInt(), sc.nextInt()); int[] arr = buildArray(n, queries); SegmentTree st = new SegmentTree(arr); boolean ans = true; for(Triple tp: queries) if(tp.val != st.query(tp.l, tp.r)) { ans = false; break; } // System.out.println(Arrays.toString(arr)); if(ans) { out.println("YES"); for(int i=1; i<=n;i++) out.print(arr[i]+" "); } else out.println("NO"); out.close(); } private static int[] buildArray(int n, Triple[] q){ int[][] bits = new int[n][30]; for(Triple tp:q) { int l = tp.l - 1, r = tp.r - 1, val = tp.val; for (int i = 0; i < 30; i++) if ((val & (1 << i)) > 0) { bits[l][i]++; if (r < n - 1) bits[r + 1][i]--; } } for (int i = 1; i < n; i++) for (int j = 0; j < 30; j++) bits[i][j] += bits[i - 1][j]; int size = 1; while (size < n) size *= 2; int[] arr = new int[size + 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < 30; j++) { if (bits[i][j] > 0) arr[i + 1] += (1 << j); } } return arr; } static class SegmentTree { // 1-based DS, OOP int N; // the number of elements in the array as a power of 2 (i.e. after padding) int[] array, sTree, lazy; SegmentTree(int[] in) { array = in; N = in.length - 1; sTree = new int[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero // lazy = new int[N << 1]; build(1, 1, N); } void build(int node, int b, int e) // O(n) { if (b == e) sTree[node] = array[b]; else { int mid = b + e >> 1; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); sTree[node] = sTree[node << 1] & sTree[node << 1 | 1]; } } void update_point(int index, int val) // O(log n) { index += N - 1; sTree[index] += val; while (index > 1) { index >>= 1; sTree[index] = sTree[index << 1] + sTree[index << 1 | 1]; } } void update_range(int i, int j) // O(log n) { update_range(1, 1, N, i, j); } void update_range(int node, int b, int e, int i, int j) { if (i > e || j < b) return; if (b >= i && e <= j) { sTree[node] = (e - b + 1) - sTree[node]; lazy[node] += 1; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node << 1, b, mid, i, j); update_range(node << 1 | 1, mid + 1, e, i, j); sTree[node] = sTree[node << 1] + sTree[node << 1 | 1]; } } void propagate(int node, int b, int mid, int e) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; if (lazy[node] % 2 == 1) { sTree[node << 1] = (mid - b + 1) - sTree[node << 1]; sTree[node << 1 | 1] = (e - mid) - sTree[node << 1 | 1]; } lazy[node] = 0; } int query(int i, int j) { return query(1, 1, N, i, j); } int query(int node, int b, int e, int i, int j) // O(log n) { if (i > e || j < b) return (1<<31) - 1; if (b >= i && e <= j) return sTree[node]; int mid = b + e >> 1; // propagate(node, b, mid, e); int q1 = query(node << 1, b, mid, i, j); int q2 = query(node << 1 | 1, mid + 1, e, i, j); return q1 & q2; } } static class Triple{ int l,r,val; public Triple(int l, int r, int val) { this.l = l; this.r = r; this.val = val; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public boolean ready() throws IOException { return br.ready(); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class InterestingArray { static final int INF = (1 << 30) - 1; void solve() { int n = in.nextInt(), m = in.nextInt(); int[] l = new int[m], r = new int[m], q = new int[m]; for (int i = 0; i < m; i++) { l[i] = in.nextInt() - 1; r[i] = in.nextInt(); q[i] = in.nextInt(); } int[] res = new int[n]; for (int i = 0; i < 30; i++) { int[] ps = new int[n + 1]; for (int j = 0; j < m; j++) { if (((q[j] >> i) & 1) == 1) { ps[l[j]]++; ps[r[j]]--; } } for (int j = 1; j < n; j++) { ps[j] += ps[j - 1]; } for (int j = 0; j < n; j++) { if (ps[j] > 0) res[j] |= 1 << i; } } SegmentTree st = new SegmentTree(res); for (int i = 0; i < m; i++) { if (st.query(l[i], r[i]) != q[i]) { out.println("NO"); return; } } out.println("YES"); for (int i = 0; i < n; i++) { if (i > 0) out.print(' '); out.print(res[i]); } out.println(); } static class SegmentTree { int n; int[] d; SegmentTree(int[] a) { int m = a.length; int x = 0; while ((1 << x) < m) x++; n = 1 << x; d = new int[2 * n - 1]; for (int i = 0; i < n; i++) { if (i < m) d[n - 1 + i] = a[i]; else d[n - 1 + i] = INF; } for (int i = n - 2; i >= 0; i--) { d[i] = d[2 * i + 1] & d[2 * i + 2]; } } int query(int a, int b) { return query(a, b, 0, 0, n); } int query(int a, int b, int k, int l, int r) { if (a >= r || b <= l) return INF; if (a <= l && r <= b) return d[k]; return query(a, b, 2 * k + 1, l, (l + r) / 2) & query(a, b, 2 * k + 2, (l + r) / 2, r); } } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new InterestingArray().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(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; const int N = 110000; vector<int> ans; struct Node { int l, r, q; }; Node a[N]; Node tree[10 * N]; void build(int root, int l, int r) { tree[root].l = l; tree[root].r = r; tree[root].q = 0; if (l == r) return; build(root * 2, l, (l + r) / 2); build(root * 2 + 1, (l + r) / 2 + 1, r); } void update(int root, int l, int r, int q) { if (tree[root].l == l && tree[root].r == r) { tree[root].q |= q; return; } else { int mid = (tree[root].l + tree[root].r) / 2; if (r <= mid) update(root * 2, l, r, q); else if (l > mid) update(root * 2 + 1, l, r, q); else { update(root * 2, l, mid, q); update(root * 2 + 1, mid + 1, r, q); } } } int query(int root, int l, int r) { if (tree[root].l == l && tree[root].r == r) return tree[root].q; else { int mid = (tree[root].l + tree[root].r) / 2; if (r <= mid) return query(root * 2, l, r); else if (l > mid) return query(root * 2 + 1, l, r); else return query(root * 2, l, mid) & query(root * 2 + 1, mid + 1, r); } } void solve(int x) { if (x != 1) tree[x].q |= tree[x / 2].q; if (tree[x].l == tree[x].r) { ans.push_back(tree[x].q); return; } solve(2 * x); solve(2 * x + 1); } int n, m; int main() { scanf("%d %d", &n, &m); build(1, 1, n); for (int i = 0; i < m; i++) { scanf("%d %d %d", &a[i].l, &a[i].r, &a[i].q); update(1, a[i].l, a[i].r, a[i].q); } bool ok = true; for (int i = 0; i < m; i++) { if (query(1, a[i].l, a[i].r) == a[i].q) continue; ok = false; break; } if (ok) { printf("YES\n"); solve(1); for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]); printf("\n"); } else printf("NO\n"); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = [] for _ in range(m): l, r, q = map(int, input().split()) l -= 1 r -= 1 a.append((l, r, q)) res = [0] * n bad = False for i in range(30): events = [0] * (n + 1) for l, r, q in a: if q & (1 << i): events[l] += 1 events[r + 1] -= 1 c = 0 for j in range(n): c += events[j] if c > 0: res[j] |= (1 << i) s = [0] * (n + 1) for j in range(n): s[j + 1] = s[j] + ((res[j] >> i) & 1) for l, r, q in a: if q & (1 << i) == 0: if s[r + 1] - s[l] == r - l + 1: bad = True break if bad: 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
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int inf = (1 << 30) - 1; int t[4 * N]; int bit[30]; vector<int> seg[N][2]; void build(int v, int tl, int tr, vector<int> &a) { if (tl == tr) { t[v] = a[tl]; return; } int tm = (tl + tr) >> 1; build(2 * v + 1, tl, tm, a); build(2 * v + 2, tm + 1, tr, a); t[v] = t[2 * v + 1] & t[2 * v + 2]; } int get(int v, int tl, int tr, int l, int r) { if (l > r) return inf; if (tl == l && tr == r) { return t[v]; } int tm = (tl + tr) >> 1; int gl = get(2 * v + 1, tl, tm, l, min(r, tm)); int gr = get(2 * v + 2, tm + 1, tr, max(l, tm + 1), r); return gl & gr; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<pair<pair<int, int>, int> > t; for (int i = 0; i < m; i++) { int l, r, q; cin >> l >> r >> q; t.push_back(make_pair(make_pair(l - 1, r - 1), q)); seg[l - 1][0].push_back(q); seg[r - 1][1].push_back(q); } vector<int> a(n, inf); for (int i = 0; i < n; i++) { for (int x : seg[i][0]) { for (int j = 0; j < 30; j++) { bit[j] += (x >> j) & 1; } } int cur = 0; for (int j = 0; j < 30; j++) { if (bit[j]) { cur |= (1 << j); } } a[i] = cur; for (int x : seg[i][1]) { for (int j = 0; j < 30; j++) { bit[j] -= (x >> j) & 1; } } } build(0, 0, n - 1, a); for (int i = 0; i < m; i++) { if (get(0, 0, n - 1, t[i].first.first, t[i].first.second) != t[i].second) { cout << "NO\n"; return 0; } } cout << "YES\n"; for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn Agrawal coderbond007 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[][] q = new int[m][]; for (int i = 0; i < m; i++) { q[i] = new int[]{ in.nextInt() - 1, in.nextInt() - 1, in.nextInt() }; } int[] ret = new int[n]; for (int d = 0; d < 30; d++) { int[] count = new int[n + 1]; for (int i = 0; i < m; i++) { if (q[i][2] << ~d < 0) { count[q[i][0]]++; count[q[i][1] + 1]--; } } for (int i = 1; i < n; i++) { count[i] += count[i - 1]; } for (int i = 0; i < n; i++) { if (count[i] > 0) count[i] = 1; } int[] cum = new int[n + 1]; for (int i = 1; i <= n; i++) { cum[i] = cum[i - 1] + count[i - 1]; } for (int i = 0; i < m; i++) { if (q[i][2] << ~d >= 0) { if (cum[q[i][1] + 1] - cum[q[i][0]] == q[i][1] - q[i][0] + 1) { out.println("NO"); return; } } } for (int i = 0; i < n; i++) { ret[i] |= count[i] << d; } } out.println("YES"); for (int i = 0; i < n; i++) { if (i != 0) out.print(" "); out.print(ret[i]); } out.println(); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c == ',') { c = read(); } 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
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& output, vector<T>& v) { output << "[ "; if (int(v.size())) { output << v[0]; } for (int i = 1; i < int(v.size()); i++) { output << ", " << v[i]; } output << " ]"; return output; } template <typename T1, typename T2> ostream& operator<<(ostream& output, pair<T1, T2>& p) { output << "( " << p.first << ", " << p.second << " )"; return output; } template <typename T> ostream& operator,(ostream& output, T x) { output << x << " "; return output; } int bitMat[32][100007]; int comuZeroBit[32][100007]; vector<pair<int, int> > v[32]; int main() { int n, m; int l, r, q; cin >> n >> m; for (int tmp = 0; tmp < m; tmp++) { cin >> l >> r >> q; for (int i = 0; i < 32; i++) { if ((q & (1 << i))) { bitMat[i][l]++; bitMat[i][r + 1]--; } else { v[i].push_back(make_pair(l, r)); } } } for (int i = 0; i < 32; i++) { int sum = 0; for (int j = 1; j <= n; j++) { sum += bitMat[i][j]; if (sum > 0) { bitMat[i][j] = 1; } else { comuZeroBit[i][j] = 1; } comuZeroBit[i][j] += comuZeroBit[i][j - 1]; } } bool pos = 1; for (int i = 0; i < 32; i++) { for (int j = 0; j < int(v[i].size()); j++) { l = v[i][j].first; r = v[i][j].second; if ((comuZeroBit[i][r] - comuZeroBit[i][l - 1]) == 0) { pos = false; } } } if (pos) { cout << "YES" << endl; for (int i = 1; i <= n; i++) { int num = 0; for (int j = 0; j < 32; j++) { if (bitMat[j][i] == 1) { num = num | (1 << j); } } cout << num << ' '; } } else { cout << "NO" << 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
#include <bits/stdc++.h> using namespace std; const int B = 31, N = 100012; int n, m, q, w, y, l[N], r[N], x[N]; int s[N][B + 1], ss[N][B + 1], cur[B + 1], a[N], ll, rr, xx; int sum(int l, int r, int x) { return ss[r][x] - ss[l - 1][x]; } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> l[i] >> r[i] >> x[i]; for (int j = 0; j < B; j++) { q = 1 << j; w = q & x[i]; if (w != 0) { s[l[i]][j]++; s[r[i] + 1][j]--; } } } for (int i = 1; i <= n; i++) { for (int j = 0; j < B; j++) { cur[j] += s[i][j]; if (cur[j] != 0) a[i] += 1 << j; ss[i][j] = ss[i - 1][j]; if (cur[j] != 0) ss[i][j]++; } } for (int i = 1; i <= m; i++) { for (int j = 0; j < B; j++) { q = 1 << j; w = q & x[i]; if (w == 0) { if (sum(l[i], r[i], j) == (r[i] - l[i] + 1)) { cout << "NO"; return 0; } } else { if (sum(l[i], r[i], j) != (r[i] - l[i] + 1)) { cout << "NO"; return 0; } } } } cout << "YES" << endl; for (int i = 1; i <= n; i++) cout << a[i] << ' '; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const long long L = 30; const long long INF = 1e9; const long long N = 1e5 + 1; const long long mod = 1e9 + 7; const long double eps = 1E-7; int n, m, now, l[N], r[N], x[N]; int a[N], p[L], d[L][N], t[4 * N]; void bld(int v, int tl, int tr) { if (tl == tr) t[v] = a[tl]; else { int tm = (tl + tr) / 2; bld(v * 2, tl, tm); bld(v * 2 + 1, tm + 1, tr); t[v] = t[v * 2] & t[v * 2 + 1]; } } int get(int v, int tl, int tr, int l, int r) { if (l > r) return (1 << 30) - 1; if (tl == l && tr == r) return t[v]; else { int tm = (tl + tr) / 2; return get(v * 2, tl, tm, l, min(tm, r)) & get(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> l[i] >> r[i] >> x[i]; for (int j = 0; j < L; j++) if (x[i] >> j & 1) d[j][l[i]]++, d[j][r[i] + 1]--; } for (int i = 1; i <= n; i++) { now = 0; for (int j = 0; j < L; j++) { p[j] += d[j][i]; if (p[j] > 0) now += (1 << j); } a[i] = now; } bld(1, 1, n); for (int i = 1; i <= m; i++) { if (get(1, 1, n, l[i], r[i]) != x[i]) { cout << "NO"; return 0; } } cout << "YES\n"; for (int i = 1; i <= n; i++) cout << a[i] << " "; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; struct Cond { int l, r, q; }; struct PCond { int type; int p, q; }; bool operator<(const PCond& left, const PCond& right) { if (left.p != right.p) return left.p < right.p; return left.type < right.type; } int build_mask(int* vmask) { int ret = 0; for (int i = 0; i < 32; i++) if (vmask[i]) ret |= (1 << i); return ret; } void add_mask(int* vmask, int q) { int i = 0; while (q) { if (q & 1) vmask[i]++; q >>= 1; i++; } } void sub_mask(int* vmask, int q) { int i = 0; while (q) { if (q & 1) vmask[i]--; q >>= 1; i++; } } class Segment { public: Segment(int l, int r, const std::vector<int>& a) { this->l = l; this->r = r; if (l + 1 == r) { this->v = a[l]; this->left = this->right = NULL; } else { int m = (l + r) / 2; this->left = new Segment(l, m, a); this->right = new Segment(m, r, a); this->v = left->v & right->v; } } ~Segment() { delete left; delete right; } int Eval(int l, int r) { if (r <= this->l || l >= this->r) return -1; if (l <= this->l && r >= this->r) return v; return left->Eval(l, r) & right->Eval(l, r); } private: int l, r, v; Segment *left, *right; }; int main(int argc, const char* argv[]) { int n, m; std::vector<int> a; vector<Cond> conds; vector<PCond> pconds; scanf("%d %d\n", &n, &m); a.resize(n); pconds.resize(2 * m); conds.resize(m); for (int i = 0; i < m; i++) { int l, r, q; scanf("%d %d %d\n", &l, &r, &q); conds[i].l = l - 1; conds[i].r = r; conds[i].q = q; pconds[2 * i].type = 1; pconds[2 * i].p = l - 1; pconds[2 * i].q = q; pconds[2 * i + 1].type = 0; pconds[2 * i + 1].p = r; pconds[2 * i + 1].q = q; } sort(pconds.begin(), pconds.end()); int vmask[32] = {}; int mask = 0; int c = 0; for (int i = 0; i < n; i++) { while (c < pconds.size() && pconds[c].p == i) { if (pconds[c].type == 0) sub_mask(vmask, pconds[c].q); else add_mask(vmask, pconds[c].q); mask = build_mask(vmask); c++; } a[i] = mask; } Segment root(0, n, a); for (typeof((conds).end()) c = (conds).begin(); c != (conds).end(); ++c) { if (root.Eval(c->l, c->r) != c->q) { printf("NO\n"); return 0; } } printf("YES\n"); for (int i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
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.FileReader; 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; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BInterestingArray solver = new BInterestingArray(); solver.solve(1, in, out); out.close(); } static class BInterestingArray { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int n = sc.nextInt(), q = sc.nextInt(), N = 1; while (N < n) N <<= 1; BInterestingArray.SegmentTree sTree = new BInterestingArray.SegmentTree(new int[N + 1]); BInterestingArray.query[] queries = new BInterestingArray.query[q]; for (int i = 0; i < q; i++) { queries[i] = new BInterestingArray.query(sc.nextInt(), sc.nextInt(), sc.nextInt()); sTree.update_range(queries[i].left, queries[i].right, queries[i].x); } for (int i = 0; i < q; i++) { int compare = sTree.query(queries[i].left, queries[i].right); if (queries[i].x != compare) { pw.println("NO"); return; } } pw.println("YES"); sTree.propagateAll(); for (int i = 1; i <= n; i++) { pw.print(sTree.sTree[sTree.N + i - 1] + " "); } } static public class SegmentTree { int N; int[] array; int[] sTree; int[] lazy; boolean[] lazy2; 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]; lazy2 = new boolean[N << 1]; } void update_range(int i, int j, int val) { update_range(1, 1, N, i, j, val); } void update_range(int node, int b, int e, int i, int j, int val) { if (i > e || j < b) return; if (b >= i && e <= j) { sTree[node] |= val; lazy[node] |= val; } else { int mid = b + e >> 1; propagate(node); update_range(node << 1, b, mid, i, j, val); update_range(node << 1 | 1, mid + 1, e, i, j, val); sTree[node] = sTree[node << 1] & sTree[node << 1 | 1]; } } void propagate(int node) { lazy[node << 1] |= lazy[node]; lazy[node << 1 | 1] |= lazy[node]; sTree[node << 1] |= lazy[node]; sTree[node << 1 | 1] |= lazy[node]; lazy[node] = 0; } void propagateAll() { propagateAll(1, 1, N); } void propagateAll(int node, int b, int e) { if (b == e) return; propagate(node); propagateAll(node << 1, b, (b + e) / 2); propagateAll((node << 1) + 1, (b + e) / 2 + 1, e); } 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) { if (i > e || j < b) return (1 << 31) - 1; if (b >= i && e <= j) return sTree[node]; int mid = b + e >> 1; propagate(node); int q1 = query(node << 1, b, mid, i, j); int q2 = query(node << 1 | 1, mid + 1, e, i, j); return q1 & q2; } } static class query { int left; int right; int x; public query(int left, int right, int x) { this.left = left; this.right = right; this.x = x; } public String toString() { return left + " " + right + " " + x; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.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 = 100000 + 10; int n, k; int l[maxn], r[maxn], q[maxn], ans[maxn], sum[maxn]; struct Segment { int val; } tree[maxn * 4]; void built(int v, int l, int r) { if (l + 1 == r) { tree[v].val = ans[l]; return; } else { int mid = (l + r) >> 1; built(v << 1, l, mid); built((v << 1) + 1, mid, r); tree[v].val = tree[v << 1].val & tree[(v << 1) + 1].val; } } int query(int v, int l, int r, int L, int R) { if (l == L && r == R) return tree[v].val; else { int mid = (L + R) >> 1; int ans = (1 << 30) - 1; if (l < mid) ans &= query(v << 1, l, min(r, mid), L, mid); if (r > mid) ans &= query((v << 1) + 1, max(l, mid), r, mid, R); return ans; } } int main() { scanf("%d %d", &n, &k); for (int i = 0; i < k; i++) { scanf("%d %d %d", &l[i], &r[i], &q[i]); l[i]--; } for (int bit = 0; bit <= 30; bit++) { memset(sum, 0, sizeof(sum)); for (int i = 0; i < k; i++) { if (q[i] & 1 << bit) { sum[l[i]]++; sum[r[i]]--; } } for (int i = 0; i < n; i++) { if (i) sum[i] += sum[i - 1]; if (sum[i] > 0) { ans[i] |= (1 << bit); } } } built(1, 0, n); for (int i = 0; i < k; i++) { if (query(1, l[i], r[i], 0, n) != q[i]) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; for (int i = 0; i < n; i++) { if (i) cout << " "; cout << 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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); ProblemBInterestingTask solver = new ProblemBInterestingTask(); solver.solve(1, in, out); out.close(); } static class ProblemBInterestingTask { int N; int M; int[][] qq; public void solve(int testNumber, FastScanner s, PrintWriter out) { N = s.nextInt(); M = s.nextInt(); qq = s.next2DIntArray(M, 3); int[] a = new int[N + 1]; for (int pp = 0, p = 1; pp < 30; pp++, p <<= 1) { int[] d = new int[N + 1]; for (int[] q : qq) { if ((p & q[2]) > 0) { d[q[0] - 1]++; d[q[1]]--; } } for (int i = 1; i <= N; i++) d[i] += d[i - 1]; for (int i = 0; i < N; i++) if (d[i] > 0) a[i] += p; } SegTree st = new SegTree(N, a); for (int[] q : qq) { if (st.and(1, q[0] - 1, q[1] - 1) != q[2]) { out.println("NO"); return; } } out.println("YES"); for (int i = 0; i < N; i++) out.printf("%d ", a[i]); out.println(); } class SegTree { int n; int[] lo; int[] hi; int[] oa; int[] and; SegTree(int nn, int[] aa) { n = nn; lo = new int[n << 2 + 1]; hi = new int[n << 2 + 1]; and = new int[n << 2 + 1]; oa = aa; init(1, 0, n - 1); } int and(int i, int a, int b) { if (b < lo[i] || hi[i] < a) return (1 << 30) - 1; if (a <= lo[i] && hi[i] <= b) return and[i]; return and(2 * i, a, b) & and(2 * i + 1, a, b); } void init(int i, int a, int b) { lo[i] = a; hi[i] = b; if (a == b) { and[i] = oa[a]; return; } int m = (a + b) >> 1; init(2 * i, a, m); init(2 * i + 1, m + 1, b); and[i] = and[2 * i] & and[2 * i + 1]; } } } static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public int[] nextIntArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = this.nextInt(); return ret; } public int[][] next2DIntArray(int N, int M) { int[][] ret = new int[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextIntArray(M); return ret; } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
//package com.himanshu.practice.july20.hour11; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by himanshubhardwaj on 20/07/18. */ public class InterestingArray { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n, m; String str[] = br.readLine().split(" "); n = Integer.parseInt(str[0]); m = Integer.parseInt(str[1]); int[] l = new int[m]; int[] r = new int[m]; int[] q = new int[m]; int[] arr = new int[n]; constructArray(n, m, br, arr, l, r, q); SegentTree st = new SegentTree(arr); for (int i = 0; i < m; i++) { if (!validate(st, l[i], r[i], q[i])) { System.out.println("NO"); return; } } System.out.println("YES"); for (int x : arr) { System.out.print(x + " "); } } private static boolean validate(SegentTree st, int l, int r, int q) { int sN = st.getAndedNumber(l, r); //System.out.println(sN + "\t\t" + q); if (sN == q) { return true; } return false; } private static void constructArray(int n, int m, BufferedReader br, int[] arr, int[] l, int[] r, int[] q) throws IOException { int count[]; getInput(l, r, q, m, br); int bit = 0; for (int bitN = 0; bitN < 31; bitN++) { count = new int[n]; bit = (1 << bitN); for (int i = 0; i < m; i++) { if ((q[i] & bit) > 0) { count[l[i]]++; if (r[i] < (n - 1)) { count[r[i] + 1]--; } } } for (int i = 1; i < n; i++) { count[i] = count[i] + count[i - 1]; } for (int i = 0; i < n; i++) { if (count[i] > 0) { arr[i] = arr[i] | bit; } } // if (bitN < 3) { // System.out.print("BIT: " + bitN + "\t\t"); // for (int i = 0; i < n; i++) { // System.out.print(count[i] + " "); // } // System.out.println(); // } } // for (int x : arr) { // System.out.print(x + " "); // } // System.out.println(); } private static void getInput(int[] l, int[] r, int[] q, int m, BufferedReader br) throws IOException { for (int i = 0; i < m; i++) { String st[] = br.readLine().split(" "); l[i] = Integer.parseInt(st[0]); r[i] = Integer.parseInt(st[1]); q[i] = Integer.parseInt(st[2]); l[i]--; r[i]--; } } } class SegentTree { int segmentTree[]; int lastPos; public SegentTree(int[] arr) { int size = (int) Math.pow(2, Math.ceil(Math.log(arr.length) / Math.log(2))); size = 2 * size - 1; segmentTree = new int[size]; for (int i = 0; i <= segmentTree.length / 2; i++) { if (i < arr.length) { segmentTree[i + segmentTree.length / 2] = arr[i]; lastPos = i + segmentTree.length / 2; } else { segmentTree[i + segmentTree.length / 2] = Integer.MAX_VALUE; } } for (int i = segmentTree.length / 2 - 1; i >= 0; i--) { segmentTree[i] = segmentTree[2 * i + 1] & segmentTree[2 * i + 2]; } //printSegmentTree(); } //both l and r included public int getAndedNumber(int l, int r) { return getAndedNumberHelper(0, segmentTree.length / 2, l, r, 0); } private int getAndedNumberHelper(int segTreeStart, int segTreeEnd, int l, int r, int segTreeIndex) { if (segTreeStart < 0 || segTreeEnd < segTreeStart || l > segTreeEnd || r < segTreeStart || r < l || segTreeIndex > lastPos || segTreeEnd < 0) { return Integer.MAX_VALUE; //FFFF...F } if (l <= segTreeStart && r >= segTreeEnd) { return segmentTree[segTreeIndex]; } int mid = segTreeStart + (segTreeEnd - segTreeStart) / 2; return getAndedNumberHelper(segTreeStart, mid, l, r, 2 * segTreeIndex + 1) & getAndedNumberHelper(mid + 1, segTreeEnd, l, r, 2 * segTreeIndex + 2); } public void printSegmentTree() { System.out.println("SEgment Tree Size: " + segmentTree.length); for (int i = 0; i < segmentTree.length; i++) { System.out.print(segmentTree[i] + " "); } System.out.println(); } } /* * 1075 25 421 597 277086208 769 973 277089792 66 506 277090304 540 762 277090048 201 809 277086208 512 561 277086208 222 726 277086208 728 1036 277089280 506 659 277086208 396 859 277086208 562 663 277090048 618 675 277090048 846 884 277089920 618 1055 277089280 1047 1057 277089760 423 956 277086208 783 1062 277089280 1000 1021 277089792 565 1027 277089792 252 1003 277086208 11 878 277086208 577 676 277090048 569 1054 277089280 562 1014 277089792 859 945 277089792 * * */
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1}; long long sum[100005 * 4], prop[100005 * 4]; pair<int, pair<int, int> > p[100005]; long long a[100005]; void propagate(int l, int r, int k) { sum[k] = sum[k] | prop[k]; if (l != r) { prop[k * 2] = prop[k * 2] | prop[k]; prop[k * 2 + 1] = prop[k * 2 + 1] | prop[k]; prop[k] = 0; } } void update(int l, int r, int k, int L, int R, int val) { propagate(l, r, k); if (l > R || r < L) return; if (l >= L && r <= R) { prop[k] = val; propagate(l, r, k); return; } int mid = (l + r) / 2; update(l, mid, k * 2, L, R, val); update(mid + 1, r, k * 2 + 1, L, R, val); sum[k] = sum[k * 2] & sum[k * 2 + 1]; } long long query(int l, int r, int k, int L, int R) { propagate(l, r, k); if (l > R || r < L) return ((long long)1 << 30) - 1; if (l >= L && r <= R) return sum[k]; int mid = (l + r) / 2; return query(l, mid, k * 2, L, R) & query(mid + 1, r, k * 2 + 1, L, R); } void init(int n) { for (int i = 0; i < (n * 4); i++) { prop[i] = ((long long)1 << 30) - 1; sum[i] = ((long long)1 << 30) - 1; } } int qry[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < (m); i++) { cin >> p[i].second.first >> p[i].second.second >> p[i].first; } for (int i = 0; i < (m); i++) { update(1, n, 1, p[i].second.first, p[i].second.second, p[i].first); } int cnt = 0; for (int i = 0; i < (m); i++) { if (query(1, n, 1, p[i].second.first, p[i].second.second) == p[i].first) cnt++; } if (cnt == m) { cout << "YES" << endl; for (int i = 0; i < (n); i++) cout << query(1, n, 1, i + 1, i + 1) << " "; cout << endl; return 0; } cout << "NO" << endl; return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int mx = 1e5 + 7; struct node { int l, r, q; } a[mx]; int col[mx << 2], lazy[mx << 2]; void push_down(int id) { if (lazy[id]) { lazy[id << 1] |= lazy[id]; lazy[id << 1 | 1] |= lazy[id]; col[id << 1] |= lazy[id]; col[id << 1 | 1] |= lazy[id]; lazy[id] = 0; } } void push_up(int id) { col[id] = col[id << 1] & col[id << 1 | 1]; } void upd(int L, int R, int id, int l, int r, int x) { if (l <= L && R <= r) { col[id] |= x; lazy[id] |= x; return; } push_down(id); int mid = (L + R) >> 1; if (l <= mid) upd(L, mid, id << 1, l, r, x); if (mid < r) upd(mid + 1, R, id << 1 | 1, l, r, x); push_up(id); } int query(int L, int R, int id, int l, int r) { if (l <= L && R <= r) return col[id]; push_down(id); int mid = (L + R) >> 1; if (l <= mid && mid < r) return query(L, mid, id << 1, l, r) & query(mid + 1, R, id << 1 | 1, l, r); if (l <= mid) return query(L, mid, id << 1, l, r); return query(mid + 1, R, id << 1 | 1, l, r); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].q); upd(1, n, 1, a[i].l, a[i].r, a[i].q); } bool ok = 1; for (int i = 1; i <= m; i++) { int q = query(1, n, 1, a[i].l, a[i].r); if (q != a[i].q) { ok = 0; break; } } if (!ok) printf("NO\n"); else { printf("YES\n"); for (int i = 1; i <= n; i++) printf("%d%c", query(1, n, 1, 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> using namespace std; const int MAXN = 1e5 + 5; const int MAXK = 30; const int MAXST = (1 << (32 - __builtin_clz(MAXN))); const int NEUT = (1 << 30) - 1; struct mon { int v; mon operator+(const mon &o) const { return {v & o.v}; } }; struct queryTyp { int l, r, q; }; int arr[MAXN]; int dp[MAXN][MAXK], acc[MAXK]; int rta[MAXN], N; queryTyp queries[MAXN]; mon ST[2 * MAXST]; void update(int p, int v) { p += N; ST[p].v = v; while (p > 1) { p /= 2; ST[p] = ST[2 * p + 1] + ST[2 * p]; } } mon query(int i, int tl, int tr, int bl, int br) { if (tl >= br || tr <= bl) return {NEUT}; if (tl >= bl && tr <= br) return ST[i]; int mid = (tl + tr) / 2; return query(2 * i, tl, mid, bl, br) + query(2 * i + 1, mid, tr, bl, br); } int main() { int n, m; scanf("%d %d", &n, &m); N = (1 << (32 - __builtin_clz(n))); for (int i = 0; i < int(m); i++) { int l, r, q; scanf("%d %d %d", &l, &r, &q); queries[i] = {l, r, q}; for (int j = 0; j < int(MAXK); j++) if (q & (1 << j)) dp[l][j]++, dp[r + 1][j]--; } for (int i = int(1); i < int(n + 1); i++) for (int j = 0; j < int(MAXK); j++) dp[i][j] += dp[i - 1][j]; for (int i = int(1); i < int(n + 1); i++) for (int j = 0; j < int(MAXK); j++) ST[i - 1 + N].v |= (dp[i][j] > 0) * (1 << j); for (int i = int(N) - 1; i >= int(1); i--) ST[i] = ST[2 * i] + ST[2 * i + 1]; bool posib = true; for (int i = 0; i < int(m); i++) { queryTyp x = queries[i]; mon val = query(1, 0, N, x.l - 1, x.r); if (val.v != x.q) { posib = false; break; } } if (posib) { printf("YES\n"); for (int i = 0; i < int(n); i++) printf("%d ", ST[i + N].v); } else printf("NO"); 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 n, m, l[100005], r[100005], q[100005], chk[33][100005], ans[100005], cur, re[33][100005]; int main() { cin >> n >> m; memset(chk, 0, sizeof(chk)); memset(re, 0, sizeof(re)); for (int ii = 0; ii < m; ii++) { cin >> l[ii] >> r[ii] >> q[ii]; for (int i = 0; i < 33; i++) if ((1ll << i) & q[ii]) { chk[i][l[ii]]++; chk[i][r[ii] + 1]--; } } for (int i = 0; i < 33; i++) { cur = 0; for (int j = 1; j < n + 1; j++) { cur += chk[i][j]; if (cur) chk[i][j] = 1; else chk[i][j] = 0; ans[j] += (1ll << i) * chk[i][j]; re[i][j] = chk[i][j] + re[i][j - 1]; } } for (int i = 0; i < m; i++) { for (int j = 0; j < 33; j++) if (!((1ll << j) & q[i])) { if (re[j][r[i]] - re[j][l[i] - 1] == r[i] - l[i] + 1) { cout << "NO"; return 0; } } } cout << "YES" << endl; for (int i = 1; i < n + 1; i++) cout << 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
#include <bits/stdc++.h> using namespace std; const long long int N = 1e5 + 10, N2 = 2e5 + 10, delta = 46639, mod = 1e9 + 7, oo = 1e18, LOG = 20; const long double PI = 3.141592653589793; long long int a[N], n, or1[N << 2], and1[N << 2]; pair<pair<long long int, long long int>, long long int> query[N]; void build(long long int l = 0, long long int r = n, long long int id = 1) { if (r - l <= 1) { and1[id] = a[l]; return; } long long int mid = (r + l) / 2; build(l, mid, 2 * id); build(mid, r, 2 * id + 1); and1[id] = and1[2 * id] & and1[2 * id + 1]; } long long int get(long long int l, long long int r, long long int b = 0, long long int e = n, long long int id = 1) { if (l <= b && e <= r) return and1[id]; long long int mid = (b + e) / 2; if (l < mid && mid < r) return get(l, r, b, mid, 2 * id) & get(l, r, mid, e, 2 * id + 1); if (l < mid) return get(l, r, b, mid, 2 * id); return get(l, r, mid, e, 2 * id + 1); } long long int getor(long long int ind, long long int l = 0, long long int r = n, long long int id = 1) { if (r - l <= 1) return or1[id]; long long int mid = (r + l) / 2, ans = 0; if (ind < mid) ans |= getor(ind, l, mid, 2 * id); else ans |= getor(ind, mid, r, 2 * id + 1); ans |= or1[id]; return ans; } void addor(long long int l, long long int r, long long int o, long long int b = 0, long long int e = n, long long int id = 1) { if (l <= b && e <= r) { or1[id] |= o; return; } long long int mid = (b + e) / 2; if (l < mid) addor(l, r, o, b, mid, 2 * id); if (mid < r) addor(l, r, o, mid, e, 2 * id + 1); } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long int m; cin >> n >> m; for (long long int i = 0; i < m; i++) { long long int l, r, q; cin >> l >> r >> q; l--; query[i] = {{l, r}, q}; addor(l, r, q); } for (long long int i = 0; i < n; i++) a[i] = getor(i); build(); for (long long int i = 0; i < m; i++) if (get(query[i].first.first, query[i].first.second) != query[i].second) return cout << "NO\n", 0; cout << "YES\n"; for (long long int i = 0; i < n; i++) cout << a[i] << ' '; cout << '\n'; return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class CF_275D { public static class RangeTree { int N; long[] tree; long[] lazy; // Stores the values to be pushed-down the tree only when needed /* * Identities of binary operations f(IDENTITY,x)=x, * 0 for sum, +INF for min, -INF for max, 0 for gcd, 0 for OR, +INF for AND */ long[] IDENTITY = {0,Long.MAX_VALUE}; // Associative binary operation used in the tree private long operation(long first, long sec, int type) { switch (type) { // The case 0 has to be used for the type of operation of the queries //case 0: return first + sec; // Addition case 0: return first |= sec; // OR case 1: return first &= sec; // AND //case 0: return Math.max(first,sec); // Maximum // Add cases as needed default: return sec; // -1 -> Assignment } } // Accumulate the operation zero (0) to be done to the sub-tree without actually pushing it down private long integrate(long act, long val, int nodeFrom, int nodeTo, int type) { switch (type) { //case 0: return act + val*(nodeTo-nodeFrom+1); // Addition case 0: return act |= val; // OR case 1: return act &= val; // AND default: return val; // -1 -> Assignment } } // Constructor for the tree with the maximum expected value public RangeTree(int maxExpected, int type) { maxExpected++; int len = Integer.bitCount(maxExpected)>1 ? Integer.highestOneBit(maxExpected)<<2 : Integer.highestOneBit(maxExpected)<<1; N = len/2; tree = new long[len]; lazy = new long[len]; if(tree[0]!=IDENTITY[type]){ Arrays.fill(tree,IDENTITY[type]); Arrays.fill(lazy,IDENTITY[type]); } } // Propagate the lazily accumulated values down the tree with query operator public void propagate(int node, int leftChild, int rightChild, int nodeFrom, int mid, int nodeTo, int type){ tree[leftChild] = integrate(tree[leftChild],lazy[node],nodeFrom,mid, type); // Calculates the value of the children by taking into account the lazy values tree[rightChild] = integrate(tree[rightChild],lazy[node],mid+1,nodeTo, type); lazy[leftChild] = operation(lazy[leftChild],lazy[node],type); lazy[rightChild] = operation(lazy[rightChild],lazy[node],type); lazy[node] = IDENTITY[type]; } // Update the values from the position i to j. Time complexity O(lg n) public void update(int i, int j, long newValue, int type) { update(1, 0, N-1, i, j,newValue, type); } private void update(int node, int nodeFrom, int nodeTo, int i, int j, long v, int type) { if (j < nodeFrom || i > nodeTo) // No part of the range needs to be updated return; else if (i <= nodeFrom && j >= nodeTo) { // Whole range needs to be updated tree[node] = integrate(tree[node], v, nodeFrom, nodeTo, type); lazy[node] = operation(lazy[node], v, type); } else { // Some part of the range needs to be updated int leftChild = node << 1; // 2 * node; int rightChild = leftChild+1; int mid = (nodeFrom + nodeTo) >> 1; // (nodeFrom + nodeTo) / 2; if(lazy[node]!=IDENTITY[type]) // Propagate lazy operations if necessary propagate(node, leftChild, rightChild, nodeFrom, mid, nodeTo, type); update(leftChild, nodeFrom, mid, i, j, v, type); // Search left child update(rightChild, mid+1, nodeTo, i, j, v, type); // Search right child tree[node] = operation(tree[leftChild], tree[rightChild],type); // Merge with the operation } } // Query the range from i to j. Time complexity O(lg n) public long query(int i, int j, int type) { return query(1, 0, N-1, i, j, type); } private long query(int node, int nodeFrom, int nodeTo, int i, int j, int type) { if (j < nodeFrom || i > nodeTo) // No part of the range belongs to the query return IDENTITY[type]; else if (i <= nodeFrom && j >= nodeTo) // The whole range is part of the query return tree[node]; else { // Partially within the range int leftChild = node << 1; // 2 * node; int rightChild = leftChild+1; int mid = (nodeFrom + nodeTo) >> 1; // (nodeFrom + nodeTo) / 2; if(lazy[node]!=IDENTITY[type]) // Propagate lazy operations if necessary propagate(node, leftChild, rightChild, nodeFrom, mid, nodeTo, type); long a = query(leftChild, nodeFrom, mid, i, j, type); // Search left child long b = query(rightChild, mid+1, nodeTo, i, j, type); // Search right child return operation(a, b, type); } } } public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] a = new int[m]; int[] b = new int[m]; int[] q = new int[m]; for(int i = 0; i < m; i++){ a[i] = sc.nextInt()-1; b[i] = sc.nextInt()-1; q[i] = sc.nextInt(); } RangeTree or = new RangeTree(n,0); // OR tree for(int i = 0; i < m; i++) or.update(a[i],b[i], q[i],0); RangeTree and = new RangeTree(n,1); for(int i = 0; i < n; i++) and.update(i, i, or.query(i,i,0), 1); boolean fail = false; for(int i = 0; i < m; i++){ long aux = and.query(a[i],b[i],1); if(aux!=q[i]){ fail = true; break; } } PrintWriter out = new PrintWriter(System.out); if(!fail){ out.println("YES"); out.print(or.query(0,0,0)); for(int i=1; i<n; i++){ out.print(" "+or.query(i,i,0)); } out.println(); } else { out.println("NO"); } out.flush(); out.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.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 * * @author xwchen */ 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 { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[] a = new int[n + 1]; int[][] b = new int[n + 2][31]; 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(); r[i] = in.nextInt(); q[i] = in.nextInt(); for (int j = 0; j < 30; ++j) { if (BitUtil.testBit(q[i], j)) { b[l[i]][j]++; b[r[i] + 1][j]--; } } } for (int j = 0; j < 30; ++j) { int cnt = 0; for (int i = 1; i <= n; ++i) { cnt += b[i][j]; int bit = ((cnt > 0) ? 1 : 0) << j; a[i] |= bit; } } SegmentTree st = new SegmentTree(n); st.build(a); boolean OK = true; for (int i = 0; i < m; ++i) { if (q[i] != st.query(l[i], r[i])) { OK = false; } } if (OK) { out.println("YES"); for (int i = 1; i <= n; ++i) { out.print(a[i] + " "); } } else { out.println("NO"); } } } static class BitUtil { public static boolean testBit(long mask, int pos) { return (mask & (1L << pos)) != 0; } } static class SegmentTree { Node root = null; public SegmentTree(int n) { root = new Node(); root.left = 1; root.right = n; } void pull_up(Node cur, Node left, Node right) { //int pair = Math.min(left.rightBracket, right.leftBracket); cur.val = left.val & right.val; //cur.leftBracket = left.leftBracket + right.leftBracket - pair; //cur.rightBracket = right.rightBracket + left.rightBracket - pair; } public void build(int[] a) { build(root, a); } public void build(Node cur, int[] a) { // System.out.println(String.format("node[%d] [%d,%d]", cur, left[cur], right[cur])); if (cur.left == cur.right) { //System.out.println(" L:"+cur.left); cur.val = a[cur.left]; // System.out.println(String.format("node[%d] [%d,%d] sum =%d", cur, left[cur], right[cur], sum[cur])); } else { Node lc = new Node(); Node rc = new Node(); int mid = (cur.left + cur.right) >> 1; lc.left = cur.left; lc.right = mid; rc.left = mid + 1; rc.right = cur.right; cur.lcld = lc; cur.rcld = rc; build(lc, a); build(rc, a); pull_up(cur, lc, rc); } cur.lazy_value = -1; } public int query(int from, int to) { Node qNode = query(root, from, to); return qNode.val; } public Node query(Node cur, int from, int to) { // System.out.println(String.format("node[%d] [%d,%d] sum =%d", cur, left[cur], right[cur], sum[cur])); if (from <= cur.left && cur.right <= to) { return cur; } else { Node lc = cur.lcld; Node rc = cur.rcld; //push_down(cur); if (to <= lc.right) { return query(lc, from, to); } else if (from >= rc.left) { return query(rc, from, to); } else { Node ql = query(lc, from, to); Node qr = query(rc, from, to); Node qNode = new Node(); pull_up(qNode, ql, qr); return qNode; } } } class Node { int left; int right; Node lcld; Node rcld; int val; int lazy_value; } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer = new StringTokenizer(""); public InputReader(InputStream inputStream) { this.reader = new BufferedReader( new InputStreamReader(inputStream)); } public String next() { while (!tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> const int SZ = 1e5 + 5; using namespace std; int n, m, res[SZ]; int l[SZ], r[SZ], q[SZ]; int d[SZ][33], cumm[SZ][33], cur[33]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d%d", &l[i], &r[i], &q[i]); for (int j = 0; j < 31; j++) if (q[i] & (1 << j)) d[l[i]][j]++, d[r[i] + 1][j]--; } for (int i = 1; i <= n; i++) { for (int j = 0; j < 31; j++) { cur[j] += d[i][j]; cumm[i][j] = cumm[i - 1][j]; if (cur[j]) res[i] |= (1 << j), cumm[i][j]++; } } for (int i = 0; i < m; i++) { for (int j = 0; j < 31; j++) { if (q[i] & (1 << j)) { if (cumm[r[i]][j] - cumm[l[i] - 1][j] != r[i] - l[i] + 1) { cout << "NO"; return 0; } } else { if (cumm[r[i]][j] - cumm[l[i] - 1][j] >= r[i] - l[i] + 1) { cout << "NO"; return 0; } } } } cout << "YES\n"; for (int i = 1; i <= n; i++) cout << res[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.awt.Point; import java.awt.geom.Line2D; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.security.GuardedObject; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.io.InputStream; import java.math.BigInteger; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in; PrintWriter out; in=new FastScanner(System.in); out=new PrintWriter(System.out); //in=new FastScanner(new FileInputStream(new File("C://Users//KANDARP//Desktop//coding_contest_creation (1).txt"))); TaskC solver = new TaskC(); long var=System.currentTimeMillis(); solver.solve(1, in, out); /*if(System.getProperty("ONLINE_JUDGE")==null){ out.println("["+(double)(System.currentTimeMillis()-var)/1000+"]"); }*/ out.close(); } } class Pair { long x,y,index; long r1,r2; Pair(long x,long y,int index){ this.x=x; this.y=y; this.index=index; } public String toString(){ return this.x+" "+this.y+" "; } } class Edge implements Comparable<Edge>{ int u; int v; int time; long cost; long wcost; public Edge(int u,int v,long cost,long wcost){ this.u=u; this.v=v; this.cost=cost; this.wcost=wcost; time=Integer.MAX_VALUE; } public int other(int x){ if(u==x)return v; return u; } @Override public int compareTo(Edge o) { // TODO Auto-generated method stub return Long.compare(cost, o.cost); } } class queary{ int type; int l; int r; int index; queary(int type,int l,int r,int index){ this.type=type; this.l=l; this.r=r; this.index=index; } } class TaskC { static long mod=1000000007; /* static int prime_len=1000010; static int prime[]=new int[prime_len]; static long n_prime[]=new long[prime_len]; static ArrayList<Integer> primes=new ArrayList<>(); static{ for(int i=2;i<=Math.sqrt(prime.length);i++){ for(int j=i*i;j<prime.length;j+=i){ prime[j]=i; } } for(int i=2;i<prime.length;i++){ n_prime[i]=n_prime[i-1]; if(prime[i]==0){ n_prime[i]++; } } // System.out.println("end"); // prime[0]=true; // prime[1]=1; } /* * long pp[]=new long[10000001]; pp[0]=0; pp[1]=1; for(int i=2;i<pp.length;i++){ if(prime[i]!=0){ int gcd=(int)greatestCommonDivisor(i/prime[i], prime[i]); pp[i]=(pp[i/prime[i]]*pp[prime[i]]*gcd)/pp[(int)gcd]; } else pp[i]=i-1; } } * */ class TrieNode{ int value; TrieNode children[]; public TrieNode(int value,TrieNode children[] ){ this.value=value; this.children=children; } } class Trie{ TrieNode root; int count; public Trie(TrieNode root,int count){ this.root=root; this.count=count; } } public void insert(Trie t,char key[]){ t.count++; int length=key.length; int level=0; TrieNode current=t.root; if(current==null){ t.root=new TrieNode(0, new TrieNode[26]); current=t.root; } for(level=0;level<length;level++){ int index=key[level]-'a'; if(current.children[index]==null){ current.children[index]=new TrieNode(0,new TrieNode[26]); } current=current.children[index]; } current.value=t.count; } public int search(Trie t,char key[]){ TrieNode current=t.root; if(current==null){ return 0; } int length=key.length; int level=0; for(level=0;level<length;level++){ int index=key[level]-'a'; if(current.children[index]==null){ return 0; } current=current.children[index]; } return current.value; } class point{ int x; int y; } int orientation(point p, point q, point r) { int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); if (val == 0) return 0; // colinear return (val > 0)? 1: 2; // clock or counterclock wise } public void solve(int testNumber, FastScanner in, PrintWriter out) throws IOException { int n=in.nextInt(); int q=in.nextInt(); int pre[][]=new int[31][n]; queary qq[]=new queary[q]; for(int i=0;i<q;i++){ int l=in.nextInt()-1; int r=in.nextInt()-1; int t=in.nextInt(); qq[i]=new queary(t,l,r, i); for(int j=0;j<31;j++){ if((t&(1<< j))!=0){ pre[j][l]+=1; if(r+1<n) pre[j][r+1]+=-1; } } } for(int j=0;j<31;j++){ for(int i=1;i<n;i++){ pre[j][i]+=pre[j][i-1]; } } long t[]=new long[2*n]; for(int i=n;i<2*n;i++){ StringBuilder sb=new StringBuilder(); for(int j=0;j<31;j++){ if(pre[j][i-(n)]>0){ sb.append(1); } else sb.append(0); } sb=sb.reverse(); // out.println(sb.toString()); t[i]=Long.parseLong(sb.toString(),2); } // out.println(Arrays.toString(t)); SegTree tree=new SegTree(n,t); //out.println(Arrays.toString(t)); for(int i=0;i<q;i++){ int x=qq[i].type; int l=qq[i].l; int r=qq[i].r; // out.println(l+" "+r+" "+tree.query(l, r)+" "+x); if(tree.query(l, r+1)!=x){ out.println("NO"); return; } } out.println("YES"); for(int i=n;i<2*n;i++){ out.print(t[i]+" "); } out.println(); } public static long[][] matrixexpo(long m[][],String n,long mod){ if(n.equals("1")){ return m.clone(); } if(n.equals("10")){ return mulmatrix(m, m , mod); } else{ long temp [][]=matrixexpo(m,n.substring(0,n.length()-1),mod); temp=mulmatrix(temp, temp, mod); if(n.charAt(n.length()-1)=='0')return temp; else return mulmatrix(temp, m,mod); } } public static long[][] mulmatrix(long m1[][],long m2[][],long mod){ long ans[][]=new long[m1.length][m2[0].length]; for(int i=0;i<m1.length;i++){ for(int j=0;j<m2[0].length;j++){ for(int k=0;k<m1.length;k++){ ans[i][j]+=(m1[i][k]*m2[k][j]); ans[i][j]%=mod; } } } return ans; } long pow(long x,long y,long mod){ if(y<=0){ return 1; } if(y==1){ return x%mod; } long temp=pow(x,y/2,mod); if(y%2==0){ return (temp*temp)%mod; } else{ return (((temp*temp)%mod)*x)%mod; } } static long greatestCommonDivisor (long m, long n){ long x; long y; while(m%n != 0){ x = n; y = m%n; m = x; n = y; } return n; } } class LcaSparseTable { int len; int[][] up; int[][] max; int[] tin; int[] tout; int time; int []lvel; void dfs(List<Integer>[] tree, int u, int p) { tin[u] = time++; lvel[u]=lvel[p]+1; up[0][u] = p; if(u!=p) //max[0][u]=weight(u,p); for (int i = 1; i < len; i++) { up[i][u] = up[i - 1][up[i - 1][u]]; max[i][u]=Math.max(max[i-1][u],max[i-1][up[i-1][u]]); } for (int v : tree[u]) if (v != p) dfs(tree, v, u); tout[u] = time++; } public LcaSparseTable(List<Integer>[] tree, int root) { int n = tree.length; len = 1; while ((1 << len) <= n) ++len; up = new int[len][n]; max=new int[len][n]; tin = new int[n]; tout = new int[n]; lvel=new int[n]; lvel[root]=0; dfs(tree, root, root); } boolean isParent(int parent, int child) { return tin[parent] <= tin[child] && tout[child] <= tout[parent]; } public int lca(int a, int b) { if (isParent(a, b)) return a; if (isParent(b, a)) return b; for (int i = len - 1; i >= 0; i--) if (!isParent(up[i][a], b)) a = up[i][a]; return up[0][a]; } public long max(int a,int b){ int lca=lca(a,b); // System.out.println("LCA "+lca); long ans=0; int h=lvel[a]-lvel[lca]; // System.out.println("Height "+h); int index=0; while(h!=0){ if(h%2==1){ ans=Math.max(ans,max[index][a]); a=up[index][a]; } h/=2; index++; } h=lvel[b]-lvel[lca]; // System.out.println("Height "+h); index=0; while(h!=0){ if(h%2==1){ ans=Math.max(ans,max[index][b]); b=up[index][b]; } h/=2; index++; } return ans; } static void dfs(List<Integer>[] graph, boolean[] used, List<Integer> res, int u,int parent,List<Integer> collection) { used[u] = true; Integer uu=new Integer(u); collection.add(uu); for (int v : graph[u]){ if (!used[v]){ dfs(graph, used, res, v,u,collection); } else if(collection.contains(v)){ System.out.println("Impossible"); System.exit(0); } } collection.remove(uu); res.add(u); } public static List<Integer> topologicalSort(List<Integer>[] graph) { int n = graph.length; boolean[] used = new boolean[n]; List<Integer> res = new ArrayList<>(); for (int i = 0; i < n; i++) if (!used[i]) dfs(graph, used, res, i,-1,new ArrayList<Integer>()); Collections.reverse(res); return res; } static void dfs1(List<Integer>[] graph, boolean[] used, List<Integer> res, int u) { used[u] = true; for (int v : graph[u]) if (!used[v]) dfs1(graph, used, res, v); res.add(u); } int phi(int n) { int res = n; for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { while (n % i == 0) { n /= i; } res -= res / i; } } if (n != 1) { res -= res / n; } return res; } public static long[][] mulmatrix(long m1[][],long m2[][],long mod){ long ans[][]=new long[m1.length][m2[0].length]; for(int i=0;i<m1.length;i++){ for(int j=0;j<m2[0].length;j++){ for(int k=0;k<m1.length;k++){ ans[i][j]+=(m1[i][k]*m2[k][j]); ans[i][j]%=mod; } } } return ans; } public static long[][] matrixexpo(long m[][],String n,long mod){ if(n.equals("1")){ return m.clone(); } if(n.equals("10")){ return mulmatrix(m, m , mod); } else{ long temp [][]=matrixexpo(m,n.substring(0,n.length()-1),mod); temp=mulmatrix(temp, temp, mod); if(n.charAt(n.length()-1)=='0')return temp; else return mulmatrix(temp, m,mod); } } public static boolean isCompatible(long x[],long y[]){ for(int i=0;i<x.length-1;i++){ if(x[i]==y[i] && x[i+1]==y[i+1] && x[i]==x[i+1] && y[i]==y[i+1]){ return false; } } return true; } long pow(long x,long y,long mod){ if(y<=0){ return 1; } if(y==1){ return x%mod; } long temp=pow(x,y/2,mod); if(y%2==0){ return (temp*temp)%mod; } else{ return (((temp*temp)%mod)*x)%mod; } } long no_of_primes(long m,long n,long k){ long count=0,i,j; int primes []=new int[(int)(n-m+2)]; if(m==1) primes[0] = 1; for(i=2; i<=Math.sqrt(n); i++) { j = (m/i); j *= i; if(j<m) j+=i; for(; j<=n; j+=i) { if(j!=i) primes[(int)(j-m)] = 1; } } for(i=0; i<=n-m; i++) if(primes[(int)i]==0 && (i-1)%k==0) count++; return count; } } class SegTree { int n; static long constt=Long.parseLong("111111111111111111111111111111",2); long t[]; long mod=(long)(1000000007); SegTree(int n,long t[]){ this.n=n; this.t=t; build(); } void build() { // build the tree for (int i = n - 1; i >= 0; --i){ t[i]=(t[i<<1]&t[i<<1|1]); } } void modify(int p, long value) { // set value at position p for (t[p += n]=value; p > 1; p >>= 1) t[p>>1] = (t[p]&t[p^1]); // System.out.println(Arrays.toString(t)); } long query(int l, int r) { // sum on interval [l, r) long res=constt; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if ((l&1)!=0) res=res&t[l++]; if ((r&1)!=0) res=res&t[--r]; } return res; } // // // } class FastScanner { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int snumChars; private SpaceCharFilter filter; public FastScanner(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 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class UF { private int[] parent; // parent[i] = parent of i private byte[] rank; // rank[i] = rank of subtree rooted at i (never more than 31) private int count; // number of components public UF(int N) { if (N < 0) throw new IllegalArgumentException(); count = N; parent = new int[N]; rank = new byte[N]; for (int i = 0; i < N; i++) { parent[i] = i; rank[i] = 0; } } public int find(int p) { if (p < 0 || p >= parent.length) throw new IndexOutOfBoundsException(); while (p != parent[p]) { parent[p] = parent[parent[p]]; // path compression by halving p = parent[p]; } return p; } public int count() { return count; } public boolean connected(int p, int q) { return find(p) == find(q); } public boolean union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return false; // make root of smaller rank point to root of larger rank if (rank[rootP] < rank[rootQ]) parent[rootP] = rootQ; else if (rank[rootP] > rank[rootQ]) parent[rootQ] = rootP; else { parent[rootQ] = rootP; rank[rootP]++; } count--; return true; } }
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{ 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("input.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()); } SegmentTree st = new SegmentTree(n); for(int i = 0; i < m; i++) { int q = queries[i].q; st.update(0, 0, st.size - 1, queries[i].l, queries[i].r, q); } for(int i = 0; i < m; i++) { int res = st.get(0, 0, st.size - 1, queries[i].l, queries[i].r); int q = queries[i].q; int r = queries[i].r; int l = queries[i].l; if(res != q) { out.println("NO"); return; } } out.println("YES"); for(int i = 0; i < n; i++) { int res = st.get(0, 0, st.size - 1, i, i); out.print(res + " "); } out.println(); } class SegmentTree { int[] cur; int[] upd; public int size = 0; public SegmentTree(int n) { int z = 0; for(int i = 0; i < 31; i++) { if((n & (1 << i)) > 0) { z = i; } } size = (1 << (z + 1)); cur = new int[size * 2 - 1]; upd = new int[size * 2 - 1]; } public void push(int node) { upd[node * 2 + 1] |= upd[node]; upd[node * 2 + 2] |= upd[node]; upd[node] = 0; } public void update(int node, int fromTree, int toTree, int fromQ, int toQ, int val) { if(fromQ > toTree || toQ < fromTree) return; if(fromTree == fromQ && toTree == toQ) { upd[node] |= val; return; } push(node); int left = 2 * node + 1; int right = 2 * node + 2; int mid = fromTree + (toTree - fromTree) / 2; update(left, fromTree, mid, fromQ, Math.min(mid, toQ), val); update(right, mid + 1, toTree, Math.max(mid + 1, fromQ), toQ, val); cur[node] = (cur[left] | upd[left]) & (cur[right] | upd[right]); } public int get(int node, int fromTree, int toTree, int fromQ, int toQ) { if(fromQ > toTree || toQ < fromTree) return Integer.MAX_VALUE; if(fromTree == fromQ && toTree == toQ) { return (cur[node] | upd[node]); } push(node); int mid = fromTree + (toTree - fromTree) / 2; int left = 2 * node + 1; int right = 2 * node + 2; int res = get(left, fromTree, mid, fromQ, Math.min(mid, toQ)) & get(right, mid + 1, toTree, Math.max(mid + 1, fromQ), toQ); int cnt = (toTree - fromTree + 1) / 2; cur[node] = (cur[left] | upd[left]) & (cur[right] | upd[right]); return res; } } } 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
#include <bits/stdc++.h> using namespace std; int tree[4 * 100010], arr[4 * 100010], n; void build(int no, int L, int R) { if (L == R) { tree[no] = arr[L]; return; } int nl = 2 * no, nr = 2 * no + 1, mid = (L + R) >> 1; build(nl, L, mid); build(nr, mid + 1, R); tree[no] = tree[nl] & tree[nr]; } void build() { build(1, 0, n - 1); } int query(int no, int L, int R, int i, int j) { int nl = 2 * no, nr = 2 * no + 1, mid = (L + R) >> 1; if (i > R || j < L) return (~0); if (i <= L && R <= j) return tree[no]; int a = query(nl, L, mid, i, j); int b = query(nr, mid + 1, R, i, j); return a & b; } int query(int i, int j) { return query(1, 0, n - 1, i, j); } pair<pair<int, int>, int> command[100010]; int act[32]; struct s { int at; int num; int ent; s() {} s(int at, int num, int ent) : at(at), num(num), ent(ent) {} bool operator<(const s &rhs) const { if (at != rhs.at) return at < rhs.at; return ent > rhs.ent; } } in[200020]; int main() { int m; scanf("%d %d", &n, &m); int k = 0; for (int i = (0), __ = (m); i < __; ++i) { int l, r, q; scanf("%d %d %d", &l, &r, &q); l--; r--; in[k++] = s(l, q, 0); in[k++] = s(r + 1, q, 1); command[i] = make_pair(make_pair(l, r), q); } memset(act, 0, sizeof act); sort(in, in + k); for (int i = (0), __ = (k); i < __; ++i) { s nxt = in[i]; for (int j = (31), __ = (0); j >= __; --j) if (nxt.num & (1 << j)) act[j] = (nxt.ent ? act[j] - 1 : act[j] + 1); int b = nxt.at; int e = (i + 1 == k) ? n : in[i + 1].at; for (int j = (b), __ = (e); j < __; ++j) { for (int w = (31), __ = (0); w >= __; --w) if (act[w]) arr[j] |= (1 << w); } } build(); for (int i = (0), __ = (m); i < __; ++i) { int l = command[i].first.first, r = command[i].first.second; int q = command[i].second; if (query(l, r) != q) { printf("NO\n"); return 0; } } printf("YES\n"); printf("%d", arr[0]); for (int i = (1), __ = (n); i < __; ++i) printf(" %d", arr[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 = 1e5 + 7; struct tree { int l, r, val; } a[maxn << 2]; int p[maxn], q[maxn], z[maxn], u = 0; void Buildtree(int l, int r, int key) { a[key].l = l; a[key].r = r; a[key].val = 0; if (l == r) return; int mid = (l + r) / 2; Buildtree(l, mid, key << 1); Buildtree(mid + 1, r, key << 1 | 1); } void update(int l, int r, int key, int w) { if (a[key].l == l && a[key].r == r) { a[key].val |= w; return; } int mid = (a[key].l + a[key].r) / 2; if (r <= mid) update(l, r, key << 1, w); else if (mid < l) update(l, r, key << 1 | 1, w); else { update(l, mid, key << 1, w); update(mid + 1, r, key << 1 | 1, w); } } int query(int l, int r, int key) { if (a[key].l == l && a[key].r == r) return a[key].val; int mid = (a[key].l + a[key].r) / 2; if (r <= mid) return query(l, r, key << 1); else if (mid < l) return query(l, r, key << 1 | 1); else return query(l, mid, key << 1) & query(mid + 1, r, key << 1 | 1); } void Show(int key) { if (a[key].l == a[key].r) { if (u++) printf(" "); printf("%d", a[key].val); return; } a[key << 1].val |= a[key].val; a[key << 1 | 1].val |= a[key].val; Show(key << 1); Show(key << 1 | 1); } int main() { int n, m, temp; scanf("%d %d", &n, &m); Buildtree(1, n, 1); for (int i = 1; i <= m; i++) { scanf("%d %d %d", &p[i], &q[i], &z[i]); update(p[i], q[i], 1, z[i]); } for (int i = 1; i <= m; i++) { temp = query(p[i], q[i], 1); if (temp != z[i]) { printf("NO\n"); return 0; } } printf("YES\n"); Show(1); 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class InterestingArray { public static void main(String[] args) throws IOException { Scanner sc= new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; int[] la = new int[m], ra = new int[m], qa = new int[m]; SegmentTree tree = new SegmentTree(arr); for (int i = 0; i < m; i++) { la[i] = sc.nextInt(); ra[i] = sc.nextInt(); qa[i] = sc.nextInt(); tree.update(la[i], ra[i], qa[i]); } for (int i = 0; i < arr.length; i++) arr[i] = tree.query(i+1, i+1); AndTree check = new AndTree(arr); for (int i = 0; i < la.length; i++) { if(check.query(la[i], ra[i]) != qa[i]) { System.out.println("NO"); return; } } PrintWriter pw = new PrintWriter(System.out); pw.println("YES"); for (int i = 0; i < arr.length; i++) pw.print(arr[i] + " "); pw.println(); pw.flush(); pw.close(); } static class AndTree { int[] arr; int[] tree; int n; public AndTree(int[] arr) { this.arr = arr; n = arr.length; tree = new int[4*arr.length]; build(); } private void build() { build(1, 1, n); } private void build(int ind, int qleft, int qright) { if(qleft == qright) { tree[ind] = arr[qleft-1]; return; } int leftChild = ind<<1; int rightChild = ind<<1|1; int mid = (qleft+qright)>>1; build(leftChild, qleft, mid); build(rightChild, mid+1, qright); tree[ind] = tree[leftChild] & tree[rightChild]; } int query(int left, int right) { return query(1, 1, n, left, right); } private int query(int ind, int left, int right, int qleft, int qright) { if(right < qleft || left > qright) return -1; if(right <= qright && left >= qleft) return tree[ind]; int leftChild = ind<<1; int rightChild = ind<<1|1; int mid = (left + right)>>1; int a = query(leftChild, left, mid, qleft, qright); int b = query(rightChild, mid+1, right, qleft, qright); return a&b; } } static class SegmentTree { int[] arr; int[] tree; int n; int[] lazy; public SegmentTree(int[] arr) { this.arr = arr; n = arr.length; tree = new int[4*arr.length]; lazy = new int[4*n]; build(); } private void build() { build(1, 1, n); } private void propagate(int ind, int left, int mid, int right) { long val = lazy[ind]; tree[ind<<1] |= val; tree[ind<<1|1] |= val; lazy[ind<<1] |= val; lazy[ind<<1|1] |= val; lazy[ind] = 0; } private void build(int ind, int qleft, int qright) { if(qleft == qright) { tree[ind] = arr[qleft-1]; return; } int leftChild = ind<<1; int rightChild = ind<<1|1; int mid = (qleft+qright)>>1; build(leftChild, qleft, mid); build(rightChild, mid+1, qright); tree[ind] = tree[leftChild] | tree[rightChild]; } int query(int left, int right) { return query(1, 1, n, left, right); } private int query(int ind, int left, int right, int qleft, int qright) { if(right < qleft || left > qright) return 0; if(right <= qright && left >= qleft) return tree[ind]; int leftChild = ind<<1; int rightChild = ind<<1|1; int mid = (left + right)>>1; propagate(ind, left, mid, right); int a = query(leftChild, left, mid, qleft, qright); int b = query(rightChild, mid+1, right, qleft, qright); return a|b; } void update(int left, int right, long val) { update(1, 1, n, left, right, val); } private void update(int ind, int left, int right, int qleft, int qright, long val) { if(right < qleft || left > qright) return; if(right <= qright && left >= qleft) { lazy[ind] |= val; tree[ind] |= val; return; } int leftChild = ind<<1; int rightChild = ind<<1|1; int mid = (right + left)>>1; propagate(ind, left, mid, right); update(leftChild, left, mid, qleft, qright, val); update(rightChild, mid+1, right, qleft, qright, val); tree[ind] = tree[left] | tree[right]; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const long long INF = 1000000000 + 7; const double esp = 1e-13; int n, m, xx[100000 + 10][50]; long long f[800000 + 10], a[100000 + 10], s, x[100000 + 10], y[100000 + 10], z[100000 + 10]; void build(int k, int l, int r) { if (l > r) return; if (l == r) { f[k] = a[l]; return; } int m = (l + r) / 2; build(k * 2, l, m); build(k * 2 + 1, m + 1, r); f[k] = f[k * 2] & f[k * 2 + 1]; } long long get(int k, int l, int r, int i, int j) { if (i > r || j < l) return s; if (i <= l && r <= j) return f[k]; int m = (l + r) / 2; long long t1 = get(k * 2, l, m, i, j); long long t2 = get(k * 2 + 1, m + 1, r, i, j); return t1 & t2; } int bit(long long x, long long y) { return x >> (y - 1) & 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(); cin >> n >> m; for (long long i = 1; i <= m; i++) { cin >> x[i] >> y[i] >> z[i]; for (long long j = 1; j <= 32; j++) if (bit(z[i], j) == 1) { xx[x[i]][j]++; xx[y[i] + 1][j]--; } } for (long long i = 1; i <= n; i++) for (long long j = 1; j <= 33; j++) xx[i][j] += xx[i - 1][j]; for (long long i = 1; i <= n; i++) for (long long j = 1; j <= 33; j++) if (xx[i][j]) a[i] += ((long long)1 << (j - 1)); s = 1; for (long long i = 1; i <= 60; i++) s *= 2; s--; build(1, 1, n); for (long long i = 1; i <= m; i++) { if (get(1, 1, n, x[i], y[i]) != z[i]) { cout << "NO"; return 0; } } cout << "YES\n"; for (long long 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
//package SegmentFenwick; import java.io.*; import java.util.StringTokenizer; public class practice { public static void main(String[] args) throws IOException, InterruptedException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); PrintWriter pw = new PrintWriter(System.out); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int N = 1; while (N < n) N <<= 1; Triple in[] = new Triple[m]; for (int i = 0; i < m; i++) { st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); in[i] = new Triple(l, r, q); } SegmentTree stt = new SegmentTree(N); for (int i = 0; i < m; i++) stt.update_range(in[i].a, in[i].b, in[i].c); for (int i = 0; i < m; i++) if (stt.query(in[i].a, in[i].b) != in[i].c) { System.out.println("NO"); return; } pw.println("YES"); for (int i = 1; i < stt.N; i++) stt.propagate(i, 1, 100000, stt.N); for (int i = 1; i <= n; i++) pw.print(stt.sTree[stt.N + i - 1] + " "); pw.flush(); } static class Triple { int a, b, c; Triple(int x, int y, int z) { a = x; b = y; c = z; } } static class SegmentTree { // 1-based DS, OOP int N; // the number of elements in the array as a power of 2 (i.e. after padding) int[] array, sTree, lazy; SegmentTree(int in) { N = 1; while (N < in) N <<= 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]; } void update_range(int i, int j, int val) // O(log n) { update_range(1, 1, N, i, j, val); } void update_range(int node, int b, int e, int i, int j, int val) { if (i > e || j < b) return; if (b >= i && e <= j) { sTree[node] |= val; lazy[node] |= val; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node << 1, b, mid, i, j, val); update_range(node << 1 | 1, mid + 1, e, i, j, val); sTree[node] = sTree[node << 1] & sTree[node << 1 | 1]; } } void propagate(int node, int b, int mid, int e) { lazy[node << 1] |= lazy[node]; lazy[node << 1 | 1] |= lazy[node]; sTree[node << 1] |= lazy[node]; sTree[node << 1 | 1] |= lazy[node]; lazy[node] = 0; } int query(int i, int j) { return query(1, 1, N, i, j); } int query(int node, int b, int e, int i, int j) // O(log n) { if (i > e || j < b) return -1; if (b >= i && e <= j) return sTree[node]; int mid = b + e >> 1; propagate(node, b, mid, e); int q1 = query(node << 1, b, mid, i, j); int q2 = query(node << 1 | 1, mid + 1, e, i, j); return q1 & q2; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(4000); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Scanner; public class ProbA { public static int a[][]; public static int b[][]; public static int l[], r[], gt[]; public static int getbit(int x, int i) { return (x >> i) & 1; } public static int mul(int x) { int res = 0; for(int i = 29; i >= 0; i--) if(a[x][i] > 0) res = (res << 1) | 1; else res = (res << 1); return res; } public static void main(String args[]) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.readInt(); a = new int[n][30]; b = new int[n][30]; for(int i = 0; i < n; i++) for(int j = 0; j < 30; j++) a[i][j] = 0; int m = in.readInt(); l = new int [m]; r = new int[m]; gt = new int [m]; for(int i = 0; i < m; i++) { l[i] = in.readInt() - 1; r[i] = in.readInt() - 1; gt[i] = in.readInt(); for(int j = 0; j < 30; j++) if(getbit(gt[i], j) == 1) { a[l[i]][j]++; if(r[i] + 1 < n) a[r[i] + 1][j]--; } } for(int j = 0; j < 30; j++) if(a[0][j] == 0) b[0][j] = 1; else b[0][j] = 0; for(int i = 1; i < n; i++) for(int j = 0; j < 30; j++) { a[i][j] += a[i - 1][j]; if(a[i][j] == 0) b[i][j] = b[i - 1][j] + 1; else b[i][j] = b[i - 1][j]; } for(int i = 0; i < m; i++) { for(int j = 0; j < 30; j++) if(getbit(gt[i], j) == 0) { if(l[i] == 0 && b[r[i]][j] == 0) { out.print("NO"); out.flush(); out.close(); return; } if(l[i] > 0 && b[r[i]][j] - b[l[i]-1][j] == 0) { out.print("NO"); out.flush(); out.close(); return; } } } out.print("YES\n"); for(int i = 0; i < n; i++) out.print("" + mul(i) + " "); out.flush(); out.close(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1000]; private int curChar, numChars; public InputReader(InputStream stream) { this.stream = stream; } private 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 long readLong() { 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 String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } }
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 Tre { int add, sum; } Tree[400001]; int ans[100001]; struct Node { int l, r, q; } Q[100001]; int x[31], i, j, m, n, p, k, q, l, r, sum; void down(int t) { if (!Tree[t].add) return; Tree[(t << 1)].add |= Tree[t].add; Tree[(t << 1)].sum |= Tree[t].add; Tree[((t << 1) + 1)].add |= Tree[t].add; Tree[((t << 1) + 1)].sum |= Tree[t].add; Tree[t].add = 0; } void update(int ll, int rr, int c, int l, int r, int t) { if (ll <= l && r <= rr) { Tree[t].sum |= c; Tree[t].add |= c; } else { down(t); if (ll <= ((l + r) >> 1)) update(ll, rr, c, l, ((l + r) >> 1), (t << 1)); if (rr > ((l + r) >> 1)) update(ll, rr, c, ((l + r) >> 1) + 1, r, ((t << 1) + 1)); Tree[t].sum = Tree[(t << 1)].sum & Tree[((t << 1) + 1)].sum; } } void ask(int ll, int rr, int l, int r, int t) { if (ll <= l && r <= rr) sum &= Tree[t].sum; else { down(t); if (ll <= ((l + r) >> 1)) ask(ll, rr, l, ((l + r) >> 1), (t << 1)); if (rr > ((l + r) >> 1)) ask(ll, rr, ((l + r) >> 1) + 1, r, ((t << 1) + 1)); } } int main() { scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) { scanf("%d%d%d", &Q[i].l, &Q[i].r, &Q[i].q); update(Q[i].l, Q[i].r, Q[i].q, 1, n, 1); } for (i = 1; i <= m; i++) { sum = (1 << 30) - 1; ask(Q[i].l, Q[i].r, 1, n, 1); if (sum != Q[i].q) { printf("NO\n"); return 0; } } for (i = 1; i <= n; i++) { sum = (1 << 30) - 1; ask(i, i, 1, n, 1); ans[i] = sum; } printf("YES\n"); for (i = 1; i <= n; i++) printf("%d ", 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
#include <bits/stdc++.h> const int inf = (1 << 30) - 1; namespace SgT { int n; int L, R, q; int V[100010 << 2], tg[100010 << 2]; inline void pushdown(int rt) { int &x = tg[rt]; V[rt << 1] |= x; V[rt << 1 | 1] |= x; tg[rt << 1] |= x; tg[rt << 1 | 1] |= x; x = 0; } void modify(int rt, int l, int r) { if (L <= l && R >= r) { V[rt] |= q; tg[rt] |= q; return; } if (tg[rt]) pushdown(rt); int mid = l + r >> 1; if (L <= mid) modify(rt << 1, l, mid); if (R > mid) modify(rt << 1 | 1, mid + 1, r); V[rt] = V[rt << 1] & V[rt << 1 | 1]; } inline void add(int ll, int rr, int qq) { L = ll, R = rr, q = qq; modify(1, 1, n); } int query(int rt, int l, int r) { if (L <= l && R >= r) return V[rt]; if (tg[rt]) pushdown(rt); int mid = l + r >> 1, ans = inf; if (L <= mid) ans = query(rt << 1, l, mid); if (R > mid) ans &= query(rt << 1 | 1, mid + 1, r); return ans; } inline int ask(int ll, int rr) { L = ll, R = rr; return query(1, 1, n); } } // namespace SgT using SgT::add; using SgT::ask; int n, m; int l[100010], r[100010], q[100010]; int main() { scanf("%d%d", &n, &m); SgT::n = n; for (int i = 1; i <= m; i++) { scanf("%d%d%d", l + i, r + i, q + i); add(l[i], r[i], q[i]); } for (int i = 1; i <= m; i++) { if (ask(l[i], r[i]) != q[i]) { puts("NO"); exit(0); } } puts("YES"); for (int i = 1; i < n; i++) printf("%d ", ask(i, i)); printf("%d\n", ask(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 st[300010], lz[300010]; void upd(long long val, long long l, long long r, long long low, long long high, long long i) { if (lz[i]) { st[i] |= lz[i]; if (high != low) { lz[2 * i + 1] |= lz[i]; lz[2 * i + 2] |= lz[i]; } lz[i] = 0; } if (high < l || low > r) { return; } if (low >= l && high <= r) { st[i] |= val; if (high != low) { lz[2 * i + 1] |= val; lz[2 * i + 2] |= val; } return; } long long mid = (high + low) / 2; upd(val, l, r, low, mid, 2 * i + 1); upd(val, l, r, mid + 1, high, 2 * i + 2); st[i] = st[2 * i + 1] & st[2 * i + 2]; } long long query(long long l, long long r, long long low, long long high, long long i) { if (lz[i]) { st[i] |= lz[i]; if (high != low) { lz[2 * i + 1] |= lz[i]; lz[2 * i + 2] |= lz[i]; } lz[i] = 0; } if (high < l || low > r) { return (-1); } if (low >= l && high <= r) { return (st[i]); } long long mid = (high + low) / 2; long long lft = query(l, r, low, mid, 2 * i + 1); long long rght = query(l, r, mid + 1, high, 2 * i + 2); st[i] = st[2 * i + 1] & st[2 * i + 2]; if (lft == -1) { return (rght); } if (rght == -1) { return (lft); } return ((lft & rght)); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m; cin >> n >> m; long long a[3][m]; for (long long i = 0; i < m; i++) { cin >> a[0][i] >> a[1][i] >> a[2][i]; upd(a[2][i], a[0][i] - 1, a[1][i] - 1, 0, n - 1, 0); } for (long long i = 0; i < m; i++) { if (query(a[0][i] - 1, a[1][i] - 1, 0, n - 1, 0) != a[2][i]) { cout << "NO" << endl; exit(0); } } cout << "YES" << endl; for (long long i = 0; i < n; i++) { cout << query(i, i, 0, n - 1, 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.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class P482B { public static void main(String[] args) { FastScanner scan = new FastScanner(); int n = scan.nextInt(); int m = scan.nextInt(); int[] a = new int[m]; int[] b = new int[m]; int[] q = new int[m]; for (int i = 0; i < m; i++) { a[i] = scan.nextInt()-1; b[i] = scan.nextInt()-1; q[i] = scan.nextInt(); } int[] num = new int[n]; for (int i = 0; i < 30; i++) { int[] delta = new int[n+1]; for (int j = 0; j < m; j++) { if ((q[j] & (1 << i)) != 0) { delta[a[j]]++; delta[b[j]+1]--; } } int[] cumul = new int[n+1]; int c = 0, t = 0; for (int j = 0; j < n; j++) { c += delta[j]; if (c == 0) cumul[j+1] = ++t; else { cumul[j+1] = t; num[j] |= (1 << i); } } for (int j = 0; j < m; j++) { if ((q[j] & (1 << i)) == 0) { if (cumul[a[j]] == cumul[b[j]+1]) { System.out.println("NO"); return; } } } } PrintWriter pw = new PrintWriter(System.out); pw.println("YES"); for (int i = 0; i < n; i++) { pw.print(num[i] + " "); } pw.flush(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } 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 String nextLine() { String line = ""; try { line = br.readLine(); } catch (Exception e) { e.printStackTrace(); } return line; } } }
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 gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long powx(long long a, long long p) { long long ans = 1; for (int i = 0; i < p; i++) ans *= a; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m; cin >> n >> m; vector<vector<long long>> cons; long long fre[n + 2][32], dp[n + 2][32]; for (long long i = 0; i <= n + 1; i++) { for (long long j = 0; j < 32; j++) fre[i][j] = 0, dp[i][j] = 0; } for (long long i = 0; i < m; i++) { long long l, r, q; cin >> l >> r >> q; cons.push_back({l, r, q}); for (long long j = 0; j < 32; j++) { if (q & (1 << j)) { fre[l][j] += 1; fre[r + 1][j] += -1; } } } for (long long j = 0; j < 32; j++) { for (long long i = 1; i <= n; i++) { fre[i][j] += fre[i - 1][j]; dp[i][j] = min((long long)1, fre[i][j]); } } for (long long j = 0; j < 32; j++) { for (long long i = 1; i <= n; i++) { dp[i][j] += dp[i - 1][j]; } } for (long long i = 0; i < m; i++) { long long l = cons[i][0]; long long r = cons[i][1]; long long q = cons[i][2]; for (long long j = 0; j < 3; j++) { long long cnt = dp[r][j] - dp[l - 1][j]; if ((q & (1 << j)) == 0) { if (cnt == r - l + 1) { cout << "NO" << '\n'; return 0; } } } } cout << "YES" << '\n'; for (long long i = 1; i <= n; i++) { long long x = 0; for (long long j = 0; j < 32; j++) { if (fre[i][j] > 0) x += (1 << j); } 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
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 100; int t[maxn * 8], a[maxn], l[maxn], r[maxn], q[maxn], ans[maxn], n, m; inline int C(int x, int y) { return x & (1 << y); } void build(int l, int r, int p) { if (l == r) { t[p] = ans[l]; return; } int mid = l + r >> 1; build(l, mid, p << 1); build(mid + 1, r, p << 1 | 1); t[p] = t[p << 1] & t[p << 1 | 1]; } int query(int l, int r, int x, int y, int p) { if (l == x && r == y) return t[p]; int mid = l + r >> 1; if (y <= mid) return query(l, mid, x, y, p << 1); if (x > mid) return query(mid + 1, r, x, y, p << 1 | 1); return query(l, mid, x, mid, p << 1) & query(mid + 1, r, mid + 1, y, p << 1 | 1); } int main() { cin >> n >> m; for (int i = 1; i <= m; ++i) scanf("%d%d%d", l + i, r + i, q + i); for (int bit = 0; bit <= 30; ++bit) { memset(a, 0, sizeof(a)); for (int i = 1; i <= m; ++i) { if (C(q[i], bit)) { ++a[l[i]]; --a[r[i] + 1]; } } for (int i = 1; i <= n; ++i) a[i] += a[i - 1]; for (int i = 1; i <= n; ++i) if (a[i]) { ans[i] |= 1 << bit; } } build(1, n, 1); int flag = 1; for (int i = 1; i <= m; ++i) { if (query(1, n, l[i], r[i], 1) != q[i]) { flag = 0; break; } } if (flag) { cout << "YES" << endl; } else { cout << "NO" << endl; } if (flag) { for (int i = 1; i <= n; ++i) { if (i != n) printf("%d ", ans[i]); else { cout << ans[i] << 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; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1}; int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; const long long inf = 998244353; vector<int> adj[200010], vis(200010, 0), par(200010, 0), dis(200010, 0), di(200010, 0), fin(200010, 0); int myrandom(int i) { return std::rand() % i; } using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> bit(32, vector<int>(n + 2, 0)); vector<vector<int>> limits; while (m--) { int l, r, q; cin >> l >> r >> q; limits.push_back({l, r, q}); for (int i = 0; i <= 30; i++) { if ((1ll << i) & q) { bit[i][l]++; bit[i][r + 1]--; } } } vector<int> pos[32]; for (int i = 0; i <= 30; i++) { for (int j = 1; j <= n; j++) { bit[i][j] += bit[i][j - 1]; if (bit[i][j] == 0) { pos[i].push_back(j); } } } bool ok = true; for (auto i : limits) { int l = i[0], r = i[1], q = i[2]; for (int i = 0; i <= 30; i++) { if ((1ll << i) & q) { auto itr = lower_bound((pos[i]).begin(), (pos[i]).end(), l); if (itr != pos[i].end() && *itr <= r) { ok = false; break; } } else { auto itr = lower_bound((pos[i]).begin(), (pos[i]).end(), l); if (itr == pos[i].end()) { ok = false; } else { if (*itr > r) { ok = false; } } } } if (!ok) { break; } } if (!ok) { cout << "NO" << '\n'; } else { cout << "YES" << '\n'; vector<long long> ans; for (int i = 1; i <= n; i++) { long long num = 0; long long pow = 1; for (int j = 0; j <= 30; j++) { if (bit[j][i] > 0) { num = num + pow; } pow = pow * 2; } ans.push_back(num); } for (auto i : ans) cout << i << " "; cout << '\n'; } }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } void __print(int first) { cerr << first; } void __print(long first) { cerr << first; } void __print(unsigned first) { cerr << first; } void __print(unsigned long first) { cerr << first; } void __print(unsigned long long first) { cerr << first; } void __print(float first) { cerr << first; } void __print(double first) { cerr << first; } void __print(long double first) { cerr << first; } void __print(char first) { cerr << '\'' << first << '\''; } void __print(const char *first) { cerr << '\"' << first << '\"'; } void __print(const string &first) { cerr << '\"' << first << '\"'; } void __print(bool first) { cerr << (first ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &first) { cerr << '{'; __print(first.first); cerr << ','; __print(first.second); cerr << '}'; } template <typename T> void __print(const T &first) { int f = 0; cerr << '{'; for (auto &i : first) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const int N = 101010, B = 31; vector<int> l(N), r(N), q(N), a(N), st(4 * N); int n, m; void build(int v, int l, int r) { if (l == r) { st[v] = a[l]; return; } int mid = (l + r) >> 1; build(2 * v + 1, l, mid); build(2 * v + 2, mid + 1, r); st[v] = st[2 * v + 1] & st[2 * v + 2]; } int query(int v, int s, int e, int l, int r) { int ans = (1 << B) - 1; if (l > r || s < 0 || e > n - 1 || e < l || s > r) return ans; if (l <= s and r >= e) { return st[v]; } int mid = (s + e) >> 1; ans &= query(v * 2 + 1, s, mid, l, r); ans &= query(v * 2 + 2, mid + 1, e, l, r); return ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); 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 < B; i++) { vector<int> pos(n + 5); for (int j = 0; j < m; j++) { if (q[j] >> i & 1) { pos[l[j]]++; pos[r[j] + 1]--; } } for (int i = 0; i < n; i++) { if (i > 0) pos[i] += pos[i - 1]; } for (int j = 0; j < n; j++) if (pos[j] > 0) a[j] |= (1 << i); } build(0, 0, n - 1); int f = 0; for (int i = 0; i < m; i++) { if (query(0, 0, n - 1, l[i], r[i]) != q[i]) { f = 1; break; } } if (!f) { cout << "YES" << "\n"; for (int i = 0; i < n; i++) cout << a[i] << " "; } else cout << "NO"; 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 = 200010; int from[N], to[N], num[N]; int s[N][30]; int a[N][30]; int sum[N][30]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i <= n; i++) { for (int j = 0; j < 30; j++) { s[i][j] = 0; } } for (int k = 0; k < m; k++) { scanf("%d %d %d", from + k, to + k, num + k); from[k]--; to[k]--; for (int j = 0; j < 30; j++) { if (num[k] & (1 << j)) { s[from[k]][j]++; s[to[k] + 1][j]--; } } } for (int j = 0; j < 30; j++) { int bal = 0; sum[0][j] = 0; for (int i = 0; i < n; i++) { bal += s[i][j]; a[i][j] = (bal > 0); sum[i + 1][j] = sum[i][j] + a[i][j]; } } for (int k = 0; k < m; k++) { for (int j = 0; j < 30; j++) { if (!(num[k] & (1 << j))) { int get = sum[to[k] + 1][j] - sum[from[k]][j]; int need = (to[k] + 1) - (from[k]); if (get == need) { puts("NO"); return 0; } } } } puts("YES"); for (int i = 0; i < n; i++) { int res = 0; for (int j = 0; j < 30; j++) { if (a[i][j]) { res += (1 << j); } } if (i > 0) printf(" "); printf("%d", res); } printf("\n"); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
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"); } } //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; const int maxn = 1e5 + 5; int c[32][maxn]; int l[maxn]; int r[maxn]; int q[maxn]; int n; int main(void) { memset(c, 0, sizeof(c)); int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &l[i], &r[i], &q[i]); int x = q[i]; for (int j = 0; j < 30; j++) { if ((x >> j) & 1) { c[j][l[i]]++; c[j][r[i] + 1]--; } } } for (int i = 0; i < 30; i++) { for (int j = 1; j <= n; j++) c[i][j] += c[i][j - 1]; for (int j = 1; j <= n; j++) if (c[i][j]) c[i][j] = 1; for (int j = 1; j <= n; j++) c[i][j] += c[i][j - 1]; } bool flag = true; for (int i = 1; i <= m; i++) { int len = r[i] - l[i] + 1; int x = 0; for (int j = 0; j < 30; j++) { if (c[j][r[i]] - c[j][l[i] - 1] == len) { x |= (1 << j); } } if (x != q[i]) { flag = false; break; } } if (flag == false) printf("NO\n"); else { printf("YES\n"); for (int i = 1; i <= n; i++) { int x = 0; for (int j = 0; j < 30; j++) { if (c[j][i] - c[j][i - 1]) { x |= (1 << j); } } printf("%d%c", x, (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; const int N = 1e5 + 11; int num[N]; struct Segment { int mark[N << 2]; int x1, y1, ans; int n; void init(int x) { n = x; build(1, 1, n); } void build(int u, int l, int r) { mark[u] = 0; if (l == r) return; build(u << 1, l, ((l + r) >> 1)); build(u << 1 | 1, ((l + r) >> 1) + 1, r); } void update(int l, int r, int d) { x1 = l, y1 = r; _update(1, 1, n, d); } void up(int u) { mark[u] |= (mark[u << 1] & mark[u << 1 | 1]); } void _update(int u, int l, int r, int d) { if (x1 <= l && r <= y1) { mark[u] |= d; return; } if (x1 <= ((l + r) >> 1)) _update(u << 1, l, ((l + r) >> 1), d); if (y1 > ((l + r) >> 1)) _update(u << 1 | 1, ((l + r) >> 1) + 1, r, d); up(u); } int query(int l, int r) { x1 = l, y1 = r; ans = (1 << 30) - 1; _query(1, 1, n); return ans; } void _query(int u, int l, int r) { if (x1 <= l && r <= y1) { ans &= mark[u]; return; } if (x1 <= ((l + r) >> 1)) _query(u << 1, l, ((l + r) >> 1)); if (y1 > ((l + r) >> 1)) _query(u << 1 | 1, ((l + r) >> 1) + 1, r); } void fun() { _fun(1, 1, n); } void _fun(int u, int l, int r) { if (u > 1) mark[u] |= mark[u >> 1]; if (l == r) { num[l] = mark[u]; return; } _fun(u << 1, l, ((l + r) >> 1)); _fun(u << 1 | 1, ((l + r) >> 1) + 1, r); } }; Segment seg; int n, m; int oper[N][3]; int main() { while (scanf("%d %d", &n, &m) == 2) { for (int i = 0; i < m; ++i) for (int j = 0; j < 3; ++j) scanf("%d", &oper[i][j]); seg.init(n); for (int i = 0; i < m; ++i) { seg.update(oper[i][0], oper[i][1], oper[i][2]); } bool mark = false; for (int i = 0; i < m; ++i) { if (seg.query(oper[i][0], oper[i][1]) != oper[i][2]) { mark = true; break; } } if (mark) printf("NO\n"); else { seg.fun(); printf("YES\n"); for (int i = 1; i <= n; ++i) { printf("%d", num[i]); if (i != n) printf(" "); else 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 mxN = 1e5 + 10; int l[mxN]; int r[mxN]; int q[mxN]; int s[mxN], ss[mxN]; int ans[mxN]; void solve() { 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 bit = 0; bit < 30; bit++) { for (int i = 0; i <= n; i++) s[i] = 0; for (int i = 0; i < m; i++) if (q[i] & (1LL << bit)) s[l[i]]++, s[r[i] + 1]--; for (int i = 1; i < n; i++) s[i] += s[i - 1]; for (int i = 0; i < n; i++) if (s[i] > 0) s[i] = 1; for (int i = 0; i < n; i++) ans[i] += (1 << bit) * s[i]; ss[0] = 0; for (int i = 0; i < n; i++) ss[i + 1] = ss[i] + s[i]; for (int i = 0; i < m; i++) { if (!(q[i] & (1 << bit))) { int a = ss[r[i] + 1] - ss[l[i]]; int b = r[i] - l[i] + 1; if (a == b) { cout << "NO\n"; exit(0); } } } } cout << "YES\n"; for (int i = 0; i < n; i++) { cout << ans[i] << " "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.InputStreamReader; import java.io.IOException; import java.io.FileReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Agostinho Junior */ 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 { OutputWriter out; InputReader in; public void solve(int testNumber, InputReader in, OutputWriter out) { this.in = in; this.out = out; int n = in.readInt(); int m = in.readInt(); int[] L = new int[m]; int[] R = new int[m]; int[] Q = new int[m]; final int MAX = 30; int[][] sums = new int[MAX + 1][n + 2]; int[] ans = new int[n + 1]; for (int i = 0; i < m; i++) { L[i] = in.readInt(); R[i] = in.readInt(); Q[i] = in.readInt(); for (int bit = 0; bit <= MAX; bit++) if ((Q[i] & (1<<bit)) > 0) { sums[bit][L[i]]++; sums[bit][R[i] + 1]--; } } for (int bit = 0; bit <= MAX; bit++) { int value = 0; for (int i = 1; i <= n; i++) { value += sums[bit][i]; sums[bit][i] = sums[bit][i - 1] + (value > 0 ? 1 : 0); ans[i] |= (sums[bit][i] - sums[bit][i - 1]) << bit; } } for (int i = 0; i < m; i++) for (int bit = 0; bit <= MAX; bit++) { boolean a = (Q[i] & (1 << bit)) > 0; boolean b = sums[bit][R[i]] - sums[bit][L[i] - 1] == R[i] - L[i] + 1; if (a != b) { out.println("NO"); return; } } out.println("YES"); for (int i = 1; i <= n; i++) { out.print(ans[i] + " "); } out.println(); } } class OutputWriter { private PrintWriter output; public OutputWriter(OutputStream out) { output = new PrintWriter(out); } public void print(Object o) { output.print(o); } public void println(Object o) { output.println(o); } public void println() { output.println(); } public void close() { output.close(); } } class InputReader { private BufferedReader input; private StringTokenizer line = new StringTokenizer(""); public InputReader(InputStream in) { input = new BufferedReader(new InputStreamReader(in)); } public void fill() { try { if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine()); } catch(IOException io) { io.printStackTrace(); System.exit(0);} } public int readInt() { fill(); return Integer.parseInt(line.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> struct tree { int l, r; int sum[33]; } tr[444444]; int a; int build(int l, int r, int i) { tr[i].l = l; tr[i].r = r; int j; for (j = 0; j <= 30; j++) tr[i].sum[j] = 0; if (l == r) return 0; int mid = (l + r) / 2; build(l, mid, i * 2); build(mid + 1, r, i * 2 + 1); } int quer(int l, int r, int i) { int j; if (tr[i].l == l && tr[i].r == r) { int temp = 0; for (j = 0; j <= 30; j++) if (tr[i].sum[j] == 1) temp += (1 << j); return temp; } for (j = 0; j <= 30; j++) if (tr[i].sum[j] == 1) tr[i * 2 + 1].sum[j] = tr[i * 2].sum[j] = tr[i].sum[j]; int mid = (tr[i].l + tr[i].r) / 2; if (mid >= r) return quer(l, r, i * 2); else if (mid < l) return quer(l, r, i * 2 + 1); else { return (quer(l, mid, i * 2) & quer(mid + 1, r, i * 2 + 1)); } for (j = 0; j <= 30; j++) tr[i].sum[j] = tr[i * 2 + 1].sum[j] & tr[i * 2].sum[j]; } int update(int l, int r, int q, int i) { int j; if (tr[i].l == l && tr[i].r == r) { for (j = 0; j <= 30; j++) if (q & (1 << j)) tr[i].sum[j] = 1; return 0; } for (j = 0; j <= 30; j++) if (tr[i].sum[j] == 1) tr[i * 2 + 1].sum[j] = tr[i * 2].sum[j] = tr[i].sum[j]; int mid = (tr[i].l + tr[i].r) / 2; if (mid >= r) update(l, r, q, i * 2); else if (mid < l) update(l, r, q, i * 2 + 1); else { update(l, mid, q, i * 2); update(mid + 1, r, q, i * 2 + 1); } for (j = 0; j <= 30; j++) tr[i].sum[j] = tr[i * 2 + 1].sum[j] & tr[i * 2].sum[j]; } int l[111111], r[111111], q[111111]; int main() { int n, m; scanf("%d%d", &n, &m); int i; int f = 0; build(1, n, 1); for (i = 0; i < m; i++) { scanf("%d%d%d", &l[i], &r[i], &q[i]); update(l[i], r[i], q[i], 1); } for (i = 0; i < m; i++) if ((quer(l[i], r[i], 1) | q[i]) > q[i]) f = 1; if (f) printf("NO\n"); else { printf("YES\n"); for (i = 1; i <= n; i++) { printf("%d ", quer(i, i, 1)); } } }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
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 == r){ tree[node]=arr[l]; return; } int mid = (l+r)/2; 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 L,int R){ if (L >= l && R <= r) { return tree[node]; } else if(r < L || l > R) return (1 << 30) - 1; else{ int mid = (L + R) >> 1; int ans = (1 << 30) - 1; ans &= query(node*2,l, r, L, mid); ans &= query(node*2+1,l,r, mid+1, 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-1); for(int i=0;i<m;i++){ if (query(1, l[i], r[i]-1, 0, n-1) != 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; bool z[100100][40]; int dp[100100][40]; pair<pair<long long, long long>, long long> q[100100]; int r[300300][35]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { scanf("%I64d%I64d%I64d", &q[i].first.second, &q[i].first.first, &q[i].second); for (long long j = 0; j < 35; j++) { if ((1LL << j) & q[i].second) { dp[q[i].first.second][j]++; dp[q[i].first.first + 1][j]--; } } } for (int i = 1; i <= n; i++) { for (int j = 0; j < 35; j++) { dp[i][j] += dp[i - 1][j]; if (dp[i][j]) { z[i][j] = 1; } } } for (int i = 1; i <= n; i++) { for (int j = 0; j < 35; j++) { r[i][j] += r[i - 1][j]; if (z[i][j] == 0) { r[i][j]++; } } } for (int i = 0; i < m; i++) { for (long long j = 0; j < 35; j++) { if (((1LL << j) & q[i].second) == 0) { if (r[q[i].first.first][j] - r[q[i].first.second - 1][j] == 0) { cout << "NO"; return 0; } } } } cout << "YES" << endl; for (int i = 1; i <= n; i++) { long long res = 0; for (long long j = 0; j < 35; j++) { res += (1LL << j) * (z[i][j]); } if (i != 1) printf(" "); printf("%I64d", res); } 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.*; public class Main { public static void process()throws IOException { int n=ni(); int m=ni(); int[][]A=new int[30][n+1]; int[][]q=new int[m][3]; for(int i=0;i<m;i++) { q[i][0]=ni(); q[i][1]=ni(); q[i][2]=ni(); int temp=q[i][2]; int curr=0; while(temp>0) { if(temp%2==1) { A[curr][q[i][0]]++; if(q[i][1]<n) A[curr][q[i][1]+1]--; } temp/=2; curr++; } } int[]val=new int[n+1]; for(int i=0;i<30;i++) { for(int j=1;j<=n;j++) { A[i][j]+=A[i][j-1]; if(A[i][j]>0) val[j]|=(1<<i); } } segTree seg=new segTree(n,val); seg.build(1,n,1); for(int i=0;i<m;i++) { int curr=seg.query(q[i][0],q[i][1],1,n,1); if(curr!=q[i][2]) {pn("NO");return;} } pn("YES"); for(int i=1;i<=n;i++) p(val[i]+" "); pn(""); } static class segTree { int tree[], n, arr[]; public segTree(int n, int arr[]) { this.arr = arr; this.n = n; tree = new int[4 * n + 4]; } void build(int l, int r, int idx) { if(l==r) {tree[idx]=arr[l];return;} int mid =l+(r-l)/2; build(l,mid,2*idx); build(mid+1,r,2*idx+1); tree[idx]=tree[2*idx]&tree[2*idx+1]; } int query(int qs, int qe, int l, int r, int idx) { if(qs<=l && qe>=r) return tree[idx]; if(qs>r || qe<l) return (1<<31)-1; int mid=l+(r-l)/2; return query(qs,qe,l,mid,2*idx)&query(qs,qe,mid+1,r,2*idx+1); } } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);} else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");} int t=1; //t=ni(); while(t-->0) {process();} out.flush();out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} 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{ 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
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; struct Tr { int l, r; int sum, add; } tr[N << 1]; void up(int rt) { tr[rt].sum = tr[rt << 1].sum & tr[rt << 1 | 1].sum; } void dwn(int rt) { if (tr[rt].add) { int tmp = tr[rt].add; tr[rt << 1].add = tr[rt << 1].add | tmp; tr[rt << 1].sum = tr[rt << 1].sum | tmp; tr[rt << 1 | 1].add = tr[rt << 1 | 1].add | tmp; tr[rt << 1 | 1].sum = tr[rt << 1 | 1].sum | tmp; tr[rt].add = 0; } } void build(int u, int l, int r) { tr[u] = {l, r, 0, 0}; if (l == r) return; int mid = (l + r) >> 1; build(u << 1, l, mid); build(u << 1 | 1, mid + 1, r); } void update(int u, int l, int r, int val) { if (tr[u].sum == tr[u].r - tr[u].l + 1) return; if (tr[u].l >= l && tr[u].r <= r) { tr[u].add = tr[u].add | val; tr[u].sum = tr[u].sum | val; return; } int mid = (tr[u].l + tr[u].r) >> 1; dwn(u); if (mid >= l) update(u << 1, l, r, val); if (mid < r) update(u << 1 | 1, l, r, val); up(u); return; } int query(int u, int l, int r) { if (tr[u].l >= l && tr[u].r <= r) { return tr[u].sum; } int mid = (tr[u].l + tr[u].r) >> 1; int ans = (1 << 30) - 1; dwn(u); if (mid >= l) ans = ans & query(u << 1, l, r); if (mid < r) ans = ans & query(u << 1 | 1, l, r); up(u); return ans; } map<pair<int, int>, int> mp; int ans[N]; int n, m; int main() { int n, m, l, r, q; scanf("%d%d", &n, &m); build(1, 1, n); int flag = 0; for (int i = 1; i <= m; i++) { scanf("%d%d%d", &l, &r, &q); if (mp.count(pair<int, int>(l, r))) { if (mp[pair<int, int>(l, r)] != q) flag = 1; } mp[pair<int, int>(l, r)] = q; if (flag == 1) continue; int res = query(1, l, r); if ((q & res) == res) { int tmp = q - res; update(1, l, r, tmp); } else flag = 1; } if (flag == 1) return 0 * puts("NO"); for (int i = 1; i <= n; i++) ans[i] = query(1, i, i); puts("YES"); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import java.text.*; public class cf482b { static BufferedReader br; static Scanner sc; static PrintWriter out; public static void initA() { try { br = new BufferedReader(new InputStreamReader(System.in)); sc = new Scanner(System.in); out = new PrintWriter(System.out); } catch (Exception e) { } } static boolean next_permutation(Integer[] p) { for (int a = p.length - 2; a >= 0; --a) { if (p[a] < p[a + 1]) { for (int b = p.length - 1;; --b) { if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } } } } return false; } public static String getString() { try { return br.readLine(); } catch (Exception e) { } return ""; } public static Integer getInt() { try { return Integer.parseInt(br.readLine()); } catch (Exception e) { } return 0; } public static Integer[] getIntArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); Integer temp2[] = new Integer[n]; for (int i = 0; i < n; i++) { temp2[i] = Integer.parseInt(temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static Long[] getLongArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); Long temp2[] = new Long[n]; for (int i = 0; i < n; i++) { temp2[i] = Long.parseLong(temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static String[] getStringArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); String temp2[] = new String[n]; for (int i = 0; i < n; i++) { temp2[i] = (temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static void print(Object a) { out.println(a); } public static void print(String s, Object... a) { out.printf(s, a); } public static int nextInt() { return sc.nextInt(); } public static double nextDouble() { return sc.nextDouble(); } public static void main(String[] ar) { initA(); cf482b c = new cf482b(); c.solve(); out.flush(); } int tree[], lazy[]; int m = 0; void update(int node, int cl, int cr, int l, int r, int v) { tree[node] |= lazy[node]; if (cl != cr) { lazy[node * 2] |= lazy[node]; lazy[node * 2 + 1] |= lazy[node]; } lazy[node] = 0; if (cl > r || cr < l) { return; } if (cl >= l && cr <= r) { tree[node] |= v; // print("SETx %d - %d = %d\n", cl, cr, tree[node]); if (cl != cr) { lazy[node * 2] |= v; lazy[node * 2 + 1] |= v; } return; } int mid = (cl + cr) / 2; update(node * 2, cl, mid, l, r, v); update(node * 2 + 1, mid + 1, cr, l, r, v); int a = tree[node * 2], b = tree[node * 2 + 1]; tree[node] = tree[node * 2] & tree[node * 2 + 1]; //print("SET NODE %d = %d - %d dari %d dan %d = %d\n", node, cl, cr, a, b, tree[node]); } int q(int node, int cl, int cr, int l, int r) { //System.out.printf("%d %d %d %d %d\n",node,cl,cr,l,r); tree[node] |= lazy[node]; if (cl != cr) { lazy[node * 2] |= lazy[node]; lazy[node * 2 + 1] |= lazy[node]; } lazy[node] = 0; if (cl > r || cr < l) { return m; } if (cl >= l && cr <= r) { return tree[node]; } int mid = (cl + cr) / 2; int a = q(node * 2, cl, mid, l, r); int b = q(node * 2 + 1, mid + 1, cr, l, r); return a & b; } void solve() { Integer xx[] = getIntArr(); int n = xx[0], q = xx[1]; tree = new int[n * 4 + 1]; lazy = new int[n * 4 + 1]; int lqx[] = new int[q]; int lqy[] = new int[q]; int lqv[] = new int[q]; for (int i = 0; i < q; i++) { xx = getIntArr(); int from = xx[0], to = xx[1], v = xx[2]; m |= v; // print("QUERY %d\n", i + 1); update(1, 1, n, from, to, v); lqx[i] = from; lqy[i] = to; lqv[i] = v; } for (int i = 0; i < q; i++) { int from = lqx[i], to = lqy[i], v = lqv[i]; int rq = q(1, 1, n, from, to); // print("%d - %d = %d\n", from, to, rq); if (rq != v) { print("NO"); return; } } print("YES"); for (int i = 1; i <= n; i++) { int v = q(1, 1, n, i, i); print("%d ", v); } print(""); } }
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("O2") using namespace std; const int N = 1e5 + 1; vector<int> st(4 * N, 0), lazy(4 * N, 0); void prop(int p, int l, int r) { if (lazy[p] == 0) return; st[p] += lazy[p]; if (l != r) lazy[(2 * p + 1)] += lazy[p], lazy[(2 * p)] += lazy[p]; lazy[p] = 0; } void update(int p, int l, int r, int i, int j, int val) { prop(p, l, r); if (l > j || r < i) return; if (l >= i && r <= j) { lazy[p] += val; prop(p, l, r); return; } update((2 * p), l, ((l + r) / 2), i, j, val); update((2 * p + 1), ((l + r) / 2) + 1, r, i, j, val); st[p] = (st[(2 * p)] & st[(2 * p + 1)]); } int query(int p, int l, int r, int i, int j) { prop(p, l, r); if (l > j || r < i) return ((1LL << 32) - 1); if (l >= i && r <= j) return st[p]; return (query((2 * p), l, ((l + r) / 2), i, j) & query((2 * p + 1), ((l + r) / 2) + 1, r, i, j)); } int main() { int n, m; cin >> n >> m; vector<vector<int>> qe(m, vector<int>(3)); for (auto &x : qe) cin >> x[0] >> x[1] >> x[2]; vector<int> temp(n + 1, 0); for (int j = 0; j <= 30; j++) { vector<int> sum(n + 2, 0); for (auto &x : qe) if ((1 << j) & x[2]) sum[x[0]]++, sum[x[1] + 1]--; for (int i = 1; i <= n; i++) { sum[i] += sum[i - 1]; if (sum[i]) temp[i] |= (1 << j); } } for (int i = 1; i <= n; i++) update(1, 1, N, i, i, temp[i]); for (auto x : qe) { if (query(1, 1, N, x[0], x[1]) != x[2]) { cout << "NO"; return 0; } } cout << "YES" << "\n"; for (int i = 1; i <= n; i++) cout << temp[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; using i64 = long long int; using ii = pair<int, int>; using ii64 = pair<i64, i64>; int bits[32][100005]; int l[100005], r[100005], q[100005]; template <typename T> class SegmentTree { public: void init(vector<T>& raw) { assert(!is_init); is_init = true; n = raw.size(); int h = (int)ceil(log2(n)); int tree_size = (1 << (h + 1)); data.resize(tree_size); init_internal(raw, 1, 0, n - 1); } T update(int idx, const T& newVal) { assert(is_init); return update_internal(1, 0, n - 1, idx, newVal); } T query(int left, int right) { assert(is_init); return query_internal(1, 0, n - 1, left, right); } virtual T merge(const T& left, const T& right) = 0; virtual T merge_with_idx(const T& left, const T& right, int left_idx, int right_idx) { return merge(left, right); } private: vector<T> data; int n; bool is_init = false; T init_internal(vector<T>& raw, int node, int start, int end) { int mid = (start + end) / 2; if (start == end) return data[node] = raw[start]; else return data[node] = merge_with_idx(init_internal(raw, node * 2, start, mid), init_internal(raw, node * 2 + 1, mid + 1, end), node * 2, node * 2 + 1); } T update_internal(int node, int start, int end, int index, const T& newVal) { if (index < start || index > end) return data[node]; if (start == end) { data[node] = newVal; } else { int mid = (start + end) / 2; data[node] = merge_with_idx( update_internal(node * 2, start, mid, index, newVal), update_internal(node * 2 + 1, mid + 1, end, index, newVal), node * 2, node * 2 + 1); } return data[node]; } T query_internal(int node, int start, int end, int left, int right) { if (left <= start && end <= right) return data[node]; int mid = (start + end) / 2; if (mid < left) return query_internal(node * 2 + 1, mid + 1, end, left, right); if (mid + 1 > right) return query_internal(node * 2, start, mid, left, right); return merge_with_idx( query_internal(node * 2, start, mid, left, right), query_internal(node * 2 + 1, mid + 1, end, left, right), node * 2, node * 2 + 1); } }; class AndTree : public SegmentTree<int> { public: virtual int merge(const int& l, const int& r) override { return l & r; } }; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d %d", &l[i], &r[i], &q[i]); for (int j = 0; j < 30; j++) { if ((q[i] & (1 << j)) == 0) continue; bits[j][l[i]]++; bits[j][r[i] + 1]--; } } vector<int> arr(n + 3); for (int j = 0; j < 30; j++) { for (int i = 1; i <= n; i++) { bits[j][i] += bits[j][i - 1]; if (bits[j][i] > 0) arr[i] |= (1 << j); } } AndTree tree; tree.init(arr); for (int i = 0; i < m; i++) { if (tree.query(l[i], r[i]) != q[i]) { printf("NO\n"); return 0; } } printf("YES\n"); for (int i = 1; i <= n; i++) printf("%d ", arr[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.*; import java.text.DecimalFormat; import java.util.*; public class Main { static class SegmentTree { // 1-based DS, OOP int N; // the number of elements in the array as a power of 2 (i.e. after padding) int[] array, sTree, lazy; SegmentTree(int[] in) { array = in; N = in.length - 1; sTree = new int[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new int[N << 1]; build(1, 1, N); } void build(int node, int b, int e) // O(n) { if (b == e) sTree[node] = array[b]; else { int mid = b + e >> 1; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); sTree[node] = sTree[node << 1] | sTree[node << 1 | 1]; } } void update_point(int index, int val) // O(log n) { index += N - 1; sTree[index] += val; while (index > 1) { index >>= 1; sTree[index] = sTree[index << 1] | sTree[index << 1 | 1]; } } void update_range(int i, int j, int val) // O(log n) { update_range(1, 1, N, i, j, val); } void update_range(int node, int b, int e, int i, int j, int val) { if (i > e || j < b) return; if (b >= i && e <= j) { sTree[node] |= val; lazy[node] |= val; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node << 1, b, mid, i, j, val); update_range(node << 1 | 1, mid + 1, e, i, j, val); sTree[node] = sTree[node << 1] | sTree[node << 1 | 1]; } } void propagate(int node, int b, int mid, int e) { lazy[node << 1] |= lazy[node]; lazy[node << 1 | 1] |= lazy[node]; sTree[node << 1] |= lazy[node]; sTree[node << 1 | 1] |= lazy[node]; lazy[node] = 0; } int query(int i, int j) { return query(1, 1, N, i, j); } int query(int node, int b, int e, int i, int j) // O(log n) { if (i > e || j < b) return 0; if (b >= i && e <= j) return sTree[node]; int mid = b + e >> 1; propagate(node, b, mid, e); int q1 = query(node << 1, b, mid, i, j); int q2 = query(node << 1 | 1, mid + 1, e, i, j); return q1 | q2; } } //////////////////////////// static class SegmentTree2 { // 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; SegmentTree2(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 (2 << 30) - 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; } } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int N = 1; while (N < n) N <<= 1; SegmentTree sg = new SegmentTree(new int[N + 1]); int m = sc.nextInt(); ArrayList<tri> a = new ArrayList<>(); for (int i = 0; i < m; i++) { a.add(new tri(sc.nextInt(), sc.nextInt(), sc.nextInt())); sg.update_range(a.get(i).x, a.get(i).y, a.get(i).z); } int ans[] = new int[N + 1]; Arrays.fill(ans, (2 << 30) - 1); for (int i = 1; i < n + 1; i++) { ans[i] = sg.query(i, i); } // System.out.println(Arrays.toString(ans)); SegmentTree2 sg2 = new SegmentTree2(ans); // System.out.println(Arrays.toString(sg2.sTree)); // System.out.println(Arrays.toString(sg.sTree)); boolean f = true; // System.out.println(sg2.query(1, 3)); // System.out.println(sg.query(3, 3)); for (tri t : a) { if (sg2.query(t.x, t.y) != t.z) f = false; } if (f) { System.out.println("YES"); for (int i = 0; i < n; i++) { System.out.print(ans[i + 1] + " "); } } else System.out.println("NO"); } /////////////////////////////////////////////////////////////////////////////////////////// static class tri { int x, y, z; public tri(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } static class pair implements Comparable<pair> { int x, y; pair(int s, int d) { x = s; y = d; } @Override public int compareTo(pair p) { return (x == p.x && y == p.y) ? 0 : 1; } @Override public String toString() { return x + " " + y; } } static long mod(long ans, int mod) { return (ans % mod + mod) % mod; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int log(int n, int base) { int ans = 0; while (n + 1 > base) { ans++; n /= base; } return ans; } static long pow(long b, long e) { long ans = 1; while (e > 0) { if ((e & 1) == 1) ans = ((ans * 1l * b)); e >>= 1; { } b = ((b * 1l * b)); } return ans; } static int powmod(int b, long e, int mod) { int ans = 1; b %= mod; while (e > 0) { if ((e & 1) == 1) ans = (int) ((ans * 1l * b) % mod); e >>= 1; b = (int) ((b * 1l * b) % mod); } return ans; } static int ceil(int a, int b) { int ans = a / b; return a % b == 0 ? ans : ans + 1; } static long ceil(long a, long b) { long ans = a / b; return a % b == 0 ? ans : ans + 1; } static HashMap<Integer, Integer> compress(int a[]) { TreeSet<Integer> ts = new TreeSet<>(); HashMap<Integer, Integer> hm = new HashMap<>(); for (int x : a) ts.add(x); for (int x : ts) { hm.put(x, hm.size() + 1); } return hm; } // Returns nCr % p static int C[]; static int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; if (C[r] != 0) return C[r]; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public int[] intArr(int n) throws IOException { int a[] = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = nextInt(); } return a; } public long[] longArr(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < a.length; i++) { a[i] = nextLong(); } return a; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } public static void shuffle(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(), m = sc.nextInt(); int [] l = new int[m]; int [] r = new int[m]; int [] q = new int[m]; int N = 1; while(N < n) N <<=1; SegmentTree sg = new SegmentTree(N); for (int i = 0; i < q.length; ++i) { l[i] = sc.nextInt(); r[i] = sc.nextInt(); q[i] = sc.nextInt(); sg.update_range(l[i], r[i], q[i]); } boolean can = true; for (int i = 0; i < q.length; ++i) { can &= sg.query(l[i], r[i]) == q[i]; } if(!can) out.println("NO"); else{ out.println("YES"); for (int i = 1; i <= n; ++i) { out.print(sg.query(i, i) + " "); } } out.flush(); out.close(); } static class SegmentTree { // 1-based DS, OOP int N; //the number of elements in the array as a power of 2 (i.e. after padding) int[] array, sTree, lazy; static final int max = (1<<30)-1; SegmentTree(int n) { N = n; sTree = new int[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new int[N<<1]; } void update_range(int i, int j, int val) // O(log n) { update_range(1,1,N,i,j,val); } void update_range(int node, int b, int e, int i, int j, int val) { if(i > e || j < b) return; if(b >= i && e <= j) { sTree[node] |= val; lazy[node] |= val; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node<<1,b,mid,i,j,val); update_range(node<<1|1,mid+1,e,i,j,val); sTree[node] = sTree[node<<1] & sTree[node<<1|1]; } } void propagate(int node, int b, int mid, int e) { lazy[node<<1] |= lazy[node]; lazy[node<<1|1] |= lazy[node]; sTree[node<<1] |= lazy[node]; sTree[node<<1|1] |= lazy[node]; lazy[node] = 0; } int query(int i, int j) { return query(1,1,N,i,j); } int query(int node, int b, int e, int i, int j) // O(log n) { if(i>e || j <b) return max; if(b>= i && e <= j) return sTree[node]; int mid = b + e >> 1; propagate(node, b, mid, e); int q1 = query(node<<1,b,mid,i,j); int q2 = query(node<<1|1,mid+1,e,i,j); return q1 & q2; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public Scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } public long nextLong() throws NumberFormatException, 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; struct SegmentTreeOR { vector<int> t; int size; void init(int n) { size = 1; while (size < n) size *= 2; t.assign(2 * size, 0); } int get(int x, int lx, int rx, int p) { if (rx - lx == 1) { return t[x]; } int mx = (lx + rx) / 2; if (p < mx) { return t[x] | get(2 * x + 1, lx, mx, p); } else { return t[x] | get(2 * x + 2, mx, rx, p); } } int get(int p) { return get(0, 0, size, p); } void set(int x, int lx, int rx, int l, int r, int q) { if (l <= lx && rx <= r) { t[x] |= q; return; } if (l >= rx || lx >= r) { return; } int mx = (lx + rx) / 2; set(2 * x + 1, lx, mx, l, r, q); set(2 * x + 2, mx, rx, l, r, q); } void set(int l, int r, int q) { set(0, 0, size, l, r, q); } }; struct SegmentTreeAND { vector<int> t; int size; void init(int n) { size = 1; while (size < n) size *= 2; t.assign(2 * size, (1 << 30)); } void build(int x, int lx, int rx, vector<int> &a) { if (rx - lx == 1) { if (lx < a.size()) t[x] = a[lx]; else t[x] = (1 << 30) - 1; return; } int mx = (lx + rx) / 2; build(2 * x + 1, lx, mx, a); build(2 * x + 2, mx, rx, a); t[x] = t[2 * x + 1] & t[2 * x + 2]; } void build(vector<int> &a) { init(a.size()); build(0, 0, size, a); } int get(int x, int lx, int rx, int l, int r) { if (l <= lx && rx <= r) { return t[x]; } if (l >= rx || lx >= r) { return (1 << 30) - 1; } int mx = (lx + rx) / 2; return get(2 * x + 1, lx, mx, l, r) & get(2 * x + 2, mx, rx, l, r); } int get(int l, int r) { return get(0, 0, size, l, r); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; SegmentTreeOR st; st.init(n); vector<tuple<int, int, int>> segs; for (int i = 0; i < m; i++) { int l, r, q; cin >> l >> r >> q; l--; st.set(l, r, q); segs.emplace_back(l, r, q); } vector<int> b(n); for (int i = 0; i < n; i++) { b[i] = st.get(i); } SegmentTreeAND a; a.build(b); for (auto seg : segs) { int l = get<0>(seg); int r = get<1>(seg); int q = get<2>(seg); if (a.get(l, r) != q) { cout << "NO\n"; return 0; } } cout << "YES\n"; for (int i = 0; i < n; i++) { cout << b[i] << " "; } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; template <class T> bool Maximize(T &v, T nv) { if (nv > v) return v = nv, 1; return 0; } template <class T> bool Minimize(T &v, T nv) { if (nv < v) return v = nv, 1; return 0; } template <class T> T Mod(T &v, T mod) { return v = (v % mod + mod) % mod; } const long long INFLL = numeric_limits<long long>::max(); const long INF = numeric_limits<long>::max(), N = (long)1e5 + 1, M = (long)1e5 + 1; class SegmentTree { struct Node { long l, r; Node *left, *right; long v, pushv; Node(long i) : l(i), r(i), v(0), pushv(0) {} Node(long l, long r, Node *left, Node *right) : l(l), r(r), left(left), right(right), pushv(0) { Update(); } void Push() { left->Or(pushv); right->Or(pushv); pushv = 0; } void Or(long x) { v |= x; pushv |= x; } void Update() { v = left->v & right->v; } bool IsLeaf() { return l == r; } }; Node *root; Node *build(long l, long r) { return l == r ? new Node(l) : new Node(l, r, build(l, (l + r) / 2), build((l + r) / 2 + 1, r)); } void rangeOr(Node *v, long l, long r, long x) { if (v->l > r || v->r < l) return; if (v->l >= l && v->r <= r) return v->Or(x); v->Push(); rangeOr(v->left, l, r, x), rangeOr(v->right, l, r, x); v->Update(); } long rangeAnd(Node *v, long l, long r) { if (v->l > r || v->r < l) return ~0; if (v->l >= l && v->r <= r) return v->v; v->Push(); return rangeAnd(v->left, l, r) & rangeAnd(v->right, l, r); } void print(Node *v) { if (v->IsLeaf()) return void(printf("%ld ", v->v)); v->Push(); print(v->left), print(v->right); } public: SegmentTree(long l, long r) { root = build(l, r); } void Or(long l, long r, long x) { return rangeOr(root, l, r, x); } long And(long l, long r) { return rangeAnd(root, l, r); } void Print() { print(root); } }; struct Query { long l, r, q; }; Query qs[M]; void solve() { long n, m; scanf("%ld %ld", &n, &m); SegmentTree *t = new SegmentTree(1, n); for (long i = 0; i < m; ++i) { Query &q = qs[i]; scanf("%ld %ld %ld", &q.l, &q.r, &q.q); t->Or(q.l, q.r, q.q); } for (long i = 0; i < m; ++i) { Query &q = qs[i]; if (q.q != t->And(q.l, q.r)) return void(printf("NO\n")); } printf("YES\n"); t->Print(); } void init() {} int main() { init(); 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
import java.io.*; import java.util.*; public class Main { static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st = new StringTokenizer(""); static String next() { try { while (!st.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } st = new StringTokenizer(s); } return st.nextToken(); } catch(Exception e) { return null; } } public static void main(String[] asda) throws Exception { int N = Integer.parseInt( next() ); int M = Integer.parseInt( next() ); int [][] v = new int [N + 2][31]; int [] a = new int [M]; int [] b = new int [M]; int [] c = new int [M]; for (int i = 0; i < M; i++) { a[i] = Integer.parseInt( next() ); b[i] = Integer.parseInt( next() ); c[i] = Integer.parseInt( next() ); int x = c[i]; for (int k = 0; k < 31; k++) if ( (x & (1 << k)) != 0 ) { v[ a[i] ][k] += 1; v[ b[i] + 1 ][k] += -1; } } // partial sums int [] key = new int [N + 1]; for (int i = 1; i <= N; i++) { int x = 0; for (int k = 0; k < 31; k++) { v[i][k] += v[i - 1][k]; if ( v[i][k] != 0 ) x |= 1 << k; } key[i] = x; } // BinarySegmentTree t = new BinarySegmentTree(N + 1); for (int i = 1; i <= N; i++) t.set(i, key[i]); boolean ok = true; for (int i = 0; i < M && ok; i++) { int L = a[i]; int R = b[i]; int ans = c[i]; int x = t.getCount(L, R); // out.println(x); ok &= ans == x; } out.println( ok ? "YES" : "NO" ); if ( ok ) { out.print( key[1] ); for (int i = 2; i <= N; i++) out.print(" " + key[i]); out.println(); } out.flush(); } } class BinarySegmentTree { int N; int [] tree; /** * Crea un BinarySegmentTree para que soporte operaciones en el rango [0, size], inclusivo. * @param size La posicion maxima que manejara el arbol. */ public BinarySegmentTree(int size) { N = 1; while ( (N <<= 1) < size); tree = new int[N << 1]; } /** * Asigna el valor n en la posicion indicada * Complejidad: O(lg n) * @param pos Posicion del punto a agregar. * @param n Valor a asignar */ void set(int pos, int n) { pos += N; tree[pos] = n; pos >>= 1; while (pos > 0) { tree[pos] = tree[pos << 1] & tree[(pos << 1) + 1]; pos >>= 1; } } /** * Cuenta el numero de puntos que estan dentro del rango [l, r], inclusivo. * Complejidad: O(lg l + lg r) * @param l Inicio del rango. * @param r Fin del rango. * @return Numero de puntos que contiene el rango especificado. */ int getCount(int l, int r) { l += N; r += N; int ans = (1 << 31) - 1; while (l <= r) { if (l == r) return ans & tree[l]; if ((l & 1) == 1) ans &= tree[l++]; if ((r & 1) == 0) ans &= tree[r--]; l >>= 1; r >>= 1; } return ans; } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main extends PrintWriter { BufferedReader in; StringTokenizer stok; final Random rand = new Random(31); final int inf = (int) 1e9; final long linf = (long) 1e18; class SegmentTree { int[] a; int n; SegmentTree(int x) { n = 1; while (n < x) { n *= 2; } a = new int[2 * n - 1]; Arrays.fill(a, -1); } void set(int i, int x) { i += n - 1; a[i] = x; while (i > 0) { i = (i - 1) / 2; a[i] = a[i * 2 + 1] & a[i * 2 + 2]; } } int get(int l, int r) { return get(0, 0, n, l, r); } int get(int i, int l, int r, int ql, int qr) { if (ql >= qr) { return -1; } if (l == ql && r == qr) { return a[i]; } int m = (l + r) >> 1; return get(i * 2 + 1, l, m, ql, min(qr, m)) & get(i * 2 + 2, m, r, max(ql, m), qr); } } class Point implements Comparable<Point> { int x, id; boolean open; public Point(int x, int id, boolean open) { this.x = x; this.id = id; this.open = open; } @Override public int compareTo(Point o) { return x == o.x ? Boolean.compare(o.open, open) : x - o.x; } } public void solve() throws IOException { int n = nextInt(); int m = nextInt(); SegmentTree st = new SegmentTree(max(n, m)); int[] l = new int[m]; int[] r = new int[m]; int[] x = new int[m]; Point[] points = new Point[2 * m]; for (int i = 0; i < m; i++) { points[2 * i] = new Point(l[i] = nextInt() - 1, i, true); points[2 * i + 1] = new Point(r[i] = nextInt() - 1, i, false); x[i] = nextInt(); } Arrays.sort(points); int last = 0; int[] ans = new int[n]; for (Point p : points) { int pr = st.get(0, m); while (last < p.x + (p.open ? 0 : 1)) { ans[last++] = ~pr; } if (p.open) { st.set(p.id, ~x[p.id]); } else { st.set(p.id, -1); } } // int pr = st.get(0, n); // while (last < n) { // ans[last++] = pr; // } for (int i = 0; i < n; i++) { st.set(i, ans[i]); } for (int i = 0; i < m; i++) { int t = st.get(l[i], r[i] + 1); if (t != x[i]) { println("NO"); return; } } println("YES"); for (int i = 0; i < n; i++) { print(ans[i]); print(" "); } } public void run() { try { solve(); close(); } catch (Exception e) { e.printStackTrace(); System.exit(abs(-1)); } } Main() throws IOException { super(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } Main(String s) throws IOException { super("".equals(s) ? "output.txt" : (s + ".out")); in = new BufferedReader(new FileReader("".equals(s) ? "input.txt" : (s + ".in"))); } public static void main(String[] args) throws IOException { try { Locale.setDefault(Locale.US); } catch (Exception ignored) { } new Main().run(); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = in.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.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()); } int[] nextIntArray(int len) throws IOException { int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = nextInt(); } return a; } void shuffle(int[] a) { for (int i = 1; i < a.length; i++) { int x = rand.nextInt(i + 1); int t = a[i]; a[i] = a[x]; a[x] = t; } } boolean nextPerm(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1; ; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } <T> List<T>[] createAdjacencyList(int countVertex) { List<T>[] res = new List[countVertex]; for (int i = 0; i < countVertex; i++) { res[i] = new ArrayList<T>(); } 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.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); int N = 1; while (N < n) { N *= 2; } int[] arr = new int[N + 1]; SegmentTree sg = new SegmentTree(arr); int l []= new int [m+1]; int r []= new int [m+1]; int q []= new int [m+1]; for (int i = 1; i <=m; i++) { l[i]= sc.nextInt(); r[i]= sc.nextInt(); q[i]= sc.nextInt(); sg.updateRange(l[i], r[i], q[i]); } for (int i = 1; i <=m; i++) { if(sg.query(l[i], r[i])!=q[i]){ System.out.println("NO"); return ; } } System.out.println("YES"); for (int i = 1; i <=n; i++) { System.out.print(sg.query(i, i)+" "); } } static class SegmentTree { int[] sTree, array, lazy; int N; public SegmentTree(int[] array) { this.array = array; N = array.length - 1; // 8 // size of array must be a power of 2 + 1 (one-based) sTree = new int[2 * N]; // 16 lazy = new int[N << 1]; build(1, 1, N); } public void build(int node, int l, int r) { if (l == r) { sTree[node] = array[l]; return; } int mid = l + r >> 1; int leftChild = 2 * node; int rightChild = 2 * node + 1; // build left child build(leftChild, l, mid); // build right child build(rightChild, mid + 1, r); // use left and right to get the value of my current node sTree[node] = sTree[leftChild] & sTree[rightChild]; } public int Query2(int node ,int l , int r, int val) { if(l==r)return l ; int mid = l+r>>1; if(sTree[node<<1|1]<val) { return Query2(node<<1|1, mid+1, r, val); } else return Query2(node<<1, l, mid, val); } public int query(int i, int j) { return query(1, 1, N, i, j); } public int query(int node, int l, int r, int i, int j) { if (i <= l && r <= j) { // if I take the whole node return sTree[node]; } if (r < i || j < l) { // if I throw this node away return (1<<31)-1; } propagate(node, l, r); int mid = l + r >> 1; int leftChild = 2 * node; // node << 1 int rightChild = 2 * node + 1; // node << 1 | 1 int left = query(leftChild, l, mid, i, j); int right = query(rightChild, mid + 1, r, i, j); return left & right; } public void updatePoint(int idx, int val) { int cur = idx + N - 1; array[idx] = val; sTree[cur] = val; while (cur != 1) { cur = cur / 2; // move the current to the parent int leftChild = 2 * cur; int rightChild = 2 * cur + 1; sTree[cur] = sTree[leftChild] * sTree[rightChild]; } } public void propagate(int node, int l, int r) { int leftChild = node << 1; int rightChild = node << 1 | 1; int mid = l + r >> 1; lazy[leftChild] |= lazy[node]; lazy[rightChild] |= lazy[node]; sTree[leftChild] |= lazy[node]; sTree[rightChild] |= lazy[node]; lazy[node] = 0; } public void updateRange(int i, int j, int val) { updateRange(1, 1, N, i, j, val); } public void updateRange(int node, int l, int r, int i, int j, int val) { if (i <= l && r <= j) { // if I take the whole node lazy[node] |= val; sTree[node] |= val ; return; } if (r < i || j < l) { // if I throw this node away return; } int leftChild = node << 1; int rightChild = node << 1 | 1; int mid = l + r >> 1; propagate(node, l, r); updateRange(leftChild, l, mid, i, j, val); updateRange(rightChild, mid + 1, r, i, j, val); sTree[node] = sTree[leftChild] & sTree[rightChild]; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader f) { br = new BufferedReader(f); } 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 double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } public boolean ready() throws IOException, IOException { return br.ready(); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; import java.math.*; import java.util.*; import java.util.Map.*; public class Main { public static void main(String[] args) throws IOException { // System.setIn(new FileInputStream("copycat.in"));// 读入文件 // System.setOut(new PrintStream(new FileOutputStream("copycat.out")));// 输出到文件 Scanner sc = new Scanner(System.in); // StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); // BufferedReader inl = new BufferedReader(new InputStreamReader(System.in)); // PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); // while (sc.hasNext()) // while (in.nextToken) != StreamTokenizer.TT_EOF) { int n = sc.nextInt(); int m = sc.nextInt(); int l[] = new int[m + 10]; int r[] = new int[m + 10]; int val[] = new int[m + 10]; for (int i = 1; i <= m; i++) { l[i] = sc.nextInt(); r[i] = sc.nextInt(); val[i] = sc.nextInt(); updaterange(1, 1, n, l[i], r[i], val[i]); } boolean f = true; for (int i = 1; i <= m; i++) { long ans = query(1, 1, n, l[i], r[i]); if (ans != val[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) + " "); } } } } 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; const int INF = 2e9; const long long INFL = 2e18; const int N = 1e5 + 5; int n, m; pair<pair<int, int>, int> a[N]; int s[N], res[N]; const int SZ_ = 4 * N; struct intervalTree { int L[SZ_], H[SZ_], val[SZ_]; void build(int x, int lo, int hi) { L[x] = lo; H[x] = hi; if (lo == hi) { val[x] = res[lo]; return; } int mid = (lo + hi) >> 1; build(((x) << 1), lo, mid); build((((x) << 1) + 1), mid + 1, hi); val[x] = val[((x) << 1)] & val[(((x) << 1) + 1)]; } int query(int x, int lo, int hi) { if (L[x] > hi || H[x] < lo) return (1 << 30) - 1; if (L[x] >= lo && H[x] <= hi) return val[x]; return query(((x) << 1), lo, hi) & query((((x) << 1) + 1), lo, hi); } int query(int lo, int hi) { return query(1, lo, hi); } } IT; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = (1), _b = (m); i <= _b; ++i) { cin >> a[i].first.first >> a[i].first.second >> a[i].second; } for (int bit = (0), _b = (30); bit < _b; ++bit) { for (int i = (0), _b = (n + 1); i <= _b; ++i) s[i] = 0; for (int i = (1), _b = (m); i <= _b; ++i) { int l = a[i].first.first, r = a[i].first.second, q = a[i].second; if ((1 << (bit)) & q) { ++s[l]; --s[r + 1]; } } for (int i = (1), _b = (n); i <= _b; ++i) { s[i] += s[i - 1]; if (s[i] > 0) res[i] |= (1 << (bit)); } } IT.build(1, 1, n); for (int i = (1), _b = (m); i <= _b; ++i) { int l = a[i].first.first, r = a[i].first.second, q = a[i].second; if (IT.query(l, r) != q) return cout << "NO" << '\n', 0; } cout << "YES" << '\n'; for (int i = (1), _b = (n); i <= _b; ++i) cout << res[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 l[100005], r[100005], q[100005]; int sum[100005]; int arr[100005]; int Tree[400000]; void build(int ind, int s, int e) { if (s == e) { Tree[ind] = arr[s]; return; } build(ind * 2, s, (s + e) / 2); build(ind * 2 + 1, (s + e) / 2 + 1, e); Tree[ind] = Tree[ind * 2] & Tree[ind * 2 + 1]; } int S, E; int query(int ind, int s, int e) { if (s > E || e < S) { return ~0; } else if (s >= S && e <= E) { return Tree[ind]; } return query(ind * 2, s, (s + e) / 2) & query(ind * 2 + 1, (s + e) / 2 + 1, e); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d%d", &l[i], &r[i], &q[i]); l[i]--; r[i]--; } for (int bit = 0; bit < 30; bit++) { memset(sum, 0, sizeof sum); for (int i = 0; i < m; i++) { if ((1 << bit) & q[i]) { sum[l[i]]++; sum[r[i] + 1]--; } } for (int i = 0; i < n; i++) { if (i) sum[i] += sum[i - 1]; if (sum[i] > 0) arr[i] |= (1 << bit); } } build(1, 0, n - 1); for (int i = 0; i < m; i++) { S = l[i]; E = r[i]; if (query(1, 0, n - 1) != q[i]) { printf("NO\n"); return 0; } } printf("YES\n"); for (int i = 0; i < n; i++) { printf("%d", arr[i]); if (i == n - 1) printf("\n"); else printf(" "); } return 0; }
CPP