https://www.lanqiao.cn/problems/3529/learning/?subject_code=2&group_code=5&match_num=14&match_flow=1&origin=cup
1 import java.util.*; 2 // 1:無需package 3 // 2: 類名必須Main, 不可修改 4 import java.util.Map.Entry; 5 6 public class Demo1 { 7 // 區間樹,儲存影子的區間 8 static TreeMap<Double, Double> m = new TreeMap<>(); 9 10 public static void main(String[] args){ 11 Scanner sc=new Scanner(System.in); 12 13 int n=sc.nextInt(); 14 double x=sc.nextDouble(); 15 double y=sc.nextDouble(); 16 17 List<Line> lines=new ArrayList<>(); 18 19 for (int i = 0; i < n; i++) { 20 double left=sc.nextDouble(); 21 double height=sc.nextDouble(); 22 double length=sc.nextDouble(); 23 24 lines.add(new Line(left, height, length)); 25 } 26 27 lines.sort((l1,l2)->Double.compare(l2.y, l1.y)); 28 29 long res=0; 30 31 for(Line o:lines) { 32 double l=GetTouYing(o.leftx,o.y,x,y); 33 double r=GetTouYing(o.rightx, o.y, x, y); 34 if(!QuaryCover(l,r)) res++; 35 AddRange(l,r); 36 } 37 38 sc.close(); 39 System.out.print(res); 40 } 41 42 public static void AddRange(double l,double r) { 43 Entry<Double,Double> L=m.floorEntry(l); 44 Entry<Double,Double> R=m.floorEntry(r); 45 46 if(L != null && L.getValue()>=l) l=L.getKey(); 47 if(R != null && R.getValue()>=r) r=R.getValue(); 48 49 m.subMap(l, r).clear(); 50 m.put(l, r); 51 } 52 53 public static boolean QuaryCover(double l,double r) { 54 Entry<Double,Double> L=m.floorEntry(l); 55 return L != null && L.getValue()>=r; 56 } 57 58 public static double GetTouYing(double x1,double y1,double x2,double y2) { 59 return x1 - y1 * (x1 - x2) / (y1 - y2); 60 } 61 62 public static class Line { 63 double leftx; 64 double rightx; 65 double y; 66 double length; 67 68 Line(double left,double height,double length){ 69 this.leftx=left; 70 this.y=height; 71 this.length=length; 72 this.rightx=left+length; 73 } 74 } 75 }