Code chef:divide The solution to the Tangerine Orange chunking algorithm

Source: Internet
Author: User
Tags bool min

Once Chef decided to divide the tangerine into several parts. At the numbered Tangerine ' s segments from1ToNIn the clockwise order starting from some segment. Then He intended to divide the fruit into several parts. In order to does it he planned to separate the neighbouring segments inkPlaces, so and he could getkParts:the1St -From segmentl1 To segmentr1 (inclusive), the2nd -FromL2 Tor2 ,..., thekth -Fromlk TorK (In the "cases in the" clockwise order). Suddenly, when Chef is absent, one naughty boy came and divided the tangerine intoPParts (also by separating the neighbouring segments one from another): the1St -From segmenta1 To segmentb1 , the2nd -Froma2 Tob2 ,..., thepth -Fromap TobP (In the "cases in the" clockwise order). Chef became very angry about it! But maybe little boy haven ' t done anything wrong, maybe everything are OK? Please, help Chef to determine whether him able to obtain the parts him wanted to have (in order to does it he can dividePCurrent parts, but, of the course, he can ' t join several parts into one.

Please, this is parts are not cyclic. That's means that even if the Tangerine Division consists of only one part, but "part" include more than one segment, the Re are two segments which were neighbouring in the initial tangerine, but not are in the neighbouring. The explanation of example case 2 to ensure your understood that clarification.

Input

The ' The ' input contains an integer T denoting the number of test cases. The description of T test cases follows.
The "a" of each test case contains three spaces separated integers n, k, p, denoting the Number of Tangerine ' s segments and number of parts in each of the two divisions. The next k lines contain pairs of space-separated integers l and RI. The next p lines contain pairs of space-separated integers ai and bI.

It is guaranteed so each tangerine ' s segment be contained in exactly one of the the One of the next p parts.

Output

For each test case, output a single line containing either "Yes" or "No" (without the quotes), denoting whether-is AB Le to obtain the parts him wanted to have.

Constraints

1T

1n5 *7

1kmin (+, N)

1pmin (+, N)

1li, rI, ai, bi n

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Example

Input:
2
3 2 1 4 5 5 6 (1 5 6) 3 1 2 5 1 6 9 1

output:
   
Yes
No
  

Explanation

Example Case 1: To achieve his goal Chef should divide the "I" (1-5) in two by separating segments 4 and 5 one from another.

Example Case 2: The boy didn ' t left the Tangerine as it is (though you may thought this way), he separated segments 1 and a from Ano Ther. But segments 1 and are in one part of Chef ' s division, so it's unable to achieve his goal.

Good long topic, the difficulty of this topic is how to read the topic.

Topic Resolution:

1 a piece of orange, and then the heap, all the heap of the fast number must be clockwise count

The 2-point heap is messed up.

3 in the chaos of the heap in the original want to divide the heap?

Additional hidden conditions: 1 All heaps need to be divided 2 can not be combined heap, can only separate heap

You can answer the questions by reading them, especially the hidden conditions.

Attention:

1 output format Yes not Yes

2 vector containers need to be zeroed every time.

The time efficiency of this program is somewhat complicated: O (LG (k) *max (k, p))

#include <iostream> #include <vector> #include <string> #include <algorithm> using  
      
namespace Std;  
    struct Tangerine {int left, right;  
    BOOL operator< (const tangerine &t) Const {return left < T.left;  
      
}  
};  
    BOOL Bidiv (vector<tangerine> &vk, int low, int up, int left) {if (Low > up) return false;  
    int mid = low + ((Up-low) >>1);  
    if (Vk[mid].left < left) return Bidiv (VK, mid+1, up, left);  
    if (left < Vk[mid].left) return Bidiv (VK, Low, mid-1, left);  
return true; String Divtangerine (vector<tangerine> &vk, vector<tangerine> &vp) {sort (vk.begin)  
    , Vk.end ());  
    for (unsigned i = 0; i < vp.size (); i++) {if (!bidiv (VK, 0, Vk.size ()-1, Vp[i].left)) return "No";  
return "Yes";  
    } void Dividethetangerine () {int n = 0, k = 0, p = 0; Tangerine Tang  
    Vector<tangerine> VK;  
      
    Vector<tangerine> VP;  
    int T = 0;  
    cin>>t;  
        while (t--) {cin>>n>>k>>p;  
            for (int i = 0; i < K; i++) {cin>>tang.left>>tang.right;  
        Vk.push_back (Tang);  
            for (int i = 0; i < p i++) {cin>>tang.left>>tang.right;  
        Vp.push_back (Tang);  
      
        Cout<<divtangerine (VK, VP) <<endl;  
    Vk.clear (), vp.clear (); }  
}

Oj:http://www.codechef.com/problems/tangdiv

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.