<p>You are given an array <code>nums</code>. You can rotate it by a non-negative integer <code>k</code> so that the array becomes <code>[nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]</code>. Afterward, any entries that are less than or equal to their index are worth one point.</p>

<ul>
	<li>For example, if we have <code>nums = [2,4,1,3,0]</code>, and we rotate by <code>k = 2</code>, it becomes <code>[1,3,0,2,4]</code>. This is worth <code>3</code> points because <code>1 &gt; 0</code> [no points], <code>3 &gt; 1</code> [no points], <code>0 &lt;= 2</code> [one point], <code>2 &lt;= 3</code> [one point], <code>4 &lt;= 4</code> [one point].</li>
</ul>

<p>Return <em>the rotation index </em><code>k</code><em> that corresponds to the highest score we can achieve if we rotated </em><code>nums</code><em> by it</em>. If there are multiple answers, return the smallest such index <code>k</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,3,1,4,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Scores for each k are listed below: 
k = 0,  nums = [2,3,1,4,0],    score 2
k = 1,  nums = [3,1,4,0,2],    score 3
k = 2,  nums = [1,4,0,2,3],    score 3
k = 3,  nums = [4,0,2,3,1],    score 4
k = 4,  nums = [0,2,3,1,4],    score 3
So we should choose k = 3, which has the highest score.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,0,2,4]
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums will always have 3 points no matter how it shifts.
So we will choose the smallest k, which is 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt; nums.length</code></li>
</ul>

class SegmentTree {
public:
    SegmentTree(int n) : n(n) {
        tree.assign(4 * n, 0);
        lazy.assign(4 * n, 0);
    }

    void update(int l, int r) {
        update(0, 0, n - 1, l, r);
    }

    int query(int l, int r) {
        return query(0, 0, n - 1, l, r);
    }


private:
    int n;
    std::vector<int> tree;
    std::vector<int> lazy;

    void push(int node, int start, int end) {
        if (lazy[node] != 0) {
            tree[node] += lazy[node];
            if (start != end) {
                lazy[2 * node + 1] += lazy[node];
                lazy[2 * node + 2] += lazy[node];
            }
            lazy[node] = 0;
        }
    }

    void update(int node, int start, int end, int l, int r) {
        push(node, start, end);
        if (start > end || start > r || end < l) return;
        if (start >= l && end <= r) {
            tree[node] += 1;
            if (start != end) {
                lazy[2 * node + 1] += 1;
                lazy[2 * node + 2] += 1;
            }
            return;
        }
        int mid = (start + end) / 2;
        update(2 * node + 1, start, mid, l, r);
        update(2 * node + 2, mid + 1, end, l, r);
        tree[node] = std::max(tree[2 * node + 1], tree[2 * node + 2]);
    }

    // Query the maximum value in the range [l, r]
    int query(int node, int start, int end, int l, int r) {
        push(node, start, end);
        if (start > end || start > r || end < l) return INT_MIN;
        if (start >= l && end <= r) return tree[node];
        int mid = (start + end) / 2;
        int leftQuery = query(2 * node + 1, start, mid, l, r);
        int rightQuery = query(2 * node + 2, mid + 1, end, l, r);
        return std::max(leftQuery, rightQuery);
    }
};

class Solution {
public:
    int bestRotation(vector<int>& A) {
        int n = A.size();
        SegmentTree s(n);
        for (int i=0 ; i<n ; i++)
            if (A[i] > i) {
                s.update(i+1, n-A[i]+i);
                // cout << "[" << i+1 << ", " << n-A[i]+i << "]\n";
            }
            else {
                s.update(0, i-A[i]);
                // cout << "[" << 0 << ", " << i-A[i] << "] ";
                if (i+1 <= n-1) {
                    s.update(i+1, n-1);
                    // cout << "[" << i+1 << ", " << n-1 << "] ";
                }
                // cout << "\n";
            }
        
        int ans=0, mx=s.query(0,0);
        for (int i=1 ; i<n ; i++) {
            int curr = s.query(i,i);
            if (curr > mx) {
                mx = curr;
                ans = i;
            }
        }
        return ans;
    }
};
97600