1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Vertex
{
    int x1;
    int x2;
    int y1;
    int y2;
    int input_position;
    int id;
    
    Vertex(int x_1, int x_2, int y_1, int y_2, int i)
    {
        x1=x_1;
        x2=x_2;
        y1=y_1;
        y2=y_2;
        id = i;
        input_position = -1;
    }
    
};


void print(std::vector<Vertex> p){
    printf("\n");
    for(int i = 0; i<p.size(); i++)
        printf("xy= %d %d %d %d  id=%d input_position=%d\n", p.at(i).x1, p.at(i).y1, p.at(i).x2, p.at(i).y2, p.at(i).id, p.at(i).input_position);
}

struct MyComparator
{
    const vector<Vertex> & value_vector;
    
    MyComparator(const vector<Vertex> & val_vec):
    value_vector(val_vec) {}
    
    bool operator()(Vertex i1, Vertex i2)
    {
        return value_vector[i1.id].x1 < value_vector[i2.id].x1;
    }
};

bool MyCompare(const Vertex& a, const Vertex& b)
{
	return a.x1 < b.x1;
}

int main(int argc, const char * argv[])
{
    int t, n, x1, x2, y1, y2, w, i;
    
/*
     FILE * pFile;
    
    
    if( ( pFile = fopen ("/Users/Natalia/Desktop/potyczki/PAR/PAR/sample_nie.txt", "r+")) == NULL)
    {
        printf("No such file\n");
        exit(1);
    }
    
    if (pFile == NULL)
    {
        printf("Error Reading File\n");
    }
    
    fscanf(pFile, "%d", &t);
    */
    scanf("%d", &t);
    
    vector<Vertex> input, output;

    
    for(int ti=0; ti<t; ti++)
    {
        //fscanf(pFile, "%d %d\n",&n, &w);
        scanf("%d %d\n",&n, &w);
        input.clear();
        output.clear();
        
        for(i=0; i<n; ++i)
        {
            //fscanf(pFile, "%d %d %d %d", &x1, &y1, &x2, &y2);
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            input.push_back(Vertex(x1, x2, y1, y2, i));
        }
        
        for(i=0; i<n; ++i)
        {
            //fscanf(pFile,"%d %d %d %d", &x1, &y1, &x2, &y2);
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            output.push_back(Vertex(x1, x2, y1, y2, i));
        }
        
        //printf("\nsort1");
        sort(output.begin(), output.end(), MyComparator(input));
        
        for(i=0; i<n; ++i)
        {
            output.at(i).input_position = i;
        }
        //printf("\nsort2");
        sort(output.begin(), output.end(), MyCompare);
       
        int carry_on = true;
        i=0;
        while (carry_on and i < output.size())
        {
            //printf("\ni=%d", i);
    
            for(int j=i+1; j < output.size(); ++j)//j <= output.at(i).input_position; j++)
            {

                if(output.at(i).input_position > output.at(j).input_position and
                   output.at(i).y2 - output.at(i).y1 +
                   output.at(j).y2 - output.at(j).y1  > w)
                {
                    printf("NIE\n");
                    carry_on = false;
                    break;
                }
                
            }
            i++;
        }
        if (i==output.size() and carry_on)
            printf("TAK\n");
        
    }
    //fclose(pFile);
    
    return 0;
    
}