Sorts the source interval by X coordinate and then merges them. The X and Y coordinates of the target interval are searched for in binary form. If the two are distributed in the same continuous interval, the interval overlaps.
Code:
1 # include <iostream> 2 # include <algorithm> 3 using namespace STD; 4 5 struct section 6 {7 int low; 8 int high; 9 bool operator <(const section & L) const10 {11 return low <L. low; 12} 13}; 14 15 int getindex (INT num); 16 17 const int n = 1001; 18 section source [N]; 19 const int K = 101; 20 Section des [k]; 21 // number of segments after source range merging 22 int ncnt = 0; 23 24 int main () 25 {26 // n: number of source ranges K: number of destination ranges 27 int N, K; 28 CIN> N> K; 29 for (INT I = 0; I <N; ++ I) 30 CIN> source [I]. low> source [I]. high; 31 for (INT I = 0; I <K; ++ I) 32 CIN> des [I]. low> des [I]. high; 33 34 sort (source, source + n); 35 36 int highnum = source [0]. high; 37 for (INT I = 1; I <n; ++ I) 38 If (source [I]. low <= highnum) 39 {40 // process [] [] Query []. In this case, 41 if (source [I]. high View code
Beauty of programming-interval coincidence judgment