#P8995. [北大集训 2021] 随机数据

[北大集训 2021] 随机数据

Background

CTT2021 D4T3

Problem Description

There are nn items, numbered 0,,n10, \cdots, n-1. The value of item ii is defined as viv_i.

A and B take turns playing a game, which consists of multiple rounds.

At the start of each round, if all available items have been taken, the game ends immediately. Otherwise, A must choose an untaken item and take it.

Suppose the item A takes has index ii. Next, B may choose one untaken item from item (id+n)modn(i - d + n) \bmod n and item (i+d)modn(i + d) \bmod n to take, or B may choose to skip this action. Then the game proceeds to the next round. In particular, if both of these items have already been taken, B can only choose to skip.

Both A and B want to maximize the sum of values of the items they take. We assume both A and B play optimally.

In addition, before the game starts, some items may be unavailable. During the game, unavailable items are ignored, i.e., neither A nor B can take unavailable items. When all available items have been taken, the game ends immediately.

Initially, all items are available. Your program needs to support qq operations. In each operation, given an xx: if item xx is unavailable, it becomes available; if it is available, it becomes unavailable. After each operation, you need to answer: assuming the game starts from the current state, what is the sum of values of the items taken by B when the game ends.

Unfortunately, this is an IO problem: the number of items may be up to 101610^{16}. As an OIer, you cannot handle data at such a large scale, so viv_i will be generated in a special way: given an array ww of length mm, vi=wimodmv_i = w_{i \bmod m}.

Input Format

The first line contains four positive integers n,d,m,qn, d, m, q, with $1 < n \le 10^{16}, 1 \le d < n, 1 \le m \le 2 \times 10^4, q \le 10^5$.

The second line contains mm integers. The ii-th integer represents the value of wi1w_{i-1}, with 1wi4001 \le w_i \le 400.

The next qq lines each contain an integer xx, representing an operation on item xx. It is guaranteed that 0x<n0 \le x < n.

Output Format

Output qq lines, each containing one integer, corresponding to the answer after each operation.

5 2 3 2
1 3 2
1
1

3
4

10 4 5 5
40 355 190 215 161
3
4
0
3
4

581
460
420
541
702

Hint

Subtask 1 (5 pts): n20,q=1n \le 20, q = 1.

Subtask 2 (10 pts): n105,q=1n \le 10^5, q = 1.

Subtask 3 (15 pts): n,q105n, q \le 10^5.

Subtask 4 (30 pts): q=1q = 1.

Subtask 5 (40 pts): No special constraints.

If needed, you can use __int128 to handle long long multiplication modulo. Below is an example of using __int128 to compute a×bmodma \times b \bmod m:

long long a = 1e15;
long long b = 1e15;
long long m = 12345678910;
long long c = ((__int128) a * b) % m;

Translated by ChatGPT 5