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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <cstdio>
#include <map>
#include <algorithm>
#include "kollib.h"
#include "message.h"
#include "math.h"

std::vector<unsigned int> ends;
std::vector<int> query_nodes;

std::vector<std::pair<int, std::pair<int,int> > > edges;
std::vector<std::pair<std::pair<int,int>, std::pair<int,int> > > siblings;


std::pair<int, int> findEnd(int last, int current, std::vector<std::pair<int,int> > &nodes) {
    if (std::binary_search(query_nodes.begin(), query_nodes.end(), last)) {
	nodes.push_back(std::make_pair(last, 0));
    }
    
    int length = 1;
    while (std::binary_search(ends.begin(), ends.end(), current) == false) {
	if (std::binary_search(query_nodes.begin(), query_nodes.end(), current)) {
	    nodes.push_back(std::make_pair(current, length));
	}
	
	if (FirstNeighbor(current) != last) {
	    last = current;
	    current = FirstNeighbor(current);
	} else {
	    last = current;
	    current = SecondNeighbor(current);
	}
	
	++length;
    }
    
    return std::make_pair(current, length);
}

int main() {
    //wygeneruj konce przedzialow
    unsigned int STUDENTS = NumberOfStudents();
    for (unsigned int i=0, size=std::min(5000, (int)sqrt(STUDENTS)); i<size; ++i) {
	ends.push_back(((i*15485653LLU) % STUDENTS) + 1);
    }
    std::sort(ends.begin(), ends.end());
    ends.erase(std::unique(ends.begin(), ends.end()), ends.end());
    
    //wygeneruj query_nodes
    for (int i=1; i<=NumberOfQueries(); ++i) {
	query_nodes.push_back(QueryFrom(i));
	query_nodes.push_back(QueryTo(i));
    }
    std::sort(query_nodes.begin(), query_nodes.end());
    
    //kazda instancja przeglada odpowiednie przedzialy
    for (int nr=MyNodeId(); nr<ends.size(); nr+=NumberOfNodes()) {
	std::vector<std::pair<int, int> > left_nodes, right_nodes;
	std::pair<int,int> left, right;
	
	left = findEnd(ends[nr], FirstNeighbor(ends[nr]), left_nodes);
	right = findEnd(ends[nr], SecondNeighbor(ends[nr]), right_nodes);
	
	edges.push_back(std::make_pair(ends[nr], left));
	edges.push_back(std::make_pair(ends[nr], right));
	
	for (int i=0; i<left_nodes.size(); ++i) {
	    siblings.push_back(std::make_pair(std::make_pair(ends[nr], left.first), left_nodes[i]));
	}
	
	for (int i=0; i<right_nodes.size(); ++i) {
	    siblings.push_back(std::make_pair(std::make_pair(ends[nr], right.first), right_nodes[i]));
	}
    }

    //wysylanie informacji o koncach i znalezionych wierzcholkach
    PutInt(0, edges.size());
    for (int i=0; i<edges.size(); ++i) {
	PutInt(0, edges[i].first);
	PutInt(0, edges[i].second.first);
	PutInt(0, edges[i].second.second);
    }
    
    PutInt(0, siblings.size());
    for (int i=0; i<siblings.size(); ++i) {
	PutInt(0, siblings[i].first.first);
	PutInt(0, siblings[i].first.second);
	PutInt(0, siblings[i].second.first);
	PutInt(0, siblings[i].second.second);
    }
    Send(0);
    
    if (MyNodeId() == 0) {
	std::vector<std::pair<int, int> > firstn(ends.size(), std::make_pair(-1, 0));
	std::vector<std::pair<int, int> > secondn(ends.size(), std::make_pair(-1, 0));
	std::map<int, std::pair<std::pair<int, int>, int> > node_position;
	for (int i=0; i<NumberOfNodes(); ++i) {
	    int instance = Receive(-1);
	    
	    //sciezki
	    int count = GetInt(instance);
	    while (count--) {
		int b = GetInt(instance);
		int e = GetInt(instance);
		int l = GetInt(instance);
		int pos = std::lower_bound(ends.begin(), ends.end(), b) - ends.begin();
		if (firstn[pos].first < 0) firstn[pos] = std::make_pair(e, l); 
		else secondn[pos] = std::make_pair(e, l);
	    }
	    
	    //wierzcholki
	    count = GetInt(instance);
	    while (count--) {
		int b = GetInt(instance);
		int e = GetInt(instance);
		int n = GetInt(instance);
		int l = GetInt(instance);
		
		node_position[n] = std::make_pair(std::make_pair(b, e), l);
	    }
	}

	//zbuduj sciezke
	std::vector<int> path(ends.size(),0);
	std::vector<int> distance(ends.size(), 0);
	int current = 0;
	path[0] = ends[0];
	while (++current <= ends.size()) {
	    int pos = std::lower_bound(ends.begin(), ends.end(), path[current-1]) - ends.begin();
	    if (current == 1 || firstn[pos].first != path[current-2]) {
		path[current % ends.size()] = firstn[pos].first;
		distance[current-1] = firstn[pos].second;
	    } else {
		path[current % ends.size()] = secondn[pos].first;
		distance[current-1] = secondn[pos].second;
	    }
	}
	
	//odpowiadaj na zapytania
	for (int i=1; i<=NumberOfQueries(); ++i) {
	    std::pair<std::pair<int, int>, int> from = node_position[QueryFrom(i)];
	    std::pair<std::pair<int, int>, int> to = node_position[QueryTo(i)];
	    int l1, l2, d=0;
	    
	    for (int pos=0; pos<path.size(); ++pos) {
		if (path[pos] == from.first.first && path[(pos+1)%path.size()] == from.first.second) l1 = d + from.second;
		if (path[pos] == from.first.second && path[(pos+1)%path.size()] == from.first.first) l1 = d + (distance[pos] - from.second);
		
		if (path[pos] == to.first.first && path[(pos+1)%path.size()] == to.first.second) l2 = d + to.second;
		if (path[pos] == to.first.second && path[(pos+1)%path.size()] == to.first.first) l2 = d + (distance[pos] - to.second);
		
		d+=distance[pos];
	    }
	    
	    int res = std::max(l1,l2) - std::min(l1,l2);
	    res = std::min(res, (int)STUDENTS - res);
	    printf("%d\n",res);
	}
    }
    
    return 0;
}