B. Interesting Array(线段树)

系统管理员 2021-11-02 08:46 294阅读 0赞

知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。
题目链接:http://codeforces.com/problemset/problem/482/B
We’ll call an array of n non-negative integers a[1], a[2], …, a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value should be equal to qi.

Your task is to find any interesting array of n elements or state that such array doesn’t exist.

Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as “&”, in Pascal — as “and”.
Input

The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits.

Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit.
Output

If the interesting array exists, in the first line print “YES” (without the quotes) and in the second line print n integers a[1], a[2], …, a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them.

If the interesting array doesn’t exist, print “NO” (without the quotes) in the single line.
Examples
Input
Copy

3 1
1 3 3

Output
Copy

YES
3 3 3

Input
Copy

3 2
1 3 3
1 3 2

Output
Copy

NO

分析:
涨姿势的一个题。还是菜鸡人生呀。 
对于给出的一个区间如 1 5 3 那么1到5这5个元素的二进制一定是&3等于3的(通俗讲就是这5个元素的二进制的第1和第2位都是为1的,因为3的二进制第一第二位是为1的)。
同理,对于给出的每个 l rq 的操作,都有这样的性质。
那么,我们可以知道val[1] 是在区间1到2 和区间1-3 ……里面的。所以val[1]的值一定要满足这些区间的性质。
同理,我们可以得到其他的值。通过更新可以得到区间的&值。
参考博客:https://blog.csdn.net/Seven_Jun/article/details/51910425
https://blog.csdn.net/qq_31759205/article/details/52454005

  1. #include"stdio.h"
  2. #include"string.h"
  3. #include"vector"
  4. #include"algorithm"
  5. using namespace std;
  6. typedef pair<int,int> PII;
  7. struct Node
  8. {
  9. int l;int r;int x;
  10. }node[100010];
  11. int n,m;
  12. int val[5000101];
  13. int num_val[2000101],top;
  14. void Push_down(int id)
  15. {
  16. val[id] = val[id << 1] & val[id << 1 | 1]; return ;
  17. }
  18. void Update(int id,int L,int R,int l,int r,int v)
  19. {
  20. if(l <= L && r >= R)
  21. {
  22. val[id] |= v; return ;
  23. }
  24. int mid = (L + R) >> 1;
  25. if(l <= mid) Update(id << 1,L,mid,l,r,v);
  26. if(r > mid) Update(id << 1 | 1,mid + 1,R,l,r,v);
  27. }
  28. void seek(int id,int L,int R,int x)
  29. {
  30. x |= val[id];
  31. if(L == R)
  32. {
  33. val[id] = x; num_val[L] = val[id];
  34. return ;
  35. }
  36. int mid = (L + R) >> 1;
  37. seek(id << 1,L,mid,x);
  38. seek(id << 1 | 1,mid + 1,R,x);
  39. Push_down(id);
  40. }
  41. int Query(int id,int L,int R,int l,int r)
  42. {
  43. if(l <= L && r >= R) return val[id];
  44. int mid = (L + R) >> 1;
  45. int A = -1,B = -1;
  46. if(l <= mid) A = Query(id << 1,L,mid,l,r);
  47. if(r > mid) B = Query(id << 1 | 1,mid + 1,R,l,r);
  48. if(A == -1) return B;
  49. if(B == -1) return A;
  50. return A & B;
  51. }
  52. int main()
  53. {
  54. scanf("%d%d",&n,&m);
  55. for(int i = 1; i <= m;i ++)
  56. {
  57. scanf("%d%d%d",&node[i].l,&node[i].r,&node[i].x);
  58. Update(1,1,n,node[i].l,node[i].r,node[i].x);
  59. }
  60. seek(1,1,n,0);
  61. for(int i = 1; i <= m; i ++)
  62. {
  63. int ans = Query(1,1,n,node[i].l,node[i].r);
  64. if(ans == node[i].x) continue;
  65. printf("NO\n"); return 0;
  66. }
  67. printf("YES\n");
  68. for(int i = 1; i<= n;i ++)
  69. printf("%d ",num_val[i]);
  70. printf("\n");
  71. }

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读