Official Editorial (C++)

Unofficial Alternate Solution (C++)

Implementation

Time Complexity: O(NlogN)\mathcal{O}(N \log N)

C++

#include <iostream>
#include <map>
#include <set>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main() {

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!