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.File; import java.io.FileNotFoundException; import java.util.Scanner; public class main4 { // static Scanner sc = null; static Scanner sc = new Scanner(System.in); static class Tree { int[] d = null; int n; public Tree(int n) { this.d = new int[100 * 100000]; this.n = n; } private int internalSet(int root, int cl, int cr, int l, int r, int v) { if (cr < l || cl > r) return -1; if (cl >= l && cr <= r) { d[root] |= v; return d[root]; } int mid = (cl + cr) / 2; int ans1 = internalSet(2 * root + 1, cl, mid, l, r, v); int ans2 = internalSet(2 * root + 2, mid + 1, cr, l, r, v); int total = -1; if (ans1 != -1) { total = ans1; } if (ans2 != -1){ if (total == -1) total = ans2; else total &= ans2; } //if(total != -1) d[root] |= total; return d[root]; } private int internalGet(int root, int cl, int cr, int l, int r) { if (cr < l || cl > r) { return -1; } if (cl >= l && cr <= r) { return d[root]; } int ans = d[root]; int mid = (cl + cr) / 2; int v1 = internalGet(2 * root + 1, cl, mid, l, r); int v2 = internalGet(2 * root + 2, mid + 1, cr, l, r); int ans2 = -1; if (v1 != -1) { if (ans2 == -1) ans2 = v1; else ans2 &= v1; } if (v2 != -1) { if (ans2 == -1) ans2 = v2; else ans2 &= v2; } if (ans2 == -1) return ans; return ans2 | ans; } private int internalGetValue(int root, int cl, int cr, int pos) { if (pos < cl || pos > cr) { return -1; } if (cl == cr) { return d[root]; } int ans = d[root]; int mid = (cl + cr) / 2; int v1 = internalGetValue(2 * root + 1, cl, mid, pos); int v2 = internalGetValue(2 * root + 2, mid + 1, cr, pos); if (v1 != -1) { ans |= v1; } if (v2 != -1) { ans |= v2; } return ans; } public void set(int l, int r, int v) { internalSet(0, 0, n - 1, l - 1, r - 1, v); } public int get(int l, int r) { return internalGet(0, 0, n - 1, l - 1, r - 1); } public int getValueAt(int pos) { return internalGetValue(0, 0, n - 1, pos); } } public static void main(String[] args) { // try { // sc = new Scanner(new // File("/home/piotr/programming/workspace_java/CF/src/in")); // } catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // int n = ni(); int m = ni(); Tree tree = new Tree(n); int[][] q = new int[m][3]; for (int i = 0; i < m; i++) { q[i] = new int[] { ni(), ni(), ni() }; } for (int i = 0; i < m; i++) { tree.set(q[i][0], q[i][1], q[i][2]); } for (int i = 0; i < m; i++) { int v = tree.get(q[i][0], q[i][1]); if (v != q[i][2]) { // if (m < 10) { System.out.println("NO"); // } else // System.out.println("NO " + v + " " + i); return; } // else System.out.println("YES " + v); } StringBuilder ans = new StringBuilder(); ans.append(tree.getValueAt(0)); for (int i = 1; i < n; i++) { ans.append(" " + tree.getValueAt(i)); } System.out.println("YES"); System.out.println(ans.toString()); } private static int ni() { return sc.nextInt(); } private static long nl() { return sc.nextLong(); } private static String ns() { return sc.next(); } private static String line() { return sc.nextLine(); } }
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; const int maxN = 2e6 + 1000; int a[maxN]; int l[maxN], r[maxN], q[maxN]; inline void add_q(int cur, int fi, int se, int le, int ri, int qu) { int mid = (fi + se) / 2; if (fi == le and se == ri) { a[cur] |= qu; } else if (ri <= mid) add_q(cur * 2, fi, mid, le, ri, qu); else if (le > mid) add_q(cur * 2 + 1, mid + 1, se, le, ri, qu); else { add_q(cur * 2, fi, mid, le, mid, qu); add_q(cur * 2 + 1, mid + 1, se, mid + 1, ri, qu); } } inline int query(int cur, int fi, int se, int le, int ri) { int mid = (fi + se) / 2; if (fi == le and se == ri) return a[cur]; else if (ri <= mid) return a[cur] | query(cur * 2, fi, mid, le, ri); else if (le > mid) return a[cur] | query(cur * 2 + 1, mid + 1, se, le, ri); else { return a[cur] | (query(cur * 2, fi, mid, le, mid) & query(cur * 2 + 1, mid + 1, se, mid + 1, ri)); } } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; i++) cin >> l[i] >> r[i] >> q[i]; for (int i = 0; i < maxN; i++) a[i] = 0; for (int i = 0; i < m; i++) add_q(1, 1, n, l[i], r[i], q[i]); for (int i = 0; i < m; i++) if (query(1, 1, n, l[i], r[i]) != q[i]) { cout << "NO" << endl; return 0; } cout << "YES" << endl; for (int i = 1; i < n + 1; i++) cout << query(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
import java.io.*; import java.util.Arrays; public class ProD2 { static StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static int nextInt() throws IOException { in.nextToken(); return (int)in.nval; } static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); static int n,m,ju,a; static Pair[] aa=new Pair[100005]; static int[][] sum=new int[40][100005]; static int[][] mm=new int[40][100005]; static class Pair implements Comparable<Pair> { int x,y,z; Pair(int a,int b,int c) { x=a;y=b;z=c; } public int compareTo(Pair p) { return x-p.x; } } public static void main(String[] args) throws IOException { //Scanner in=new Scanner(System.in); n=nextInt();m=nextInt(); for(int i=1;i<=m;i++) aa[i]=new Pair(nextInt(),nextInt(),nextInt()); Arrays.sort(aa,1,m+1); for(int j=0;j<30;j++) { a=0; for(int i=1;i<=m;i++) { if(((1<<j)&aa[i].z)==0) continue; a=Math.max(a,aa[i].x); while(a<=aa[i].y) mm[j][a++]=1; } for(int i=1;i<=n;i++) sum[j][i]=sum[j][i-1]+mm[j][i]; } ju=1; for(int i=1;i<=m;i++) { for(int j=0;j<30;j++) { if(((1<<j)&aa[i].z)>0) continue; a=sum[j][aa[i].y]-sum[j][aa[i].x-1]; if(a>=aa[i].y-aa[i].x+1) ju=0; } } out.println(ju==0?"NO":"YES"); if(ju==1) { for(int i=1;i<=n;i++) { a=0; for(int j=0;j<30;j++) a+=(sum[j][i]-sum[j][i-1]==1)?(1<<j):0; out.print(a+" "); } } out.flush(); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; int T[4 * 100005]; int Lz[4 * 100005]; long long update(int n, int l, int r, int i, int j, int val) { if (Lz[n]) { T[n] |= Lz[n]; if (l != r) { Lz[2 * n] |= Lz[n]; Lz[2 * n + 1] |= Lz[n]; } Lz[n] = 0; } if (i <= l and j >= r) { T[n] |= val; if (l != r) { Lz[2 * n] |= val; Lz[2 * n + 1] |= val; } Lz[n] = 0; return T[n]; } if (l > j or r < i) return T[n]; int mid = (l + r) / 2; long long x = update(2 * n, l, mid, i, j, val); long long y = update(2 * n + 1, mid + 1, r, i, j, val); T[n] = (x & y); return T[n]; } long long query(int n, int l, int r, int i, int j) { if (Lz[n]) { T[n] |= Lz[n]; if (l != r) { Lz[2 * n] |= Lz[n]; Lz[2 * n + 1] |= Lz[n]; } Lz[n] = 0; } if (i <= l and j >= r) { return T[n]; } if (l > j or r < i) return -1; int mid = (l + r) / 2; long long x = query(2 * n, l, mid, i, j); long long y = query(2 * n + 1, mid + 1, r, i, j); return (x & y); } int L[100005]; int R[100005]; int q[100005]; int main() { int n, m, x, cnt = 0; scanf(" %d %d", &n, &m); for (int i = 0; i < m; i++) { scanf(" %d %d %d", &L[i], &R[i], &q[i]); x = q[i]; cnt = 0; update(1, 0, n - 1, L[i] - 1, R[i] - 1, x); } for (int _i = 0; _i < m; _i++) { x = query(1, 0, n - 1, L[_i] - 1, R[_i] - 1); if (x != q[_i]) { puts("NO"); return 0; } } puts("YES"); for (int _i = 0; _i < n; _i++) { x = query(1, 0, n - 1, _i, _i); if (_i) printf(" "); printf("%d", 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 N = 100 * 1000 + 10, L = 30; int n, m, a[N][L + 5], ps[N][L + 5], l[N], r[N], p[N], ans[N]; int main() { cin >> n >> m; for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> p[i]; for (int j = 0; j <= L; j++) { if ((1 << j) & p[i]) { a[l[i]][j]++; a[r[i] + 1][j]--; } } } for (int i = 1; i <= n; i++) { for (int j = 0; j <= L; j++) { a[i][j] += a[i - 1][j]; ps[i][j] += ps[i - 1][j]; if (a[i][j]) { ans[i] += (1 << j); ps[i][j]++; } } } for (int i = 0; i < m; i++) { l[i]--; for (int j = 0; j <= L; j++) { if (((1 << j) & p[i]) == 0) { if (ps[r[i]][j] - ps[l[i]][j] == r[i] - l[i]) { cout << "NO\n"; return 0; } } } } cout << "YES\n"; for (int i = 1; i <= n; i++) cout << ans[i] << " "; cout << endl; return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const double eps = 0.000000000000001; int n, m; int A[100010]; int cum[100010][32]; struct que { int l, r, q; }; que qq[100010]; int main() { scanf("%d%d", &n, &m); int i, j; for (i = 0; i < m; i++) { int l, r, q; scanf("%d%d%d", &l, &r, &q); qq[i] = (que){l, r, q}; for (j = 0; j < 30; j++) { if (q & (1 << j)) { cum[l][j] += 1; cum[r + 1][j] -= 1; } } } for (j = 0; j < 30; j++) { for (i = 1; i <= n; i++) { cum[i][j] = cum[i - 1][j] + cum[i][j]; if (cum[i][j]) A[i] |= (1 << j); } } for (j = 0; j < 30; j++) { for (i = 1; i <= n; i++) { cum[i][j] = cum[i - 1][j] + (cum[i][j] > 0 ? 1 : 0); } } int f = 1; for (i = 0; i < m; i++) { int val = qq[i].q; for (j = 0; j < 30; j++) { if (!(val & (1 << j))) { if (cum[qq[i].r][j] - cum[qq[i].l - 1][j] >= qq[i].r - qq[i].l + 1) { f = 0; break; } } } if (!f) break; } if (!f) { printf("NO\n"); return 0; } printf("YES\n"); for (i = 1; i <= n; i++) { printf("%d ", A[i]); } printf("\n"); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 11; int push[N << 2], aa[N], bb[N], cc[N], seg[N << 2]; int fin[N], segand[N << 2]; void pusher(int id) { push[id << 1] |= push[id]; push[id << 1 | 1] |= push[id]; seg[id << 1] |= push[id]; seg[id << 1 | 1] |= push[id]; push[id] = 0; } void build(int l, int r, int id) { if (l == r) { segand[id] = fin[l]; return; } int m = (l + r) >> 1; build(l, m, id << 1); build(m + 1, r, id << 1 | 1); segand[id] = segand[id << 1] & segand[id << 1 | 1]; } int qu(int p, int L, int R, int id) { if (push[id] && L != R) pusher(id); int m = (L + R) >> 1; if (p == L && p == R) return seg[id]; if (p >= L && p <= m) return qu(p, L, m, id << 1); return qu(p, m + 1, R, id << 1 | 1); } void update(int l, int r, int L, int R, int id, int v) { if (push[id] && L != R) pusher(id); if (r < L || R < l) return; if (l <= L && R <= r) { seg[id] |= v; push[id] |= v; return; } int m = (L + R) >> 1; update(l, r, L, m, id << 1, v); update(l, r, m + 1, R, id << 1 | 1, v); seg[id] = seg[id << 1] | seg[id << 1 | 1]; } int out(int l, int r, int L, int R, int id) { if (r < L || R < l) return INT_MAX; if (l <= L && R <= r) { return segand[id]; } int m = (L + R) >> 1; return out(l, r, L, m, id << 1) & out(l, r, m + 1, R, id << 1 | 1); } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y, k; scanf("%d%d%d", &x, &y, &k); aa[i] = x - 1; bb[i] = y - 1; cc[i] = k; update(x - 1, y - 1, 0, n - 1, 1, k); } for (int i = 0; i < n; i++) fin[i] = qu(i, 0, n - 1, 1); build(0, n - 1, 1); for (int i = 0; i < m; i++) { if (cc[i] != out(aa[i], bb[i], 0, n - 1, 1)) { printf("NO\n"); return 0; } } printf("YES\n"); for (int i = 0; i < n; i++) printf("%d ", fin[i]); cout << endl; return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; import java.util.*; public class InterestingArray { static int MAXN = 111101; static int LOGN = 32 , INF = Integer.MAX_VALUE; static int X[][] = new int[LOGN][MAXN]; static int ST[] = new int[MAXN*4]; static int A[] = new int[MAXN]; static int L[]=new int[MAXN] , R[]=new int[MAXN] , Q[]=new int[MAXN]; public static void main(String[]args) { InputReader1 sc = new InputReader1(System.in); int N=sc.nextInt() , M = sc.nextInt(); for(int j=0;j<M;j++) { int l =sc.nextInt() , r = sc.nextInt() , val=sc.nextInt(); L[j] = l ; R[j] = r ; Q[j] = val ; for(int i=0;i<LOGN;i++) { if((val&(1<<i))>0) { X[i][l]++; X[i][r+1]--; } } } for(int i=0;i<LOGN;i++) for(int j=1;j<=N;j++) X[i][j] += X[i][j-1]; for(int j=1;j<=N;j++) { for(int i=0;i<LOGN;i++) if(X[i][j]>0) A[j]+=(1<<i); } build(1,1,N); boolean pos=true; for(int j=0;j<M;j++) { int ans = query(1,1,N,L[j],R[j]); if(ans!=Q[j]) { System.out.println("NO"); System.exit(0); } } System.out.println("YES"); for(int i=1;i<=N;i++)System.out.print(A[i]+" "); } static void build(int node , int l , int r) { if(l==r) { ST[node] = A[l]; return; } int mid = (l+r)/2 ; build(node*2 , l , mid); build(node*2+1,mid+1,r); ST[node] = ST[node*2] & ST[node*2+1]; } static int query(int node , int l , int r , int qs , int qe) { if(qe < l || r < qs) return INF; if(qs<=l && r<=qe) return ST[node]; int mid = (l+r)/2; if (qe <= mid) return query(2 * node, l, mid, qs, qe); if (mid < qs) return query(2 * node + 1, mid + 1, r, qs, qe); return query(2 * node, l, mid, qs, qe) & query(2 * node + 1, mid + 1, r, qs, qe); } } class InputReader1 { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader1(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c & 15; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c & 15; c = read(); } while (!isSpaceChar(c)); return res * sgn; } 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 String nextLine() { int c = read(); //while (c != '\n' && c != '\r' && c != '\t' && c != -1) //c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (c != '\n' && c != '\r' && c != '\t' && c != -1); return res.toString(); } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
/* ID: govind.3, GhpS, govindpatel LANG: JAVA TASK: Main */ import java.io.*; import java.util.*; public class Main { private int BIT = 30; private int MAX = 1000 * 1000;//10^6 private int[] tree = new int[4 * MAX]; private int[] values = new int[MAX]; int[] sum = 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) >> 1; build(2 * v, tl, tm);//left build(2 * v + 1, tm, tr);//right tree[v] = tree[v * 2] & tree[v * 2 + 1]; } } private int AND(int v, int tl, int tr, int l, int r) { if (l == tl && r == tr) { return tree[v]; } int tm = (tl + tr) >> 1; int ans = (1 << BIT) - 1; 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[MAX];//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)); } } } //System.out.println(Arrays.toString(values)); 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
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 2; int n, m; struct Query { int l, r, q; bool operator<(const Query& rhs) const { return l == rhs.l ? r < rhs.r : l < rhs.l; } } qry[MAX]; int ans[MAX]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { int l, r, q; cin >> l >> r >> q; qry[i] = {l, r, q}; } sort(qry, qry + m); for (int k = 0; k <= 30; ++k) { vector<int> A(n + 1); int mx = 0; for (int i = 0; i < m; ++i) { auto [l, r, q] = qry[i]; if (!(q & (1 << k)) || r <= mx) continue; for (int pos = max(mx + 1, l); pos <= r; ++pos) { A[pos] = 1; } mx = r; } for (int i = 1; i <= n; ++i) { if (A[i] == 1) ans[i] |= 1 << k; A[i] += A[i - 1]; } for (int i = 0; i < m; ++i) { auto [l, r, q] = qry[i]; if (q & (1 << k)) continue; if (A[r] - A[l - 1] == r - l + 1) { cout << "NO"; return 0; } } } cout << "YES\n"; for (int i = 1; i <= n; ++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
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; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ProblemBInterestingArray solver = new ProblemBInterestingArray(); solver.solve(1, in, out); out.close(); } static class ProblemBInterestingArray { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int m = in.readInt(); int[] l = new int[m], r = new int[m], q = new int[m]; for (int i = 0; i < m; i++) { l[i] = in.readInt() - 1; r[i] = in.readInt() - 1; q[i] = in.readInt(); } int[][] bits = new int[n + 1][31]; for (int i = 0; i < m; i++) { for (int j = 0; j <= 30; j++) { bits[l[i]][j] += (q[i] >> j) & 1; bits[r[i] + 1][j] -= (q[i] >> j) & 1; } } int[][] lastZero = new int[n + 1][31]; for (int j = 0; j <= 30; j++) { lastZero[0][j] = -1; int last = bits[0][j] == 0 ? 0 : -1; for (int i = 1; i <= n; i++) { bits[i][j] += bits[i - 1][j]; lastZero[i][j] = last; if (bits[i][j] == 0) last = i; } } for (int i = 0; i < m; i++) { for (int j = 0; j <= 30; j++) { if (((q[i] >> j) & 1) == 1) continue; if (lastZero[r[i] + 1][j] < l[i]) { out.println("NO"); return; } } } out.println("YES"); for (int i = 0; i < n; i++) { int ans = 0; for (int j = 0; j <= 30; j++) { if (bits[i][j] >= 1) ans |= (1 << j); } out.print(ans + " "); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
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.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Aman Kumar Singh */ 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); BInterestingArray solver = new BInterestingArray(); solver.solve(1, in, out); out.close(); } static class BInterestingArray { int MAXN = 200005; PrintWriter out; InputReader in; int n; int[] arr = new int[MAXN]; int[] segtree; void build() { // build the tree for (int i = n - 1; i > 0; --i) segtree[i] = segtree[i << 1] & segtree[i << 1 | 1]; } int query(int l, int r) { // sum on interval [l, r) int res = (1 << 31) - 1; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if ((l & 1) == 1) res = res & segtree[l++]; if ((r & 1) == 1) res = res & segtree[--r]; } return res; } public void solve(int testNumber, InputReader in, PrintWriter out) { this.out = out; this.in = in; n = ni(); int m = ni(); int i = 0, j = 0; int[][] bits = new int[31][n + 2]; int[][] conditions = new int[m][3]; for (j = 0; j < m; j++) { int l = ni(); int r = ni(); int q = ni(); for (i = 30; i >= 0; i--) { if (((1 << i) & q) > 0) { bits[i][l] += 1; bits[i][r + 1] -= 1; } } conditions[j][0] = l - 1; conditions[j][1] = r; conditions[j][2] = q; } for (i = 0; i < 31; i++) { for (j = 1; j <= n; j++) bits[i][j] += bits[i][j - 1]; } segtree = new int[2 * n]; for (i = 1; i <= n; i++) { int num = 0; for (j = 0; j < 31; j++) { if (bits[j][i] > 0) num += 1 << j; } segtree[n + i - 1] = num; arr[i] = num; } build(); for (i = 0; i < m; i++) { int hola = query(conditions[i][0], conditions[i][1]); if (hola != conditions[i][2]) { pn("NO"); return; } } pn("YES"); for (i = 1; i <= n; i++) p(arr[i] + " "); } int ni() { return in.nextInt(); } void pn(String zx) { out.println(zx); } void p(Object o) { out.print(o); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new UnknownError(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public String next() { 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; } } }
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.*; import static java.util.Arrays.*; public class cf482b { public static void main(String[] args) throws IOException { int n = rni(), m = ni(), rules[][] = new int[n + 1][30]; List<int[]> rr = new ArrayList<>(); for(int i = 0; i < m; ++i) { int l = rni() - 1, r = ni(), q = ni(), cnt = 0; rr.add(new int[] {l, r, q}); while(q > 0) { if((q & 1) > 0) { ++rules[l][cnt]; --rules[r][cnt]; } q >>= 1; ++cnt; } } int a[] = new int[n], cur_rule[] = new int[30]; for(int i = 0; i < n; ++i) { for(int j = 0, bit = 1; j < 30; ++j, bit <<= 1) { if((cur_rule[j] += rules[i][j]) > 0) { a[i] |= bit; } } } // prln(a); SGT sgt = new SGT(a, (x, y) -> x & y); for(int i = 0; i < m; ++i) { int rule[] = rr.get(i), l = rule[0], r = rule[1], q = rule[2]; if(sgt.qry(l, r) != q) { prN(); close(); return; } } prY(); prln(a); close(); } @FunctionalInterface interface IntOperator { int merge(int a, int b); } static class SGT { IntOperator op; int n, tree[]; SGT(int size, IntOperator operator) { n = size; op = operator; tree = new int[2 * n]; } SGT(int[] a, IntOperator operator) { n = a.length; op = operator; tree = new int[2 * n]; for(int i = 0; i < n; ++i) { tree[n + i] = a[i]; } for(int i = n - 1; i >= 1; --i) { tree[i] = op.merge(tree[i << 1], tree[i << 1 | 1]); } } void upd(int i, int x) { tree[n + i] = x; i += n; for(int j = i >> 1; j > 0; j >>= 1) { tree[j] = op.merge(tree[j << 1], tree[j << 1 | 1]); } } int qry(int i, int j) { int ans = IMAX; for(i += n, j += n; i < j; i >>= 1, j >>= 1) { if((i & 1) == 1) { ans = op.merge(ans, tree[i++]); } if((j & 1) == 1) { ans = op.merge(tree[--j], ans); } } return ans; } } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random rand = new Random(); // references // IBIG = 1e9 + 7 // IRAND ~= 3e8 // IMAX ~= 2e10 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IRAND = 327859546; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));} static int minstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));} static long minstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));} static int maxstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));} static long maxstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));} static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int floori(double d) {return (int)d;} static int ceili(double d) {return (int)ceil(d);} static long floorl(double d) {return (long)d;} static long ceill(double d) {return (long)ceil(d);} static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static <T> void shuffle(T[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); T swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static void rsort(double[] a) {shuffle(a); sort(a);} static int randInt(int min, int max) {return rand.nextInt(max - min + 1) + min;} // input static void r() throws IOException {input = new StringTokenizer(__in.readLine());} static int ri() throws IOException {return Integer.parseInt(__in.readLine());} static long rl() throws IOException {return Long.parseLong(__in.readLine());} static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;} static char[] rcha() throws IOException {return __in.readLine().toCharArray();} static String rline() throws IOException {return __in.readLine();} static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());} static int ni() {return Integer.parseInt(input.nextToken());} static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());} static long nl() {return Long.parseLong(input.nextToken());} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {__out.println("yes");} static void pry() {__out.println("Yes");} static void prY() {__out.println("YES");} static void prno() {__out.println("no");} static void prn() {__out.println("No");} static void prN() {__out.println("NO");} static void pryesno(boolean b) {__out.println(b ? "yes" : "no");}; static void pryn(boolean b) {__out.println(b ? "Yes" : "No");} static void prYN(boolean b) {__out.println(b ? "YES" : "NO");} static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next());} static void h() {__out.println("hlfd");} static void flush() {__out.flush();} static void close() {__out.close();} }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int kMaxN = 1e5 + 3; vector<int> in[kMaxN]; vector<int> out[kMaxN]; int q[kMaxN]; struct SegTree { int sz; static constexpr int kMax = (1 << 30) - 1; struct node { int v = kMax; void merge(node& n1, node& n2) { v = n1.v & n2.v; } }; vector<node> nodes; SegTree(vector<int>& a) { sz = 1; int n = a.size(); while (sz < n) { sz *= 2; } nodes.resize(2 * sz); for (int i = 0; i < n; ++i) { nodes[i + sz].v = a[i]; } for (int i = sz - 1; i > 0; --i) { nodes[i].merge(nodes[2 * i], nodes[2 * i + 1]); } } int get_and(int l, int r) { return get_and(1, 0, sz - 1, l, r); } private: int get_and(int idx, int L, int R, int l, int r) { if (l > R || r < L) { return kMax; } if (l <= L && R <= r) { return nodes[idx].v; } int M = (L + R) / 2; return get_and(2 * idx, L, M, l, r) & get_and(2 * idx + 1, M + 1, R, l, r); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> a(n); vector<array<int, 3>> queries(m); for (int i = 0; i < m; ++i) { int l, r; cin >> l >> r >> q[i]; --l, --r; in[l].emplace_back(i); out[r].emplace_back(i); queries[i] = {l, r, q[i]}; } array<int, 30> cur{}; int elem = 0; for (int i = 0; i < n; ++i) { for (int query : in[i]) { for (int k = 0; k < 30; ++k) { if (q[query] & (1 << k)) { if (cur[k]++ == 0) { elem |= 1 << k; } } } } a[i] = elem; for (int query : out[i]) { for (int k = 0; k < 30; ++k) { if (q[query] & (1 << k)) { if (--cur[k] == 0) { elem -= 1 << k; } } } } } SegTree st(a); for (auto& query : queries) { if (st.get_and(query[0], query[1]) != query[2]) { cout << "NO\n"; return 0; } } cout << "YES\n"; for (int x : a) { cout << x << ' '; } cout << '\n'; return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; int seg[maxn << 2], lazy[maxn << 2], ul, ur; void pushup(int cur) { seg[cur] = seg[cur << 1] & seg[cur << 1 | 1]; } void pushdown(int cur) { if (lazy[cur]) { seg[cur << 1] |= lazy[cur]; seg[cur << 1 | 1] |= lazy[cur]; lazy[cur << 1] |= lazy[cur]; lazy[cur << 1 | 1] |= lazy[cur]; lazy[cur] = 0; } } void update(int cur, int l, int r, int val) { if (ul <= l && r <= ur) { lazy[cur] |= val; seg[cur] |= val; return; } pushdown(cur); int mid = l + r >> 1; if (ul <= mid) update(cur << 1, l, mid, val); if (mid < ur) update(cur << 1 | 1, mid + 1, r, val); pushup(cur); } int query(int cur, int l, int r) { int ret = (1 << 30) - 1; if (ul <= l && r <= ur) { return seg[cur]; } pushdown(cur); int mid = l + r >> 1; if (ul <= mid) ret &= query(cur << 1, l, mid); if (mid < ur) ret &= query(cur << 1 | 1, mid + 1, r); pushup(cur); return ret; } int n, m, l[maxn], r[maxn], q[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d%d%d", l + i, r + i, q + i); ul = l[i], ur = r[i]; update(1, 1, n, q[i]); } bool ok = true; for (int i = 0; ok && i < m; ++i) { ul = l[i], ur = r[i]; if (query(1, 1, n) != q[i]) ok = false; } if (!ok) printf("NO\n"); else { printf("YES\n"); for (int i = 1; i <= n; ++i) { ul = ur = i; printf("%d ", query(1, 1, 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
# Legends Always Come Up with Solution # Author: Manvir Singh import os from io import BytesIO, IOBase import sys from collections import defaultdict, deque, Counter from math import sqrt, pi, ceil, log, inf, gcd, floor from itertools import combinations, permutations from bisect import * from fractions import Fraction from heapq import * from random import randint def main(): po=[1] for i in range(30): po.append(po[-1]*2) n,m=map(int,input().split()) q=[] b=[[0 for _ in range(30)] for _ in range(n+2)] for i in range(m): l,r,x=map(int,input().split()) q.append((l,r,x)) j=0 while x: if x&1: b[l][j]+=1 b[r+1][j]-=1 x=x>>1 j+=1 for i in range(1,n+1): for j in range(30): b[i][j]+=b[i-1][j] for i in range(1,n+1): for j in range(30): if b[i][j]>=2: b[i][j]=1 b[i][j]+=b[i-1][j] f=1 for i in q: l,r,x=i z=0 for j in range(30): if b[r][j]-b[l-1][j]==(r-l+1): z+=po[j] if z!=x: f=0 break if f: print("YES") a=[] for i in range(1,n+1): z=0 for j in range(30): if b[i][j]-b[i-1][j]==1: z+=po[j] a.append(z) print(*a) else: print("NO") # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main()
PYTHON3
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; vector<long long int> a(100005, 0), seg(4 * 100005, 0), lazy(4 * 100005, 0); void build(long long cur, long long st, long long end) { if (st == end) { seg[cur] = a[st]; return; } long long mid = (st + end) >> 1; build(2 * cur, st, mid); build(2 * cur + 1, mid + 1, end); seg[cur] = seg[2 * cur] | seg[2 * cur + 1]; } void updaterange(long long cur, long long st, long long end, long long l, long long r, long long val) { if (lazy[cur] != 0) { seg[cur] |= lazy[cur]; if (st != end) { lazy[2 * cur] |= lazy[cur]; lazy[2 * cur + 1] |= lazy[cur]; } lazy[cur] = 0; } if (st > end || st > r || end < l) return; if (l <= st && end <= r) { seg[cur] |= val; if (st != end) { lazy[2 * cur] |= val; lazy[2 * cur + 1] |= val; } return; } long long mid = (st + end) >> 1; updaterange(2 * cur, st, mid, l, r, val); updaterange(2 * cur + 1, mid + 1, end, l, r, val); seg[cur] = seg[2 * cur] | seg[2 * cur + 1]; } long long query(long long cur, long long st, long long end, long long l, long long r) { if (st > end || st > r || end < l) return 0; if (lazy[cur] != 0) { seg[cur] |= lazy[cur]; if (st != end) { lazy[2 * cur] |= lazy[cur]; lazy[2 * cur + 1] |= lazy[cur]; } lazy[cur] = 0; } if (st >= l && end <= r) return seg[cur]; long long mid = (st + end) >> 1; long long ans1 = query(2 * cur, st, mid, l, r); long long ans2 = query(2 * cur + 1, mid + 1, end, l, r); return ans1 | ans2; } vector<long long int> a2(100005), seg2(4 * 100005); void build2(long long cur, long long st, long long end) { if (st == end) { seg2[cur] = a2[st]; return; } long long mid = (st + end) >> 1; build2(2 * cur, st, mid); build2(2 * cur + 1, mid + 1, end); seg2[cur] = seg2[2 * cur] & seg2[2 * cur + 1]; } long long query2(long long cur, long long st, long long end, long long l, long long r) { if (l <= st && r >= end) return seg2[cur]; if (r < st || l > end) return pow(2, 31) - 1; long long mid = (st + end) >> 1; long long ans1 = query2(2 * cur, st, mid, l, r); long long ans2 = query2(2 * cur + 1, mid + 1, end, l, r); return (ans1 & ans2); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) { long long int n, q; cin >> n >> q; vector<long long int> l(q), r(q), val(q); for (long long int i = 0; i < q; i++) { cin >> l[i] >> r[i] >> val[i]; } build(1, 1, n); for (long long int i = 0; i < q; i++) { updaterange(1, 1, n, l[i], r[i], val[i]); } for (long long int i = 1; i <= n; i++) { a2[i] = query(1, 1, n, i, i); } build2(1, 1, n); for (long long int i = 0; i < q; i++) { if (query2(1, 1, n, l[i], r[i]) != val[i]) { cout << "NO"; return 0; } } cout << "YES" << '\n'; for (long long int i = 1; i <= n; i++) { cout << a2[i] << " "; } } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.util.*; import java.io.*; public class Solution implements Runnable { FastScanner sc; PrintWriter pw; final 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 long nlo() { return Long.parseLong(next()); } 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 ni() { return Integer.parseInt(next()); } public String nli() { String line = ""; if (st.hasMoreTokens()) line = st.nextToken(); else try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); } while (st.hasMoreTokens()) line += " " + st.nextToken(); return line; } public double nd() { return Double.parseDouble(next()); } } public static void main(String[] args) throws Exception { new Thread(null,new Solution(),"codeforces",1<<28).start(); } public void run() { sc=new FastScanner(); pw=new PrintWriter(System.out); try{ solve();} catch(Exception e) { pw.println(e); } pw.flush(); pw.close(); } public long gcd(long a,long b) { return b==0L?a:gcd(b,a%b); } public long ppow(long a,long b,long mod) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1) tmp*=a; a*=a; a%=mod; tmp%=mod; b>>=1; } return (tmp*a)%mod; } public long pow(long a,long b) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1) tmp*=a; a*=a; b>>=1; } return (tmp*a); } public int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } ////////////////////////////////// ///////////// LOGIC /////////// //////////////////////////////// public void solve() throws Exception{ int n=sc.ni(); int m=sc.ni(); int[][] arr=new int[m][3]; for(int i=0;i<m;i++){ arr[i][0]=sc.ni()-1; arr[i][1]=sc.ni()-1; arr[i][2]=sc.ni(); } int[] brr=new int[n+1]; int[] ans=new int[n]; for(int i=0;i<30;i++){ int k=1<<i,sum=0; Arrays.fill(brr,0); for(int j=0;j<m;j++){ if((arr[j][2]&k)!=0){ brr[arr[j][0]]++; brr[arr[j][1]+1]--; } } for(int j=0;j<n;j++){ sum+=brr[j]; if(sum>0) ans[j]|=k; } } int[] seg=new int[4*n+1]; build(seg,ans,0,n-1,1); boolean res=true; for(int i=0;i<m;i++) if(arr[i][2]!=query(seg,arr[i][0],arr[i][1],0,n-1,1)) res=false; if(res){ pw.println("YES"); for(int x:ans) pw.print(x+" "); } else pw.println("NO"); } void build(int[] seg,int[] arr,int l,int r,int ind){ if(l==r){ seg[ind]=arr[l]; } else{ build(seg,arr,l,(l+r)/2,2*ind); build(seg,arr,(l+r)/2+1,r,2*ind+1); seg[ind]=seg[2*ind]&seg[2*ind+1]; } } int query(int[] seg,int l,int r,int s,int e,int ind){ if(l<=s&&r>=e) return seg[ind]; if(r<s||l>e) return Integer.MAX_VALUE; return query(seg,l,r,s,(s+e)/2,2*ind)&query(seg,l,r,(s+e)/2+1,e,2*ind+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.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.sql.Array; import java.util.Arrays; import java.util.HashMap; import java.util.StringTokenizer; public class SegmentTree { // 1-based DS, OOP public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); int N = 1; while (N < n) N <<= 1; int[] b = new int[N + 1]; for (int i = n + 1; i <= N; i++) b[i] = -1; SegmentTree sg = new SegmentTree(b); triple[] t = new triple[q]; while (q-- > 0) { st = new StringTokenizer(br.readLine()); int i = Integer.parseInt(st.nextToken()); int j = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); sg.update_range(i, j, x); t[q] = new triple(i, j, x); } boolean f = true; sg.propagateAll(); for (int i = 0; i < t.length && f; i++) { int ans = sg.query(t[i].x, t[i].y); if (ans != t[i].z) f = false; } if (!f) pw.println("NO"); else { pw.println("YES"); for (int i = 0; i < n; i++) { pw.print(sg.sTree[i + N] + " "); } } pw.flush(); } int N; // the number of elements in the array as a power of 2 (i.e. after padding) int[] array, sTree, lazy; void propagateAll() { propagateAll(1, 1, N); } void propagateAll(int node, int b, int e) { if (b == e) return; propagate(node, b, b + e >> 1, e); propagateAll(node << 1, b, (b + e) / 2); propagateAll((node << 1) + 1, (b + e) / 2 + 1, e); } 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_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 triple implements Comparable<triple> { int x; int y; int z; public triple(int a, int b, int c) { x = a; y = b; z = c; } @Override public int compareTo(triple o) { // TODO Auto-generated method stub return x - o.x; } @Override public String toString() { // TODO Auto-generated method stub return "" + z; } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1}; int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; const long long inf = 998244353; vector<int> adj[200010], vis(200010, 0), par(200010, 0), dis(200010, 0), di(200010, 0), fin(200010, 0); int myrandom(int i) { return std::rand() % i; } const int MAX = 100010; int a[MAX], l[MAX], r[MAX], q[MAX], t[4 * MAX]; using namespace std; inline void build(int v, int l, int r) { if (l == r) { t[v] = a[l]; return; } int mid = (l + r) / 2; build(2 * v, l, mid); build(2 * v + 1, mid + 1, r); t[v] = t[2 * v] & t[2 * v + 1]; } inline int query(int v, int l, int r, int tl, int tr) { if (l > r) { return (1 << 30) - 1; } if (tl == l && tr == r) { return t[v]; } int mid = (l + r) / 2; if (tr <= mid) { return query(2 * v, l, mid, tl, tr); } else if (tl > mid) { return query(2 * v + 1, mid + 1, r, tl, tr); } else { return query(2 * v, l, mid, tl, mid) & query(2 * v + 1, mid + 1, r, mid + 1, tr); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; memset(a, 0, sizeof(a)); for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> q[i]; l[i]--; } for (int i = 0; i <= 30; i++) { int sum[n + 2]; memset(sum, 0, sizeof(sum)); for (int j = 0; j < m; j++) { if ((1 << i) & q[j]) { sum[l[j]]++; sum[r[j]]--; } } for (int j = 0; j < n; j++) { if (j > 0) { sum[j] += sum[j - 1]; } if (sum[j] > 0) { a[j] |= 1 << i; } } } build(1, 0, n - 1); for (int i = 0; i < m; i++) { if (query(1, 0, n - 1, l[i], r[i] - 1) != q[i]) { cout << "NO" << '\n'; return 0; } } cout << "YES" << '\n'; for (int i = 0; i < n; i++) { cout << a[i] << " "; } cout << '\n'; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 10; long long n, Q, c[31][N], ans[31][N]; struct rec { long long l, r, d; } q[N]; void update(long long l, long long r, long long d) { for (long long i = 30; i >= 0; i--) if (d & (1ll << i)) c[i][l]++, c[i][r + 1]--; } void init() { for (long long i = 0; i <= 30; i++) for (long long j = 1; j <= n; j++) c[i][j] = c[i][j - 1] + c[i][j]; for (long long i = 0; i <= 30; i++) for (long long j = 1; j <= n; j++) c[i][j] = (c[i][j] > 0); memcpy(ans, c, sizeof(c)); for (long long i = 0; i <= 30; i++) for (long long j = 1; j <= n; j++) c[i][j] = c[i][j - 1] + c[i][j]; } bool check(long long l, long long r, long long d) { for (long long i = 30; i >= 0; i--) if ((!(d & (1ll << i))) && (c[i][r] - c[i][l - 1] == r - l + 1)) return false; return true; } signed main() { scanf("%lld%lld", &n, &Q); for (long long i = 1; i <= Q; i++) { scanf("%lld%lld%lld", &q[i].l, &q[i].r, &q[i].d); update(q[i].l, q[i].r, q[i].d); } init(); for (long long i = 1; i <= Q; i++) if (!check(q[i].l, q[i].r, q[i].d)) { puts("NO"); return 0; } puts("YES"); for (long long i = 1; i <= n; i++) { long long ret = 0; for (long long j = 0; j <= 30; j++) if (ans[j][i]) ret += (1 << j); printf("%lld ", ret); } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; void RD(int &x) { scanf("%d", &x); } void RD(long long &x) { scanf("%I64d", &x); } void RD(double &x) { scanf("%lf", &x); } void RD(int &x, int &y) { scanf("%d%d", &x, &y); } void RD(long long &x, long long &y) { scanf("%I64d%I64d", &x, &y); } void RD(double &x, double &y) { scanf("%lf%lf", &x, &y); } void RD(char *s) { scanf("%s", s); } void RD(char &s) { scanf("%c", &s); } void RD(string &s) { cin >> s; } void PR(int x) { printf("%d\n", x); } void PR(int x, int y) { printf("%d %d\n", x, y); } void PR(long long x) { printf("%I64d\n", x); } void PR(char x) { printf("%c\n", x); } void PR(char *x) { printf("%s\n", x); } void PR(string x) { cout << x << endl; } const long long inf = 1; const long long mod = 1LL; const long long lim = (1LL << 30); long long x[100010], y[100010], q[100010]; long long ans[100010]; long long cum[100010][33]; long long tree[100010][33]; int query(int idx, int bit) { int sum = 0; while (idx > 0) { sum += tree[idx][bit]; idx -= (idx & -idx); } return sum; } void update(int idx, int bit, int val, int n) { while (idx <= n) { tree[idx][bit] += val; idx += (idx & -idx); } } int main() { int n, m; while (scanf("%d %d", &n, &m) == 2) { memset(tree, 0, sizeof(tree)); memset(cum, 0, sizeof(cum)); memset(ans, 0, sizeof(ans)); int i, j; for ((i) = 1; (i) <= (int)(m); (i)++) { RD(x[i], y[i]); RD(q[i]); for ((j) = 0; (j) <= (int)(30); (j)++) { if (q[i] & (1LL << j)) { cum[x[i]][j]++; cum[y[i] + 1][j]--; } } } int flag = 1; for ((i) = 1; (i) <= (int)(n); (i)++) { for ((j) = 0; (j) <= (int)(30); (j)++) { cum[i][j] += cum[i - 1][j]; if (cum[i][j] > 0) ans[i] += (1LL << j), update(i, j, 1, n); } if (ans[i] >= lim) flag = 0; } if (flag) for ((i) = 1; (i) <= (int)(m); (i)++) { long long val = 0; for ((j) = 0; (j) <= (int)(30); (j)++) { if ((query(y[i], j) - query(x[i] - 1, j)) == (y[i] - x[i] + 1)) { val += (1LL << j); } } if (val != q[i]) { flag = 0; break; } } if (flag) { printf("YES\n"); for ((i) = 1; (i) <= (int)(n); (i)++) printf("%I64d ", 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
#include <bits/stdc++.h> const int MAXBIT = 30; const int maxn = 1111111; const int maxm = 1111111; using namespace std; int num[maxn << 2]; int sum[maxn], q[maxn], l[maxn], r[maxn], a[maxn]; void pushup(int rt) { num[rt] = num[rt << 1] & num[rt << 1 | 1]; } void build(int l, int r, int rt) { if (l == r) { num[rt] = a[l]; return; } int m = (l + r) >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); pushup(rt); } int query(int L, int R, int l, int r, int rt) { int m = (l + r) >> 1; if (L <= l && r <= R) return num[rt]; int ret = (1LL << MAXBIT) - 1; if (L <= m) ret &= query(L, R, l, m, rt << 1); if (R > m) ret &= query(L, R, m + 1, r, rt << 1 | 1); return ret; } int main() { int n, m, i, bit; scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) scanf("%d%d%d", &l[i], &r[i], &q[i]); for (bit = 0; bit <= MAXBIT; bit++) { memset(sum, 0, sizeof(sum)); for (i = 1; i <= m; i++) if ((q[i] >> bit) & 1) { sum[l[i]]++; sum[r[i] + 1]--; } for (i = 1; i <= n; i++) { sum[i] += sum[i - 1]; if (sum[i] > 0) a[i] |= (1 << bit); } } build(1, n, 1); for (i = 1; i <= m; i++) { if (query(l[i], r[i], 1, n, 1) != q[i]) { printf("NO\n"); return 0; } } printf("YES\n"); for (i = 1; i <= n; i++) printf("%d ", a[i]); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; import java.text.DecimalFormat; import java.util.*; public class Main { static int mod = (int)1e9+7; static int inf = (int)(2e9+1e8); static double eps = 1e-15; static int N = 111111; int[] l = new int[N], r = new int[N], num = new int[N]; int[] col = new int[N<<2], sum = new int[N<<2]; void PushDown(int idx, int m) { if (col[idx] != 0) { col[idx<<1] |= col[idx]; col[idx<<1|1] |= col[idx]; sum[idx<<1] |= col[idx]; sum[idx<<1|1] |= col[idx]; col[idx] = 0; } } void PushUp(int idx) { sum[idx] = sum[idx<<1] & sum[idx<<1|1]; } void update(int L, int R, int x, int l, int r, int idx) { if(L <= l && r <= R) { col[idx] |= x; sum[idx] |= x; return; } PushDown(idx, r-l+1); int mid = (l+r)>>1; if(L <= mid) update(L, R, x, l, mid, idx<<1); if(R > mid) update(L, R, x, mid+1, r, idx<<1|1); PushUp(idx); } int query(int L, int R, int l, int r, int idx) { if(L <= l && r <= R) { return sum[idx]; } PushDown(idx, r-l+1); int mid = (l+r)>>1; int tmp = 0, mark = 0; if(L <= mid) { tmp = query(L, R, l, mid, idx<<1); mark = 1; } if(R > mid) { if(mark==1) { tmp &= query(L, R, mid+1, r, idx<<1|1); } else { tmp = query(L, R, mid+1, r, idx<<1|1); } } PushUp(idx); return tmp; } void work() throws Exception { int n = nextInt(), m = nextInt(); Arrays.fill(col, 0); Arrays.fill(sum, 0); for(int i = 0; i < m; i ++) { l[i] = nextInt(); r[i] = nextInt(); num[i] = nextInt(); update(l[i], r[i], num[i], 1, n, 1); } boolean flag = false; for(int i = 0; i < m; i ++) { if(query(l[i], r[i], 1, n, 1) != num[i]) { flag = true; break; } } if(flag) out.println("NO"); else { out.println("YES"); for(int i = 1; i <= n; i ++) { int ans = query(i, i, 1, n, 1); out.print(ans + " "); } out.println(); } } static PrintWriter out = new PrintWriter(System.out); Scanner cin = new Scanner(System.in); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer str = null; private String next() throws Exception{ while (str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return str.nextToken(); } private int nextInt() throws Exception{ return Integer.parseInt(next()); } private long nextLong() throws Exception{ return Long.parseLong(next()); } static int dx[] = {1, 0, 0, -1}; static int dy[] = {0, -1, 1, 0}; class pair implements Comparable<pair>{ int first, second; pair(int x, int y) { first = x; second = y; } public int compareTo(pair o) { // TODO auto-generated method stub if(first != o.first) return ((Integer)first).compareTo(o.first); else return ((Integer)second).compareTo(o.second); } } int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A; int pos = r; r--; while (l <= r) { int mid = (l + r) >> 1; if (A[mid] <= val) { l = mid + 1; } else { pos = mid; r = mid - 1; } } return pos; } int lower_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A; int pos = r; r--; while (l <= r) { int mid = (l + r) >> 1; if (A[mid] < val) { l = mid + 1; } else { pos = mid; r = mid - 1; } } return pos; } DecimalFormat df=new DecimalFormat("0.000000"); public static void main(String[] args) throws Exception{ Main wo = new Main(); wo.work(); out.close(); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.util.*; import java.util.concurrent.ArrayBlockingQueue; import java.awt.List; import java.io.*; import java.lang.*; import java.lang.reflect.Array; public class code2 { public static long INF = Long.MAX_VALUE/100; public static long[] a; public static long[] stree; public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); // Code starts.. int n = in.nextInt(); int m = in.nextInt(); int[][] set = new int[n+1][35]; long[][] qr = new long[m][3]; for(int i=0; i<m; i++) { int l = in.nextInt()-1; int r = in.nextInt()-1; long d = in.nextLong(); qr[i][0] = l; qr[i][1] = r; qr[i][2] = d; int k= 0 ; while(d>0) { if(d%2==1) { set[l][k] += 1; set[r+1][k] += -1; } k++; d /= 2L; } } long[][] num = new long[n][35]; for(int j=0; j<35; j++) { int var = 0; for(int i=0; i<n; i++) { var += set[i][j]; if(var>0) num[i][j] = 1; } } /*for(int i=0; i<n; i++) { for(int j=0; j<6; j++) pw.print(num[i][j]); pw.println(); } */ a = new long[n]; for(int i=0; i<n; i++) { long tmp = 0; long mul = 1; for(int j=0; j<35; j++) { if(num[i][j]>=1) tmp = ((long)tmp+(long)mul); mul *= (long)2; } a[i] = tmp; } //for(int i=0; i<n; i++) // pw.print(a[i]+" "); //pw.println(); stree = new long[10000007]; build(0, 0, n-1); //for(int i=0; i<10; i++) // pw.print(stree[i]+" "); //pw.println(); boolean flag = true; for(int i=0; i<m; i++) { long ans = query(0, 0, n-1, qr[i][0], qr[i][1]); // pw.println(ans); if(ans-qr[i][2]!=0) flag = false; } if(flag) { pw.println("YES"); for(int i=0; i<n; i++) pw.print(a[i]+" "); } else pw.println("NO"); // pw.print((long)((long)536870911 & (long)536870912)); //Code ends... pw.flush(); pw.close(); } public static void build(int node, int ll, int rl) { if(ll==rl) { // System.out.println(node+"--"); stree[node] = a[ll]; return; } int mid = (ll+rl)/2; build(2*node +1, ll, mid); build(2*node +2, mid+1, rl); //System.out.println(node+" "+stree[2*node+1]+" "+stree[2*node+2]+" "+(stree[2*node+1] & stree[2*node+2])); stree[node] = (long)(stree[2*node+1]) & (long)(stree[2*node+2]); } public static void update(int node, int ll, int rl, int idx, int n, long el) { if(ll>rl) return; if(ll==rl) { stree[node] = el; return; } int mid = (ll+rl)/2; if(ll<=idx && idx<=mid) update(2*node+1, ll, mid, idx, n-1, el); else update(2*node+2, mid+1, rl, idx, n-1, el); if(n%2==0) stree[node] = stree[2*node+1] ^ stree[2*node+2]; else stree[node] = stree[2*node+1] | stree[2*node+2]; } public static long query(int node, int start, int end, long ll, long rl) { if(rl < start || end < ll) return -1; if(ll <= start && end <= rl) return stree[node]; int mid = (start + end) / 2; long p1 = query(2*node+1, start, mid, ll, rl); long p2 = query(2*node+2, mid+1, end, ll, rl); if(p1==-1) return p2; else if(p2==-1) return p1; return (p1 & p2); } public static double slope(pair p1, pair p2) { double m = INF; if((p1.x - p2.x)!=0) m = (p1.y - p2.y)/(p1.x - p2.x); return Math.abs(m); } public static void lps(String s, int[] lps) { int i = 0; int j = 1; while(j<s.length()) { if(s.charAt(j)==s.charAt(i)) { i++; lps[j] = i; j++; } else { if(i!=0) { i = lps[i-1]; } else { lps[j] = i; j++; } } } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static long c = 0; public static long mod = (long)Math.pow(10, 6)+3; public static int d; public static int p; public static int q; public static boolean flag; //public static long INF = Long.MAX_VALUE; public static void factSieve(int[] fact, long n) { for (int i = 2; i <= n; i += 2) fact[i] = 2; for (int i = 3; i <= n; i += 2) { if (fact[i] == 0) { fact[i] = i; for (int j = i; (long) j * i <= n; j++) { fact[(int) ((long) i * j)] = i; } } } /* * int k = 1000; while(k!=1) { System.out.print(a[k]+" "); k /= a[k]; * * } */ } public static long floorsqrt(long n) { long ans = 0; long l = 0; long r = n; long mid = 0; while (l <= r) { mid = (l + r) / 2; if (mid * mid == n) return mid; if (mid * mid > n) { r = mid - 1; } else { ans = mid; l = mid + 1; } } return ans; } public static long[][] matmul(long[][] a, long[][] b){ int n = a.length; int m = b[0].length; long[][] c = new long[n][m]; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { long tmp = 0; for(int k=0; k<a[0].length; k++) tmp =( (tmp+mod)%mod+(a[i][k]*b[k][j]+mod)%mod)%mod; c[i][j] = (tmp+mod)%mod; } return c; } public static long[][] matpow(long[][] a,int n) { long[][] res = a; while(n>0) { if(n%2==1) { res = matmul(a, res); } a = matmul(a, a); n/=2; } return res; } public static int gcd(int p2, int p22) { if (p2 == 0) return (int) p22; return gcd(p22 % p2, p2); } public static int findGCD(int arr[], int n) { int result = arr[0]; for (int i = 1; i < n; i++) result = gcd(arr[i], result); return result; } public static void nextGreater(long[] a, int[] ans) { Stack<Integer> stk = new Stack<>(); stk.push(0); for (int i = 1; i < a.length; i++) { if (!stk.isEmpty()) { int s = stk.pop(); while (a[s] < a[i]) { ans[s] = i; if (!stk.isEmpty()) s = stk.pop(); else break; } if (a[s] >= a[i]) stk.push(s); } stk.push(i); } return; } public static long lcm(int[] numbers) { long lcm = 1; int divisor = 2; while (true) { int cnt = 0; boolean divisible = false; for (int i = 0; i < numbers.length; i++) { if (numbers[i] == 0) { return 0; } else if (numbers[i] < 0) { numbers[i] = numbers[i] * (-1); } if (numbers[i] == 1) { cnt++; } if (numbers[i] % divisor == 0) { divisible = true; numbers[i] = numbers[i] / divisor; } } if (divisible) { lcm = lcm * divisor; } else { divisor++; } if (cnt == numbers.length) { return lcm; } } } public static long fact(long n) { long factorial = 1; for (int i = 1; i <= n; i++) { factorial *= i; } return factorial; } public static long choose(long total, long choose) { if (total < choose) return 0; if (choose == 0 || choose == total) return 1; return (choose(total - 1, choose - 1) + choose(total - 1, choose)) % mod; } public static int[] suffle(int[] a, Random gen) { int n = a.length; for (int i = 0; i < n; i++) { int ind = gen.nextInt(n - i) + i; int temp = a[ind]; a[ind] = a[i]; a[i] = temp; } return a; } public static int floorSearch(int arr[], int low, int high, int x) { if (low > high) return -1; if (x > arr[high]) return high; int mid = (low + high) / 2; if (mid > 0 && arr[mid - 1] < x && x < arr[mid]) return mid - 1; if (x < arr[mid]) return floorSearch(arr, low, mid - 1, x); return floorSearch(arr, mid + 1, high, x); } public static void swap(int a, int b) { int temp = a; a = b; b = temp; } public static ArrayList<Integer> primeFactorization(int n) { ArrayList<Integer> a = new ArrayList<Integer>(); for (int i = 2; i * i <= n; i++) { while (n % i == 0) { a.add(i); n /= i; } } if (n != 1) a.add(n); return a; } public static void sieve(boolean[] isPrime, int n) { for (int i = 1; i < n; i++) isPrime[i] = true; isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i < n; i++) { if (isPrime[i] == true) { for (int j = (2 * i); j < n; j += i) isPrime[j] = false; } } } public static int lowerbound(ArrayList<Long> net, long c2) { int i = Collections.binarySearch(net, c2); if (i < 0) i = -(i + 2); return i; } public static int lowerboundArray(int[] dis, int c2) { int i = Arrays.binarySearch(dis, c2); if (i < 0) i = -(i + 2); return i; } public static int uperbound(ArrayList<Integer> list, int c2) { int i = Collections.binarySearch(list, c2); if (i < 0) i = -(i + 1); return i; } public static int uperboundArray(int[] dis, int c2) { int i = Arrays.binarySearch(dis, c2); if (i < 0) i = -(i + 1); return i; } public static long[] sort(long[] a) { Random gen = new Random(); int n = a.length; for (int i = 0; i < n; i++) { int ind = gen.nextInt(n - i) + i; long temp = a[ind]; a[ind] = a[i]; a[i] = temp; } Arrays.sort(a); return a; } public static int[] sort(int[] a) { Random gen = new Random(); int n = a.length; for (int i = 0; i < n; i++) { int ind = gen.nextInt(n - i) + i; int temp = a[ind]; a[ind] = a[i]; a[i] = temp; } Arrays.sort(a); return a; } public static int GCD(int a, int b) { if (b == 0) return a; else return GCD(b, a % b); } public static long GCD(long a, long b) { if (b == 0) return a; else return GCD(b, a % b); } public static void extendedEuclid(int A, int B) { if (B == 0) { d = A; p = 1; q = 0; } else { extendedEuclid(B, A % B); int temp = p; p = q; q = temp - (A / B) * q; } } public static long LCM(long a, long b) { return (a * b) / GCD(a, b); } public static int LCM(int a, int b) { return (a * b) / GCD(a, b); } public static int binaryExponentiation(int x, int n) { int result = 1; while (n > 0) { if (n % 2 == 1) result = result * x; x = x * x; n = n / 2; } return result; } public static int[] countDer(int n) { int der[] = new int[n + 1]; der[0] = 1; der[1] = 0; der[2] = 1; for (int i = 3; i <= n; ++i) der[i] = (i - 1) * (der[i - 1] + der[i - 2]); // Return result for n return der; } static long binomialCoeff(int n, int k) { long C[][] = new long[n + 1][k + 1]; int i, j; // Calculate value of Binomial Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using previosly stored values else C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } return C[n][k]; } public static long binaryExponentiation(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = result * x; x = (x % mod * x % mod) % mod; n = n / 2; } return result; } public static int modularExponentiation(int x, int n, int M) { int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } public static long modularExponentiation(long x, long n, long M) { long result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } public static int modInverse(int A, int M) { return modularExponentiation(A, M - 2, M); } public static long sie(long A, long M) { return modularExponentiation(A, M - 2, M); } public static boolean checkYear(int year) { if (year % 400 == 0) return true; if (year % 100 == 0) return false; if (year % 4 == 0) return true; return false; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } static class pair implements Comparable<pair> { Long x, y; pair(long x, long y) { this.x = x; this.y = y; } public int compareTo(pair o) { int result = x.compareTo(o.x); if (result == 0) result = y.compareTo(o.y); return result; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; if(p.x-x==0 && p.y-y==0) return true; else return false; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class spair implements Comparable<spair> { String x; Integer y; spair(String x, int y) { this.x = x; this.y = y; } public int compareTo(spair o) { long p1 = y; long p2 = o.y; if(p1==p2) return x.length()>o.x.length()?1:-1; int result = p1<p2?-1:1; return result; } public String toString() { return x + " " + y; } /*public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class triplet implements Comparable<triplet> { Integer x, y, z; triplet(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public int compareTo(triplet o) { int result = x.compareTo(o.x); if (result == 0) result = y.compareTo(o.y); if (result == 0) result = z.compareTo(o.z); return result; } public boolean equlas(Object o) { if (o instanceof triplet) { triplet p = (triplet) o; return x == p.x && y == p.y && z == p.z; } return false; } public String toString() { return x + " " + y + " " + z; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode() + new Long(z).hashCode(); } } static class query implements Comparable<query> { Integer l,r, x, idx, tp; query(int l, int r, int x, int id, int tp) { this.l = l ; this.r = r; this.x = x; this.idx = id; this.tp = tp; } public int compareTo(query o) { int result = x.compareTo(o.x); return result; } } /* * static class node implements Comparable<node> * * { Integer x, y, z; node(int x,int y, int z) { this.x=x; this.y=y; this.z=z; } * * public int compareTo(pair o) { int result = x.compareTo(o.x); if(result==0) * result = y.compareTo(o.y); if(result==0) result = z.compareTo(z); return * result; } * * @Override public int compareTo(node o) { // TODO Auto-generated method stub * return 0; } } */ } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; import java.util.*; public class Main { // main public static void main(String [] args) throws IOException { // makes the reader and writer BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // read in StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int[][] queries = new int[m][3]; for (int i=0;i<m;i++) { st = new StringTokenizer(f.readLine()); queries[i] = new int[]{Integer.parseInt(st.nextToken())-1,Integer.parseInt(st.nextToken())-1, Integer.parseInt(st.nextToken())}; } // sort by starting point Arrays.sort(queries,new Comparator<int[]>() { public int compare(int[] a, int[] b) { return (new Integer(a[0])).compareTo(b[0]); } }); // for each bit set settings and check int[] arr = new int[n]; for (int i=0;i<30;i++) { ArrayList<int[]> queriesY = new ArrayList<int[]>(); ArrayList<int[]> queriesN = new ArrayList<int[]>(); for (int j=0;j<m;j++) { if ((queries[j][2]&(1<<i))!=0) queriesY.add(new int[]{queries[j][0],queries[j][1]}); else queriesN.add(new int[]{queries[j][0],queries[j][1]}); } // make intervals disjoint and add ones int prev = 0; int[] nums = new int[n]; for (int[] span: queriesY) { if (span[0]<prev) { if (span[1]<prev) continue; else for (int j=prev;j<=span[1];j++) nums[j] = 1; } else for (int j=span[0];j<=span[1];j++) nums[j] = 1; prev = span[1]+1; } // prefix sums int[] ones = new int[n+1]; for (int j=1;j<=n;j++) ones[j] = ones[j-1]+nums[j-1]; // check whether at least one 0 for each 0 interval for (int[] span: queriesN) { if (ones[span[1]+1]-ones[span[0]]==span[1]-span[0]+1) { out.println("NO"); out.close(); System.exit(0); } } // add to arr for (int j=0;j<n;j++) arr[j]+=nums[j]*(1<<i); } // write to out out.println("YES"); for (int i=0;i<n;i++) { out.print(arr[i]); out.print(" "); } out.println(); // cleanup out.close(); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; struct node { int l, r, q; } s[100005]; int sum[100005 << 2], ans[100005], dis[100005]; void pustup(int i) { sum[i] = sum[i << 1] & sum[i << 1 | 1]; } void build(int l, int r, int i) { if (l == r) { sum[i] = ans[l]; return; } int mid = (l + r) >> 1; build(l, mid, i << 1); build(mid + 1, r, i << 1 | 1); pustup(i); } int query(int L, int R, int l, int r, int i) { if (L <= l && r <= R) { return sum[i]; } int ans = (1 << 30) - 1, mid = (l + r) >> 1; if (L <= mid) ans &= query(L, R, l, mid, i << 1); if (R > mid) ans &= query(L, R, mid + 1, r, i << 1 | 1); return ans; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &s[i].l, &s[i].r, &s[i].q); } for (int i = 0; i <= 29; i++) { memset(dis, 0, sizeof(dis)); for (int j = 1; j <= m; j++) { if ((s[j].q >> i) & 1) { dis[s[j].l]++; dis[s[j].r + 1]--; } } for (int j = 1; j <= n; j++) { dis[j] += dis[j - 1]; if (dis[j] > 0) ans[j] |= (1 << i); } } build(1, n, 1); bool flag = false; for (int i = 1; i <= m; i++) { int answer = query(s[i].l, s[i].r, 1, n, 1); if (answer != s[i].q) { flag = true; break; } } if (flag) printf("NO\n"); else { printf("YES\n"); for (int i = 1; i <= n; i++) printf("%d%c", ans[i], i == n ? '\n' : ' '); } }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using std::cerr; using std::cin; using std::cout; using std::deque; using std::endl; using std::fixed; using std::ios_base; using std::make_pair; using std::map; using std::max; using std::min; using std::ostream; using std::pair; using std::set; using std::setprecision; using std::string; using std::stringstream; using std::swap; using std::vector; using namespace std; long long init = 0; const long long BITY = 30; long long bitInfo[100 * 1000 + 5][30 + 5]; long long queries[100 * 1000 + 5][3]; long long M = 131072; long long w[262144]; long long getInterval(long long a, long long b) { long long va = M + a, vb = M + b; long long wyn = w[va]; if (va != vb) wyn &= w[vb]; while (va / 2 != vb / 2) { if (va % 2 == 0) wyn &= w[va + 1]; if (vb % 2 == 1) wyn &= w[vb - 1]; va /= 2; vb /= 2; } return wyn; } void clearTree() { for (long long i = 0; i < (long long)(262144); ++i) { w[i] = init; } } void setInterval(long long a, long long x) { long long va = M + a; w[va] &= x; while (va != 0) { w[va] &= x; va /= 2; } } vector<long long> result; int main() { for (long long i = 0; i < (long long)(BITY); ++i) { init += powl(2, i); } clearTree(); long long n, m; cin >> n >> m; for (long long i = 0; i < (long long)(m); ++i) { long long l, r, q; cin >> l >> r >> q; l--; r--; queries[i][0] = l; queries[i][1] = r; queries[i][2] = q; for (long long i = 0; i < (long long)(BITY); ++i) { if (q & (1 << i)) { bitInfo[l][i]++; bitInfo[r + 1][i]--; } } } long long aktBity[35]; for (long long i = 0; i < (long long)(BITY); ++i) { aktBity[i] = 0; } for (long long i = 0; i < (long long)(n); ++i) { long long current = 0; for (long long j = 0; j < (long long)(BITY); ++j) { aktBity[j] += bitInfo[i][j]; } for (long long j = 0; j < (long long)(BITY); ++j) { if (aktBity[j]) { current += 1 << j; } } result.push_back(current); } for (long long i = 0; i < (long long)(n); ++i) { setInterval(i, result[i]); } bool flaga = true; for (long long i = 0; i < (long long)(m); ++i) { if (getInterval(queries[i][0], queries[i][1]) != queries[i][2]) { flaga = false; break; } } if (flaga) { cout << "YES\n"; for (long long i = 0; i < (long long)(n); ++i) { cout << result[i] << " "; } cout << "\n"; } else { cout << "NO\n"; if (n == 1000 && m == 1000) { for (__typeof((result).begin()) it = ((result).begin()); it != (result).end(); ++it) { cout << *it << ' '; } } } 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 Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); String s[]=bu.readLine().split(" "); int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]),i,j,c[][]=new int[n+2][30],qr[][]=new int[m][3]; boolean pos=true; for(i=0;i<m;i++) { s=bu.readLine().split(" "); int l=Integer.parseInt(s[0]),r=Integer.parseInt(s[1]),q=Integer.parseInt(s[2]); qr[i][0]=l; qr[i][1]=r; qr[i][2]=q; for(j=0;j<30;j++) if(((q>>j)&1)==1) { c[l][j]++; c[r+1][j]--; } } for(i=1;i<=n;i++) for(j=0;j<30;j++) c[i][j]+=c[i-1][j]; for(i=1;i<=n;i++) { int val=0; for(j=0;j<30;j++) if(c[i][j]>0) val|=1<<j; update(1,1,n,i,i,val); } for(i=0;i<m;i++) { long val=query(1,1,n,qr[i][0],qr[i][1]); if(val!=qr[i][2]) {pos=false; break;} } if(!pos) {System.out.print("NO\n"); return;} sb.append("YES\n"); for(i=1;i<=n;i++) sb.append(query(1,1,n,i,i)+" "); System.out.print(sb); } static int N=800001; static long st[]=new long[N],lazy[]=new long[N]; static void push(int i) { if(lazy[i]==0) return; st[2*i]=lazy[i]; st[2*i+1]|=lazy[i]; lazy[2*i]=lazy[i]; lazy[2*i+1]=lazy[i]; lazy[i]=0; } static void update(int i,int sl,int sr,int l,int r,int v) { if(sl>r || sr<l) return; push(i); if(sl==l && sr==r) { lazy[i]=v; st[i]=v; return; } else if(sl>=l && sr<=r) { lazy[i]=v; st[i]|=v; return; } int m=(sl+sr)/2; update(2*i,sl,m,l,r,v); update(2*i+1,m+1,sr,l,r,v); st[i]=(st[2*i]&st[2*i+1]); } static long query(int i,int sl,int sr,int l,int r) { if(sl>r || sr<l) return (1L<<40)-1; push(i); if(sl>=l && sr<=r) return st[i]; int m=(sl+sr)/2; return (query(2*i,sl,m,l,r)&query(2*i+1,m+1,sr,l,r)); } }
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; int lazy[600005]; bool haslazy[600005]; int tree[600005]; int arr[200005]; int lft(int x) { return 2 * x + 1; } int ryt(int x) { return 2 * x + 2; } struct lol { int x; int y; int z; }; void updt(int x, int l, int r, int value) { tree[x] = (value | tree[x]); if (haslazy[x]) lazy[x] = (lazy[x] | value); else lazy[x] = value; haslazy[x] = true; } void laze(int x, int l, int r) { if (!haslazy[x]) return; int mid = (l + r) / 2; updt(ryt(x), mid + 1, r, lazy[x]); updt(lft(x), l, mid, lazy[x]); lazy[x] = 0; haslazy[x] = false; } void prop(int x, int l, int r, int a, int b, int value) { if (r < a || l > b) return; if (l >= a && r <= b) { updt(x, l, r, value); return; } laze(x, l, r); int mid = (l + r) / 2; prop(lft(x), l, mid, a, b, value); prop(ryt(x), mid + 1, r, a, b, value); int ta = tree[lft(x)]; int tb = tree[ryt(x)]; tree[x] = (ta & tb); } int query(int x, int l, int r, int a, int b) { if (l > b || r < a) return ((long long)1 << (long long)31) - 1; if (l >= a && r <= b) { return tree[x]; } laze(x, l, r); int mid = (l + r) / 2; int ta = query(lft(x), l, mid, a, b); int tb = query(ryt(x), mid + 1, r, a, b); return (ta & tb); } void build(int x, int a, int b) { if (a == b) { tree[x] = 0; return; } int mid = (a + b) / 2; build(lft(x), a, mid); build(ryt(x), mid + 1, b); tree[x] = 0; } int main() { cin >> n; build(0, 0, n - 1); int m; cin >> m; vector<lol> q; for (int i = 0; i < m; i++) { lol temp; cin >> temp.x >> temp.y >> temp.z; prop(0, 0, n - 1, temp.x - 1, temp.y - 1, temp.z); q.push_back(temp); } bool flag = false; for (int i = 0; i < m; i++) { if (query(0, 0, n - 1, q[i].x - 1, q[i].y - 1) != q[i].z) { flag = true; break; } } if (flag) { cout << "NO"; } else { cout << "YES" << endl; for (int i = 0; i < n; i++) { int temp = query(0, 0, n - 1, i, i); cout << temp << " "; } } }
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 = 100007; const int MAXBIT = 30; int a[N]; int sum[N]; int tree[3 * N]; inline void build(int v, int l, int r) { if (l + 1 == r) { tree[v] = a[l]; return; } int mid = (l + r) >> 1; build(v * 2, l, mid); build(v * 2 + 1, mid, r); tree[v] = tree[v * 2] & tree[v * 2 + 1]; } inline 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 res = (1 << MAXBIT) - 1; if (l < mid) res &= query(v * 2, l, min(r, mid), L, mid); if (mid < r) res &= query(v * 2 + 1, max(l, mid), r, mid, R); return res; } int main() { int n, m; cin >> n >> m; vector<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]; } memset(a, 0, sizeof(a)); for (int b = 0; b <= MAXBIT; ++b) { for (int i = 0; i < n; ++i) sum[i] = 0; for (int i = 0; i < m; ++i) { if ((q[i] >> b) & 1) { ++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) a[i] |= (1 << b); } } build(1, 0, n); for (int i = 0; i < m; ++i) { if (query(1, l[i], r[i] + 1, 0, n) != q[i]) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; for (int i = 0; i < n; ++i) cout << a[i] << " "; return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; int poo(int a, int b) { long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y); } y = (y * y); b /= 2; } return x; } int p[32], fck[100005][32], pre[100005][32], val[100005]; vector<pair<int, int> > v[32], chd; vector<int> ans; int main() { int n, m, i, j, k; int sum = 0, x, y, q, st, en; scanf("%d", &n); scanf("%d", &m); for (i = 0; i < n; i++) { for (j = 0; j < 32; j++) { fck[i][j] = 0; } } for (i = 0; i < m; i++) { scanf("%d", &x); scanf("%d", &y); scanf("%d", &q); for (k = 0; k < 32; k++) { p[k] = 0; } k = 0; x--; y--; chd.push_back(make_pair(x, y)); val[i] = q; while (q) { if (q & 1 == 1) { p[k] = 1; } q /= 2; k++; } for (k = 0; k < 32; k++) { if (p[k] == 1) { v[k].push_back(make_pair(x, y)); } } } for (i = 0; i < 32; i++) { sort(v[i].begin(), v[i].end()); } for (i = 0; i < 32; i++) { st = 0; en = -1; if (v[i].size()) { st = v[i][0].first; en = v[i][0].second; } for (j = 0; j < v[i].size(); j++) { if (j == 0) { continue; } if (v[i][j].first > en) { for (k = st; k <= en; k++) { fck[k][i] = 1; } st = v[i][j].first; en = v[i][j].second; } else { if (v[i][j].second > en) { en = v[i][j].second; } } } for (k = st; k <= en; k++) { fck[k][i] = 1; } } for (i = 0; i < n; i++) { sum = 0; for (j = 0; j < 32; j++) { if (fck[i][j] == 1) { sum += poo(2, j); } } ans.push_back(sum); } for (i = 0; i < 32; i++) { st = -1; for (j = 0; j < n; j++) { if (fck[j][i] == 0) { st = j; pre[j][i] = n + 1; } else { pre[j][i] = st; } } } for (i = 0; i < chd.size(); i++) { for (k = 0; k < 32; k++) { p[k] = 0; } k = 0; q = val[i]; x = chd[i].first; y = chd[i].second; while (q) { if (q & 1 == 1) { p[k] = 1; } q /= 2; k++; } for (k = 0; k < 32; k++) { if (p[k] == 0) { if (pre[y][k] + 1 <= x) { cout << "NO"; return 0; } } } } cout << "YES"; printf("\n"); for (i = 0; i < ans.size(); i++) { printf("%d", ans[i]); printf(" "); } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const long long mod = 1e9 + 7; const long long MOD = 998244353; const double EPS = 1e-8; const double PI = acos(-1.0); template <class T> class SegmentTree { public: SegmentTree(std::vector<T> data, T value, T (*combine)(T obj1, T obj2)); SegmentTree(T ar[], int n, T value, T (*combine)(T obj1, T obj2)); T query(int l, int r); void update(int idx, T val); private: T *tree; void buildTree(std::vector<T> data); int segTreeSize; T valueForExtraNodes; T (*combine)(T obj1, T obj2); int calculateSize(int n); T queryHelper(int l, int r, int st, int ed, int node); }; template <class T> SegmentTree<T>::SegmentTree(std::vector<T> data, T value, T (*combine)(T obj1, T obj2)) { this->combine = combine; valueForExtraNodes = value; segTreeSize = calculateSize(data.size()); buildTree(data); } template <class T> SegmentTree<T>::SegmentTree(T ar[], int n, T value, T (*combine)(T obj1, T obj2)) { this->combine = combine; valueForExtraNodes = value; segTreeSize = calculateSize(n); std::vector<T> data; for (int i = 0; i < n; i++) data.push_back(ar[i]); buildTree(data); } template <class T> int SegmentTree<T>::calculateSize(int n) { int pow2 = 1; while (pow2 < n) { pow2 = pow2 << 1; } return 2 * pow2 - 1; } template <class T> T SegmentTree<T>::query(int l, int r) { int st = 0, ed = segTreeSize / 2; return queryHelper(l, r, st, ed, 0); } template <class T> T SegmentTree<T>::queryHelper(int l, int r, int st, int ed, int node) { if ((r < st) || (l > ed) || (l > r)) return valueForExtraNodes; if (st >= l && ed <= r) return tree[node]; T leftVal = queryHelper(l, r, st, (st + ed) / 2, (2 * node + 1)); T rightVal = queryHelper(l, r, (st + ed) / 2 + 1, ed, (2 * node + 2)); return combine(leftVal, rightVal); } template <class T> void SegmentTree<T>::buildTree(std::vector<T> data) { int n = data.size(); tree = new T[segTreeSize]; int extraNodes = (segTreeSize / 2 + 1) - n; for (int i = segTreeSize - 1; i >= 0; i--) { if (extraNodes > 0) { tree[i] = valueForExtraNodes; extraNodes--; } else if (n > 0) { tree[i] = data[n - 1]; n--; } else tree[i] = combine(tree[(2 * i + 1)], tree[(2 * i + 2)]); } } template <class T> void SegmentTree<T>::update(int idx, T val) { int segTreeIdx = (segTreeSize / 2) + idx; tree[segTreeIdx] = val; while (((segTreeIdx - 1) / 2) >= 0) { segTreeIdx = ((segTreeIdx - 1) / 2); if ((2 * segTreeIdx + 2) < segTreeSize) tree[segTreeIdx] = combine(tree[(2 * segTreeIdx + 1)], tree[(2 * segTreeIdx + 2)]); if (segTreeIdx == 0) break; } } const int N = 1e5 + 5; const int mxbit = 32; long long ors(long long a, long long b) { return a & b; } void solve() { int n, m; cin >> n >> m; vector<long long> l(N), r(N), q(N); vector<long long> a(N); for (int i = 0; i < m; ++i) { cin >> l[i] >> r[i] >> q[i]; l[i]--; r[i]--; } for (int bit = 0; bit < mxbit; ++bit) { vector<long long> sums(n + 1); for (int i = 0; i < m; ++i) { if ((q[i] >> bit) & 1) { sums[l[i]]++; sums[r[i] + 1]--; } } for (int i = 0; i < n; ++i) { if (i - 1 >= 0) sums[i] += sums[i - 1]; if (sums[i] > 0) { a[i] = a[i] | (1 << bit); } } } long long mx = 1; long long k = 1; for (int i = 1; i <= mxbit; ++i) { k *= 2; mx += (k); } SegmentTree<long long> sgtree(a, mx, ors); for (int i = 0; i < m; ++i) { long long p = sgtree.query(l[i], r[i]); if (p != q[i]) { cout << "NO\n"; return; } } cout << "YES\n"; for (int i = 0; i < n; ++i) { cout << a[i] << " "; } cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int MAXN = 100005; int n, m, li, ri, qi; int a[800001], upd[800001]; void add(int x, int val) { a[x] |= val; upd[x] |= val; } void push(int x) { add(x + x, upd[x]); add(x + x + 1, upd[x]); upd[x] = 0; } void update(int x, int l, int r, int ll, int rr, int val) { if (r < ll || rr < l) return; if (ll <= l && r <= rr) { add(x, val); return; } push(x); int mid = (l + r) >> 1; update(x + x, l, mid, ll, rr, val); update(x + x + 1, mid + 1, r, ll, rr, val); a[x] = a[x + x] & a[x + x + 1]; } int get(int x, int l, int r, int ll, int rr) { if (r < ll || rr < l) return -1; if (ll <= l && r <= rr) { return a[x]; } int mid = (l + r) >> 1; push(x); int u = get(x + x, l, mid, ll, rr); int v = get(x + x + 1, mid + 1, r, ll, rr); return u & v; } int L[100001], R[100001], Q[100001]; void solve() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d%d", &L[i], &R[i], &Q[i]); update(1, 1, n, L[i], R[i], Q[i]); } for (int i = 0; i < m; i++) if (get(1, 1, n, L[i], R[i]) != Q[i]) { printf("NO\n"); return; } printf("YES\n"); for (int i = 1; i <= n; i++) printf("%d ", get(1, 1, n, i, i)); printf("\n"); } int main() { solve(); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashMap; import java.awt.Point; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[] a = new int[n]; int[][] bit = new int[n + 1][30]; HashMap<Point, Integer> map = new HashMap<>(); for (int i = 0; i < m; i++) { int l = in.nextInt() - 1; int r = in.nextInt() - 1; int q = in.nextInt(); Integer tmp = map.get(new Point(l, r)); if (tmp != null && tmp != q) { out.println("NO"); return; } map.put(new Point(l, r), q); for (int j = 0; (1 << j) <= q; j++) { if ((q & (1 << j)) != 0) { bit[l][j]++; bit[r + 1][j]--; } } } int[][] sum = new int[n + 1][30]; for (int j = 0; j < 30; j++) { if (bit[0][j] != 0) { sum[0][j] = 1; a[0] |= (1 << j); } else sum[0][j] = 0; } for (int i = 1; i < n; i++) for (int j = 0; j < 30; j++) { bit[i][j] += bit[i - 1][j]; if (bit[i][j] != 0) { a[i] |= (1 << j); sum[i][j] = sum[i - 1][j] + 1; } else sum[i][j] = sum[i - 1][j]; } ArrayList<Point> tmp = new ArrayList<>(map.keySet()); for (Point p : tmp) { int q = map.get(p); for (int j = 0; (1 << j) <= q; j++) { int s = 0; if (p.x != 0) s = sum[p.y][j] - sum[p.x - 1][j]; else s = sum[p.y][j]; if ((q & (1 << j)) == 0 && s == p.y - p.x + 1) { out.println("NO"); return; } else if ((q & (1 << j)) != 0 && p.y - p.x + 1 != s) { out.println("NO"); return; } } } out.println("YES"); for (int i = 0; i < n; i++) out.print(a[i] + (i == n - 1 ? "\n" : " ")); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[16384]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; struct node { int l, r, q; bool operator<(const node &b) const { if (r == b.r) return l < b.l; else return r < b.r; } } a[maxn]; int tree[maxn << 2]; int lazy[maxn << 2]; void pushup(int rt) { tree[rt] = (tree[rt << 1] & tree[rt << 1 | 1]); } void pushdown(int rt) { if (lazy[rt] == 0) return; lazy[rt << 1] |= lazy[rt]; lazy[rt << 1 | 1] |= lazy[rt]; tree[rt << 1] |= lazy[rt]; tree[rt << 1 | 1] |= lazy[rt]; lazy[rt] = 0; } void update(int rt, int l, int r, int begin, int end, int val) { if (begin <= l && r <= end) { tree[rt] |= val; lazy[rt] |= val; return; } pushdown(rt); int mid = (l + r) >> 1; if (begin <= mid) update(rt << 1, l, mid, begin, end, val); if (mid + 1 <= end) update(rt << 1 | 1, mid + 1, r, begin, end, val); pushup(rt); } int query(int rt, int l, int r, int begin, int end) { if (begin <= l && r <= end) { return tree[rt]; } pushdown(rt); int mid = (l + r) >> 1; int res = (1 << 30) - 1; if (begin <= mid) res &= query(rt << 1, l, mid, begin, end); if (mid + 1 <= end) res &= query(rt << 1 | 1, mid + 1, r, begin, end); return res; } int main(void) { memset(tree, 0, sizeof(tree)); memset(lazy, 0, sizeof(lazy)); 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); update(1, 1, n, a[i].l, a[i].r, a[i].q); } bool flag = true; for (int i = 1; i <= m; i++) { int t = query(1, 1, n, a[i].l, a[i].r); if (t != a[i].q) { flag = false; break; } } if (flag == false) printf("NO\n"); else { printf("YES\n"); for (int i = 1; i <= n; i++) { int x = query(1, 1, n, i, i); 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; int a[100005], b[100005], c[100005]; struct node { long long left, right, sum, lazy; }; node f[100005 << 2]; void build(int i, int l, int r) { f[i].left = l; f[i].right = r; if (l == r) return; int mid = (l + r) >> 1; build(i << 1, l, mid); build(i << 1 | 1, mid + 1, r); } void pushdown(int i) { if (f[i].lazy) { f[i << 1].sum |= f[i].lazy; f[i << 1 | 1].sum |= f[i].lazy; f[i << 1].lazy |= f[i].lazy; f[i << 1 | 1].lazy |= f[i].lazy; f[i].lazy = 0L; } } long long flag; void update(int i, int l, int r, int x) { if (l <= f[i].left && f[i].right <= r) { f[i].sum |= x; f[i].lazy |= x; return; } pushdown(i); int mid = (f[i].left + f[i].right) >> 1; if (mid >= l) update(i << 1, l, r, x); if (mid < r) update(i << 1 | 1, l, r, x); f[i].sum = f[i << 1].sum & f[i << 1 | 1].sum; } void query(int i, int l, int r) { if (l <= f[i].left && f[i].right <= r) { flag &= f[i].sum; return; } pushdown(i); int mid = (f[i].left + f[i].right) >> 1; if (mid >= l) query(i << 1, l, r); if (mid < r) query(i << 1 | 1, l, r); f[i].sum = f[i << 1].sum & f[i << 1 | 1].sum; } int main() { int n, m; cin >> n >> m; build(1, 1, n); for (int i = 1; i <= m; i++) { cin >> a[i] >> b[i] >> c[i]; update(1, a[i], b[i], c[i]); } for (int i = 1; i <= m; i++) { flag = (1 << 30) - 1; query(1, a[i], b[i]); if (flag != c[i]) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; for (int i = 1; i <= n; i++) { flag = (1 << 30) - 1; query(1, i, i); cout << flag << " "; } 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 t[400000 + 5], n, m, atas[100003], bawah[100003], nilai[100003], haha[33][100003], a[100003]; bool ada; void build(int node, int l, int r) { if (l == r) { t[node] = a[l]; return; } int mid = (l + r) >> 1; build(2 * node, l, mid); build(2 * node + 1, mid + 1, r); t[node] = t[2 * node] & t[2 * node + 1]; } int query(int node, int l, int r, int le, int ri) { if (l >= le && r <= ri) return t[node]; int mid = (l + r) >> 1; if (ri <= mid) return query(2 * node, l, mid, le, ri); else if (le > mid) return query(2 * node + 1, mid + 1, r, le, ri); else { int lefty = query(2 * node, l, mid, le, ri); int righty = query(2 * node + 1, mid + 1, r, le, ri); return lefty & righty; } } int main(void) { scanf("%d%d", &n, &m); memset(haha, 0, sizeof(haha)); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &atas[i], &bawah[i], &nilai[i]); for (int j = 0; j <= 30; j++) if ((nilai[i] & (1 << j)) > 0) { haha[j][atas[i]]++; haha[j][bawah[i] + 1]--; } } memset(a, 0, sizeof(a)); for (int i = 0; i <= 30; i++) for (int j = 1; j <= n; j++) { haha[i][j] += haha[i][j - 1]; if (haha[i][j] > 0) a[j] += (1 << i); } build(1, 1, n); ada = true; for (int i = 1; i <= m; i++) { if (query(1, 1, n, atas[i], bawah[i]) != nilai[i]) { ada = false; break; } } if (ada == false) printf("NO\n"); else { printf("YES\n"); for (int i = 1; i <= n; i++) { printf("%d", a[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; long long tree[3 * 100005]; bool lazy[3 * 100005]; void update(long long node, long long b, long long e, long long i, long long j, long long v) { if (i > e || j < b) { return; } if (b >= i && e <= j) { lazy[node] = true; if (tree[node] == (1LL << 33) - 1) tree[node] = v; else { tree[node] = tree[node] | v; } return; } if (lazy[node]) { if (tree[node * 2] == (1LL << 33) - 1) tree[node * 2] |= tree[node]; else tree[node * 2] |= tree[node]; if (tree[node * 2 + 1] == (1LL << 33) - 1) tree[node * 2 + 1] |= tree[node]; else tree[node * 2 + 1] |= tree[node]; lazy[node * 2] = 1; lazy[node * 2 + 1] = 1; lazy[node] = 0; } update(node * 2, b, (b + e) / 2, i, j, v); update(node * 2 + 1, (b + e) / 2 + 1, e, i, j, v); tree[node] = tree[node * 2] & tree[node * 2 + 1]; } long long query(long long node, long long b, long long e, long long i, long long j) { if (i > e || j < b) { return (1LL << 33) - 1; } if (b >= i && e <= j) { return tree[node]; } if (lazy[node]) { if (tree[node * 2] == (1LL << 33) - 1) tree[node * 2] |= tree[node]; else tree[node * 2] |= tree[node]; if (tree[node * 2 + 1] == (1LL << 33) - 1) tree[node * 2 + 1] |= tree[node]; else tree[node * 2 + 1] |= tree[node]; lazy[node * 2] = 1; lazy[node * 2 + 1] = 1; lazy[node] = 0; } return query(node * 2, b, (b + e) / 2, i, j) & query(node * 2 + 1, (b + e) / 2 + 1, e, i, j); } int main() { long long n, m; cin >> n >> m; pair<long long, long long> p[m + 1]; long long a[m + 1]; for (long long i = 0; i < 3 * 100002; i++) { tree[i] = 0; } memset(lazy, 0, sizeof lazy); for (long long i = 1; i <= m; i++) { long long x, y, z; cin >> x >> y >> z; update(1, 1, n, x, y, z); p[i].first = x; p[i].second = y; a[i] = z; } int f = 1; for (long long i = 1; i <= m; i++) { long long x = query(1, 1, n, p[i].first, p[i].second); if (x != a[i]) { f = 0; } } if (f == 1) { cout << "YES\n"; for (long long i = 1; i <= n; i++) { cout << query(1, 1, n, i, i) << " "; } cout << endl; } else { cout << "NO\n"; } }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> const int N = 1e5 + 5; int n, m, seg[N << 2], tag[N << 2], L[N], R[N], Q[N]; void pushup(int rt) { seg[rt] = seg[rt << 1] & seg[rt << 1 | 1]; } void update(int rt, int k) { seg[rt] |= k, tag[rt] |= k; } void pushdown(int rt) { if (!tag[rt]) return; update(rt << 1, tag[rt]), update(rt << 1 | 1, tag[rt]); tag[rt] = 0; } void modify(int x, int y, int rt, int l, int r, int k) { if (x <= l && r <= y) return update(rt, k); pushdown(rt); int mid = (l + r) >> 1; if (x <= mid) modify(x, y, rt << 1, l, mid, k); if (mid < y) modify(x, y, rt << 1 | 1, mid + 1, r, k); pushup(rt); } int query(int x, int y, int rt, int l, int r) { if (x <= l && r <= y) return seg[rt]; pushdown(rt); int mid = (l + r) >> 1, ret = (1LL << 32) - 1; if (x <= mid) ret &= query(x, y, rt << 1, l, mid); if (mid < y) ret &= query(x, y, rt << 1 | 1, mid + 1, r); return ret; } void print(int rt, int l, int r) { if (l == r) { printf("%d%c", seg[rt], " \n"[l == n]); return; } pushdown(rt); int mid = (l + r) >> 1; print(rt << 1, l, mid); print(rt << 1 | 1, mid + 1, r); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { scanf("%d%d%d", &L[i], &R[i], &Q[i]); modify(L[i], R[i], 1, 1, n, Q[i]); } for (int i = 1; i <= m; ++i) if (Q[i] != query(L[i], R[i], 1, 1, n)) return puts("NO"), 0; puts("YES"), print(1, 1, n); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d %d", &n, &m); vector<int> ans(n, 0); vector<pair<pair<int, int>, int> > queries(m); for (auto &x : queries) { scanf("%d %d %d", &x.first.first, &x.first.second, &x.second); x.first.first--; } for (int b = 1; b < (1 << 30); b <<= 1) { vector<int> pos(n + 1, 0); for (auto &x : queries) if (x.second & b) { pos[x.first.first]++; pos[x.first.second]--; } int psum = 0; for (int i = 0; i < n; ++i) { psum += pos[i]; if (psum) ans[i] |= b; } } vector<int> ands(n * 18); copy(ans.begin(), ans.end(), ands.begin()); for (int step = 1; step < 18; ++step) for (int i = 0; i < n; ++i) { ands[step * n + i] = ands[(step - 1) * n + i] & ands[(step - 1) * n + min(i + (1 << (step - 1)), n - 1)]; } bool valid = true; for (auto &x : queries) { if (!valid) break; int p = 31 - __builtin_clz(x.first.second - x.first.first); int temp = ands[p * n + x.first.first] & ands[p * n + (x.first.second - (1 << p))]; valid = temp == x.second; } if (!valid) printf("NO\n"); else { printf("YES\n"); for (int x : ans) printf("%d ", x); 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.InputStreamReader; import java.util.StringTokenizer; public class Main { public static int N, M; public static int[][] delta, value, prefixes, inputs; public static int[] res; public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(f.readLine()); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); delta = new int[32][N + 1]; value = new int[32][N + 1]; prefixes = new int[32][N + 1]; inputs = new int[M][3]; res = new int[N + 1]; for (int iter = 0; iter < M; iter++) { st = new StringTokenizer(f.readLine()); int l = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); inputs[iter][0] = l; inputs[iter][1] = r; inputs[iter][2] = a; for (int k = 31; k >= 0; k--) { if (((a >> k) & 1) == 1) { delta[k][l - 1]++; delta[k][r]--; } } } for (int k = 31; k >= 0; k--) { int curr = 0; for (int i = 1; i <= N; i++) { curr += delta[k][i - 1]; if (curr > 0) { value[k][i] = 1; res[i] |= (1 << k); } prefixes[k][i] = prefixes[k][i - 1] + value[k][i]; } } for (int[] is : inputs) { int l = is[0]; int r = is[1]; int a = is[2]; for (int k = 31; k >= 0; k--) { if (((a >> k) & 1) == 0) { if (prefixes[k][r] - prefixes[k][l - 1] == (r - l + 1)) { System.out.println("NO"); return; } } } } StringBuilder sb = new StringBuilder(); sb.append("YES").append("\n"); for (int i = 1; i <= N; i++) { sb.append(res[i]).append(" "); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb.toString()); } } //
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int m = in.readInt(); int[] l = new int[m]; int[] r = new int[m]; int[] v = new int[m]; int[][] oneBits = new int[32][n + 1]; int[][] sum = new int[32][n]; int[] res = new int[n]; for (int i = 0; i < m; i++) { l[i] = in.readInt() - 1; r[i] = in.readInt() - 1; v[i] = in.readInt(); for (int bitPosition = 0; bitPosition <= 30; bitPosition++) { if ((v[i] & (1 << bitPosition)) != 0) { oneBits[bitPosition][l[i]] += 1; oneBits[bitPosition][r[i] + 1] -= 1; } } } for (int bitPosition = 0; bitPosition <= 30; bitPosition++) { for (int i = 0; i < n; i++) { oneBits[bitPosition][i] += (i == 0 ? 0 : oneBits[bitPosition][i - 1]); sum[bitPosition][i] += (i == 0 ? 0 : sum[bitPosition][i - 1]); if (oneBits[bitPosition][i] > 0) { res[i] |= (1 << bitPosition); sum[bitPosition][i]++; } } } boolean ok = true; for (int i = 0; i < m; i++) { int segmentLength = r[i] - l[i] + 1; for (int bitPosition = 0; bitPosition < 30; bitPosition++) { if ((v[i] & (1 << bitPosition)) == 0) { int onesInSegment = sum[bitPosition][r[i]] - (l[i] == 0 ? 0 : sum[bitPosition][l[i] - 1]); if (onesInSegment >= segmentLength) { ok = false; } } } } if (!ok) { out.printLine("NO"); } else { out.printLine("YES"); for (int x : res) { out.print(x + " "); } out.printLine(); } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine() { writer.println(); } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; struct cc { int l, r, len, num; bool operator<(const cc &x) const { return len < x.len; } } a[maxn]; int b[maxn], sum[maxn << 2], add[maxn << 2], ans, f[maxn][20]; void push_down(int rt) { if (add[rt]) { sum[rt << 1] |= add[rt]; sum[rt << 1 | 1] |= add[rt]; add[rt << 1] |= add[rt]; add[rt << 1 | 1] |= add[rt]; add[rt] = 0; } } void update(int L, int R, int l, int r, int rt, int x) { if (l >= L && r <= R) { sum[rt] = sum[rt] | x; add[rt] = add[rt] | x; return; } push_down(rt); int mid = (l + r) >> 1; if (L <= mid) update(L, R, l, mid, rt << 1, x); if (mid < R) update(L, R, mid + 1, r, rt << 1 | 1, x); } int ask(int l, int r) { int k = log2(r - l + 1); return f[l][k] & f[r - (1 << k) + 1][k]; } void query(int x, int l, int r, int rt) { if (l == r) { ans = sum[rt]; return; } push_down(rt); int mid = (l + r) >> 1; if (x <= mid) query(x, l, mid, rt << 1); else query(x, mid + 1, r, rt << 1 | 1); } int main() { int n, m, i, j, k, num; scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) { scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].num); update(a[i].l, a[i].r, 1, n, 1, a[i].num); } for (i = 1; i <= n; i++) { query(i, 1, n, 1); f[i][0] = ans; } for (j = 1; (1 << j) <= n; j++) for (i = 1; i + (1 << j) - 1 <= n; i++) { f[i][j] = f[i][j - 1] & f[i + (1 << (j - 1))][j - 1]; } for (i = 1; i <= m; i++) { if (ask(a[i].l, a[i].r) != a[i].num) { printf("NO\n"); return 0; } } printf("YES\n"); for (i = 1; i <= n; i++) printf("%d ", f[i][0]); 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.*; import java.io.*; import java.text.*; public class B482 { 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; 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]; } } 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 << 30) - 1; if (b >= i && e <= j) return sTree[node] | lazy[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) | lazy[node]; } } public static void main(String[] args) throws IOException { 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 st = new SegmentTree(arr); 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(); st.update_range(l[i], r[i], q[i]); } for (int i = 0; i < m; i++) { if (st.query(l[i], r[i]) != q[i]) { pw.println("NO"); pw.flush(); pw.close(); return; } } pw.println("YES"); for (int i = 1; i <= n; i++) { pw.print(st.query(i, i) + " "); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { private static final int MAXBIT = 30; private int seg[], l[], r[], q[], sum[], ans[]; public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int M = in.nextInt(); l = new int[M]; r = new int[M]; q = new int[M]; sum = new int[N+1]; ans = new int[N]; seg = new int[4 * N]; for (int i = 0; i < M; i++) { l[i] = in.nextInt() - 1; r[i] = in.nextInt() - 1; q[i] = in.nextInt(); } for (int bit = 0; bit <= MAXBIT; bit++) { for (int i = 0; i < N; i++) sum[i] = 0; for (int i = 0; i < M; i++) { if (((q[i] >> bit) & 1) == 1) { sum[l[i]]++; sum[r[i]+1]--; } } for (int i = 0; i < N; i++) { if (i > 0) sum[i] += sum[i - 1]; if (sum[i] > 0) { ans[i] |= (1 << bit); } } } build(1, 0, N-1); for (int i = 0; i < M; i++) { if (query(1, l[i], r[i], 0, N-1) != q[i]) { out.println("NO"); return; } } out.println("YES"); for (int i = 0; i < N; i++) { out.print(ans[i] + " "); } out.println(); } private void build(int idx, int l, int r) { if (l == r) { seg[idx] = ans[l]; return; } int mid = (l + r) >> 1; build(2 * idx, l, mid); build(2 * idx + 1, mid+1, r); seg[idx] = seg[idx * 2] & seg[idx * 2 + 1]; } private int query(int idx, int l, int r, int L, int R) { if (l == L && r == R) { return seg[idx]; } int mid = (L + R) >> 1; int ans = (1 << MAXBIT) - 1; if (l <= mid) { ans &= query(idx * 2, l, Math.min(r, mid), L, mid); } if (mid+1 <= r) { ans &= query(idx * 2 + 1, Math.max(l, mid+1), r, mid+1, R); } return ans; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.StringTokenizer; public class Main{ public static class SegTree{ int n; long[] dat; public SegTree(int n_) { int n = 1; while(n < n_){ n *= 2;} this.n = n; dat = new long[this.n * 2 - 1]; for(int i = 0; i < this.n * 2 - 1 ; i++){ dat[i] = 0; } } private void lazy_evaluate_node(int k, int a, int b){ if(k < n - 1){ dat[2 * k + 1] |= dat[k]; dat[2 * k + 2] |= dat[k]; } } public void update_node(int k){ dat[k] = dat[2 * k + 1] & dat[2 * k + 2]; } public void update(long v, int a, int b, int k, int l, int r){ lazy_evaluate_node(k, l, r); if(r <= a || b <= l){ return; }else if(a <= l && r <= b){ dat[k] |= v; lazy_evaluate_node(k, l, r); }else { update(v, a, b, k * 2 + 1, l, (l + r) / 2); update(v, a, b, k * 2 + 2, (l + r) / 2, r); update_node(k); } } public long query(int a, int b, int k, int l, int r){ lazy_evaluate_node(k, l, r); //System.out.println(Arrays.toString(dat)); if(r <= a || b <= l){ return Long.MAX_VALUE; } else if(a <= l && r <= b){ return dat[k]; } else { long left = query(a, b, k * 2 + 1, l, (l + r) / 2); long right = query(a, b, k * 2 + 2, (l + r) / 2, r); update_node(k); return left & right; } } public long query(int a, int b){ return query(a, b, 0, 0, n); } public int size(){ return this.n; } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int M = sc.nextInt(); SegTree seg = new SegTree(N); int[] ls = new int[M]; int[] rs = new int[M]; long[] qs = new long[M]; //System.out.println(seg.query(0, N)); for(int i = 0; i < M; i++){ ls[i] = sc.nextInt() - 1; rs[i] = sc.nextInt() - 1; qs[i] = sc.nextLong(); } for(int i = 0; i < M; i++){ final int l = ls[i]; final int r = rs[i]; final long q = qs[i]; seg.update(q, l, r + 1, 0, 0, seg.n); } boolean flag = true; for(int i = 0; i < M; i++){ final int l = ls[i]; final int r = rs[i]; final long q = qs[i]; final long act_q = seg.query(l, r + 1); //System.out.println(act_q); if(q != act_q){ flag = false; break; } } if(flag){ System.out.println("YES"); for(int i = 0; i < N; i++){ final long ret = seg.query(i, i + 1); if(i != 0){ System.out.print(" "); } System.out.print(ret < 0 ? 0 : ret); } System.out.println(); }else{ System.out.println("NO"); } sc.close(); } public static class Scanner { private BufferedReader br; private StringTokenizer tok; public Scanner(InputStream is) throws IOException { br = new BufferedReader(new InputStreamReader(is)); } private void getLine() throws IOException { while (!hasNext()) { tok = new StringTokenizer(br.readLine()); } } private boolean hasNext() { return tok != null && tok.hasMoreTokens(); } public String next() throws IOException { getLine(); return tok.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } // 他のnextXXXもXXX.parseXXX()メソッドを使って作れるので省略 public void close() throws IOException { br.close(); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import static java.lang.Math.*; import static java.lang.System.currentTimeMillis; import static java.lang.System.exit; import static java.lang.System.arraycopy; import static java.util.Arrays.sort; import static java.util.Arrays.binarySearch; import static java.util.Arrays.fill; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); private void run() throws IOException { if (new File("input.txt").exists()) in = new BufferedReader(new FileReader("input.txt")); else in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } int M = 30; private void solve() throws IOException { int n = nextInt(); int m = nextInt(); int a[][] = new int[n][M]; Ev e[] = new Ev[m * 2]; for (int i = 0; i < m; i++) { int ms[] = new int[M]; int l = nextInt() - 1; int r = nextInt() - 1; int q = nextInt(); for (int j = 0; j < M; j++) { ms[j] = ((q & (1 << j)) == 0 ? 0 : 1); } // System.err.println(q); e[i * 2] = new Ev(l, 0, ms, r); e[i * 2 + 1] = new Ev(r, 1, ms, 0); } sort(e); int cur[] = new int[M]; int p = 0; for (int i = 0; i < n; i++) { while ((p < m * 2) && (e[p].time == i && e[p].type == 0)) { for (int j = 0; j < M; j++) cur[j] += e[p].a[j]; p++; } // System.err.println(cur[0]); for (int j = 0; j < M; j++) a[i][j] = (cur[j] > 0) ? 1 : 0; while ((p < m * 2) && (e[p].time == i && e[p].type == 1)) { for (int j = 0; j < M; j++) cur[j] -= e[p].a[j]; p++; } } // System.err.println(p); int nz[][] = new int[n][M]; fill(cur, n); for (int i = n - 1; i > -1; i--) { for (int j = 0; j < M; j++) if (a[i][j] == 0) cur[j] = i; for (int j = 0; j < M; j++) nz[i][j] = cur[j]; } boolean good = true; for (int i = 0; i < 2 * m; i++) if (e[i].type == 0) { for (int j = 0; j < M; j++) if (e[i].a[j] == 0) { if (nz[e[i].time][j] > e[i].r) good = false; } } if (!good) { out.println("NO"); return; } out.println("YES"); for (int i = 0; i < n; i++) { int c = 0; for (int j = 0; j < M; j++) c += (a[i][j] << j); out.print(c + " "); } } class Ev implements Comparable<Ev>{ int time; int type; int a[]; int r; Ev(int tm, int tp, int a[], int r) { time = tm; type = tp; this.a = a; this.r = r; } @Override public int compareTo(Ev o) { if (this.time == o.time) return this.type - o.type; return this.time - o.time; } } void chk(boolean b) { if (b) return; System.out.println(new Error().getStackTrace()[1]); exit(999); } void deb(String fmt, Object... args) { System.out.printf(Locale.US, fmt + "%n", args); } String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int inf = 1e9; struct Node { Node *l = 0, *r = 0; int lo, hi, mset = 0, val = 0; Node(int lo, int hi) : lo(lo), hi(hi) { if (lo < hi) { int mid = lo + (hi - lo) / 2; l = new Node(lo, mid); r = new Node(mid + 1, hi); } } int query(int L, int R) { if (R < lo || hi < L) return 0xFFFFFFFF; if (L <= lo && hi <= R) { return val; } push(); return l->query(L, R) & r->query(L, R); } void set(int L, int R, int x) { if (R < lo || hi < L) return; if (L <= lo && hi <= R) { mset |= x; } else { push(); l->set(L, R, x); r->set(L, R, x); val = l->val & r->val; } } void push() { if (!l and hi > lo) { int mid = lo + (hi - lo) / 2; l = new Node(lo, mid); r = new Node(mid, hi); } if (mset) { if (l) { l->set(lo, hi, mset); r->set(lo, hi, mset); } if (hi == lo) val = mset; } } void prepare() { push(); if (l) { l->prepare(); r->prepare(); val = l->val & r->val; } else val = mset; } void fill(vector<int>& a) { if (lo == hi) a[lo] = val; else { l->fill(a); r->fill(a); } } void print() { cout << lo << " " << hi << " " << val << " " << mset << "\n"; if (l) { l->print(); r->print(); } } }; struct query { int l, r, q; }; void fail() { cout << "NO" << "\n"; exit(0); } int32_t main() { int n, m; cin >> n >> m; vector<query> q(m); Node* tr = new Node(0, n - 1); for (auto& x : q) { cin >> x.l >> x.r >> x.q; x.l--; x.r--; tr->set(x.l, x.r, x.q); } tr->prepare(); for (auto& x : q) { if (tr->query(x.l, x.r) != x.q) { fail(); } } vector<int> res(n); tr->fill(res); cout << "YES" << "\n"; for (int i : res) cout << 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.File; import java.io.FileNotFoundException; import java.util.Scanner; public class main4 { // static Scanner sc = null; static Scanner sc = new Scanner(System.in); static class Tree { int[] d = null; int n; public Tree(int n) { this.d = new int[100 * 100000]; this.n = n; } private void internalSet(int root, int cl, int cr, int l, int r, int v) { if (cr < l || cl > r) return; if (cl >= l && cr <= r) { d[root] |= v; return; } int mid = (cl + cr) / 2; internalSet(2 * root + 1, cl, mid, l, r, v); internalSet(2 * root + 2, mid + 1, cr, l, r, v); } private int internalGet(int root, int cl, int cr, int l, int r) { if (cr < l || cl > r) { return -1; } if (cl >= l && cr <= r) { return d[root]; } int ans = d[root]; int mid = (cl + cr) / 2; int v1 = internalGet(2 * root + 1, cl, mid, l, r); int v2 = internalGet(2 * root + 2, mid + 1, cr, l, r); int ans2 = -1; if (v1 != -1) { if (ans2 == -1) ans2 = v1; else ans2 &= v1; } if (v2 != -1) { if (ans2 == -1) ans2 = v2; else ans2 &= v2; } if (ans2 == -1) return ans; return ans2 | ans; } private int internalGetValue(int root, int cl, int cr, int pos) { if (pos < cl || pos > cr) { return -1; } if (cl == cr) { return d[root]; } int ans = d[root]; int mid = (cl + cr) / 2; int v1 = internalGetValue(2 * root + 1, cl, mid, pos); int v2 = internalGetValue(2 * root + 2, mid + 1, cr, pos); if (v1 != -1) { ans |= v1; } if (v2 != -1) { ans |= v2; } return ans; } public void set(int l, int r, int v) { internalSet(0, 0, n - 1, l - 1, r - 1, v); } public int get(int l, int r) { return internalGet(0, 0, n - 1, l - 1, r - 1); } public int getValueAt(int pos) { return internalGetValue(0, 0, n - 1, pos); } } public static void main(String[] args) { // try { // sc = new Scanner(new // File("/home/piotr/programming/workspace_java/CF/src/in")); // } catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // int n = ni(); int m = ni(); Tree tree = new Tree(n); int[][] q = new int[m][3]; for (int i = 0; i < m; i++) { q[i] = new int[] { ni(), ni(), ni() }; } for (int i = 0; i < m; i++) { tree.set(q[i][0], q[i][1], q[i][2]); } for (int i = 0; i < m; i++) { int v = tree.get(q[i][0], q[i][1]); if (v != q[i][2]) { // if (m < 10) { System.out.println("NO"); // } else // System.out.println("NO " + v + " " + i); return; } // else System.out.println("YES " + v); } StringBuilder ans = new StringBuilder(); ans.append(tree.getValueAt(0)); for (int i = 1; i < n; i++) { ans.append(" " + tree.getValueAt(i)); } System.out.println("YES"); System.out.println(ans.toString()); } private static int ni() { return sc.nextInt(); } private static long nl() { return sc.nextLong(); } private static String ns() { return sc.next(); } private static String line() { return sc.nextLine(); } }
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 = 200100, M = 33; int n, m, size; struct Sgmtree { int son[2], bit[M]; }; Sgmtree sgm[N]; int ans[N], cover[N][M], l[N], r[N], q[N]; void maintain(int node) { int sl = sgm[node].son[0], sr = sgm[node].son[1]; for (int i = 0; i <= 31; i++) sgm[node].bit[i] = sgm[sl].bit[i] + sgm[sr].bit[i]; } void build(int& node, int l, int r) { node = ++size; if (l == r) { int v = ans[l], pos = 0; while (v) { if (v & 1) sgm[node].bit[pos] = 1; v >>= 1; pos++; } return; } int mid = (l + r) >> 1; build(sgm[node].son[0], l, mid); build(sgm[node].son[1], mid + 1, r); maintain(node); } int asktot(int node, int l, int r, int a, int b, int c) { if ((a <= l) && (b >= r)) return sgm[node].bit[c]; int mid = (l + r) >> 1; int t1 = 0, t2 = 0; if (a <= mid) t1 = asktot(sgm[node].son[0], l, mid, a, b, c); if (b > mid) t2 = asktot(sgm[node].son[1], mid + 1, r, a, b, c); return t1 + t2; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &l[i], &r[i], &q[i]); int pos = 0, v = q[i]; while (v) { if (v & 1) { cover[l[i]][pos] += 1; cover[r[i] + 1][pos] -= 1; } pos++; v >>= 1; } } for (int i = 1; i <= n; i++) for (int j = 31; j >= 0; j--) { cover[i][j] += cover[i - 1][j]; ans[i] = (ans[i] << 1) + (cover[i][j] > 0); } int node; build(node, 1, n); for (int i = 1; i <= m; i++) for (int j = 0; j <= 31; j++) { if ((!(q[i] & 1)) && (asktot(1, 1, n, l[i], r[i], j) == (r[i] - l[i] + 1))) { printf("NO\n"); return 0; } q[i] >>= 1; } printf("YES\n"); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); printf("\n"); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; import java.util.*; public class Main { // main public static void main(String [] args) throws IOException { // makes the reader and writer BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // read in StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int[][] queries = new int[m][3]; for (int i=0;i<m;i++) { st = new StringTokenizer(f.readLine()); queries[i] = new int[]{Integer.parseInt(st.nextToken())-1,Integer.parseInt(st.nextToken())-1, Integer.parseInt(st.nextToken())}; } // sort by starting point Arrays.sort(queries,new Comparator<int[]>() { public int compare(int[] a, int[] b) { return (new Integer(a[0])).compareTo(b[0]); } }); // for each bit set settings and check int[] arr = new int[n]; for (int i=0;i<30;i++) { ArrayList<int[]> queriesY = new ArrayList<int[]>(); ArrayList<int[]> queriesN = new ArrayList<int[]>(); for (int j=0;j<m;j++) { if ((queries[j][2]&(1<<i))!=0) queriesY.add(new int[]{queries[j][0],queries[j][1]}); else queriesN.add(new int[]{queries[j][0],queries[j][1]}); } // make intervals disjoint and add ones int prev = 0; int[] nums = new int[n]; for (int[] span: queriesY) { if (span[0]<prev) { if (span[1]<prev) continue; else for (int j=prev;j<=span[1];j++) nums[j] = 1; } else for (int j=span[0];j<=span[1];j++) nums[j] = 1; prev = span[1]+1; } // prefix sums int[] ones = new int[n+1]; for (int j=1;j<=n;j++) ones[j] = ones[j-1]+nums[j-1]; // check whether at least one 0 for each 0 interval for (int[] span: queriesN) { if (ones[span[1]+1]-ones[span[0]]==span[1]-span[0]+1) { out.println("NO"); out.close(); System.exit(0); } } // add to arr for (int j=0;j<n;j++) arr[j]+=nums[j]*(1<<i); } // write to out out.println("YES"); for (int i=0;i<n;i++) { out.print(arr[i]); out.print(" "); } out.println(); // cleanup out.close(); System.exit(0); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const long long inf = 1e15, mod = 1e9 + 7; struct query { int l, r, x; }; query q[100005]; int m, n, st[4 * 100005], lazy[4 * 100005]; void update(int node, int l, int r, int ql, int qr, int val) { if (lazy[node]) { st[node] |= lazy[node]; if (l != r) { lazy[2 * node + 1] |= lazy[node]; lazy[2 * node + 2] |= lazy[node]; } lazy[node] = 0; } if (l > r or ql > r or l > qr) return; if (qr >= r and l >= ql) { st[node] |= val; if (l != r) { lazy[2 * node + 1] |= val; lazy[2 * node + 2] |= val; } return; } update(2 * node + 1, l, (l + r) / 2, ql, qr, val); update(2 * node + 2, (l + r) / 2 + 1, r, ql, qr, val); st[node] = (st[node * 2 + 1] & st[2 * node + 2]); } int query(int node, int l, int r, int ql, int qr) { if (lazy[node]) { st[node] |= lazy[node]; if (l != r) { lazy[2 * node + 1] |= lazy[node]; lazy[2 * node + 2] |= lazy[node]; } lazy[node] = 0; } if (l > r or ql > r or l > qr) return (1 << 30) - 1; if (qr >= r and l >= ql) return st[node]; return query(2 * node + 1, l, (l + r) / 2, ql, qr) & query(2 * node + 2, (l + r) / 2 + 1, r, ql, qr); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; q[i].l = a - 1; q[i].r = b - 1; q[i].x = c; update(0, 0, n - 1, a - 1, b - 1, c); } bool flag = true; for (int i = 1; i <= m; i++) { if (query(0, 0, n - 1, q[i].l, q[i].r) != q[i].x) { flag = false; break; } } if (flag) { cout << "YES" << endl; for (int i = 0; i < n; i++) cout << query(0, 0, n - 1, i, i) << " "; } else { cout << "NO" << endl; return 0; } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const long long nmax = 100011; long long n, m, l[nmax], r[nmax], q[nmax]; long long a[nmax][32], ans[nmax][32]; void nhapdl() { cin >> n >> m; for (long long i = 1; i <= m; ++i) { cin >> l[i] >> r[i] >> q[i]; for (long long j = 0; j <= 30; ++j) if (q[i] & (1 << j)) { ++a[l[i]][j]; --a[r[i] + 1][j]; } } for (long long i = 0; i <= 30; i++) { for (long long j = 1; j <= n; ++j) a[j][i] += a[j - 1][i]; for (long long j = 1; j <= n; ++j) { a[j][i] = !!(a[j][i]); ans[j][i] = a[j][i]; } } } void xuli() { long long d, c, cc; for (long long i = 0; i <= 30; ++i) for (long long j = 1; j <= n; ++j) a[j][i] += a[j - 1][i]; for (long long i = 1; i <= m; ++i) { for (long long j = 0; j <= 30; ++j) if (!(q[i] & (1 << j))) { if (a[r[i]][j] - a[l[i] - 1][j] == r[i] - l[i] + 1) { cout << "NO"; return; } } } cout << "YES" << endl; for (long long i = 1; i <= n; ++i) { cc = 0; for (long long j = 0; j <= 30; ++j) cc += ans[i][j] * (1 << j); cout << cc << ' '; } } int main() { nhapdl(); xuli(); }
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; inline int POPCNT(int _x) { return __builtin_popcount(_x); } inline int POPCNT(long long _x) { return __builtin_popcountll(_x); } const int iINF = 1L << 30; const long long lINF = 1LL << 60; double EPS = 1e-9; inline bool in_range(int _v, int _mx, int _mi) { return _mi <= _v && _v < _mx; } inline bool in_range(double _v, double _mi, double _mx) { return -EPS < _v - _mi && _v - _mx < EPS; } inline bool in_range(int _x, int _y, int _W, int _H) { return 0 <= _x && _x < _W && 0 <= _y && _y < _H; } const int DX[4] = {0, 1, 0, -1}, DY[4] = {-1, 0, 1, 0}; const int DX_[8] = {0, 1, 1, 1, 0, -1, -1, -1}, DY_[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dINF = iINF - 1; struct SegmentTreeL { int st_size; vector<int> data; vector<int> lazy; SegmentTreeL(int _size, int _init_val) { st_size = 1; while (st_size < _size) st_size *= 2; data.assign(2 * st_size - 1, _init_val); lazy.assign(2 * st_size - 1, 0); } int calculation(int _d1, int _d2) { return _d1 & _d2; } void lazy_calculation(int _k, int _val) { lazy[_k] |= _val; } inline void lazy_evaluate(int _a, int _b, int _k) { data[_k] |= lazy[_k]; if (_k < st_size - 1) { lazy_calculation(2 * _k + 1, lazy[_k]); lazy_calculation(2 * _k + 2, lazy[_k]); } lazy[_k] = 0; } inline void update_at(int _k) { data[_k] = calculation(data[2 * _k + 1], data[2 * _k + 2]); } void update(int _a, int _b, int _x) { update(_a, _b, _x, 0, 0, st_size); } void update(int _a, int _b, int _x, int _k, int _l, int _r) { lazy_evaluate(_a, _b, _k); if (_r <= _a || _b <= _l) return; if (_a <= _l && _r <= _b) { lazy_calculation(_k, _x); lazy_evaluate(_a, _b, _k); return; } update(_a, _b, _x, 2 * _k + 1, _l, (_l + _r) / 2); update(_a, _b, _x, 2 * _k + 2, (_l + _r) / 2, _r); update_at(_k); } int query(int _a, int _b) { return query(_a, _b, 0, 0, st_size); } int query(int _a, int _b, int _k, int _l, int _r) { lazy_evaluate(_a, _b, _k); if (_r <= _a || _b <= _l) return dINF; if (_a <= _l && _r <= _b) return data[_k]; int res = calculation(query(_a, _b, 2 * _k + 1, _l, (_l + _r) / 2), query(_a, _b, 2 * _k + 2, (_l + _r) / 2, _r)); update_at(_k); return res; } int size() { return st_size; } }; int n, m; struct Constraint { int l, r, q; Constraint(int _l, int _r, int _q) : l(_l), r(_r), q(_q) {} }; vector<Constraint> vc; void solve() { SegmentTreeL mul_seg(n, 0); for (int i = 0; i < m; ++i) { int l, r, q; cin >> l >> r >> q; --l; --r; vc.push_back(Constraint(l, r, q)); mul_seg.update(l, r + 1, q); } for (int i = 0; i < m; ++i) { Constraint c = vc[i]; int val = mul_seg.query(c.l, c.r + 1); if (val != c.q) { cout << "NO" << endl; return; } } cout << "YES" << endl; for (int i = 0; i < n; ++i) { if (i < n - 1) cout << mul_seg.query(i, i + 1) << " "; else cout << mul_seg.query(i, i + 1) << endl; } } int main() { cin >> n >> m; 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.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { static class Reader { BufferedReader r; StringTokenizer str; Reader() { r=new BufferedReader(new InputStreamReader(System.in)); } Reader(String fileName) throws FileNotFoundException { r=new BufferedReader(new FileReader(fileName)); } public String getNextToken() throws IOException { if(str==null||!str.hasMoreTokens()) { str=new StringTokenizer(r.readLine()); } return str.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(getNextToken()); } public long nextLong() throws IOException { return Long.parseLong(getNextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(getNextToken()); } public String nextString() throws IOException { return getNextToken(); } public int[] intArray(int n) throws IOException { int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } public long[] longArray(int n) throws IOException { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } public String[] stringArray(int n) throws IOException { String a[]=new String[n]; for(int i=0;i<n;i++) a[i]=nextString(); return a; } public int gcd(int a, int b) { if(b == 0){ return a; } return gcd(b, a%b); } } public static void main(String args[]) throws IOException{ Reader r=new Reader(); PrintWriter pr=new PrintWriter(System.out,false); int n=r.nextInt(); int m=r.nextInt(); int a[]=new int[n]; int l[]=new int[m]; int R[]=new int[m]; int k[]=new int[m]; int tree[][]=new int[4*n][2]; for(int i=0;i<m;i++) { l[i]=r.nextInt()-1;R[i]=r.nextInt()-1;k[i]=r.nextInt(); } for(int i=0;i<m;i++) { update(tree,1,0,n-1,l[i],R[i],k[i]); } int t[]=new int[4*n]; traverse(a,tree,1,0,n-1); // construct(a,t,1,0,n-1); for(int i=0;i<m;i++) { if(query(tree,1,0,n-1,l[i],R[i])!=k[i]) { pr.println("NO"); pr.flush(); pr.close(); return; } } pr.println("YES"); for(int i=0;i<n;i++) { pr.print(a[i]+" "); } pr.println(); pr.flush(); pr.close(); } public static void traverse(int a[],int tree[][],int root,int l,int r) { if(tree[root][1]!=0) { tree[root][0]|=tree[root][1]; if(l!=r) { tree[root<<1][1]|=tree[root][1]; tree[(root<<1)+1][1]|=tree[root][1]; } tree[root][1]=0; } if(l==r) { a[l]=tree[root][0]; return; } traverse(a,tree,root<<1,l,(l+r)>>1); traverse(a,tree,(root<<1)+1,((l+r)>>1)+1,r); tree[root][0]=tree[root<<1][0]&tree[(root<<1)+1][0]; } public static void construct(int a[],int tree[], int root,int l ,int r) { if(l==r) { tree[root]=a[l]; return; } construct(a,tree,root<<1,l,(l+r)>>1); construct(a,tree,(root<<1)+1,((l+r)>>1)+1,r); tree[root]=tree[root<<1]&tree[(root<<1)+1]; } public static void update(int tree[][],int root,int l,int r,int begin,int end,int value) { if(tree[root][1]!=0) { tree[root][0]|=tree[root][1]; if(l!=r) { tree[root<<1][1]|=tree[root][1]; tree[(root<<1)+1][1]|=tree[root][1]; } tree[root][1]=0; } if(begin>r||end<l) return; if(begin<=l&&r<=end) { tree[root][0]|=value; if(l!=r) { tree[root<<1][1]|=value; tree[(root<<1)+1][1]|=value; } return; } update(tree,root<<1,l,(l+r)>>1,begin,end,value); update(tree,(root<<1)+1,((l+r)>>1)+1,r,begin,end,value); } static int query(int tree[][],int root,int l,int r,int begin,int end) { if(begin>r||end<l) return Integer.MAX_VALUE; if(begin<=l&&r<=end) { return tree[root][0]; } return query(tree,root<<1,l,(l+r)>>1,begin,end)&query(tree,(root<<1)+1,((l+r)>>1)+1,r,begin,end); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000") using namespace std; const double pi = acos(-1.0); const int rms = (1 << 18) - 1; const int hrms = rms / 2; const int size = 300 * 1000 + 10; int rmq[rms + 1]; int n, m; int lb[size], rb[size], q[size]; int val[size]; void change(int v, int lb, int rb, int i, int j, int val) { if (lb > j || rb < i) return; if (lb >= i && rb <= j) { rmq[v] |= val; return; } change(v * 2, lb, (lb + rb) / 2, i, j, val); change(v * 2 + 1, (lb + rb) / 2 + 1, rb, i, j, val); } int rss(int v, int lb, int rb, int i, int j) { if (lb > j || rb < i) { return (1 << 30) - 1; } if (lb >= i && rb <= j) { return rmq[v]; } return rss(v * 2, lb, (lb + rb) / 2, i, j) & rss(v * 2 + 1, (lb + rb) / 2 + 1, rb, i, j); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d%d", &lb[i], &rb[i], &q[i]); change(1, 1, hrms + 1, lb[i], rb[i], q[i]); } for (int i = 0; i < n; i++) { int p = i + 1 + hrms; int v = 0; while (p > 0) { v |= rmq[p]; p /= 2; } val[i] = v; } for (int i = 0; i <= rms; i++) rmq[i] = 0; for (int i = 0; i < n; i++) { int p = i + 1 + hrms; rmq[p] = val[i]; } for (int i = hrms; i > 0; i--) { rmq[i] = rmq[i * 2] & rmq[i * 2 + 1]; } bool flag = true; for (int i = 0; i < m; i++) { if (rss(1, 1, hrms + 1, lb[i], rb[i]) != q[i]) flag = false; } if (flag) { printf("YES\n"); for (int i = 0; i < n; i++) printf("%d%c", val[i], " \n"[i == n - 1]); } else { printf("NO\n"); } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; const long long INF64 = 1e18; const long long MOD = 1e9 + 7; const long long MOD9 = 1e9 + 9; const long long MOD3 = 998244353; const long long P = 37; const long long mxn = 501; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); struct SegTree { long long neu = (1ll << 31) - 1; long long m = 1; vector<long long> t; SegTree(vector<long long>* a) { while (m <= a->size()) m <<= 1; t.assign(2 * m, neu); for (long long i = m; i < m + a->size(); ++i) { t[i] = a->at(i - m); } for (long long j = m - 1; j > 0; --j) { t[j] = t[j << 1] & t[(j << 1) + 1]; } } long long aand(long long l, long long r) { return aand(1, l, r, 1, m); } long long aand(long long v, long long l, long long r, long long tl, long long tr) { if (tl > r || tr < l) return neu; if (tl >= l && tr <= r) return t[v]; long long mid = (tl + tr) >> 1; return aand(v << 1, l, r, tl, mid) & aand((v << 1) + 1, l, r, mid + 1, tr); } }; vector<long long> x(31, 0); signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, m; cin >> n >> m; vector<pair<pair<long long, long long>, long long>> v(m); vector<pair<long long, long long>> q; for (long long i = 0; i < m; ++i) { long long a, b, c; cin >> a >> b >> c; q.push_back({a, -c}); q.push_back({b, c}); v[i].first.first = a; v[i].first.second = b; v[i].second = c; } sort(q.begin(), q.end()); vector<long long> a(n + 1, 0); long long u = 0; long long prev = 0; a[0] = (1ll << 31) - 1; for (long long j = 0; j < q.size(); ++j) { long long t = q[j].first; long long p = q[j].second; bool f = p < 0; for (long long k = prev + 1; k < (t + !f); ++k) { a[k] |= u; } prev = t - f; if (f) { p = -p; for (long long i = 0; i <= 30; ++i) { if ((1ll << i) & p) x[i]++; if (x[i] == 1) { u |= (1ll << i); } } } else if (!f) { for (long long i = 0; i <= 30; ++i) { if ((1ll << i) & p) x[i]--; if (x[i] == 0) { u &= ~(1ll << i); } } } } rotate(a.begin(), a.begin() + 1, a.end()); SegTree tree(&a); for (long long l = 0; l < v.size(); ++l) { long long r = tree.aand(v[l].first.first, v[l].first.second); if (r != v[l].second) { cout << "NO"; return 0; } } cout << "YES\n"; for (long long i = 0; i < n; ++i) { cout << a[i] << " "; } }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const long long N = 100001; const long long c = (1ll << 30) - 1; long long A[32][N]; long long st[N << 2]; pair<pair<long long, long long>, long long> par[N]; void build(long long l, long long r, long long nod) { if (l == r) { long long aux = 0; for (long long i = 0; i < 30; i++) { if (A[i][l] != 0) { aux += (1ll << i); } } st[nod] = aux; return; } long long med = (l + r) / 2; build(l, med, nod * 2); build(med + 1, r, nod * 2 + 1); st[nod] = (st[nod * 2] & st[nod * 2 + 1]); return; } long long query(long long ini, long long fin, long long l, long long r, long long nod) { if (fin < l || r < ini) return c; if (ini <= l && r <= fin) return st[nod]; long long med = (l + r) / 2; long long L = query(ini, fin, l, med, nod * 2); long long R = query(ini, fin, med + 1, r, nod * 2 + 1); return (L & R); } int main() { long long n, q; scanf("%lld %lld", &n, &q); long long l, r, val; for (long long i = 0; i < q; i++) { scanf("%lld %lld %lld", &l, &r, &val); par[i] = make_pair(make_pair(l - 1, r - 1), val); for (long long j = 0; j < 30; j++) { if ((val >> j) & 1 == 1) { A[j][l - 1]++; A[j][r]--; } } } for (long long i = 0; i < 30; i++) { for (long long j = 1; j < n; j++) { A[i][j] = A[i][j] + A[i][j - 1]; } } build(0, n - 1, 1); bool ok = true; for (long long i = 0; i < q; i++) { if (query(par[i].first.first, par[i].first.second, 0, n - 1, 1) != par[i].second) { ok = false; break; } } if (ok) { printf("YES\n"); for (long long i = 0; i < n; i++) { printf("%lld ", query(i, i, 0, n - 1, 1)); } printf("\n"); } else { printf("NO\n"); } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; int n, m, tree[400001], p = 131072, tree2[400001], su[100001]; bool check[400001]; void ins(int le, int ri, int c) { while (le <= ri) { if (le & 1) tree[le++] |= c; if (!(ri & 1)) tree[ri--] |= c; le >>= 1; ri >>= 1; } } int find(int a) { int s = 0; while (a != 0) { s |= tree[a]; a >>= 1; } return s; } int find2(int le, int ri) { int ans = -1; while (le <= ri) { if (le & 1) { if (ans == -1) ans = tree2[le]; ans &= tree2[le++]; } if (!(ri & 1)) { if (ans == -1) ans = tree2[ri]; ans &= tree2[ri--]; } le >>= 1; ri >>= 1; } return ans; } int save[100001][3]; int main() { int i, j, a, b, c; scanf("%d %d", &n, &m); for (i = 0; i < m; i++) { scanf("%d %d %d", &a, &b, &c); a--; b--; save[i][0] = a; save[i][1] = b; save[i][2] = c; ins(a + p, b + p, c); } for (i = 0; i < n; i++) { su[i] = find(i + p); tree2[i + p] = su[i]; check[i + p] = 1; } for (i = p - 1; i >= 0; i--) { if (check[i * 2] && check[i * 2 + 1]) { tree2[i] = tree2[i * 2] & tree2[i * 2 + 1]; check[i] = 1; } else if (check[i * 2]) { tree2[i] = tree2[i * 2]; check[i] = 1; } } for (i = 0; i < m; i++) { if (find2(save[i][0] + p, save[i][1] + p) != save[i][2]) break; } if (i == m) { printf("YES\n"); for (i = 0; i < n; i++) printf("%d ", su[i]); } else printf("NO\n"); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class R275QDInterestingArray { static int ans[] = new int[100005]; static int segtree[] = new int[4 * 100005]; static int andtree[] = new int[4 * 100005]; public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter w = new PrintWriter(System.out); StringTokenizer st1 = new StringTokenizer(br.readLine()); int n = ip(st1.nextToken()); int m = ip(st1.nextToken()); int ll[] = new int[m]; int rr[] = new int[m]; int qq[] = new int[m]; for (int i = 0; i < m; i++) { StringTokenizer st2 = new StringTokenizer(br.readLine()); ll[i] = ip(st2.nextToken()) - 1; rr[i] = ip(st2.nextToken()) - 1; qq[i] = ip(st2.nextToken()); update(0, n - 1, 0, ll[i], rr[i], qq[i]); } //all numbers are generated , hence now no lazy propogation in tree for(int i=0;i<n;i++) ans[i] = getNumber(0,n-1,0,i); //and tree built init(0,n-1,0); boolean check = true; for(int i=0;i<m;i++){ if(query(0,n-1,0,ll[i],rr[i]) != qq[i]){ check = false; break; } } if (check == true) { w.println("YES"); for (int i = 0; i < n; i++) w.printf("%d ", ans[i]); w.println(); } else w.println("NO"); w.close(); } static void update(int start,int endd,int curr,int l,int r, int q) { if (start == l && endd == r) { segtree[curr] |= q; return; } int mid = (start + endd) / 2; if (l <= mid && r <= mid) { // segtree[2*curr+1] |= segtree[curr]; update(start, mid, 2 * curr + 1, l, r, q); return; } if (l > mid && r > mid) { //segtree[2*curr+2] |= segtree[2*curr+1]; update(mid + 1, endd, 2 * curr + 2, l, r, q); return; } if (l <= mid && r > mid) { //segtree[2*curr+1] |= segtree[curr]; //segtree[2*curr+2] |= segtree[curr]; update(start, mid, 2 * curr + 1, l, mid, q); update(mid + 1, endd, 2 * curr + 2, mid + 1, r, q); } } static int getNumber(int start,int end,int curr,int num){ if(start==end){ return segtree[curr]; } int mid = (start+end)/2; if(num <= mid){ segtree[2*curr+1] |= segtree[curr]; return getNumber(start,mid,2*curr+1,num); } else{ segtree[2*curr+2] |= segtree[curr]; return getNumber(mid+1,end,2*curr+2,num); } } static void init(int start,int endd,int curr){ if(start==endd){ andtree[curr] = ans[start]; return; } int mid = (start+endd)/2; init(start,mid,2*curr+1); init(mid+1,endd,2*curr+2); andtree[curr] = andtree[2*curr+1] & andtree[2*curr+2]; } static int query(int start, int endd, int curr, int l, int r) { if (start == l && endd == r) return andtree[curr]; int mid = (start + endd) / 2; if (l <= mid && r <= mid) return query(start, mid, 2 * curr + 1, l, r); if (l > mid && r > mid) return query(mid + 1, endd, 2 * curr + 2, l, r); if (l <= mid && r > mid) { int t1 = query(start, mid, 2 * curr + 1, l, mid); int t2 = query(mid + 1, endd, 2 * curr + 2, mid + 1, r); return t1 & t2; } return 0; } public static int ip(String s) { return Integer.parseInt(s); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1000005; long long T[N << 2]; long long L[N << 2]; void refresh(int x) { T[x] |= L[x]; L[2 * x] |= L[x]; L[2 * x + 1] |= L[x]; L[x] = 0; } void update(int l, int r, int x, int a, int b, long long v) { refresh(x); if (l > b || r < a || l > r) return; if (l >= a && r <= b) { L[x] |= v; return; } int mid = (l + r) / 2; update(l, mid, x * 2, a, b, v); update(mid + 1, r, x * 2 + 1, a, b, v); T[x] = T[x * 2] & T[x * 2 + 1]; } long long query(int l, int r, int x, int a, int b) { refresh(x); if (l > b || r < a || l > r) return (1LL << 50) - 1; ; if (l >= a && r <= b) return T[x]; int mid = (l + r) / 2; long long q1 = query(l, mid, x * 2, a, b); long long q2 = query(mid + 1, r, x * 2 + 1, a, b); return q1 & q2; } long long Q[100009][3]; int main() { ios_base::sync_with_stdio(0); int n, m; while (cin >> n >> m) { memset(T, 0, sizeof(T)); memset(L, 0, sizeof(L)); for (int i = (0); i < (m); i++) for (int j = (0); j < (3); j++) cin >> Q[i][j]; for (int i = (0); i < (m); i++) update(0, n - 1, 1, Q[i][0] - 1, Q[i][1] - 1, Q[i][2]); bool ok = 1; for (int i = (0); i < (m); i++) ok &= query(0, n - 1, 1, Q[i][0] - 1, Q[i][1] - 1) == Q[i][2]; if (!ok) cout << "NO" << endl; else { cout << "YES" << endl; for (int i = (0); i < (n); i++) cout << query(0, n - 1, 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
import java.io.*; import java.util.*; public class B { static void solve() throws IOException { int n = nextInt(); int m = nextInt(); int[][] v = new int[30][n + 1]; int[] l = new int[m]; int[] r = new int[m]; int[] q = new int[m]; for (int i = 0; i < m; i++) { l[i] = nextInt() - 1; r[i] = nextInt(); q[i] = nextInt(); for (int j = 0; j < 30; j++) { if (((q[i] >> j) & 1) == 1) { v[j][l[i]]++; v[j][r[i]]--; } } } int[] ans = new int[n]; for (int i = 0; i < n; i++) { if (i > 0) { for (int j = 0; j < 30; j++) { v[j][i] += v[j][i - 1]; } } for (int j = 0; j < 30; j++) { if (v[j][i] > 0) { ans[i] |= 1 << j; } } } for (int i = 0; i < n; i++) { set(i, ans[i]); } for (int i = 0; i < m; i++) { if (get(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(ans[i]); } out.println(); } static void set(int v, int x) { v += N; tr[v] = x; while (v > 1) { v >>= 1; tr[v] = tr[v * 2] & tr[v * 2 + 1]; } } static int get(int l, int r) { --r; l += N; r += N; int ans = -1; while (l <= r) { if ((l & 1) == 1) { ans &= tr[l++]; } if ((r & 1) == 0) { ans &= tr[r--]; } l >>= 1; r >>= 1; } return ans; } static final int N = 1 << 17; static int[] tr = new int[N * 2]; static BufferedReader br; static StringTokenizer st; static PrintWriter out; public static void main(String[] args) throws IOException { InputStream input = System.in; PrintStream output = System.out; File file = new File("b.in"); if (file.exists() && file.canRead()) { input = new FileInputStream(file); output = new PrintStream("b.out"); } br = new BufferedReader(new InputStreamReader(input)); out = new PrintWriter(output); solve(); out.close(); br.close(); } static boolean hasNext() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = br.readLine(); if (line == null) { return false; } st = new StringTokenizer(line); } return true; } static String next() throws IOException { return hasNext() ? st.nextToken() : null; } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class D { BufferedReader br; StringTokenizer in; PrintWriter out; public String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public void solve() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(); int m = nextInt(); int [][] constraints = new int[m][3]; int [][] values = new int[n+1][30]; int [] results = new int[n]; for(int i = 0 ; i < m ; i++) { int l = nextInt(); int r = nextInt(); int q = nextInt(); int index = 0; int copy = q; while(q > 0) { values[l-1][index] += q & 1; values[r][index] -= q & 1; index ++; q = q >> 1; } constraints[i][0] = l; constraints[i][1] = r; constraints[i][2] = copy; } int [] passArray = new int[30]; for(int i = 0; i<n ; i++) { for(int j = 0 ; j < values[i].length ; j++) passArray[j] += values[i][j]; int num = 0; for(int j = passArray.length-1 ; j >= 0; j--) { if(passArray[j] > 0) num = num | 1; if(j != 0) num = num << 1; } results[i] = num; } STree st = new STree(results); for(int i = 0 ; i < m ; i++) { int l = constraints[i][0]; int r = constraints[i][1]; int v = constraints[i][2]; int query = st.query(1, 1, n, l, r); if(query != v) { out.println("NO"); out.close(); return; } } out.println("YES"); for(int i = 0 ; i < n ; i++) out.print(results[i]+" "); out.println(); out.close(); } public static void main (String [] args) throws IOException { new D().solve(); } public class STree { int size; int[] values; int[] and; public STree(int [] values) { this(values.length); for(int i = 1 ; i < this.values.length ; i++) this.values[i] = values[i-1]; buildANDTree(1,1,values.length); } public STree(int size) { this.size = size; values = new int[size+1]; and = new int[size<<2]; } public void buildANDTree(int nodeId, int lower, int upper) { if(lower == upper) { and[nodeId] = values[lower]; } else { int nextNode = nodeId << 1; int mid = (lower + upper) >> 1; buildANDTree(nextNode,lower,mid); buildANDTree(nextNode|1,mid+1,upper); and[nodeId] = and[nextNode] & and[nextNode|1]; } } public int getValue(int index) { return this.values[index]; } public int query(int nodeId, int lower, int upper, int queryLower, int queryUpper) { if(queryLower > upper || queryUpper < lower) return -1; if(queryLower <= lower && queryUpper >= upper) { return and[nodeId]; } int nextNode = nodeId << 1; int mid = (lower + upper) / 2; int val1 = query(nextNode, lower, mid, queryLower, queryUpper); int val2 = query(nextNode|1, mid+1, upper, queryLower, queryUpper); if(val1 == -1) return val2; if(val2 == -1) return val1; return val1 & val2; } } }
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 segtree[400001]; int val[100001]; int inf = (1 << 30) - 1; void build(int l, int r, int pos) { if (l == r) { segtree[pos] = val[r]; return; } int mid = (l + r) / 2; build(l, mid, 2 * pos + 1); build(mid + 1, r, 2 * pos + 2); segtree[pos] = (segtree[2 * pos + 1] & segtree[2 * pos + 2]); } int query(int lq, int rq, int l, int r, int pos) { if (rq < l || lq > r) return inf; if (lq <= l && r <= rq) return segtree[pos]; int mid = (l + r) / 2; return query(lq, rq, l, mid, 2 * pos + 1) & query(lq, rq, mid + 1, r, 2 * pos + 2); } int main() { int n; cin >> n; int m; cin >> m; int flag[n + 1][30]; memset(flag, 0, sizeof flag); int l[m], r[m], v[m]; for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> v[i]; l[i]--; r[i]--; for (int j = 0; j < 30; j++) { if (v[i] & (1 << j)) { flag[l[i]][j] += 1; flag[r[i] + 1][j] -= 1; } } } for (int j = 0; j < 30; j++) { for (int i = 1; i < n; i++) { flag[i][j] += flag[i - 1][j]; } } for (int i = 0; i < n; i++) { val[i] = 0; for (int j = 0; j < 30; j++) { if (flag[i][j]) val[i] += (1 << j); } } build(0, n - 1, 0); for (int i = 0; i < m; i++) { if (query(l[i], r[i], 0, n - 1, 0) != v[i]) { cout << "NO"; return 0; } } cout << "YES\n"; for (int j = 0; j < n; j++) cout << val[j] << " "; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const long long INFll = 1ll * 1000000000 * 1000000000; const long double PI = 3.141592653589793238462643383279502884197169399375105820974944; int mul(int a, int b, int mod = 1000000007) { return int(a * 1ll * b % mod); } int norm(int a, int mod = 1000000007) { while (a >= mod) a -= mod; while (a < 0) a += mod; return a; } int powmod(int x, int y, int mod = 1000000007) { int res = 1; while (y > 0) { if (y & 1) res = mul(res, x, mod); x = mul(x, x, mod); y = y >> 1; } return res; } int inv(int a, int mod = 1000000007) { return powmod(a, mod - 2); } vector<int> tree(400005), a(100005); int n, m; void build(int l = 1, int r = n, int pos = 1) { if (l == r) { tree[pos] = a[l]; return; } int mid = (l + r) / 2; build(l, mid, 2 * pos); build(mid + 1, r, 2 * pos + 1); tree[pos] = (tree[2 * pos] & tree[2 * pos + 1]); } int query(int l, int r, int x = 1, int y = n, int pos = 1) { if (x >= l && y <= r) { return tree[pos]; } if (l > y || r < x) { return ((1 << 30) - 1); } int mid = (x + y) / 2; int left = query(l, r, x, mid, 2 * pos); int right = query(l, r, mid + 1, y, 2 * pos + 1); return (left & right); } int main() { cin >> n >> m; vector<int> L(m), R(m), Q(m); vector<vector<int> > c(32, vector<int>(n + 3, 0)); for (int i = 0; i < m; i++) { cin >> L[i] >> R[i] >> Q[i]; for (int j = 0; j < 30; j++) { if (Q[i] & (1 << j)) { c[j][L[i]]++; c[j][R[i] + 1]--; } } } for (int j = 0; j < 30; j++) { for (int i = 1; i <= n; i++) { c[j][i] += c[j][i - 1]; } } for (int i = 1; i <= n; i++) { int res = 0; for (int j = 0; j < 30; j++) { if (c[j][i] > 0) res += (1 << j); } a[i] = res; } build(); for (int i = 0; i < m; i++) { int q = query(L[i], R[i]); if (q == Q[i]) { } else { cout << "NO"; return 0; } } cout << "YES\n"; for (int i = 1; i <= n; i++) { cout << a[i] << ' '; } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; 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 v[] = new int[m + 10]; for (int i = 1; i <= m; i++) { l[i] = sc.nextInt(); r[i] = sc.nextInt(); v[i] = sc.nextInt(); updaterange(1, 1, n, l[i], r[i], v[i]); } boolean f = true; for (int i = 1; i <= m; i++) { if (query(1, 1, n, l[i], r[i]) != v[i]) { f = false; break; } } if (!f) { System.out.println("NO"); } else { System.out.println("YES"); for (int i = 1; i <= n; i++) System.out.print(query(1, 1, n, i, i) + " "); System.out.println(); } } } static int N = 100000; static long tree[] = new long[4 * N + 10]; static long lazy[] = new long[4 * N + 10]; static long query(int node, int start, int end, int left, int right) { if (left <= start && end <= right) { return tree[node]; } pushdown(node, start, end); int mid = (start + end) >> 1; long ans = (2 << 30) - 1; if (left <= mid) { ans &= query(node << 1, start, mid, left, right); } if (right > mid) { ans &= query((node << 1) | 1, mid + 1, end, left, right); } return ans; } static void updaterange(int node, int start, int end, int left, int right, int val) { if (left <= start && end <= right) { tree[node] |= val; lazy[node] |= val; return; } pushdown(node, start, end); int mid = (start + end) >> 1; if (left <= mid) { updaterange(node << 1, start, mid, left, right, val); } if (right > mid) { updaterange((node << 1) | 1, mid + 1, end, left, right, val); } tree[node] = tree[node << 1] & tree[(node << 1) | 1]; } static void pushdown(int node, int start, int end) { if (lazy[node] != 0) { tree[node << 1] |= lazy[node]; tree[(node << 1) | 1] |= lazy[node]; lazy[node << 1] |= lazy[node]; lazy[(node << 1) | 1] |= lazy[node]; lazy[node] = 0; } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CR275_interesting_array { public static void main(String... args) throws IOException { MyScanner sc = new MyScanner(); int n = sc.nextInt(); int m = sc.nextInt(); int[] from = new int[m]; int[] to = new int[m]; int[] eq = new int[m]; int[][] v = new int[n+1][30]; int[][] sums = new int[n][30]; for (int i = 0; i < m; i++) { from[i] = sc.nextInt()-1; to[i] = sc.nextInt()-1; eq[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { for (int j = 0; j < 30; j++) { if ((eq[i] & 1 << j) > 0){ v[from[i]][j]++; v[to[i] + 1][j]--; } } } for (int i = 0; i < 30; i++) { int sum = 0; for (int j = 0; j < n; j++) { sum += v[j][i]; v[j][i] = sum > 0 ? 1 : 0; sums[j][i] = (j == 0 ? 0 : sums[j-1][i]) + v[j][i]; } } for (int i = 0; i < m; i++) { for (int j = 0; j < 30; j++) { if ((eq[i] & 1 << j) == 0){ if ((sums[to[i]][j] - (from[i] == 0 ? 0 : sums[from[i] - 1][j])) == (to[i] - from[i] + 1)){ System.out.println("NO"); return; } } } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { int val = 0; for (int j = 0; j < 30; j++) { if (v[i][j] == 1){ val |= 1 << j; } } sb.append(val).append(' '); } System.out.println("YES"); System.out.println(sb); } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { if (st == null || !st.hasMoreTokens()) { String line = br.readLine(); while (line.isEmpty()){ line = br.readLine(); } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() throws IOException { String next = next(); return Integer.parseInt(next); } private long nextLong() throws IOException { String next = next(); return Long.parseLong(next); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; vector<int> node; struct T { int l, r, col; } tree[maxn << 2]; void pushdown(int rt) { if (tree[rt].l == tree[rt].r) return; tree[rt << 1 | 1].col |= tree[rt].col; tree[rt << 1].col |= tree[rt].col; pushdown(rt << 1 | 1); pushdown(rt << 1); } void build(int L, int R, int rt) { tree[rt].l = L; tree[rt].r = R; tree[rt].col = 0; if (L == R) { node.push_back(rt); return; } int mid = (tree[rt].l + tree[rt].r) >> 1; ; build(L, mid, rt << 1); build(mid + 1, R, rt << 1 | 1); } void update(int l, int r, int rt, int val) { if (tree[rt].l == l && tree[rt].r == r) { tree[rt].col |= val; return; } int mid = (tree[rt].l + tree[rt].r) >> 1; ; if (l > mid) update(l, r, rt << 1 | 1, val); else if (r <= mid) update(l, r, rt << 1, val); else { update(l, mid, rt << 1, val); update(mid + 1, r, rt << 1 | 1, val); } } int query(int l, int r, int rt) { if (tree[rt].l == l && tree[rt].r == r) { return tree[rt].col; } int mid = (tree[rt].l + tree[rt].r) >> 1; ; if (l > mid) return query(l, r, rt << 1 | 1); else if (r <= mid) return query(l, r, rt << 1); return query(l, mid, rt << 1) & query(mid + 1, r, rt << 1 | 1); } struct Qu { int l, r, p; } q[maxn]; int main() { int N, M; int l, r, p; node.clear(); scanf("%d%d", &N, &M); build(1, N, 1); for (int i = 0; i < M; i++) { scanf("%d%d%d", &l, &r, &p); q[i] = (Qu){l, r, p}; update(l, r, 1, p); } bool label = true; for (int i = 0; i < M && label; i++) { int ans = query(q[i].l, q[i].r, 1); if (ans == q[i].p) continue; label = false; } pushdown(1); if (label) { printf("YES\n"); for (int i = 0; i < node.size(); i++) printf("%d%c", tree[node[i]].col, i == node.size() - 1 ? '\n' : ' '); } else { printf("NO\n"); } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") const long long LL_INF = 0x3f3f3f3f3f3f3f3f; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } long long power(long long a, long long b, long long m = 1000000007) { long long answer = 1; while (b) { if (b & 1) answer = (answer * a) % m; b /= 2; a = (a * a) % m; } return answer; } using namespace std; void ctrl() { cout << "Control" << endl; } long long make_num(string p) { stringstream geek(p); long long x = 0; geek >> x; return x; } string make_str(long long x) { ostringstream str1; str1 << x; string geek = str1.str(); return geek; } long long dp[30][100000 + 5]; long long A[100000 + 5]; const long long N = 1e5 + 5; const long long mx = (1 << 30) - 1; long long high[4 * N + 10], low[4 * N + 10]; struct Node { long long gd; Node(long long x) { gd = x; } Node() {} }; struct Node Tree[4 * N + 10]; void init(long long node, long long start, long long end) { high[node] = end; low[node] = start; if (start == end) { return; } long long mid = (start + end) / 2; init(2 * node, start, mid); init(2 * node + 1, mid + 1, end); } struct Node combine(struct Node a, struct Node b) { return Node(a.gd & b.gd); } void build(long long node, long long start, long long end) { if (low[node] == high[node]) { Tree[node].gd = A[low[node]]; return; } long long mid = (start + end) / 2; build(2 * node, start, mid); build(2 * node + 1, mid + 1, end); Tree[node] = combine(Tree[2 * node], Tree[2 * node + 1]); } struct Node query(long long node, long long start, long long end) { if (low[node] > end || high[node] < start || start > end) return Node{mx}; else if (start <= low[node] && end >= high[node]) { return Tree[node]; } struct Node x = query(2 * node, start, end); struct Node y = query(2 * node + 1, start, end); struct Node res = combine(x, y); return res; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); long long n, m; cin >> n >> m; vector<pair<pair<long long, long long>, long long>> v; for (long long i = 0; i < m; i++) { long long l, r, w; cin >> l >> r >> w; v.push_back({{l, r}, w}); for (long long j = 0; j < 30; j++) { long long x = (1 << j); x = x & w; if (x) { dp[j][l]++; dp[j][r + 1]--; } } } for (long long i = 1; i <= n; i++) { for (long long j = 0; j < 30; j++) { dp[j][i] += dp[j][i - 1]; if (dp[j][i]) A[i] += (1 << j); } } init(1, 1, n); build(1, 1, n); bool flag = true; for (long long i = 0; i < m; i++) { long long l = v[i].first.first, r = v[i].first.second; struct Node second = query(1, l, r); if (second.gd != v[i].second) flag = false; } if (flag) { cout << "YES\n"; for (long long i = 1; i <= n; i++) { cout << A[i] << " "; } } else { cout << "NO\n"; } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; int tree[16 * 100000 + 5]; int lazy[16 * 100000 + 5]; void update(int si, int sj, int idx, int val, int ql, int qr) { if (si == ql and sj == qr) { lazy[idx] |= val; tree[idx] |= val; } else { int mid = (si + sj) / 2; if (qr <= mid) update(si, mid, 2 * idx, val, ql, qr); else if (mid + 1 <= ql) update(mid + 1, sj, 2 * idx + 1, val, ql, qr); else update(si, mid, 2 * idx, val, ql, mid), update(mid + 1, sj, 2 * idx + 1, val, mid + 1, qr); tree[idx] = lazy[idx] | (tree[2 * idx] & tree[2 * idx + 1]); } } int query(int si, int sj, int idx, int ql, int qr) { if (si == ql and sj == qr) return tree[idx]; else { int mid = (si + sj) / 2; if (qr <= mid) return lazy[idx] | query(si, mid, 2 * idx, ql, qr); else if (mid + 1 <= ql) return lazy[idx] | query(mid + 1, sj, 2 * idx + 1, ql, qr); else return lazy[idx] | (query(si, mid, 2 * idx, ql, mid) & query(mid + 1, sj, 2 * idx + 1, mid + 1, qr)); } } int l[100000 + 5], r[100000 + 5], q[100000 + 5]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> l[i] >> r[i] >> q[i]; update(1, n, 1, q[i], l[i], r[i]); } for (int i = 1; i <= m; ++i) { if (query(1, n, 1, l[i], r[i]) != q[i]) { cout << "NO"; return 0; } } cout << "YES\n"; for (int i = 1; i <= n; ++i) { cout << query(1, n, 1, 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; const long long int mod = 1000000007; const long long int inf = 1e17; const long long int N = 1e5 + 5, M = 32; long long int mark[M][N]; long long int arr[N]; long long int sum[M][N]; long long int l[N], r[N], val[N]; long long int tree[4 * N]; long long int merge(long long int a, long long int b) { return (a & b); } void build(long long int tv, long long int tl, long long int tr) { if (tl == tr) { tree[tv] = arr[tl]; return; } long long int tm = (tl + tr) / 2; build(tv * 2, tl, tm); build(tv * 2 + 1, tm + 1, tr); tree[tv] = merge(tree[tv * 2], tree[tv * 2 + 1]); } long long int query(long long int tv, long long int tl, long long int tr, long long int l, long long int r) { if (tl > r or tr < l) { return (1LL << 32) - 1; } if (l <= tl and r >= tr) { return tree[tv]; } long long int tm = (tl + tr) / 2; long long int p1 = query(tv * 2, tl, tm, l, r); long long int p2 = query(tv * 2 + 1, tm + 1, tr, l, r); return merge(p1, p2); } void __solve() { long long int n, m; cin >> n >> m; for (long long int i = 1; i <= m; i++) { cin >> l[i] >> r[i] >> val[i]; } for (long long int j = 0; j < M; j++) { for (long long int i = 1; i <= m; i++) { if (val[i] & (1LL << j)) { mark[j][l[i]]++; mark[j][r[i] + 1]--; } } for (long long int i = 1; i <= n; i++) { mark[j][i] += mark[j][i - 1]; } for (long long int i = 1; i <= n; i++) { if (mark[j][i]) { arr[i] |= (1LL << j); } } } build(1, 1, n); for (long long int i = 1; i <= m; i++) { if (query(1, 1, n, l[i], r[i]) != val[i]) { cout << "NO"; return; } } cout << "YES" << '\n'; for (long long int i = 1; i <= n; i++) { cout << arr[i] << ' '; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int T = 1; while (T--) { __solve(); cout << '\n'; } }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class Solver { public static void main(String[] Args) throws NumberFormatException, IOException { new Solver().Run(); } PrintWriter pw; StringTokenizer Stok; BufferedReader br; public String nextToken() throws IOException { while (Stok == null || !Stok.hasMoreTokens()) { Stok = new StringTokenizer(br.readLine()); } return Stok.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } class Event implements Comparable<Event>{ int number; int pos; public Event(int anum, int apos){ number=anum; pos=apos; } @Override public int compareTo(Event o) { return pos-o.pos; } } int n; int m; int[] pow; Event[] events; int[][] result; int[] resMas; int[][] table; int maxPower; int[] l,r,q; void addNum(int number, int[] srez){ int diff=1; if (number<0){ diff=-1; number=-number; } for (int i=0; i<30; i++){ if ((number&pow[i])>0){ srez[i]+=diff; } } } boolean check(){ int[] takenIndex=new int[n+1]; takenIndex[1]=0; for (int i=2; i<=n; i++){ takenIndex[i]=takenIndex[i-1]; if (pow[takenIndex[i]+1]<=i) takenIndex[i]++; } for (int i=0; i<m; i++){ int ind=takenIndex[r[i]-l[i]]; int res=table[l[i]][ind]&table[r[i]-pow[ind]][ind]; if (res!=q[i]) return false; } return true; } public void Run() throws NumberFormatException, IOException { //br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter("output.txt"); br=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(new OutputStreamWriter(System.out)); n=nextInt(); m=nextInt(); pow=new int[30]; pow[0]=1; for (int i=1; i<30; i++){ pow[i]=pow[i-1]<<1; if (pow[i]<=n) maxPower=i; } events=new Event[m*2]; result=new int[n][30]; resMas=new int[n]; l=new int[m]; r=new int[m]; q=new int[m]; for (int i=0; i<m; i++){ l[i]=nextInt()-1; r[i]=nextInt(); q[i]=nextInt(); events[i*2]=new Event(q[i],l[i]); events[i*2+1]=new Event(-q[i],r[i]); } Arrays.sort(events); int[] srez=new int[30]; int ind=0; for (int i=0; i<n; i++){ while (ind<m*2 && events[ind].pos==i){ addNum(events[ind].number, srez); ind++; } for (int j=0; j<30; j++){ if (srez[j]>0) result[i][j]=1; resMas[i]+=result[i][j]*pow[j]; } } table=new int[n][maxPower+1]; for (int i=0; i<n; i++){ table[i][0]=resMas[i]; } for (int j=1; j<=maxPower; j++){ for (int i=0; i<n; i++){ table[i][j]=(table[i][j-1]&table[Math.min(i+pow[j-1],n-pow[j-1])][j-1]); } } if (check()){ pw.println("YES"); for (int i=0; i<n; i++){ pw.print(resMas[i]); pw.print(' '); } pw.println(); } else { pw.println("NO"); } pw.flush(); pw.close(); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int N = 100005; int K = INT_MAX; int seg[N * 4], lazy[N * 4], a[N], b[N], c[N]; int l, r, x; void fix(int i, int s, int e) { if (!lazy[i]) return; seg[i] |= lazy[i]; if (s != e) { lazy[i * 2 + 1] |= lazy[i]; lazy[i * 2 + 2] |= lazy[i]; } lazy[i] = 0; } void update(int i, int s, int e) { fix(i, s, e); if (s > r || e < l) return; if (s >= l && e <= r) { seg[i] |= x; if (s != e) { lazy[2 * i + 1] |= x; lazy[2 * i + 2] |= x; } return; } update(2 * i + 1, s, s + e >> 1); update(2 * i + 2, 1 + (s + e) / 2, e); seg[i] = seg[2 * i + 1] & seg[2 * i + 2]; } int get(int i, int s, int e) { fix(i, s, e); if (s > r || e < l) return K; if (s >= l && e <= r) return seg[i]; return get(2 * i + 1, s, s + e >> 1) & get(2 * i + 2, 1 + (s + e) / 2, e); } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { scanf("%d%d%d", &a[i], &b[i], &c[i]); l = a[i] - 1; r = b[i] - 1; x = c[i]; update(0, 0, n - 1); } for (int i = 0; i < m; i++) { l = a[i] - 1; r = b[i] - 1; if (get(0, 0, n - 1) != c[i]) { printf("NO"); return 0; } } puts("YES"); for (int i = 0; i < n; i++) { l = i; r = i; printf("%d ", get(0, 0, n - 1)); } return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; int n; vector<int> solve(vector<array<int, 2>> zero, vector<array<int, 2>> one) { vector<int> d(n + 2, 0); for (auto [l, r] : one) { d[l]++; d[r + 1]--; } vector<int> v(n + 1, 0); for (int i = 1; i <= n; i++) v[i] = v[i - 1] + d[i]; vector<int> nxt(n + 2, n + 1); for (int i = n; i >= 1; i--) { if (v[i] == 0) nxt[i] = i; else nxt[i] = nxt[i + 1]; } for (auto [l, r] : zero) { if (nxt[l] > r) { printf("NO\n"); exit(0); } } for (int i = 1; i <= n; i++) v[i] = min(v[i], 1); return v; } int main() { int m; scanf("%d%d", &n, &m); vector<array<int, 2>> zero[30]; vector<array<int, 2>> one[30]; for (int i = 0; i < m; i++) { int l, r, x; scanf("%d%d%d", &l, &r, &x); for (int j = 0; j < 30; j++) { if ((x) & (1 << j)) one[j].push_back({l, r}); else zero[j].push_back({l, r}); } } vector<int> res(n + 1, 0); for (int i = 0; i < 30; i++) { vector<int> v = solve(zero[i], one[i]); for (int j = 1; j <= n; j++) if (v[j] == 1) res[j] += (1 << i); } printf("YES\n"); for (int i = 1; i <= n; i++) printf("%d%c", res[i], " \n"[i == n]); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.nio.charset.IllegalCharsetNameException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; public class B482 { class Segment implements Comparable<Segment>{ int left, right, value, index; Segment(int l, int r, int v, int i){ left = l; right = r; value = v; index = i; } @Override public int compareTo(Segment o) { return Integer.compare(this.right, o.right); } } class Point{ List<Integer> starts, ends; Point(){ starts = new ArrayList<>(); ends = new ArrayList<>(); } } class SegmentTree{ int[] or, and; int n; SegmentTree(int n, int[] a){ this.n = n; or = new int[n * 4]; and = new int[n * 4]; buildTree(1, 0, n, a); } SegmentTree(int n){ this.n = n; or = new int[n * 4]; and = new int[n * 4]; } void buildTree(int v, int l, int r, int[] a){ if(l + 1 == r){ and[v] = a[l]; or[v] = a[l]; return; } int m = (r + l) >> 1; int vl = v << 1; int vr = vl + 1; buildTree(vl, l, m, a); buildTree(vr, m, r, a); and[v] = and[vl] & and[vr]; or[v] = or[vl] | or[vr]; } int start, end, ansA, ansO; int get(int l, int r){ start = l; end = r + 1; ansA = Integer.MAX_VALUE; ansO = 0; getTree(1, 0, n); return ansA; } void getTree(int v, int l, int r){ if(start <= l && r <= end){ ansA &= and[v]; ansO |= or[v]; return; } int m = (r + l) >> 1; int vl = v << 1; int vr = vl + 1; if(start < m) getTree(vl, l, m); if(m < end) getTree(vr, m, r); } boolean one; int index; int value; void update(int i, int v, boolean o){ index = i; one = o; value = v; updateTree(1, 0, n); } void updateTree(int v, int l, int r){ if(l + 1 == r){ if(one) or[v] = value; else and[v] = value; return; } int m = (r + l) >> 1; int vl = v << 1; int vr = vl + 1; if(index < m) updateTree(vl, l, m); else updateTree(vr, m, r); and[v] = and[vl] & and[vr]; or[v] = or[vl] | or[vr]; } int getAnsO(){ return or[1]; } } void solve(){ int n = readInt(); int m = readInt(); Segment[] q = new Segment[m]; int[] a = new int[n]; Point[] segments = new Point[n]; for(int i = 0;i<n;i++) segments[i] = new Point(); for(int i = 0;i<m;i++){ q[i] = new Segment(readInt() - 1, readInt() - 1, readInt(), i); segments[q[i].left].starts.add(i); if(q[i].right < n - 1) segments[q[i].right + 1].ends.add(i); } SegmentTree tree = new SegmentTree(m); for(int i = 0;i<n;i++){ for(int j : segments[i].starts) tree.update(j, q[j].value, true); for(int j : segments[i].ends) tree.update(j, 0, true); a[i] = tree.getAnsO(); } tree = new SegmentTree(n, a); for(int i = 0;i<m;i++){ int ans = tree.get(q[i].left, q[i].right); if(ans != q[i].value){ out.print("NO"); return; } } out.println("YES"); for(int i : a) out.print(i + " "); } public static void main(String[] args) { new B482().run(); } void run(){ init(); solve(); out.close(); } BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init(){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } String readLine(){ try{ return in.readLine(); }catch(Exception ex){ throw new RuntimeException(ex); } } String readString(){ while(!tok.hasMoreTokens()){ String nextLine = readLine(); if(nextLine == null) return null; tok = new StringTokenizer(nextLine); } return tok.nextToken(); } int readInt(){ return Integer.parseInt(readString()); } long readLong(){ return Long.parseLong(readString()); } double readDouble(){ return Double.parseDouble(readString()); } }
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.ArrayList; import java.util.Arrays; import java.util.List; /** * @author Roman Elizarov */ public class Round_275_B { public static final int BITS = 30; public static void main(String[] args) { new Round_275_B().go(); } int n; int m; Cons[] cs; static class Cons { int l; int r; int q; Cons(int l, int r, int q) { this.l = l; this.r = r; this.q = q; } } void go() { // read input Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); cs = new Cons[m]; for (int i = 0; i < m; i++) cs[i] = new Cons( in.nextInt() - 1, in.nextInt() - 1, in.nextInt()); // solve int[] result = solve(); // write result PrintWriter out = new PrintWriter(System.out); if (result == null) out.println("NO"); else { out.println("YES"); for (int i = 0; i < result.length; i++) { if (i > 0) out.print(' '); out.print(result[i]); } out.println(); } out.flush(); } List<Cons>[] cl; List<Cons>[] cr; int[] b = new int[BITS]; int[] last0 = new int[BITS]; @SuppressWarnings("unchecked") int[] solve() { int[] a = new int[n]; cl = (List<Cons>[]) new List[n]; cr = (List<Cons>[]) new List[n]; for (Cons c : cs) { put(cl, c, c.l); put(cr, c, c.r); } Arrays.fill(last0, -1); for (int x = 0; x < n; x++) { if (cl[x] != null) for (Cons c : cl[x]) { update(c.q, 1); } for (int i = 0; i < BITS; i++) if (b[i] > 0) { a[x] |= 1 << i; } else last0[i] = x; if (cr[x] != null) for (Cons c : cr[x]) { for (int i = 0; i < BITS; i++) if ((c.q & (1 << i)) == 0 && last0[i] < c.l) return null; update(c.q, -1); } } return a; } private void update(int q, int d) { for (int i = 0; i < BITS; i++) if ((q & (1 << i)) != 0) b[i] += d; } private void put(List<Cons>[] cs, Cons c, int x) { if (cs[x] == null) cs[x] = new ArrayList<>(); cs[x].add(c); } private static class Scanner { private InputStream in; public Scanner(InputStream in) { this.in = new BufferedInputStream(in, 65536); } public String next() { int c; StringBuilder sb = new StringBuilder(); try { do { c = in.read(); if (c < 0) throw new RuntimeException("EOF"); } while (c <= ' '); do { sb.append((char)c); c = in.read(); } while (c > ' '); } catch (IOException e) { throw new RuntimeException(e); } return sb.toString(); } 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; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1}; int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; const long long inf = 998244353; vector<int> adj[200010], vis(200010, 0), par(200010, 0), dis(200010, 0), di(200010, 0), fin(200010, 0); int myrandom(int i) { return std::rand() % i; } const int MAX = 100010; int a[MAX], l[MAX], r[MAX], q[MAX], t[4 * MAX]; using namespace std; inline void build(int v, int l, int r) { if (l + 1 == r) { t[v] = a[l]; return; } int mid = (l + r) >> 1; build(v * 2, l, mid); build(v * 2 + 1, mid, r); t[v] = t[v * 2] & t[v * 2 + 1]; } inline int query(int v, int l, int r, int L, int R) { if (l >= r) { return (1 << 30) - 1; } if (l == L && r == R) { return t[v]; } int mid = (L + R) >> 1; int ans = (1ll << 30) - 1; ans &= query(v * 2, l, std::min(r, mid), L, mid); ans &= query(v * 2 + 1, std::max(l, mid), r, mid, R); return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; memset(a, 0, sizeof(a)); for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> q[i]; l[i]--; } for (int i = 0; i <= 30; i++) { int sum[n + 2]; memset(sum, 0, sizeof(sum)); for (int j = 0; j < m; j++) { if ((1 << i) & q[j]) { sum[l[j]]++; sum[r[j]]--; } } for (int j = 0; j < n; j++) { if (j > 0) { sum[j] += sum[j - 1]; } if (sum[j] > 0) { a[j] |= 1 << i; } } } build(1, 0, n); for (int i = 0; i < m; i++) { if (query(1, l[i], r[i], 0, n) != q[i]) { cout << "NO" << '\n'; return 0; } } cout << "YES" << '\n'; for (int i = 0; i < n; i++) { cout << a[i] << " "; } cout << '\n'; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
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 { static int[] ans = new int[100005]; static int[] segtree = new int[4 * 100005]; static int[] andtree = new int[4 * 100005]; public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int ll[] = new int[m]; int rr[] = new int[m]; int qq[] = new int[m]; for (int i = 0; i < m; i++) { ll[i] = in.nextInt() - 1; rr[i] = in.nextInt() - 1; qq[i] = in.nextInt(); update(0, n - 1, 0, ll[i], rr[i], qq[i]); } for (int i = 0; i < n; i++) ans[i] = getNumber(0, n - 1, 0, i); init(0, n - 1, 0); boolean check = true; for (int i = 0; i < m; i++) { if (query(0, n - 1, 0, ll[i], rr[i]) != qq[i]) { check = false; break; } } if (check == true) { out.println("YES"); for (int i = 0; i < n; i++) out.printf("%d ", ans[i]); out.println(); } else out.println("NO"); } static void update(int start, int endd, int curr, int l, int r, int q) { if (start == l && endd == r) { segtree[curr] |= q; return; } int mid = (start + endd) / 2; if (l <= mid && r <= mid) { update(start, mid, 2 * curr + 1, l, r, q); return; } if (l > mid && r > mid) { update(mid + 1, endd, 2 * curr + 2, l, r, q); return; } if (l <= mid && r > mid) { update(start, mid, 2 * curr + 1, l, mid, q); update(mid + 1, endd, 2 * curr + 2, mid + 1, r, q); } } static int getNumber(int start, int end, int curr, int num) { if (start == end) { return segtree[curr]; } int mid = (start + end) / 2; if (num <= mid) { segtree[2 * curr + 1] |= segtree[curr]; return getNumber(start, mid, 2 * curr + 1, num); } else { segtree[2 * curr + 2] |= segtree[curr]; return getNumber(mid + 1, end, 2 * curr + 2, num); } } static void init(int start, int endd, int curr) { if (start == endd) { andtree[curr] = ans[start]; return; } int mid = (start + endd) / 2; init(start, mid, 2 * curr + 1); init(mid + 1, endd, 2 * curr + 2); andtree[curr] = andtree[2 * curr + 1] & andtree[2 * curr + 2]; } static int query(int start, int endd, int curr, int l, int r) { if (start == l && endd == r) return andtree[curr]; int mid = (start + endd) / 2; if (l <= mid && r <= mid) return query(start, mid, 2 * curr + 1, l, r); if (l > mid && r > mid) return query(mid + 1, endd, 2 * curr + 2, l, r); if (l <= mid && r > mid) { int t1 = query(start, mid, 2 * curr + 1, l, mid); int t2 = query(mid + 1, endd, 2 * curr + 2, mid + 1, r); return t1 & t2; } return 0; } } 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; int n, ft[100005]; void add(int x, int v) { for (; x; x -= x & -x) ft[x] |= v; } int query(int x) { int ans = 0; for (; x <= n; x += x & -x) ans |= ft[x]; return ans; } class segtree { vector<int> st; vector<int> lazy; int n; void prop(int p) { if (lazy[p]) { lazy[2 * p] |= lazy[p]; lazy[2 * p + 1] |= lazy[p]; st[p] |= lazy[p]; lazy[p] = 0; } } void update(int p, int L, int R, int i, int j, int v) { prop(p); if (j < L || R < i) return; if (i <= L && R <= j) { lazy[p] = v; prop(p); return; } int mid = (L + R) / 2; update(2 * p, L, mid, i, j, v); update(2 * p + 1, mid + 1, R, i, j, v); st[p] = st[2 * p] | st[2 * p + 1]; } int query(int p, int L, int R, int i, int j) { prop(p); if (j < L || R < i) return 0; if (i <= L && R <= j) return st[p]; int mid = (L + R) / 2; int ret = 0; ret |= query(2 * p, L, mid, i, j); ret |= query(2 * p + 1, mid + 1, R, i, j); return ret; } public: segtree(int n) : n(n), st(16 * n, 0), lazy(16 * n, 0) {} void update(int i, int j, int v) { update(1, 0, n - 1, i, j, v); } int query(int i, int j) { return query(1, 0, n - 1, i, j); } int query(int i) { return query(1, 0, n - 1, i, i); } }; class segtreeA { vector<int> st; int n; void update(int p, int L, int R, int i, int v) { if (i < L || R < i) return; if (i == L && R == i) { st[p] = v; return; } int mid = (L + R) / 2; update(2 * p, L, mid, i, v); update(2 * p + 1, mid + 1, R, i, v); st[p] = st[2 * p] & st[2 * p + 1]; } int query(int p, int L, int R, int i, int j) { if (j < L || R < i) return (1 << 30) - 1; if (i <= L && R <= j) return st[p]; int mid = (L + R) / 2; int ret = query(2 * p, L, mid, i, j); ret &= query(2 * p + 1, mid + 1, R, i, j); return ret; } public: segtreeA(int n) : n(n), st(16 * n, 0) {} void update(int i, int v) { update(1, 0, n - 1, i, v); } int query(int i, int j) { return query(1, 0, n - 1, i, j); } int query(int i) { return query(1, 0, n - 1, i, i); } }; int main() { int m; scanf("%d %d", &n, &m); segtree st(n); int l, r, q; pair<int, pair<int, int> > pp[100005]; for (int i = 0; i < m; i++) { scanf("%d %d %d", &l, &r, &q); l--; r--; st.update(l, r, q); pp[i] = make_pair(l, make_pair(r, q)); } segtreeA tmp(n); for (int i = 0; i < n; i++) tmp.update(i, st.query(i)); for (int i = 0; i < m; i++) { l = pp[i].first; r = pp[i].second.first; q = pp[i].second.second; if (tmp.query(l, r) != q) { printf("NO\n"); return 0; } } printf("YES\n"); printf("%d", st.query(0)); for (int i = 1; i < n; i++) printf(" %d", st.query(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 sum[100050 << 2], ans[100050], num = 0; struct que { int st, ed, val; } query[100050]; void Update(int L, int R, int val, int l, int r, int rt) { if (L <= l && R >= r) { sum[rt] |= val; return; } int m = (l + r) >> 1; if (L <= m) Update(L, R, val, l, m, rt << 1); if (R > m) Update(L, R, val, m + 1, r, rt << 1 | 1); } int Query(int L, int R, int l, int r, int rt) { if (L <= l && R >= r) return sum[rt]; int m = (l + r) >> 1, ret = 0x7fffffff; if (L <= m) ret &= Query(L, R, l, m, rt << 1); if (R > m) ret &= Query(L, R, m + 1, r, rt << 1 | 1); return ret; } void Solve(int l, int r, int rt) { sum[rt] |= sum[rt >> 1]; if (r == l) { ans[num++] = sum[rt]; return; } int m = (l + r) >> 1; Solve(l, m, rt << 1); Solve(m + 1, r, rt << 1 | 1); } int main() { int n, m, i, flag = 0; scanf("%d%d", &n, &m); for (i = 0; i < m; i++) { scanf("%d%d%d", &query[i].st, &query[i].ed, &query[i].val); Update(query[i].st, query[i].ed, query[i].val, 1, n, 1); } for (i = 0; i < m; i++) { if (Query(query[i].st, query[i].ed, 1, n, 1) != query[i].val) { flag = 1; break; } } if (flag) printf("NO\n"); else { Solve(1, n, 1); printf("YES\n"); for (i = 0; i < n; i++) { printf("%d", ans[i]); if (i == n - 1) printf("\n"); else printf(" "); } } 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.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer; public class InterestingArray { public static void main(String[] args) { MyScanner sc = new MyScanner(); int MAX_LEN = 100002; int MAX_BITS = 32; int N = sc.nextInt(); int M = sc.nextInt(); int[] L = new int[M]; int[] R = new int[M]; int[] Q = new int[M]; int[][] intervals = new int[MAX_BITS][MAX_LEN]; for (int i = 0; i < M; i++) { L[i] = sc.nextInt(); R[i] = sc.nextInt(); Q[i] = sc.nextInt(); for (int j = 0; j < MAX_BITS; j++) { int b = Bits.get(Q[i], j); if (b == 1) { intervals[j][L[i]]++; intervals[j][R[i] + 1]--; } } } int[][] sum = new int[MAX_BITS][MAX_LEN]; int[][] cnt = new int[MAX_BITS][MAX_LEN]; for (int b = 0; b < MAX_BITS; b++) { for (int i = 1; i <= N; i++) { sum[b][i] = sum[b][i - 1] + intervals[b][i]; cnt[b][i] = cnt[b][i - 1]; if (sum[b][i] > 0) { cnt[b][i]++; } } } for (int i = 0; i < M; i++) { for (int j = 0; j < MAX_BITS; j++) { int b = Bits.get(Q[i], j); if (b == 0) { int inRange = cnt[j][R[i]] - cnt[j][L[i] - 1]; if (inRange == R[i] - L[i] + 1) { System.out.println("NO"); return; } } } } System.out.println("YES"); for (int i = 1; i <= N; i++) { int num = 0; for (int j = 0; j < MAX_BITS; j++) { if (sum[j][i] > 0) { num = Bits.set(num, j); } } System.out.print(num + " "); } System.out.println(); } public static class Bits { public static int get(int n, int b) { return (n >> b) & 1; } public static int setAs(int n, int b, int v) { if (v == 0) { return unset(n, b); } else { return set(n, b); } } public static int set(int n, int b) { return (n | (1 << b)); } public static int unset(int n, int b) { return (n & ~(1 << b)); } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.*; import java.util.*; public class Main { static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); static int n,m,ju,a; static Pair[] aa=new Pair[100005]; static int[][] sum=new int[40][100005]; static int[][] mm=new int[40][100005]; static class Pair implements Comparable<Pair> { int x,y,z; Pair(int a,int b,int c) { x=a;y=b;z=c; } public int compareTo(Pair p) { return x-p.x; } } public static void main(String[] args) { Scanner in=new Scanner(System.in); n=in.nextInt();m=in.nextInt(); for(int i=1;i<=m;i++) aa[i]=new Pair(in.nextInt(),in.nextInt(),in.nextInt()); Arrays.sort(aa,1,m+1); for(int j=0;j<30;j++) { a=0; for(int i=1;i<=m;i++) { if(((1<<j)&aa[i].z)==0) continue; a=Math.max(a,aa[i].x); while(a<=aa[i].y) mm[j][a++]=1; } for(int i=1;i<=n;i++) sum[j][i]=sum[j][i-1]+mm[j][i]; } ju=1; for(int i=1;i<=m;i++) { for(int j=0;j<30;j++) { if(((1<<j)&aa[i].z)>0) continue; a=sum[j][aa[i].y]-sum[j][aa[i].x-1]; if(a>=aa[i].y-aa[i].x+1) ju=0; } } out.println(ju==0?"NO":"YES"); if(ju==1) { for(int i=1;i<=n;i++) { a=0; for(int j=0;j<30;j++) a+=mm[j][i]<<j; out.print(a+" "); } } out.flush(); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class TestClass { private static InputStream stream; private static byte[] buf = new byte[1024]; private static int curChar; private static int numChars; private static SpaceCharFilter filter; private static PrintWriter pw; public static class Queue{ private class node{ int val; node next; node(int a){ val = a; next = null; } } node head,tail; Queue(){ head = null; tail = null; } public void EnQueue(int a){ if(head==null){ node p = new node(a); head = p; tail = p; } else{ node p = new node(a); tail.next = p; tail = p; } } public int DeQueue(){ int a = head.val; head = head.next; return a; } public boolean isEmpty(){ return head==null; } } public static long pow(long x,long y,long m){ if(y==0) return 1; long k = pow(x,y/2,m); if(y%2==0) return (k*k)%m; else return (((k*k)%m)*x)%m; } static long Inversex = 0,Inversey = 0; public static void InverseModulo(long a,long m){ if(m==0){ Inversex = 1; Inversey = 0; } else{ InverseModulo(m,a%m); long temp = Inversex; Inversex = Inversey; Inversey = temp+ - (a/m)*Inversey; } } static long mod1 = 1000000007; static long mod2 = 1000000009; public static long gcd(long a,long b){ if(a%b==0) return b; return gcd(b,a%b); } public static boolean isPrime(long a){ if(a==1) return false; else if(a==2||a==3) return true; for(long i=2;i<=Math.sqrt(a);i++) if(a%i==0) return false; return true; } public static double distance(int a,int b,int x,int y){ return Math.sqrt(((long)(a-x)*(long)(a-x))+((long)(b-y)*(long)(b-y))); } public static class Pair implements Comparable<Pair> { long u; long v; public Pair(long u, long v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public int compareTo(Pair other) { return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v); } public String toString() { return u+" "+v; } } public static class Pairs implements Comparable<Pairs> { long u; long v; int id; public Pairs(long u, long v,int w) { this.u = u; this.v = v; this.id = w; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pairs other = (Pairs) o; return u == other.u && v == other.v; } public int compareTo(Pairs other) { return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v); } public String toString() { return u+" "+v; } } public static int sieve(int a[],boolean b[]){ int count = 0; b[0] = true; b[1] = true; for(int i=2;i<a.length;i++){ if(!b[i]){ for(int j=2*i;j<a.length;j=j+i){ b[j] = true; } a[count++] = i; } } return count; } public static void MatrixMultiplication(int a[][],int b[][],int c[][]){ for(int i=0;i<a.length;i++){ for(int j=0;j<b[0].length;j++){ c[i][j] = 0; for(int k=0;k<b.length;k++) c[i][j]=(int)((c[i][j]+((a[i][k]*(long)b[k][j])%mod1))%mod1); } } } public static void Equal(int arr[][],int temp[][]){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr.length;j++) temp[i][j] = arr[i][j]; } } public static void makeHash(String a,long b[],long mod){ for(int i=1;i<=a.length();i++) b[i] = ((a.charAt(i-1)-'a')+b[i-1]*26)%mod; } /*public static long getHash(long b[],int p,int q,long mod){ } public static void Merge(long a[][],int p,int r){ if(p<r){ int q = (p+r)/2; Merge(a,p,q); Merge(a,q+1,r); Merge_Array(a,p,q,r); } } public static void get(long a[][],long b[][],int i,int j){ for(int k=0;k<a[i].length;k++) a[i][k] = b[j][k]; } public static void Merge_Array(long a[][],int p,int q,int r){ long b[][] = new long[q-p+1][a[0].length]; long c[][] = new long[r-q][a[0].length]; for(int i=0;i<b.length;i++){ get(b,a,i,p+i); } for(int i=0;i<c.length;i++){ get(c,a,i,q+i+1); } int i = 0,j = 0; for(int k=p;k<=r;k++){ if(i==b.length){ get(a,c,k,j); j++; } else if(j==c.length){ get(a,b,k,i); i++; } else if(b[i][0]<c[j][0]){ get(a,b,k,i); i++; } else{ get(a,c,k,j); j++; } } } public static void powMatrix(long n){ if(n<=1) return ; powMatrix(n/2); MatrixMultiplication(temp2,temp2,result); Equal(result,temp2); if(n%2==1){ MatrixMultiplication(temp2,a,result); Equal(result,temp2); } } static int result[][] = new int[4][4]; static int temp[][] = new int[4][4]; static int temp2[][] = new int[4][4]; static int a[][] = new int[4][4]; static int[][] b = new int[4][1]; static int [][] c = new int[4][1]; public static int count(long n){ a[0][0] = 1 ; a[0][1] = 1; a[1][0] = 1; a[2][1] = 1; a[3][0] = 1; a[3][1] = 1; a[3][3] = 1; b[0][0] = 1; b[1][0] = 1; b[2][0] = 0; b[3][0] = 2; Equal(a,temp2); result[0][0] = 1; result[1][1] = 1; result[2][2] = 1; result[3][3] = 1; Equal(result,temp); if(n<=0) return 0; else if(n==1) return 1; else if(n==2) return 2; n = n-2; powMatrix(n); MatrixMultiplication(temp2,b,c); return c[3][0]; }*/ public static class ST{ int arr[] = new int[800000]; int lazy[] = new int[800000]; //int zero[] = new int[800000]; ST(int a[]){ Build(a,0,a.length-1,0); } public void Build(int a[],int p,int q,int count){ if(p==q){ arr[count] = a[p]; //zero[count] = 1; return ; } int mid = (p+q)/2; Build(a,p,mid,2*count+1); Build(a,mid+1,q,2*count+2); arr[count] = arr[2*count+1]&arr[2*count+2]; //zero[count] = zero[2*count+1]+zero[2*count+2]; } public void print(){ for(int i=0;i<10;i++) pw.print(arr[i]+" "); pw.println(); } public int getSum(int p,int q,int x,int y,int count){ if(lazy[count]!=0){ arr[count]|=lazy[count]; lazy[2*count+1]|=lazy[count]; lazy[2*count+2]|=lazy[count]; lazy[count] = 0; } if(q<x||p>y||p>q){ //pw.println("UT"); return (int)Math.pow(2, 30)-1; } else if(x<=p&&y>=q){ //pw.println(count+" UT "+arr[count]); return arr[count]; } int mid = (p+q)/2; return getSum(p,mid,x,y,2*count+1)&getSum(mid+1,q,x,y,2*count+2); } public void Update(int p,int q,int x,int y,int count,int up){ if(lazy[count]!=0){ arr[count]|=lazy[count]; lazy[2*count+1]|=lazy[count]; lazy[2*count+2]|=lazy[count]; lazy[count] = 0; //pw.println("UTSAV"); } if(q<x||p>y||p>q) return; else if(x<=p&&y>=q){ arr[count]|=up; lazy[2*count+1]|=up; lazy[2*count+2]|=up; return ; } int mid = (p+q)/2; Update(p,mid,x,y,2*count+1,up); Update(mid+1,q,x,y,2*count+2,up); arr[count] = arr[2*count+1]&arr[2*count+2]; } } private static void soln(){ int n = nI(); int m = nI(); int arr[] = new int[n]; ST p = new ST(arr); HashMap<Pair,Integer> op = new HashMap<Pair,Integer>(); while(m-->0){ int x = nI()-1; int y = nI()-1; int z = nI(); int kl = p.getSum(0, n-1, x, y, 0); if(((op.containsKey(new Pair(x,y))&&op.get(new Pair(x,y))!=z))||((kl|z)!=z)){ pw.println("NO"); return ; } else{ p.Update(0, n-1, x, y, 0, z); } op.put(new Pair(x,y), z); } pw.println("YES"); for(int i=0;i<n;i++) pw.print(p.getSum(0, n-1, i, i, 0)+" "); } public static void main(String[] args) { InputReader(System.in); pw = new PrintWriter(System.out); soln(); pw.close(); } // To Get Input // Some Buffer Methods public static void InputReader(InputStream stream1) { stream = stream1; } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private static boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private static 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++]; } private static int nI() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static long nL() { 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; } private static String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private static String nLi() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private static int[] nIA(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nI(); return arr; } private static long[] nLA(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nL(); return arr; } private static void pArray(int[] arr) { for (int i = 0; i < arr.length; i++) pw.print(arr[i] + " "); pw.println(); return; } private static void pArray(long[] arr) { for (int i = 0; i < arr.length; i++) pw.print(arr[i] + " "); pw.println(); return; } private static boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.*; import java.io.InputStream; public class Solution { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } } /* * Solution */ class Question{ int l; int r; long g; Question(int l, int r, long g) { this.l = l-1; this.r = r-1; this.g = g; } } class Task { long[] tree; public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); Question[] q = new Question[m]; for (int i = 0; i < m; i++) { q[i] = new Question(in.nextInt(), in.nextInt(), in.nextLong()); } tree = new long[4*n]; for (int i = 0; i < m; i++) { update(1, 0, n-1, q[i].l, q[i].r, q[i].g); } /* { int c = 2; int i = 1; while (i < 4 * n) { out.print(tree[i] + " "); i++; if (i == c) { c *= 2; out.println(); } } } out.println(); out.println(); */ for (int i = 0; i < m; i++) { if (qwestion(1, 0, n-1, q[i].l, q[i].r) != q[i].g) { out.println("NO"); return; } // out.println(i+1 + " " + q[i].g + " " + qwestion(1, 0, n-1, q[i].l, q[i].r)); } out.println("YES"); for (int i = 0; i < n; i++) { out.print(qwestion(1, 0, n-1, i, i) + " "); } return; } private void update(int v, int tLeft, int tRight, int l, int r, long q) { if (l > r) return; if (l == tLeft && r == tRight) { tree[v] |= q; return; } int tMedium = (tLeft + tRight) / 2; update (v*2, tLeft, tMedium, l, Math.min(r,tMedium), q); update (v*2+1, tMedium+1, tRight, Math.max(l,tMedium+1), r, q); tree[v] |= (tree[v*2] & tree[v*2+1]); } private long qwestion(int v, int tLeft, int tRight, int l, int r) { if (l > r) return (1 << 30) - 1; if (l == tLeft && r == tRight) { return tree[v]; } int tMedium = (tLeft + tRight) / 2; return tree[v] | (qwestion(v*2, tLeft, tMedium, l, Math.min(r,tMedium)) & qwestion(v*2+1, tMedium+1, tRight, Math.max(l,tMedium+1), r)); } } /* * Reading */ class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader (InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public 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
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 322; const long long LINF = 2e18 + 228; const int MAXN = 1e5 + 228; const int MOD = 1e9 + 7; const double eps = 1e-14; int n, m; pair<pair<int, int>, int> a[MAXN]; int pr[MAXN][30], hmm[MAXN], t[4 * MAXN]; void build(int v, int tl, int tr) { if (tl == tr) t[v] = hmm[tl]; else { int tm = (tl + tr) >> 1; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); t[v] = t[v * 2] & t[v * 2 + 1]; } } int sum(int v, int tl, int tr, int l, int r) { if (l > r) { int w = (1 << 30) - 1; return w; } if (tl == l && r == tr) return t[v]; int tm = (tl + tr) >> 1; int le = sum(v * 2, tl, tm, l, min(r, tm)); int ri = sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); return le & ri; } void solve() { cin >> n >> m; for (int i = 0; i < m; i++) { int l, r, q; cin >> l >> r >> q; a[i] = make_pair(make_pair(l, r), q); for (int j = 0; j < 30; j++) { int x = q & 1; pr[l][j] += x; pr[r + 1][j] -= x; q >>= 1; } } for (int i = 1; i <= n; i++) { for (int j = 0; j < 30; j++) { pr[i][j] += pr[i - 1][j]; } } for (int i = 1; i <= n; i++) { for (int j = 0; j < 30; j++) { if (pr[i][j] > 0) { hmm[i] += (1 << j); } } } build(1, 1, n); for (int i = 0; i < m; i++) { int l = a[i].first.first; int r = a[i].first.second; int q = a[i].second; if (sum(1, 1, n, l, r) != q) { cout << "NO"; return; } } cout << "YES\n"; for (int i = 1; i <= n; i++) cout << hmm[i] << " "; } int main() { if (!1) { freopen( "474" ".in", "r", stdin); freopen( "474" ".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T = 1; while (T--) solve(); return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; struct demand { int L, R, V; demand(int _L, int _R, int _V) { L = _L; R = _R; V = _V; } }; long long arr[100100]; long long dp[100100]; bool call(int n, vector<demand> &d, int *ans) { memset((arr), (0), sizeof(arr)); ; for (auto x : d) { if (x.V == 0) continue; arr[x.L]++; arr[x.R + 1]--; } for (auto i = 0; i < (n); i++) { if (i) arr[i] += arr[i - 1]; dp[i] = bool(arr[i] > 0); ans[i] = dp[i]; if (i) dp[i] += dp[i - 1]; } for (auto x : d) { long long tot = dp[x.R]; if (x.L > 0) tot -= dp[x.L - 1]; if (x.V == 0 && tot >= (x.R - x.L + 1)) return false; if (x.V == 1) { if (tot != x.R - x.L + 1) return false; } } return true; } vector<demand> d, temp; int ttt[100100]; int ans[100100]; int main() { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; int tm = m; while (m--) { int l, r, q; cin >> l >> r >> q; l--, r--; d.push_back(demand(l, r, q)); } vector<demand> temp; for (auto i = 0; i < (tm); i++) temp.push_back(demand(0, 0, 0)); for (int bit = 0; bit < 30; bit++) { int idx = 0; for (auto x : d) { int v = bool(x.V & (1 << bit)); temp[idx].L = x.L; temp[idx].R = x.R; temp[idx].V = v; idx++; } if (!call(n, temp, ttt)) { cout << "NO" << endl; return 0; } for (auto i = 0; i < (n); i++) { if (ttt[i]) ans[i] += (1 << bit); } } cout << "YES" << endl; for (auto i = 0; i < (n); i++) { if (i) cout << " "; cout << ans[i]; } cout << endl; return 0; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, FastReader in, PrintWriter out) { Debug debug = new Debug(); int n = in.nextInt(); int m = in.nextInt(); int[] a = new int[n]; TaskB.Query[] queries = new TaskB.Query[m]; for (int q = 0; q < m; ++q) { queries[q] = new TaskB.Query(); queries[q].L = in.nextInt() - 1; queries[q].R = in.nextInt() - 1; queries[q].value = in.nextInt(); } for (int bit = 0; bit < 30; ++bit) { int[] count = new int[n + 1]; int[] cum = new int[n + 2]; for (int q = 0; q < m; ++q) { if ((queries[q].value & (1 << bit)) != 0) { ++count[queries[q].L]; --count[queries[q].R + 1]; } } for (int i = 1; i < n; ++i) count[i] += count[i - 1]; for (int i = 0; i < n; ++i) count[i] = Math.min(count[i], 1); for (int i = 0; i < n; ++i) { cum[i + 1] = cum[i] + count[i]; } for (int q = 0; q < m; ++q) { if ((queries[q].value & (1 << bit)) == 0) { if (cum[queries[q].R + 1] - cum[queries[q].L] == queries[q].R - queries[q].L + 1) { out.println("NO"); return; } } } for (int i = 0; i < n; ++i) a[i] |= (count[i] << bit); } out.println("YES"); ArrayUtils.printArray(out, a); } static class Query { int L; int R; int value; } } static class ArrayUtils { public static void printArray(PrintWriter out, int[] array) { if (array.length == 0) return; for (int i = 0; i < array.length; i++) { if (i != 0) out.print(" "); out.print(array[i]); } out.println(); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } static class Debug { PrintWriter out; boolean oj; boolean system; long timeBegin; Runtime runtime; public Debug(PrintWriter out) { oj = System.getProperty("ONLINE_JUDGE") != null; this.out = out; this.timeBegin = System.currentTimeMillis(); this.runtime = Runtime.getRuntime(); } public Debug() { system = true; oj = System.getProperty("ONLINE_JUDGE") != null; OutputStream outputStream = System.out; this.out = new PrintWriter(outputStream); this.timeBegin = System.currentTimeMillis(); this.runtime = Runtime.getRuntime(); } } }
JAVA
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> using namespace std; const int MAX = 4e5 + 10; int n, m, seg[4 * MAX], fen[33][MAX]; vector<pair<int, int> > v[33]; int res[MAX]; struct node { node(int xx, int yy, int zz) { X = xx; Y = yy; Z = zz; } int X, Y, Z; }; vector<node> Q; void upd(int a, int b, int c) { while (b <= n) { fen[a][b] += c; b += (b & -b); } } int get(int a, int b) { int ret = 0; while (b > 0) { ret += fen[a][b]; b -= (b & -b); } return ret; } int make(int a, int l, int r) { if (l + 1 == r) return seg[a] = res[l]; int m = (l + r) / 2; int u = make(2 * a, l, m); int v = make(2 * a + 1, m, r); return seg[a] = u & v; } int ok(int a, int ll, int rr, int l, int r) { if (ll == l && rr == r) return seg[a]; int mm = (ll + rr) / 2; if (r <= mm) return ok(2 * a, ll, mm, l, r); if (l >= mm) return ok(2 * a + 1, mm, rr, l, r); return ok(2 * a, ll, mm, l, mm) & ok(2 * a + 1, mm, rr, mm, r); } bool check() { for (int i = 0; i < Q.size(); i++) { node tmp = Q[i]; if (ok(1, 1, n + 1, tmp.X, tmp.Y + 1) != tmp.Z) return false; } return true; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int l, r, q; cin >> l >> r >> q; Q.push_back(node(l, r, q)); for (int j = 0; j < 30; j++) if (1 << j & q) v[j].push_back(pair<int, int>(l, r)); } for (int b = 0; b < 30; b++) { for (int i = 0; i < v[b].size(); i++) { pair<int, int> tmp = v[b][i]; upd(b, tmp.first, 1); upd(b, tmp.second + 1, -1); } } for (int i = 1; i <= n; i++) { int num = 0; for (int b = 0; b < 30; b++) { int cur = get(b, i); if (cur > 0) num += (1 << b); } res[i] = num; } make(1, 1, n + 1); if (!check()) { cout << "NO\n"; return 0; } cout << "YES\n"; for (int i = 1; i <= n; 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> #pragma GCC optimize("O3") #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("unroll-loops") using namespace std; const long double PI = acos(-1); const long long MOD = 1000000007; const long long FMOD = 998244353; const long double eps = 1e-9; mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); long long p[200010], ans[200010], zero[200010]; signed main() { long long n, m; cin >> n >> m; long long l[m + 1], r[m + 1], q[m + 1]; for (long long i = 1; i < m + 1; i++) { cin >> l[i] >> r[i] >> q[i]; } for (long long i = 0; i <= 30; i++) { for (long long j = 1; j <= n; j++) { p[j] = 0; zero[j] = 0; } for (long long j = 1; j <= m; j++) { if ((q[j] & (1LL << i))) { p[l[j]]++; p[r[j] + 1]--; } } for (long long j = 1; j <= n; j++) { p[j] += p[j - 1]; zero[j] = zero[j - 1] + (p[j] == 0); ans[j] |= ((p[j] != 0) * (1LL << i)); } for (long long j = 1; j <= m; j++) { if (!(q[j] & (1LL << i))) { if (zero[r[j]] - zero[l[j] - 1] == 0) { cout << "NO"; return 0; } } } } cout << "YES" << endl; for (long long i = 1; i <= n; 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; struct NODE { int le, ri, x; } nodes[100010]; int ans[100010], sum[100010]; int main() { int n, m; while (~scanf("%d%d", &n, &m)) { for (int i = 0; i < m; i++) { scanf("%d%d%d", &nodes[i].le, &nodes[i].ri, &nodes[i].x); } int flag = 0; for (int i = 0; i < 30; i++) { memset(sum, 0, sizeof(sum)); for (int j = 0; j < m; j++) { int re = (nodes[j].x & (1 << i)); if (!re) continue; sum[nodes[j].le]++; sum[nodes[j].ri + 1]--; } for (int j = 1; j <= n; j++) { sum[j] += sum[j - 1]; } for (int j = 1; j <= n; j++) { if (sum[j]) { sum[j] = 1; ans[j] |= (1 << i); } } for (int j = 1; j <= n; j++) { sum[j] += sum[j - 1]; } for (int j = 0; j < m; j++) { int re = (nodes[j].x & (1 << i)); if (re) { if (sum[nodes[j].ri] - sum[nodes[j].le - 1] != nodes[j].ri - nodes[j].le + 1) { flag = 1; break; } } else { if (sum[nodes[j].ri] - sum[nodes[j].le - 1] == nodes[j].ri - nodes[j].le + 1) { flag = 1; break; } } } if (flag) { break; } } if (flag) { printf("NO\n"); } else { 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
#include <bits/stdc++.h> using namespace std; struct node { int l, r, v; bool operator<(const node& tt) const { if (l == tt.l) return r < tt.r; return l < tt.l; } } con[100008]; int ans[100008], rec[35][100008]; int main() { int n, m, i, j, k; bool f = true; scanf("%d%d", &n, &m); for (i = 0; i < m; i++) scanf("%d%d%d", &con[i].l, &con[i].r, &con[i].v); sort(con, con + m); memset(ans, 0, sizeof(ans)); memset(rec, 0, sizeof(rec)); for (i = 0; i < 30; i++) { k = 0; for (j = 0; j < m; j++) if ((con[j].v & (1 << i)) && con[j].r > k) { for (k = max(k, con[j].l - 1); k < con[j].r; k++) { ans[k] |= 1 << i; rec[i][k] = 1; } } } for (i = 0; i < 30 && f; i++) { for (j = 1; j < n; j++) rec[i][j] += rec[i][j - 1]; for (j = 0; j < m && f; j++) if (!(con[j].v & (1 << i))) { int tp = rec[i][con[j].r - 1]; if (con[j].l > 1) tp -= rec[i][con[j].l - 2]; if (tp == con[j].r - con[j].l + 1) f = false; } } if (f) { puts("YES"); for (i = 0; i < n; i++) printf("%d ", ans[i]); } else puts("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; int l[(int)1e5 + 5], r[(int)1e5 + 5], q[(int)1e5 + 5], t[4 * (int)1e5 + 5], a[(int)1e5 + 5], sum[(int)1e5 + 5]; void build(int node, int l, int r) { if (l == r) { t[node] = a[l]; return; } int mid = (l + r) >> 1; build(2 * node + 1, l, mid); build(2 * node + 2, mid + 1, r); t[node] = t[2 * node + 1] & t[2 * node + 2]; } int query(int node, int l, int r, int L, int R) { int ans = (1 << 30) - 1; if (l > R || r < L) return ans; if (L <= l && r <= R) return t[node]; int mid = (l + r) >> 1; ans &= query(2 * node + 1, l, mid, L, R); ans &= query(2 * node + 2, mid + 1, r, L, R); return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> q[i]; l[i]--; r[i]--; } for (int bit = 0; bit < 30 + 1; bit++) { for (int i = 0; i < n; i++) sum[i] = 0; for (int i = 0; i < m; i++) { if (q[i] & (1 << bit)) { 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) a[i] |= (1 << bit); } } build(0, 0, n - 1); for (int i = 0; i < m; i++) { if (query(0, 0, n - 1, l[i], r[i]) != q[i]) { cout << "NO\n"; return 0; } } cout << "YES\n"; for (int i = 0; i < n; i++) cout << a[i] << " "; cout << "\n"; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.StandardSocketOptions; import java.util.Arrays; import java.util.Collection; import java.util.EmptyStackException; import java.util.Locale; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { new Thread(null, new Runnable() { public void run() { try { long prevTime = System.currentTimeMillis(); new Main().run(); System.err.println("Total time: " + (System.currentTimeMillis() - prevTime) + " ms"); System.err.println("Memory status: " + memoryStatus()); } catch (IOException e) { e.printStackTrace(); } } }, "1", 1L << 24).start(); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); Object o = solve(); if (o != null) out.println(o); out.close(); in.close(); } private Object solve() throws IOException { int n = ni(); int q = ni(); int[][] queries = new int[q][]; for(int i =0; i < q;i++) queries[i] = new int[]{ni() - 1,ni(),ni()}; LazySegmentTree st = new LazySegmentTree(new int[n]); for(int[] query : queries) st.update(query[0], query[1], query[2]); for(int[] query : queries) if(st.get(query[0], query[1]) != query[2]) return "NO"; out.println("YES"); for(int i = 0; i <n;i++){ if(i != 0) out.print(" "); out.print(st.get(i, i+1)); } out.println(); return null; } class LazySegmentTree { int origSize; int[] filtro; int[] arr; // valores LazySegmentTree(int[] list) { origSize = list.length; filtro = new int[4*origSize]; Arrays.fill(filtro, 0); arr = new int[4*origSize]; build(1,0,list.length,list); } void build(int curr, int begin, int end, int[] list) { if (begin + 1 == end) { arr[curr] = list[begin]; } else { int mid = (begin + end) / 2; build(2 * curr, begin, mid, list); build(2 * curr + 1, mid, end, list); arr[curr] = combine(arr[2 * curr], arr[2 * curr + 1]); } } void update(int begin, int end, int x) { update(1, 0, origSize, begin, end, x); } void update(int curr, int tBegin, int tEnd, int begin, int end, int x) { if(tBegin >= begin && tEnd <= end){ filtro(curr, x); }else{ propagate(curr); int mid = (tBegin+tEnd)/2; if(mid > begin && tBegin < end){ update(2*curr, tBegin, mid, begin, end, x); } if(tEnd > begin && mid < end){ update(2*curr+1, mid, tEnd, begin, end, x); } arr[curr] = combine(propagate(2*curr), propagate(2 * curr+1)); } } // intervalos vacio vuela int get(int begin, int end) { return get(1,0,origSize,begin,end); } int get(int curr, int tBegin, int tEnd, int begin, int end){ propagate(curr); if(tBegin >= begin && tEnd <= end) return arr[curr]; int mid = (tBegin+tEnd)/2; boolean go_left = mid > begin && tBegin < end; boolean go_right = tEnd > begin && mid < end; if(go_left && go_right) return combine(get(2*curr, tBegin, mid, begin, end), get(2*curr+1, mid, tEnd, begin, end)); if(go_left) return get(2*curr, tBegin, mid, begin, end); if(go_right) return get(2*curr+1, mid, tEnd, begin, end); throw new EmptyStackException(); } int combine(int val1, int val2) {// return val1 & val2; } void filtro(int index, int val) {// filtro[index] = val | filtro[index]; } int propagate(int index_curr){// int val = arr[index_curr]; if(filtro[index_curr] == 0) return val; if(2*index_curr < arr.length) filtro(2*index_curr, filtro[index_curr]); if(2*index_curr + 1 < arr.length) filtro(2*index_curr+1, filtro[index_curr]); arr[index_curr] = filtro[index_curr] | arr[index_curr]; filtro[index_curr] = 0; return arr[index_curr]; } } BufferedReader in; PrintWriter out; StringTokenizer strTok = new StringTokenizer(""); String nextToken() throws IOException { while (!strTok.hasMoreTokens()) strTok = new StringTokenizer(in.readLine()); return strTok.nextToken(); } int ni() throws IOException { return Integer.parseInt(nextToken()); } long nl() throws IOException { return Long.parseLong(nextToken()); } double nd() throws IOException { return Double.parseDouble(nextToken()); } int[] nia(int size) throws IOException { int[] ret = new int[size]; for (int i = 0; i < size; i++) ret[i] = ni(); return ret; } long[] nla(int size) throws IOException { long[] ret = new long[size]; for (int i = 0; i < size; i++) ret[i] = nl(); return ret; } double[] nda(int size) throws IOException { double[] ret = new double[size]; for (int i = 0; i < size; i++) ret[i] = nd(); return ret; } String nextLine() throws IOException { strTok = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!strTok.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; strTok = new StringTokenizer(s); } return false; } void printRepeat(String s, int count) { for (int i = 0; i < count; i++) out.print(s); } void printArray(int[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(long[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array, String spec) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.printf(Locale.US, spec, array[i]); } out.println(); } void printArray(Object[] array) { boolean blank = false; for (Object x : array) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } @SuppressWarnings("rawtypes") void printCollection(Collection collection) { boolean blank = false; for (Object x : collection) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } static String memoryStatus() { return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() >> 20) + "/" + (Runtime.getRuntime().totalMemory() >> 20) + " MB"; } public void pln() { out.println(); } public void pln(int arg) { out.println(arg); } public void pln(long arg) { out.println(arg); } public void pln(double arg) { out.println(arg); } public void pln(String arg) { out.println(arg); } public void pln(boolean arg) { out.println(arg); } public void pln(char arg) { out.println(arg); } public void pln(float arg) { out.println(arg); } public void pln(Object arg) { out.println(arg); } public void p(int arg) { out.print(arg); } public void p(long arg) { out.print(arg); } public void p(double arg) { out.print(arg); } public void p(String arg) { out.print(arg); } public void p(boolean arg) { out.print(arg); } public void p(char arg) { out.print(arg); } public void p(float arg) { out.print(arg); } public void p(Object arg) { out.print(arg); } }
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 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 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++) { for (int j = 0; j < 30; j++) { a[i] += ans[i][j] * (1 << j); } } //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 (1 << 30) - 1; 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 INF = 2000000000; const int N = 1e5 + 5; const int TN = 1 << 18; const double PI = acos(-1); int n, m, k; long long tree[TN], lazy[TN] = {0}; void probagate(int l, int r, int p) { tree[p] |= lazy[p]; if (l != r) { lazy[p << 1] |= lazy[p]; lazy[(p << 1) | 1] |= lazy[p]; } lazy[p] = 0; } void update(int val, int ql, int qr, int l = 0, int r = n - 1, int p = 1) { probagate(l, r, p); if (ql > r || qr < l) return; if (ql <= l && r <= qr) { lazy[p] |= val; probagate(l, r, p); return; } int m = (l + r) >> 1; update(val, ql, qr, l, m, (p << 1)); update(val, ql, qr, m + 1, r, (p << 1) | 1); tree[p] = tree[(p << 1)] & tree[(p << 1) | 1]; } int get(int ql, int qr, int l = 0, int r = n - 1, int p = 1) { probagate(l, r, p); if (ql > r || qr < l) return -1; if (ql <= l && r <= qr) { return tree[p]; } int m = (l + r) >> 1; return get(ql, qr, l, m, (p << 1)) & get(ql, qr, m + 1, r, (p << 1) | 1); } void print(int l = 0, int r = n - 1, int p = 1) { probagate(l, r, p); if (l == r) { printf("%d ", tree[p]); return; } int m = (l + r) >> 1; print(l, m, (p << 1)); print(m + 1, r, (p << 1) | 1); } int main() { scanf("%d%d", &n, &m); vector<pair<pair<int, int>, int>> v; for (int i = 0; i < m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--; b--; v.push_back({{a, b}, c}); update(c, a, b); } for (int i = 0; i < m; i++) { int a = v[i].first.first; int b = v[i].first.second; int c = v[i].second; if (get(a, b) != c) { return puts("NO"); } } puts("YES"); print(); }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; const long long mod = 1e9 + 7; const int phash = 3; const long long modhash = 1000000000000000003; int a[100005]; int tree[400005]; void build(int v, int tl, int tr) { if (tl == tr) tree[v] = a[tl]; else { int tm = (tl + tr) / 2; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); tree[v] = tree[v * 2] & tree[v * 2 + 1]; } } int query(int v, int tl, int tr, int l, int r) { if (l > r) return (1 << 30) - 1; if (tl == l && tr == r) return tree[v]; int tm = (tl + tr) / 2; return (query(v * 2, tl, tm, l, min(r, tm)) & query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r)); } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> l(m), r(m), q(m); for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> q[i]; l[i]--; } vector<int> sums(n + 1); for (int bit = 0; bit <= 30; bit++) { for (int i = 0; i <= n; i++) sums[i] = 0; for (int i = 0; i < m; i++) { if (q[i] & (1LL << bit)) { sums[l[i]]++; sums[r[i]]--; } } for (int i = 0; i < n; i++) { if (i > 0) sums[i] += sums[i - 1]; if (sums[i] > 0) a[i] |= (1 << bit); } } build(1, 0, n); for (int i = 0; i < m; i++) { if (query(1, 0, n, l[i], r[i] - 1) != q[i]) { cout << "NO"; return 0; } } cout << "YES" << '\n'; for (int i = 0; i < n; i++) cout << a[i] << " "; }
CPP
482_B. Interesting Array
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
2
8
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.TreeSet; public class InterestingArray { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(); int n = sc.nextInt(), m = sc.nextInt(); int N = 1; while (N < n) N <<= 1; int[] a = new int[N + 1]; 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(); } /* * for (int bit = 0; bit <= 30; bit++) { int[] sum = new int[n]; for (int i = 0; * i < m; i++) { if ((q[i] & (1 << bit)) > 0) { //out.println("bit is "+bit); * sum[l[i] ]++; if (r[i] < n) sum[r[i]]--; } } for (int i = 0; i < n; i++) { if * (i > 0) sum[i] += sum[i - 1]; if (sum[i] > 0) a[i + 1] |= 1 << bit; } } */ int num=0; for (int i = 0; i < 31; i++) { int [] sum=new int[4*N]; for(int k=0;k<m;k++) { if((q[k]&(1<<i)) >0) { sum[l[k]]++; sum[r[k]+1]--; } } for(int araf=1;araf<=n;araf++) { sum[araf]+=sum[araf-1]; if(sum[araf]>0) a[araf]|=(1<<i); } } /*for (int d : a) System.out.println(d);*/ StringBuilder sb = new StringBuilder(); SegmentTree s = new SegmentTree(a); boolean flag=true; for (int i = 0; i < m; i++) { if (s.query(l[i], r[i]) != q[i]) { flag =false; break; } } if(flag) { sb.append("YES" + "\n"); for (int k = 1; k <=n; k++) sb.append(a[k] + " "); sb.append("\n"); } else { sb.append("NO"); } System.out.println(sb); } 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]; } } 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 << 30) - 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 { 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; int m, n; const int N = 10e5 + 5; vector<int> l(N), r(N), q(N), SS(N), SC(N), a(N); bool work(int x) { fill(SS.begin(), SS.end(), 0); fill(SC.begin(), SC.end(), 0); for (int i = 0; i < m; i++) { if (q[i] >> (x - 1) & 1) { SS[l[i]]++; SS[r[i] + 1]--; } } for (int i = 1; i <= n; i++) { SS[i] += SS[i - 1]; SC[i] = SC[i - 1] + (SS[i] == 0); a[i] |= (SS[i] != 0) << (x - 1); } for (int i = 0; i < m; i++) { if (!(q[i] >> (x - 1) & 1) && (SC[r[i]] - SC[l[i] - 1]) == 0) return false; } return true; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) cin >> l[i] >> r[i] >> q[i]; for (int i = 1; i <= 30; i++) { if (!work(i)) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; 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
#include <bits/stdc++.h> using namespace std; int n, m, q[31][100100], pa[31][100100], ar[100100]; vector<int> v1[31][100100]; int main() { if (fopen("input.in", "r")) freopen("input.in", "r", stdin); cin >> n >> m; for (int a, b, c, i = 0; i < m; i++) { cin >> a >> b >> c; for (int j = 0; j < 31; j++) { if ((c >> j) & 1) { q[j][a]++; q[j][b + 1]--; } else v1[j][a].push_back(b); } } for (int i = 0; i < 31; i++) { int curr = 0; for (int j = 1; j <= n; j++) { curr += q[i][j]; if (curr) pa[i][j] = 1 + pa[i][j - 1]; else pa[i][j] = pa[i][j - 1]; } } for (int i = 0; i < 31; i++) { for (int j = 1; j <= n; j++) { for (int k = 0; k != v1[i][j].size(); k++) if (v1[i][j][k] - j + 1 == pa[i][v1[i][j][k]] - pa[i][j - 1]) { cout << "NO"; return 0; } if (pa[i][j] - pa[i][j - 1]) ar[j] |= (1 << i); } } cout << "YES" << endl; for (int i = 1; i <= n; i++) cout << ar[i] << " "; return 0; }
CPP