codeforce1237 C2. Balanced Removals (Harder) [贪心]

This is a harder version of the problem. In this version, n≤50000.

There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (xi,yi,zi). The number of points n is even.

You’d like to remove all n points using a sequence of n2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.

Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(xa,xb)≤xc≤max(xa,xb), min(ya,yb)≤yc≤max(ya,yb), and min(za,zb)≤zc≤max(za,zb). Note that the bounding box might be degenerate.

Find a way to remove all points in n2 snaps.

Input

The first line contains a single integer n (2≤n≤50000; n is even), denoting the number of points.

Each of the next n lines contains three integers xi, yi, zi (−108≤xi,yi,zi≤108), denoting the coordinates of the i-th point.

No two points coincide.

Output

Output n2 pairs of integers ai,bi (1≤ai,bi≤n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.

We can show that it is always possible to remove all points. If there are many solutions, output any of them.

Examples

inputCopy
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
outputCopy
3 6
5 1
2 4
inputCopy
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
outputCopy
4 5
1 6
2 7
3 8

给了一些点,每次删除一对点,要求点对形成的长方体中间没有点
按xyz排序,先删xy相等的,因为排过序,这些点就都在一条线上排列,中肯定没有点,然后删x相等的,然后直接删

#include
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int MAXN = 1e5+10;

struct node{
    int x, y, z, id;
}a[MAXN];
bool cmp(node a, node b){
    if(a.x != b.x) return a.x < b.x;
    else{
        if(a.y != b.y) return a.y < b.y;
        else return a.z < b.z;
    }
}
bool used[MAXN];
int n;
vector<node> ans;
int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> a[i].x >> a[i].y >> a[i].z;
        a[i].id = i;
    }
    sort(a+1, a+1+n, cmp);
    for(int i = 1; i < n; ++i){
        if(a[i].x == a[i+1].x && a[i].y == a[i+1].y){
            cout << a[i].id << ' ' << a[i+1].id << endl;
            used[i] = used[i+1] = true;
            ++i;
        }
    }
    for(int i = 1; i <= n; ++i)
        if(!used[i])
            ans.push_back(a[i]);
    ms(used, 0);
    for(int i = 0; i < ans.size(); ++i){
        if(ans[i].x == ans[i+1].x){
            cout << ans[i].id << ' ' << ans[i+1].id << endl;
            used[i] = used[i+1] = true;
            ++i;
        }
    }
    int cnt = 0;
    for(int i = 0; i < ans.size(); ++i)
        if(!used[i])
            a[cnt++] = ans[i];
    for(int i = 0; i < cnt; i+=2)
        cout << a[i].id << ' ' << a[i+1].id << endl;

    return 0;
}

你可能感兴趣的:(codeforce1237 C2. Balanced Removals (Harder) [贪心])