This problem gives us a graph, where any edge is permissible by a given range of integers. It then asks how many distinct integers can pass from one node to another by any path.

It should be clear from the limit that brute forcing by evaluating each integer individually will be too slow, and the most work we'll be able to do is either L^2 or N^2 or N*L (+or - a log).

A first try might be to bfs, and store the maximum set of numbers which can reach a given node. The issue here is the termination condition. When we reach a node, we have to recurse, but then we can reach that node again with an expanded set of nodes via some other path, and have to re-recurse. This leads to an exponential runtime. There is no way to "wait until we have all the integers that can reach a node" because the graph may not have a topological sort.

Looking back at the limits, we see that though the integer range goes up to 10^9, since we only have 5000 edges, we can compress the range. We are guaranteed that there are only 10,000 "elementary intervals," that is, intervals such that the range will either entirely pass through any edge, or be entirely blocked. We can thus BFS once for each of these intervals and see if we can reach the appropriate node.

Total runtime is 10k * 5k, or plenty fast.

One caveat while writing the code: To avoid all sorts of fence post issues, it makes sense to evaluate all ranges NON-INCLUSIVELY and then explicitly evaluate the endpoints. Otherwise, you end up having to deal with overlapping ranges, which is a pain.

``` 1 import java.awt.Point;
2 import java.util.*;
3 public class b {
4 	static Scanner in=new Scanner(System.in);
5 	public static void main(String[] args) {
6 		int n=in.nextInt(),l=in.nextInt(),b=in.nextInt(),s=in.nextInt()-1,d=in.nextInt()-1;
7 		ArrayList<HashMap<Integer,Point>>al=new ArrayList<>();
9 		for(int i=0;i<l;i++)al.get(in.nextInt()-1).put(in.nextInt()-1, new Point(in.nextInt(),in.nextInt()));
10 		TreeSet<Integer>ts=new TreeSet<>();
11 		for(HashMap<Integer,Point>i:al)for(int j:i.keySet()){
14 		}
15 		int ans=0;
16 		//do it for ranges, non-inclusive
17 		while(ts.size()>1) {
18 			int on=ts.pollFirst()+1;
20 			q.offer(s);
21 			boolean[] visited=new boolean[n];
22 			visited[s]=true;
23 			while(!q.isEmpty()&&!visited[d]) {
24 				int next=q.poll();
25 				for(int i:al.get(next).keySet()) if(!visited[i]&&al.get(next).get(i).x<=on&&al.get(next).get(i).y>=ts.first()-1){
26 					visited[i]=true;
27 					q.offer(i);
28 				}
29 			}
30 			if(visited[d])ans+=ts.first()-on;
31 		}
32 		//do it again for endpoints
33 		for(HashMap<Integer,Point>i:al)for(int j:i.keySet()){
36 		}
37 		while(ts.size()>0) {
38 			int on=ts.pollFirst();
40 			q.offer(s);
41 			boolean[] visited=new boolean[n];
42 			visited[s]=true;
43 			while(!q.isEmpty()&&!visited[d]) {
44 				int next=q.poll();
45 				for(int i:al.get(next).keySet()) if(!visited[i]&&al.get(next).get(i).x<=on&&al.get(next).get(i).y>=on){
46 					visited[i]=true;
47 					q.offer(i);
48 				}
49 			}
50 			if(visited[d])ans+=1;
51 		}
52 		System.out.println(ans);
53 	}
54 }
```