fork download
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.StreamTokenizer;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.Iterator;
  9. import java.util.TreeSet;
  10. import java.lang.Integer;
  11.  
  12. public class waterfall_kp {
  13.  
  14.  
  15. static int nextInt() {
  16. try {
  17. st.nextToken();
  18. return (int) st.nval;
  19. } catch (IOException ex) {
  20. return 0;
  21. }
  22. }
  23.  
  24. static class Segment implements Comparable<Segment> {
  25.  
  26. int l, r, h;
  27. int max = 0;
  28.  
  29. public Segment(int l, int r, int h) {
  30. this.l = l;
  31. this.r = r;
  32. this.h = h;
  33.  
  34. }
  35.  
  36. @Override
  37. public int compareTo(Segment o) {
  38. int cmp = -Integer.valueOf(h).compareTo(Integer.valueOf(o.h));
  39. return cmp == 0 ? Integer.valueOf(l).compareTo(Integer.valueOf(o.l)) : cmp;
  40. }
  41.  
  42. boolean overlaps(Segment o) {
  43. return l < o.r && o.l < r;
  44. }
  45. }
  46.  
  47. static class Part implements Comparable<Part> {
  48.  
  49. int l, r;
  50.  
  51. public Part(int l, int r, Segment s) {
  52. this.l = l;
  53. this.r = r;
  54. this.s = s;
  55. }
  56.  
  57. public Part(Segment s) {
  58. this.l = s.l;
  59. this.r = s.r;
  60. this.s = s;
  61. }
  62.  
  63. @Override
  64. public int compareTo(Part o) {
  65. int cmp = Integer.valueOf(l).compareTo(Integer.valueOf(o.l));
  66. return cmp == 0 ? Integer.valueOf(r).compareTo(Integer.valueOf(o.r)) : cmp;
  67. }
  68.  
  69. int overlap(Part o) {
  70. return Math.min(r,o.r)-Math.max(l,o.l);
  71. }
  72.  
  73. private int length() {
  74. return r-l;
  75. }
  76. }
  77.  
  78. public static void main(String[] args) {
  79. int n = nextInt();
  80. ArrayList<Segment> segments = new ArrayList<Segment>();
  81. Segment top = new Segment(Integer.MIN_VALUE, Integer.MAX_VALUE, nextInt());
  82. top.max = Integer.MAX_VALUE;
  83. Segment bottom = new Segment(Integer.MIN_VALUE+1, Integer.MAX_VALUE-1, 0);
  84. segments.add(top);
  85. segments.add(bottom);
  86. for (int i = 0; i < n; i++) {
  87. int h = nextInt();
  88. segments.add(new Segment(nextInt(), nextInt(), h));
  89. }
  90. Collections.sort(segments);
  91.  
  92. TreeSet<Part> parts = new TreeSet<Part>();
  93. parts.add(new Part(top));
  94. for (int i = 1; i < segments.size(); i++) {
  95. Segment s = segments.get(i);
  96. Part p = new Part(s);
  97. Part first = parts.lower(new Part(s.l, s.l, null));
  98. if(first.r==p.l) {
  99. first=parts.higher(first);
  100. }
  101. Part last = parts.lower(new Part(s.r, s.r, null));
  102. TreeSet<Part> oParts = (TreeSet<Part>) parts.subSet(first, true, last, true);
  103. Iterator<Part> it = oParts.iterator();
  104.  
  105. Part l = null;
  106. Part m = null;
  107. Part r = null;
  108.  
  109. while (m == null) {
  110. m = r;
  111. if(it.hasNext()) {
  112. r=it.next();
  113. it.remove();
  114. } else {
  115. r=null;
  116. }
  117. }
  118. while (m != null) {
  119. boolean use = true;
  120. if (l != null) {
  121. if (l.s.overlaps(m.s)&&l.s.h<m.s.h) {
  122. use = false;
  123. }
  124. }
  125. if (r != null) {
  126. if (r.s.overlaps(m.s)&&r.s.h<m.s.h) {
  127. use = false;
  128. }
  129. }
  130. if(use) {
  131. s.max=Math.max(s.max,Math.min(m.s.max, p.overlap(m)));
  132. }
  133. l=m;
  134. m=r;
  135. if(it.hasNext()) {
  136. r=it.next();
  137. it.remove();
  138. } else {
  139. r=null;
  140. }
  141. }
  142.  
  143. parts.add(p);
  144. if(p.l>first.l) {
  145. parts.add(new Part(first.l,p.l,first.s));
  146. }
  147. if(p.r<last.r){
  148. parts.add(new Part(p.r,last.r,last.s));
  149. }
  150. }
  151. System.out.println(bottom.max);
  152. }
  153. }
  154.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty