Editorial for Trò chơi vòng kẹo


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: YugiHacker

Để làm được bài này, bạn cần biết kĩ thuật mảng hiệu:

VNOI prefix sum

Gọi $d[i]$ là hiệu của ~a[i] - a[i-1]~.

Vậy ~a[i] = d[1] + d[2] + \cdots + d[i]~

Với mỗi truy vấn, có ~2~ trường hợp:

  • ~l \le r~: Thao tác của mảng hiệu bình thường, ~d[l] += 1~, ~d[r+1] -= 1~.
  • ~r < l~: Do đoạn cần cập nhật có thể xoay vòng, nên thao tác này có thể tách thành ~2~ thao tác là tăng đoạn ~l~ đến ~n~, và đoạn từ ~1~ đến ~r~

Sau khi có được mảng ~d~, ta chỉ cần cộng dồn lại để được mảng ~a~ và lấy số max và in ra.


Comments

Please read the guidelines before commenting.


There are no comments at the moment.