#P8502. 「CGOI-2」No cost too great
「CGOI-2」No cost too great
Background
The Radiance seeps into Hallownest; she is making a huge mistake.
With only a little faith left, why does she still insist.
I will be a lighthouse, shining upon the kingdom.
But before that, there are more important things to do.
No matter what the cost is, I will not hesitate, even though I have little left...
Problem Description
The Pale King is making his final visit to the magnificent palace he built. Now suppose the palace consists of rooms, with some directed corridors between rooms. Due to the Pale King's strange decorating habit (not referring to putting circular saws everywhere), for room , it has a directed corridor to every room whose index lies in the interval . For example, if room has directed corridors to , it means there is a directed corridor from room to each of rooms (a room may have a corridor to itself). If a room has corridors to , it means there is no outgoing corridor from that room.
The Pale King asks questions. Each query asks for the number of ways to go from room to room using exactly directed corridors and without passing through room (two ways are different if and only if there exists an such that the -th corridor used is different). Since this number can be very large, output the answer modulo .
Input Format
The first line contains two integers , representing the number of nodes and the number of queries.
The next lines each contain two integers . The integers on the -th line indicate that node has a directed edge to every node in the interval . When , it means this node has no outgoing edges.
The next lines each contain four integers , representing one query.
Output Format
Output lines. Each line contains one integer. The -th line is the number of ways for the -th query modulo .
4 5
2 3
1 1
2 4
0 0
1 3 4 5
1 4 2 4
2 3 1 2
4 4 3 0
1 3 2 5
5
1
0
1
1
10 10
6 6
4 10
2 5
1 7
3 4
5 7
4 10
1 7
1 3
2 5
8 8 5 1
4 7 5 3
5 9 4 4
1 5 5 2
6 2 10 2
3 3 7 4
1 10 1 2
6 2 4 4
9 2 1 4
9 10 3 2
0
17
2
0
0
46
0
12
23
1
10 10
2 6
6 9
5 7
3 9
0 0
0 0
3 5
5 5
3 6
1 10
5 9 6 3
10 8 6 4
10 8 5 1
8 6 5 4
7 2 5 4
6 1 5 3
10 4 5 1
5 5 6 0
7 9 6 4
4 9 6 2
0
17
1
0
0
0
1
1
4
1
Hint
Sample Explanation
In sample 1, room can reach rooms . Room can reach room . Room can reach rooms . Room cannot reach any room.
For the first query, the ways to go from room to room using corridors and not passing through room are 121213, 121333, 133213, 132133, 133333, totaling five ways.
Constraints
This problem uses bundled testdata.
| ID | Special Property | Memory Limit | Score |
|---|---|---|---|
| 0 | ,, | 256MB | 10pts |
| 1 | ,, | 15pts | |
| 2 | For all queries, | ||
| 3 | None | 30pts | |
| 4 | 128MB |
For of the data, , , , , . if and only if . The time limit is 1s for all subtasks.
Hint
Pay attention to the memory constant factors.
Translated by ChatGPT 5