DMOJ - Bob Equilibrium

Author: Benjamin Qi

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

Using centroid decomposition, we can

  • Update the value at some vertex vv in O(logN)\mathcal{O}(\log N) time
  • Query the sum of the values of all vertices that are distance exactly dd away from some vertex vv in O(logN)\mathcal{O}(\log N) time.

You can check the second approach for USACO At Large for an explanation of how to do this.

What we need for this problem is slightly different; first we need to process some number of updates of the following form:

  • Add 1 to the values of all vertices at most dd away from some vertex vv.

And then output the values at all vertices at the end. We can use prefix sums for this.

(Note: quite close to TL ...)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using pi = pair<int,int>;
using pl = pair<ll,ll>;

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!