#P5673. 「SWTR-2」Picking Gifts

「SWTR-2」Picking Gifts

Description

There are many items in the shop. Each item has:

  • An index, from left to right as 1,2,,n1,2,\dots,n.

  • A category, as p1,p2,,pnp_1,p_2,\dots,p_n.

  • A value, as v1,v2,,vnv_1,v_2,\dots,v_n.

Sunny\mathrm{Sunny} wants to choose an interval and buy all gifts in this interval to give to little b\mathrm{b}.

Little b\mathrm{b} will check the gifts from right to left. If she sees that gifts of the same category have appeared kk times, then she will not check this category anymore (including the kk-th one). Of course, those gifts will then lose their original value.

Now, Sunny\mathrm{Sunny} gives you mm intervals and wants you to compute, in little b\mathrm{b}'s view, the value of each interval.

See the sample for the detailed value calculation.

Input Format

The first line contains three positive integers n,m,kn,m,k separated by spaces.

The second line contains nn positive integers separated by spaces, where the ii-th number represents pip_i.

The third line contains nn positive integers separated by spaces, where the ii-th number represents viv_i.

The next mm lines each contain two positive integers li,ril_i,r_i, representing the interval of the ii-th query.

Output Format

Output mm lines. For each query, output one integer vv in one line, the value of this interval in little b\mathrm{b}'s view.

6 11 3
1 1 1 2 1 3
7 3 8 9 6 5
1 1
1 2
1 3
1 4
1 5
1 6
2 6
3 6
4 6
5 6
6 6
7
10
11
20
23
28
28
28
20
11
5

Hint


Sample Explanation

[1,1]:7[1,1]:7.

[1,2]:3+7=10[1,2]:3+7=10.

[1,3]:8+3=11[1,3]:8+3=11, because the category of the gift with index 11 is 11, and this is the k(k=3)k(k=3)-th time category 11 appears, so she will not check this category anymore (including this one).

[1,4]:9+8+3=20[1,4]:9+8+3=20.

[2,6]:5+6+9+8=28[2,6]:5+6+9+8=28.

[3,6]:5+6+9+8=28[3,6]:5+6+9+8=28.


Constraints and Notes

Testdata 141-4: n100,m100n\leq 100,m\leq 100.

Testdata 565-6: n5000,m5000n\leq 5000,m\leq 5000.

Testdata 7107-10: n2×104,m104n\leq 2\times 10^4,m\leq 10^4.

Testdata 111511-15: n2×105,m2×105n\leq 2\times 10^5,m\leq 2\times 10^5.

Testdata 162016-20: n106,m5×105n\leq 10^6,m\leq 5\times 10^5.

For testdata 1,2,7,8,11,12,16,171,2,7,8,11,12,16,17, k=nk=n. For the other testdata, 2k<n2\leq k<n.

For all testdata, $1\leq p_i\leq n,1\leq v_i\leq 2000,1\leq l \leq r \leq n$.


For testdata 1101-10, each is worth 33 points.

For testdata 112011-20 where k=nk=n, each is worth 44 points.

All other testdata are worth 99 points each.


For testdata 161-6, the time limit is 500ms500\rm ms.

For testdata 7157-15, the time limit is 750ms750\rm ms.

For testdata 162016-20, the time limit is 1.5s1.5\rm s.


Of course, the setters of SWTR-02 cannot possibly have girlfriends.

Translated by ChatGPT 5