[Codeforces] MultiSet

Posted lz87

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Codeforces] MultiSet相关的知识,希望对你有一定的参考价值。


Consider a multiset of integers S, the union of n closed intervals of positive integers: S = [l1..r1] ∪ [l2..r2] ∪ · · · ∪ [ln..rn]
(recall that a closed interval [l..r] contains integers {l, l + 1, . . . , r}).
Let D be the set of unique integers in S. For each x in D, find the number of occurrences of x in S.

Input:
The first line contains an integer n (1 ≤ n ≤ 100 000), the number of intervals in the union. Each of the next n lines contains
two integers li and ri (1 ≤ li ≤ ri ≤ 100 000), the left and right boundaries of the i-th interval.

Output:
For each integer x in D, print two integers on a separate line: x and its number of occurrences in S.

 

A naive solution is to add update all numbers‘ occurences in all intervals. This takes O(N^2) time, which is too slow for an input size of 10^6.

 

Prefix Sum solution in O(N) runtime and space, assuming there are at most N intervals, with all unique integers in range [1, N].

freq[i] is number of intervals that cover integer i. An integer‘s occurrence number is the same with the number of different intervals covering it.

At the start of interval[j],  we have 1 more covering interval at integer intervals[j][0];

At the end of an interval[j], we have 1 fewer covering interval at integer intervals[j][1] + 1. 

After the first for loop, we need 1 more for loop to calculate prefix sum. This sums up how many different intervals cover each integer.

 

 

public class IntervalFrequency {
    //Find the frequencies of all unique numbers covered by all intervals in O(N) time,
    //where N is Math.max(interval total number, total possible unique integers)
    //Assume all integers covered by all intervals  are in range [1, N]

    public int[] getFrequency(int[][] intervals, int N) {
        int[] freq = new int[N + 2];

        for(int i = 0; i < intervals.length; i++) {
            freq[intervals[i][0]]++;
            freq[intervals[i][1] + 1]--;
        }

        for(int i = 1; i < freq.length; i++) {
            freq[i] += freq[i - 1];
        }

        return freq;
    }
}

 

 

 

以上是关于[Codeforces] MultiSet的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces-808D Array Division multiset

Codeforces-808D Array Division (multiset 折半???)

(字典树)codeforces - 706D Vasiliy's Multiset

CodeForces 706D Vasiliy's Multiset

[codeforces1284D]New Year and Conference 离散化+multiset或排序+线段树

Codeforces 706D Vasiliy's Multiset(可持久化字典树)