Beauty of programming-interval coincidence judgment

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.