Happy segments

3.2

98 votes
Advanced Data Structures, Data Structures, Lazy Propagation in Interval/Segment Trees
Problem

You are given the following:

  • An integer m
  • An array a1, a2, , an
  • Integers from 1 to m

For every number c in range [1:m], you know its happiness is hc. A subarray is called happy, if for every number c in this subarray, the occurrence of c is equal to hc

You are given q queries. The ith query consists of two numbers l,r. Your task is to determine if subarray al, al+1, ..., ar is happy or not.

Input format

  • The first line contains two integers n and m denoting the length of the array and constant number.
  • The second line contains n numbers a1, a2, , an denoting the elements of the array.
  • The third line contains m numbers h1, h2, , hm denoting the elements of the array.
  • The next line contains a number q.
  • The next q lines describe queries and contain two integers l,r.

Output format

For each query, print 1 if subarray al, al+1, , aris happy. Otherwise, print 0.

Constraints

1n,m,q5×105

1aim

1hin

1lrn

 

Sample Input
8 4
2 2 1 4 4 2 4 4
1 2 3 4
6
1 2
1 5
1 3
2 8
3 3
5 8
Sample Output
1
0
1
1
1
0
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Subarray a2,a3,...,a8 is happy, because occurrenceof 1 is equal to 1,  occurrenceof  2 is equal to 2,  occurrenceof 3 is equal to 3 and occurrenceof 4 is equal to 4.

Happiness condition also holds for 1st, 3rd and 5th queries. Numbers 4 and 2 are making subarray unhappy for 2nd and 6 query.

Editor Image

?