<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p>

<p>Find a pair of indices <code>(i, j)</code> such that:</p>

<ul>
	<li><code>i != j</code>,</li>
	<li><code>abs(i - j) &lt;= indexDiff</code>.</li>
	<li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li>
</ul>

<p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> We can choose (i, j) = (0, 3).
We satisfy the three conditions:
i != j --&gt; 0 != 3
abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3
abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
<strong>Output:</strong> false
<strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
</pre>

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

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

#ifndef FENWICK_TREE_HPP
#define FENWICK_TREE_HPP
#include <vector>
template <typename T, typename V = std::vector<T>>
class FenwickTree {
    // One-based Fenwick tree with a twist (A[0] saves nums[0])
    V A;
    // for all operations T must support 'a += b'
    // for get(), set(), range_sum(), cast to V, T must support 'a -= b'
    // for range_query(), lower_bound(), T must support 'a -= b' and 'a < b'
    public:
    typedef typename V::size_type size_type;
    typedef typename V::value_type value_type;
    private:
    static const size_type LSB(const size_type i) {
        return i & -i;
    }

    // Convert A[] in place to Fenwick tree form
    static void init(V &A) {
        for (size_type i = 1; i < A.size(); ++i) {
            auto j = i + LSB(i);
            if (j < A.size())
                A[j] += A[i];
        }
    }

    // Convert back to array of per-element counts
    static void fini(V &A) {
        for (size_type i = A.size(); i-- > 1;) {
            auto j = i + LSB(i);
            if (j < A.size())
                A[j] -= A[i];
        }
    }
    public:
    FenwickTree(size_type size = 0):A(size) {
    }

    FenwickTree(const V &nums):A(nums) {
        init(A);
    }

    FenwickTree(V &&nums):A(std::move(nums)) {
        init(A);
    }

    FenwickTree(const FenwickTree &) = default;
    FenwickTree(FenwickTree &&) = default;

    FenwickTree &operator = (const V & A) {
        this->A = A;
        init(this->A);
        return *this;
    }

    FenwickTree &operator = (V && A) {
        this->A = std::move(A);
        init(this->A);
        return *this;
    }

    FenwickTree &operator = (const FenwickTree &) = default;   
    FenwickTree &operator = (FenwickTree &&) = default;

    operator V () const & {
        V ret = A;
        fini(ret);
        return ret;
    }
    operator V () && {
        fini(A);
        return std::move(A);
    }

    // Returns the sum of the first i elements (indices 0 to i)
    // Equivalent to range_sum(0, i)
    T prefix_sum(size_type i) const {
        // assert(i < A.size());
        T sum = A[0];
        for (; i != 0; i -= LSB(i))
            sum += A[i];
        return sum;
    }

    // Add delta to element with index i (zero-based)
    void add(size_type i, T delta) {
        // assert(i < A.size());
        if (i == 0) {
            A[0] += delta;
            return;
        }
        for (; i < A.size(); i+= LSB(i))
            A[i] += delta;
    }

    // Returns sum of nums[i + 1] to nums[j].
    // Same as prefix_sum(j) - prefix_sum(i), but a bit faster
    T range_sum(size_type i, size_type j) const {
        T sum = 0;
        // assert(0 < i);
        // assert(i < j);
        // assert(j < A.size());
        for (; j > i; j -= LSB(j))
            sum += A[j];
        for (; i > j; i -= LSB(i))
            sum -= A[i];
        return sum;
    }
    constexpr size_type size() const { return A.size(); }
    constexpr bool empty() const { return A.empty(); }
};
#endif
class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        const unsigned n = nums.size();
        #if 0
        deque<unsigned> mins;
        deque<unsigned> maxs;
        for (unsigned i = 0; i < n; ++i) {
            if (mins.front() + k + 1 == i)
                mins.pop_front();
            if (maxs.front() + k + 1 == i)
                maxs.pop_front();
            auto num = nums[i];
            while(!mins.empty() && nums[mins.back()] >= num) {
                if (nums[mins.back()] - num <= t)
                    return true;
                mins.pop_back();
            }
            while(!maxs.empty() && nums[maxs.back()] <= num) {
                if (num - nums[maxs.back()] <= t)
                    return true;
                maxs.pop_back();
            }
            if (!mins.empty() && num - nums[mins.back()] <= t) {
                //cout << mins.back() << '>' << i << endl;
                return true;
            }
            if (!maxs.empty() && nums[maxs.back()] - num <= t) {
                //cout << maxs.back() << '<' << i << endl;
                return true;
            }
            mins.push_back(i);
            maxs.push_back(i);
        }
        #elif 0
        map<int, unsigned> s;
        for (unsigned i = 0; i < n; ++i) {
            if (i > k) {
                auto prev = s.find(nums[i - k - 1]);
                if (--prev->second == 0)
                    s.erase(prev);
            }
            auto num = nums[i];
            auto lb = s.lower_bound(num);
            if (lb != s.end() && lb->first - num <= t)
                return true;
            if (lb != s.begin()) {
                auto ub = prev(lb);
                if (num - ub->first <= t)
                    return true;
            }
            ++s[num];
        }
        #else
        auto sorted = nums;
        sort(sorted.begin(), sorted.end());
        sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
        FenwickTree<unsigned> ft(sorted.size());
        map<int, unsigned> s;
        for (unsigned i = 0; i < n; ++i) {
            if (i > k) {
                auto prev = lower_bound(sorted.begin(), sorted.end(), nums[i - k - 1]) - sorted.begin();
                ft.add(prev, -1);
            }
            auto num = nums[i];
            auto lb = lower_bound(sorted.cbegin(), sorted.cend(), nums[i]);
            auto ni = lb - sorted.cbegin();
            auto li = lower_bound(sorted.cbegin(), lb, nums[i] - t) - sorted.cbegin();
            auto ui = upper_bound(lb, sorted.cend(), nums[i] + t) - sorted.cbegin();
            if ((li == 0 ? ft.prefix_sum(ui - 1) : ft.range_sum(li - 1, ui - 1)))
                return true;
            ft.add(ni, +1);
        }
        #endif
        return false;
    }
};
98540