fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main
  6. {
  7. static PrintWriter out = new PrintWriter(System.out);
  8. static int nextInt() throws IOException {
  9. in.nextToken();
  10. return (int)in.nval;
  11. }
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. int x = nextInt(), n = nextInt();
  15. int N[][] = new int[n][4];;
  16. int tmp = 0;
  17. for (int i = 0; i < n; i++)
  18. for (int j = 0; j < 4; j++)
  19. N[i][j] = nextInt();
  20. for (int i = 0; i < n; i++)
  21. for (int j = 0; j < n; j++)
  22. for (int q = 0; q < 4; q++)
  23. if ((N[i][1] > N[j][1] || N[i][3] > N[j][3]) || ( (N[i][1] == N[j][1] || N[i][3] == N[j][3] ) && (N[i][0] > N[j][0] || N[i][2] > N[j][2]) ) ) {
  24. tmp = N[i][q];
  25. N[i][q] = N[j][q];
  26. N[j][q] = tmp;
  27. }
  28. for (int i = 0; i < n; i++) {
  29. if (x >= N[i][0] && x <= N[i][2])
  30. x = (N[i][1] < N[i][3]) ? N[i][0] : N[i][2];
  31. for (int j = 0; j < 4; j++)
  32. N[i][j] = 0;
  33. }
  34. out.println(x);
  35. out.flush();
  36. }
  37. }
Success #stdin #stdout 0.04s 2184192KB
stdin
30 4
25 35 40 30
1 32 20 30
33 22 50 29
18 10 33 19
stdout
18