[Tips] Use tree arrays to maintain unequal relations

For example, we want to find the logarithms of i and j that satisfy the following inequality: 1.i
<j
2.i> j-p[j]
3.j <i + p[i]

In this way, we can form a pair of j-p[j] and j. Sort j-p[j] from small to large, and then enumerate i from 1-n. For each i, first satisfy i> j-p[j ] J is added to the tree array, and then the value of the tree array from i + 1 to i + p[i]-1 can be queried.
The following is the approximate wording. In order to save trouble, the priority queue for pair sorting is not used.

        for(int i = 1; i <= n; i++)
        {
    
    
            while(!Q.empty() && Q.top().val < i)
            {
    
    
                add(Q.top().x, 1);
                Q.pop();
            }
            //printf("%d %d\n", i + p[i * 2] / 2 - 2, i - 1);
            ans += ask(i + p[i] - 1) - ask(i);
        }

Guess you like

Origin blog.csdn.net/weixin_43891021/article/details/109208321