Question number: | 201809-2 |
Question name: | Grocery shopping |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description Little H and Little W came to a street, and they bought groceries separately. The process of buying groceries can be described as going to the store to buy some groceries and then going to a square next to load the groceries on the cart. Both of them buy n kinds of vegetables. Food, so it must be loaded n times. Specifically, for small H, there are n disjoint time periods [a1,b1],[a2,b2]...[an,bn] are loading, and for small W, there are n disjoint periods The time period [c1,d1],[c2,d2]...[cn,dn] is loading. Among them, a time period [s, t] represents the time from time s to time t, and the duration is ts. Input format The first line of input contains a positive integer n, which represents the number of time periods. Output format Output one line, a positive integer, indicating how long the two can talk. Sample input 4 Sample output 3 Data size and convention For all evaluation use cases, 1 ≤ n ≤ 2000, ai <bi <ai+1, ci <di <ci+1, for all i (1 ≤ i ≤ n), 1 ≤ ai, bi, ci, di ≤ 1000000. |
import java.util.Scanner;
public class 买菜 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[1000001];
int[] b=new int[1000001];
for(int i=1;i<=n;i++){
int s=sc.nextInt();
int t=sc.nextInt();
for(int j=s;j<t;j++){
a[j]=1;
}
}
for(int i=1;i<=n;i++){
int s=sc.nextInt();
int t=sc.nextInt();
for(int j=s;j<t;j++){
b[j]=1;
}
}
int res=0;
for(int i=1;i<=1000000;i++){
if(a[i]==b[i]&&a[i]!=0&&b[i]!=0){
res++;
}
}
System.out.println(res);
}
}