-
-
Save drey7925/98c9de430b33db7c3f603af882c32693 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
import java.util.TreeMap; | |
public class Problem7 { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int count = 0; | |
TreeMap<Integer, HorzLine> horz = new TreeMap<>(); | |
TreeMap<Integer, VertLine> vert = new TreeMap<>(); | |
int n = sc.nextInt(); | |
for (int i = 0; i < n; i++) { | |
int x1 = sc.nextInt(); | |
int y1 = sc.nextInt(); | |
int x2 = sc.nextInt(); | |
int y2 = sc.nextInt(); | |
if (y1 == y2) { | |
// horz | |
HorzLine ln = horz.computeIfAbsent(y1, HorzLine::new); | |
ln.ranges.add(new Range(Math.min(x1, x2), Math.max(x1, x2))); | |
} else { | |
VertLine ln = vert.computeIfAbsent(x1, VertLine::new); | |
ln.ranges.add(new Range(Math.min(y1, y2), Math.max(y1, y2))); | |
} | |
} | |
horz.forEach((i_, ln) -> ln.mergeRanges()); | |
vert.forEach((i_, ln) -> ln.mergeRanges()); | |
System.out.println(horz.toString()); | |
System.out.println(vert.toString()); | |
for(int y1 : horz.keySet()){ | |
for(int y2 : horz.keySet()){ | |
if(y2 <= y1) continue; | |
int dist = y2-y1; | |
HorzLine h1 = horz.get(y1); | |
HorzLine h2 = horz.get(y2); | |
for(int x : vert.keySet()){ | |
if(!vert.containsKey(x+dist)) continue; | |
VertLine l1 = vert.get(x); | |
VertLine l2 = vert.get(x+dist); | |
if(l1.containsRange(new Range(y1, y2)) && l2.containsRange(new Range(y1, y2)) | |
&& h1.containsRange(new Range(x, x+dist))&& h2.containsRange(new Range(x, x+dist))){ | |
count++; | |
} | |
} | |
} | |
} | |
System.out.println(count); | |
} | |
static class Range { | |
int s, e; | |
public Range(int s, int e) { | |
super(); | |
this.s = s; | |
this.e = e; | |
} | |
public boolean overlaps(Range r) { | |
return contains(r) || r.contains(this) || ((e >= r.s && e <= r.e) || (r.e <= s && r.e <= e)); | |
} | |
public Range merge(Range r) { | |
//precondition | |
if (!overlaps(r)) | |
throw new RuntimeException(); | |
return new Range(Math.min(s, r.s), Math.max(e, r.e)); | |
} | |
@Override | |
public String toString() { | |
return "[" + s + ", " + e + "]"; | |
} | |
public boolean contains(Range r) { | |
return (r.s >= s && r.e <= e); | |
} | |
} | |
static class HorzLine { | |
public HorzLine(Integer y) { | |
this.y = y; | |
ranges = new ArrayList<>(); | |
} | |
boolean containsRange(Range r){ | |
for(Range rz : ranges){ | |
if(rz.contains(r)){ | |
return true; | |
} | |
} | |
return false; | |
} | |
public void mergeRanges() { | |
List<Range> tCopy = new ArrayList<Problem7.Range>(ranges); | |
ranges.clear(); | |
Range current = null; | |
for(Range r : tCopy){ | |
if(current == null) | |
current = r; | |
else { | |
if(current.overlaps(r)){ | |
current = current.merge(r); | |
} else { | |
ranges.add(current); | |
current = r; | |
} | |
} | |
} | |
ranges.add(current); | |
} | |
int y; | |
@Override | |
public String toString() { | |
return "HorzLine [y=" + y + ", ranges=" + ranges + "]"; | |
} | |
List<Range> ranges; | |
} | |
static class VertLine { | |
public VertLine(Integer x) { | |
this.x = x; | |
ranges = new ArrayList<>(); | |
} | |
boolean containsRange(Range r){ | |
for(Range rz : ranges){ | |
if(rz.contains(r)){ | |
return true; | |
} | |
} | |
return false; | |
} | |
int x; | |
List<Range> ranges; | |
public void mergeRanges() { | |
List<Range> tCopy = new ArrayList<Problem7.Range>(ranges); | |
ranges.clear(); | |
Range current = null; | |
for(Range r : tCopy){ | |
if(current == null) | |
current = r; | |
else { | |
if(current.overlaps(r)){ | |
current = current.merge(r); | |
} else { | |
ranges.add(current); | |
current = r; | |
} | |
} | |
} | |
ranges.add(current); | |
} | |
@Override | |
public String toString() { | |
return "VertLine [x=" + x + ", ranges=" + ranges + "]"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment