#P112. [TFXOI Round 1] 过往与现实之历史

[TFXOI Round 1] 过往与现实之历史

Source

This problem is proposed by . All rights reserved.

Problem Source: https://www.luogu.com.cn/problem/T565374

Problem Background

Time and space intertwine chaotically, where "deities" blend with illusions.

Problem Description

You are given a sequence {sn}\{s_n\} consisting of nn distinct integers.

There are mm modification operations, each inserting all numbers from the original sequence's interval [li,ri][l_i, r_i] at the end of the current sequence.

Then, there are qq queries. Each query asks for the maximum length of a "good sequence" starting with the number tit_i on the interval inserted in the xix_i-th operation.

Note that all queries are performed after all insertions are completed.

A "good sequence" [l,r][l, r] satisfies that the count of every number in [l,r][l, r] is k\le k.

Input Format

The first line contains four integers n,k,m,qn, k, m, q.

The second line contains nn integers {sn}\{s_n\}.

The next mm lines each contain two integers li,ril_i, r_i.

The following qq lines each contain two integers xi,tix_i, t_i.

Output Format

Output qq lines, each containing one integer as the answer.

Samples

4 1 2 1
1 2 3 4
1 2
2 4
0 1
4
8 2 4 2
1 2 3 4 5 6 7 8
1 5
2 8
6 8
3 6
1 1
0 6
15
15
3 1 1 1
76546 39549 18452
1 1
0 18452
2

Sample 1 Explanation

  • Initial sequence: S0={1,2,3,4}S_0= \{1,2,3,4\}.
  • After the first operation: S1={1,2,3,4,1,2}S_1= \{1,2,3,4,1,2\}.
  • After the second operation: S2={1,2,3,4,1,2,2,3,4}S_2= \{1,2,3,4,1,2,2,3,4\}.

Sample 2 Explanation

  • Initial sequence: S0={6,7,8,1,2,3,4,5,2,3,4,5,6,7,8}S_0= \{6,7,8,1,2,3,4,5,2,3,4,5,6,7,8\}.
  • After the first operation: S1={1,2,3,4,5,2,3,4,5,6,7,8,6,7,8}S_1= \{1,2,3,4,5,2,3,4,5,6,7,8,6,7,8\}.
  • Final sequence: $S_4= \{1,2,3,4,5,6,7,8,1,2,3,4,5,2,3,4,5,6,7,8,6,7,8,3,4,5,6\}$.

Data Range

This problem uses bundled tests.

The problem is slightly time-sensitive, so please optimize your code for efficiency.

For all data:

  • 1n,m,k2×1051\le n,m,k\le2\times10^5.
  • 1q3×1051\le q\le3\times10^5.
  • 1lirin1\le l_i\le r_i\le n.
  • 1si,ti1091\le s_i,t_i\le10^9.
Subtask ID nn kk mm qq Memory Limit Points
11 200\le200 =2×105=2\times10^5 200\le200 3×105\le3\times10^5 6464 MB 1010
22 200\le200 200\le200
33 3×103\le3\times10^3 =1=1 2×105\le2\times10^5 3×103\le3\times10^3
44 2×105\le2\times10^5 2×105\le2\times10^5 50\le50 3×105\le3\times10^5 1515
55 3×103\le3\times10^3 2×105\le2\times10^5 3\le3 256256 MB
66 105\le10^5 105\le10^5 6464 MB 2020
77 2×105\le2\times10^5 2×105\le2\times10^5 3×105\le3\times10^5