#P8502. 「CGOI-2」No cost too great

    ID: 9560 远端评测题 1000ms 128~256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DPO2优化容斥原理差分

「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 nn 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 ii, it has a directed corridor to every room whose index lies in the interval [li,ri][l_i, r_i]. For example, if room 44 has directed corridors to [2,5][2,5], it means there is a directed corridor from room 44 to each of rooms 2,3,4,52, 3, 4, 5 (a room may have a corridor to itself). If a room has corridors to [0,0][0,0], it means there is no outgoing corridor from that room.

The Pale King asks qq questions. Each query asks for the number of ways to go from room aa to room bb using exactly mm directed corridors and without passing through room cc (two ways are different if and only if there exists an ii such that the ii-th corridor used is different). Since this number can be very large, output the answer modulo 998244353998244353.

Input Format

The first line contains two integers n,qn, q, representing the number of nodes and the number of queries.

The next nn lines each contain two integers l,rl, r. The integers li,ril_i, r_i on the (i+1)(i+1)-th line indicate that node ii has a directed edge to every node in the interval [li,ri][l_i, r_i]. When li=ri=0l_i=r_i=0, it means this node has no outgoing edges.

The next qq lines each contain four integers a,b,c,ma, b, c, m, representing one query.

Output Format

Output qq lines. Each line contains one integer. The ii-th line is the number of ways for the ii-th query modulo 998244353998244353.

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 11 can reach rooms 2,32, 3. Room 22 can reach room 11. Room 33 can reach rooms 2,3,42, 3, 4. Room 44 cannot reach any room.

For the first query, the ways to go from room 11 to room 33 using 55 corridors and not passing through room 44 are 121213, 121333, 133213, 132133, 133333, totaling five ways.


Constraints

This problem uses bundled testdata.

ID Special Property Memory Limit Score
0 n10n\le10q10q\le10m4m\le4 256MB 10pts
1 n100n\le100q104q\le10^4m40m\le40 15pts
2 For all queries, lc=rc=0l_c=r_c=0
3 None 30pts
4 128MB

For 100%100\% of the data, 1n5001\le n \le 500, 1q1051\le q \le 10^5, 1m1001\le m \le 100, 0lirin0 \le l_i \le r_i \le n, 1a,b,cn1 \le a,b,c \le n. ri=0r_i=0 if and only if li=0l_i=0. The time limit is 1s for all subtasks.


Hint

Pay attention to the memory constant factors.

Translated by ChatGPT 5