/* Season 6 Round 6 Problem 4 Senior Author: Anton Chernev */ #include #include #include using namespace std; const int MAX_COORDINATE = 100001; const int MAX_N = 100001; const int inf = MAX_COORDINATE * 100; struct Point { int x, y, temp; int index; int answer; bool operator < (const Point &other) const { return y > other.y; } }; Point points[MAX_N]; queue queued; int it[4 * MAX_N]; int N; void read() { scanf("%d", &N); for(int i = 0; i < N; ++i) { scanf("%d%d%d", &points[i].x, &points[i].y, &points[i].temp); points[i].index = i; } } void update(int pos, int val) { int k = 1, l = 1, r = MAX_COORDINATE; while(l != r) { it[k] = max(it[k], val); if(pos <= (l + r) / 2) { k = k * 2; r = (l + r) / 2; } else { k = k * 2 + 1; l = (l + r) / 2 + 1; } } it[k] = max(it[k], val); } int findLeft(int k, int l, int r, int l1, int r1, int threshold) { if(l > r1 || l1 > r) return 0; if(l1 <= l && r <= r1 && it[k] < threshold) return 0; if(l == r) return l; int tmp = findLeft(k * 2 + 1, (l + r) / 2 + 1, r, l1, r1, threshold); if(tmp != 0) return tmp; return findLeft(k * 2, l, (l + r) / 2, l1, r1, threshold); } int findRight(int k, int l, int r, int l1, int r1, int threshold) { if(l > r1 || l1 > r) return inf; if(l1 <= l && r <= r1 && it[k] < threshold) return inf; if(l == r) return l; int tmp = findRight(k * 2, l, (l + r) / 2, l1, r1, threshold); if(tmp != inf) return tmp; return findRight(k * 2 + 1, (l + r) / 2 + 1, r, l1, r1, threshold); } void solve() { int leftBound, rightBound; int tmp; sort(points, points + N); for(int i = 0; i < N; ++i) { if(i && points[i].y != points[i - 1].y) { while(!queued.empty()) { tmp = queued.front(); queued.pop() ; update(points[tmp].x, points[tmp].temp); } } leftBound = findLeft(1, 1, MAX_COORDINATE, 1, points[i].x, points[i].temp); rightBound = findRight(1, 1, MAX_COORDINATE, points[i].x, MAX_COORDINATE, points[i].temp); if(leftBound == 0 || rightBound == inf) points[i].answer = inf; else points[i].answer = rightBound - leftBound; queued.push(i); //printf("%d %d %d %d %d %d\n", points[i].index, points[i].x, points[i].y, leftBound, rightBound, points[i].answer); } } bool cmpByIndex(const Point &el1, const Point &el2) { return el1.index < el2.index; } void print() { sort(points, points + N, cmpByIndex); for(int i = 0; i < N; ++i) { if(points[i].answer != inf) printf("%d\n", points[i].answer); else printf("inf\n"); } } int main() { freopen("magnet.in", "r", stdin); freopen("magnet.out", "w", stdout); read(); solve(); print(); } /* Test case 7 3 4 20 4 3 10 2 6 20 5 6 200 5 1 1 1 3 100 1 6 30 */