Common goods

0

0 votes
Segment Trees, Data Structures, Segment tree, Hard, Open, Data Structures
Problem

There are N packets of goods each having some number of items in it. The number of items is in the form of an array A (A[i] items of the ith type). There are M number of people in total each having a share in the goods. They have shares in the form of L and R which means that they hold a share of goods [L....R]. You require Q items. You must identify the items one by one.
For each time you take an item, you are required to determine the number of people who have lost all the items from their share.

Note

  • You cannot get the items that are not available.
  • Here, 'lost all items' means that there are no items left in the share that you hold.

Input format

  • First line: An integer N representing the number of goods
  • Next N lines: N integers follow representing the number of items of each type of good
  • Next line: Two integers M and 2 where M denotes the number of people 
  • Next M lines: Two integers L and R representing their shares
  • Next line: An integer Q followed by Q integers representing the goods that you require
    Note: Each integer must be represented in a separate line.

Output format

Print Q space-separated integers representing the number of people who lost all the items of their share.

Constraints

1N,M,Q105

1A[i]105

1LRN

All Q integers will be in the range of 1 to N.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

After 2nd query goods are 2 1 0 0. So pair (3,3) is lost.

After 4th query goods are 1 0 0 0. So pairs (3,3) and (2,3) are lost .

After 5th query goods are 0 0 0 0. So all the 3 pairs are lost.

Editor Image

?