#P10052. [CCO 2022] Double Attendance

[CCO 2022] Double Attendance

Problem Description

Because your school’s schedule is so strange, your two classes will start at the same time in two different classrooms. You can only be in one place at a time, so you can only walk back and forth between the two rooms, hoping to catch the key parts of both classes.

The two classrooms are numbered 11 and 22. In classroom ii, the teacher will show NiN_{i} different slides. The jj-th slide is shown during the time interval $\left(A_{i, j}, B_{i, j}\right)\left(0 \leq A_{i, j}<B_{i, j}\right)$, where Ai,jA_{i, j} and Bi,jB_{i, j} are times in seconds measured from the start of the class. In each classroom, there will never be multiple slides shown at the same time. For example, a class may show slides during (1,2)(1,2) and (5,6)(5,6), or during (10,20)(10,20) and (20,30)(20,30), but it will not show slides during (10,20)(10,20) and (19,30)(19,30).

You start the day in classroom 11, and both classes start at time 00. After that, at any moment (not necessarily an integer second), you may spend KK seconds moving from your current classroom to the other one. If you spend a positive amount of time in the correct classroom during a slide’s time interval, then you are considered to have seen that slide. During the KK seconds while you are moving between classrooms, you are in neither classroom, so you cannot see any slides.

For example, if classroom 11 shows a slide during (10,20)(10,20), classroom 22 shows a slide during (15,25)(15,25), and K=8K=8, then if you move from classroom 11 to classroom 22 at time 11.511.5 seconds (arriving at time 19.519.5 seconds), you can see both slides. On the other hand, if you leave classroom 11 at time 1717 seconds (arriving at time 2525 seconds), then you will enter classroom 22 only after its slide stops being shown, so you will miss it.

You want to know the maximum number of different slides you can see.

Input Format

The first line contains three integers N1,N2N_{1}, N_{2}, and KK, separated by spaces.

The next N1N_{1} lines each contain two integers A1,iA_{1, i} and B1,i(1iN1)B_{1, i}\left(1 \leq i \leq N_{1}\right), separated by spaces.

The next N2N_{2} lines each contain two integers A2,iA_{2, i} and B2,i(1iN2)B_{2, i}\left(1 \leq i \leq N_{2}\right), separated by spaces.

Output Format

Output one integer, the maximum number of different slides you can see.

3 1 8
10 20
100 101
20 21
15 25
3
1 5 3
1 100
1 2
2 3
3 4
4 5
5 6
4

Hint

Explanation for Sample 1

One possible optimal strategy is to stay in classroom 11 until time 11.511.5, then move to classroom 22 (arriving at time 19.519.5), stay until time 19.619.6, and finally return to classroom 11 (arriving at time 27.627.6). During this process, you will see all slides except the 33-rd slide. It is impossible to see all 44 slides.

Explanation for Sample 2

Even if you move to classroom 22 immediately when the classes begin (for example, at time 0.00010.0001), you will miss the first two slides shown there. Therefore, you can see at most four slides.

Constraints

For all testdata, 1Ni3×1051 \leq N_{i} \leq 3\times 10^5, 0Ai,j<Bi,j1090 \leq A_{i, j}<B_{i, j} \leq 10^{9}, 1K1091 \leq K \leq 10^{9}.

Subtask ID Points NiN_{i} Ai,j,Bi,jA_{i, j},B_{i, j}
11 2020 1Ni101 \leq N_{i} \leq 10 0Ai,j<Bi,j20000 \leq A_{i, j}<B_{i, j} \leq 2000
22 4040 1Ni20001 \leq N_{i} \leq 2000
33 2424 Bi,jAi,j2KB_{i, j}-A_{i, j} \leq 2 K
44 1616 1Ni3×1051 \leq N_{i} \leq 3\times 10^5 0Ai,j<Bi,j1090 \leq A_{i, j}<B_{i, j} \leq 10^{9}

Translated by ChatGPT 5