fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MAX_N = 500005;
  6.  
  7. int n, k, a[MAX_N], p[MAX_N], ip[MAX_N];
  8. int numQuery, l[MAX_N], r[MAX_N];
  9. bool ans[MAX_N];
  10.  
  11. vector<int> ids[MAX_N];
  12.  
  13. /// xu li offline cho nen phai luu lai cau hoi
  14.  
  15.  
  16. map<int, int> mark[MAX_N];
  17.  
  18. /// lưu lại thông tin
  19.  
  20. /// mark[i][j] có nghĩa là vị trí gần nhất
  21.  
  22. /// có giá trị bằng j mà ta có thể với tới.
  23.  
  24. /// với tới có nghĩa là xóa được hoàn toàn giữa chúng
  25.  
  26.  
  27.  
  28. map<int, bool> reach[MAX_N];
  29.  
  30. /// reach[i][j] = true. nếu có thể xóa được đoạn [i, j]
  31.  
  32.  
  33. int main() {
  34. ios_base::sync_with_stdio(0);
  35. cin.tie(0);
  36. cout.tie(0);
  37.  
  38. freopen("XMAS.inp", "r", stdin);
  39. freopen("XMAS.out", "w", stdout);
  40.  
  41. cin >> n >> k;
  42. for (int i = 1; i <= n; i++) {
  43. cin >> a[i];
  44. }
  45. cin >> numQuery;
  46. for (int i = 1; i <= numQuery; i++) {
  47. cin >> l[i] >> r[i];
  48. ids[r[i]].push_back(i);
  49. }
  50.  
  51. a[0] = -1;
  52. reach[0][0] = true;
  53.  
  54. for (int i = 1; i <= n; i++) {
  55.  
  56. /// xét a[i]
  57.  
  58. p[i] = mark[i - 1][k - a[i]];
  59.  
  60. /// p[i] = mark[i - 1] [k - a[i]]
  61.  
  62. /// vị trí gần nhất có giá trị là k - a[i]
  63.  
  64.  
  65.  
  66. ////ip[p[i]] = i;
  67.  
  68.  
  69. if (p[i]) {
  70.  
  71. /// em tìm được
  72.  
  73.  
  74. swap(mark[i], mark[p[i] - 1]);
  75.  
  76. swap(reach[i], reach[p[i] - 1]);
  77. }
  78.  
  79. mark[i][a[i]] = i;
  80.  
  81.  
  82.  
  83. reach[i][i] = true;
  84. for (int id : ids[i]) {
  85.  
  86. /// [l1, i] -> reach[l1][i] = true hay false
  87.  
  88. /// [l2, i]
  89.  
  90. /// [l3, i]
  91.  
  92.  
  93.  
  94. ans[id] = reach[r[id]][l[id] - 1];
  95. }
  96. }
  97.  
  98. for (int i = 1; i <= numQuery; i++) {
  99. cout << (ans[i] ? "YES\n" : "NO\n");
  100. }
  101.  
  102. return 0;
  103. }
  104.  
  105.  
Success #stdin #stdout 0.02s 66488KB
stdin
Standard input is empty
stdout
Standard output is empty