Editorial for Nhỏ lớn lớn nhỏ


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Yunan

Tiến hành sắp xếp mảng a. Với mỗi truy vấn, tiến hành tìm chỉ số phần tử nhỏ nhất thỏa mãn lớn hơn hoặc bằng x và chỉ số phần tử nhỏ nhất thỏa mãn lớn hơn x bằng Tìm kiếm nhị phân (có thể sử dụng hàm lower_boundupper_bound). Gọi hai chỉ số đó lần lượt là ij. Khi đó:

  • Phần tử nhỏ nhất thỏa mãn lớn hơn hoặc bằng xa_i.
  • Phần tử nhỏ nhất thỏa mãn lớn hơn xa_j.
  • Phần tử lớn nhất thỏa mãn nhỏ hơn hoặc bằng xa_{j-1}.
  • Phần tử lớn nhất thỏa mãn nhỏ hơn xa_{i-1}.

Độ phức tạp: O(n.log(n)+q.log(n)).


Comments