当前位置:   article > 正文

Codeforces Round 932 (Div. 2) ---- E. Distance Learning Courses in MAC ---- 题解

Codeforces Round 932 (Div. 2) ---- E. Distance Learning Courses in MAC ---- 题解

E. Distance Learning Courses in MAC:

题目大意:

思路解析:

// 对于这种二进制多个数计算答案,我们应该灵敏的想到是否可以通过枚举二进制位来计算答案。

就是对每一个查询找出或和的最大值,那我们想xi 和 yi中哪些位一定会出现在答案中,假设为25 和 31,他们两转为二进制为 (11001) 和 (11111)我们可以想到24一定会进入答案,如果它不是答案的一部分,那无论怎么选都无法满足选择的数大于等于x。那我们这样就可以对[l,r]的答案进行简单计算(这里利用线段树或者树状数组的区间查询即可),那后续剩下的答案怎么办。后续 x 和 y剩下的二进制位为 (00001) 和 (0111)我们发现对r的剩余无论选择哪一位都可以满足大于等于x,那我们可以对于这些剩下的数做一个前缀和的处理,如果有我们一定会把它假如答案。

那么如果假如剩下 有00100这一位,之前粗略的答案中也有00100这一位可以发现我们可以将其转化为00111,后面就可以不用枚举了。

代码实现:

  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4. public class Main {
  5. static int inf = (int) 1e9;
  6. static int mod = 998244353;
  7. public static void main(String[] args) throws IOException {
  8. int t = f.nextInt();
  9. while (t > 0) {
  10. solve();
  11. t--;
  12. }
  13. w.flush();
  14. w.close();
  15. br.close();
  16. }
  17. static int n;
  18. static int[] l;
  19. static int[] r;
  20. static int[] v;
  21. static int maxN = (int) 2e5 + 5;
  22. static int[] t = new int[maxN * 2];
  23. public static void solve() {
  24. n = f.nextInt();
  25. l = new int[n + 1];
  26. r = new int[n + 1];
  27. v = new int[n + 1];
  28. for (int i = 1; i <= n; i++) {
  29. l[i] = f.nextInt();
  30. r[i] = f.nextInt();
  31. }
  32. fix();
  33. int[][] bits = new int[31][n + 1];
  34. for (int i = 1; i <= n; i++) {
  35. update(i, v[i]);
  36. for (int j = 30; j >= 0; j--) {
  37. bits[j][i] = bits[j][i-1];
  38. if (((r[i] >> j) & 1) == 1) bits[j][i]++;
  39. }
  40. }
  41. int q = f.nextInt();
  42. for (int i = 0; i < q; i++) {
  43. int x = f.nextInt();
  44. int y = f.nextInt();
  45. int ans = sum(x, y);
  46. for (int j = 30; j >= 0; j--) {
  47. int cnt = bits[j][y] - bits[j][x-1] + ((ans >> j) & 1);
  48. if (cnt > 1) {
  49. ans |= (2 << j) - 1;
  50. break;
  51. } else if (cnt == 1) {
  52. ans |= (1 << j);
  53. }
  54. }
  55. w.print(ans + " ");
  56. }
  57. w.println();
  58. for (int i = 0; i <= n; i++) {
  59. t[i] = 0;
  60. }
  61. }
  62. // public static void fix(){
  63. // for (int i = 1; i <= n; i++) {
  64. // if (l[i] == r[i]){
  65. // v[i] = r[i];
  66. // l[i] = r[i] = 0;
  67. // continue;
  68. // }
  69. // int pref = (1 << (lg(l[i] ^ r[i]) + 1)) - 1;
  70. // v[i] = r[i] - (r[i] & pref);
  71. // r[i] = r[i] & pref;
  72. // l[i] = l[i] & pref;
  73. // }
  74. // }
  75. public static void fix() {
  76. for (int i = 1; i <= n; ++i) {
  77. if (l[i] == r[i]) {
  78. v[i] = l[i];
  79. l[i] = r[i] = 0;
  80. continue;
  81. }
  82. int pref = (1 << (lg(l[i] ^ r[i]) + 1)) - 1;
  83. v[i] = r[i] - (r[i] & pref);
  84. r[i] &= pref;
  85. l[i] &= pref;
  86. }
  87. }
  88. public static void update(int x, int val) {
  89. for (int i = x; i <= n; i += lowbit(i)) {
  90. t[i] |= val;
  91. }
  92. }
  93. public static int lg(int x) {
  94. for (int i = 30; i >= 0; i--) {
  95. if (((x >> i) & 1) == 1) return i;
  96. }
  97. return 0;
  98. }
  99. public static int sum(int l, int r) {
  100. int res = 0;
  101. while (l <= r) {
  102. res |= v[r];
  103. r--;
  104. while (r - lowbit(r) >= l) {
  105. res |= t[r];
  106. r -= lowbit(r);
  107. }
  108. }
  109. return res;
  110. }
  111. public static int lowbit(int x) {
  112. return x & -x;
  113. }
  114. static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
  115. static Input f = new Input(System.in);
  116. static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  117. static class Input {
  118. public BufferedReader reader;
  119. public StringTokenizer tokenizer;
  120. public Input(InputStream stream) {
  121. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  122. tokenizer = null;
  123. }
  124. public String next() {
  125. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  126. try {
  127. tokenizer = new StringTokenizer(reader.readLine());
  128. } catch (IOException e) {
  129. throw new RuntimeException(e);
  130. }
  131. }
  132. return tokenizer.nextToken();
  133. }
  134. public String nextLine() {
  135. String str = null;
  136. try {
  137. str = reader.readLine();
  138. } catch (IOException e) {
  139. // TODO 自动生成的 catch 块
  140. e.printStackTrace();
  141. }
  142. return str;
  143. }
  144. public int nextInt() {
  145. return Integer.parseInt(next());
  146. }
  147. public long nextLong() {
  148. return Long.parseLong(next());
  149. }
  150. public Double nextDouble() {
  151. return Double.parseDouble(next());
  152. }
  153. public BigInteger nextBigInteger() {
  154. return new BigInteger(next());
  155. }
  156. }
  157. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/401482
推荐阅读
相关标签
  

闽ICP备14008679号