Repository navigation
Expand file tree
/
Copy pathE.cpp
More file actions
109 lines (108 loc) · 3.01 KB
/
Copy pathE.cpp
File metadata and controls
109 lines (108 loc) · 3.01 KB
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
/*
© 2021-01-04 08:35:01 Franco1010 All Rights Reserved
*/
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<lli,lli> ii;
typedef vector<lli> vi;
#define deb(x) cout << #x": " << (x) << endl;
const lli INF = 1e18;
struct Segtree{
#define mid (l + r) / 2
#define left(u) (u + 1)
#define right(u) (u + ((mid - l + 1) << 1))
struct Node{
ii mn, mx;
Node(ii mn = {INF, INF}, ii mx = {INF, INF}): mn(mn), mx(mx) {}
Node operator + (const Node &n){
ii foo = min(mx, n.mx, [&](const ii& a, const ii& b){
return tuple{a.f, -a.s} < tuple{b.f, -b.s};
});
return Node(min(mn, n.mn), foo);
}
};
vector<Node> st;
Segtree(int n): st(2 * n) {}
void update(int u, int l, int r, int kth, ii val){
if(l == r){
st[u].mn = min(st[u].mn, val);
st[u].mx = min(st[u].mx, val, [&](const ii& a, const ii& b){
return tuple{a.f, -a.s} < tuple{b.f, -b.s};
});
return;
}
if(kth <= mid) update(left(u), l, mid, kth, val);
else update(right(u), mid + 1, r, kth, val);
st[u] = st[left(u)] + st[right(u)];
}
Node query(int u, int l, int r, int ll, int rr){
if(l > r or r < ll or l > rr) return Node();
if(ll <= l and r <= rr) return st[u];
return query(left(u), l, mid, ll, rr) + query(right(u), mid + 1, r, ll, rr);
}
};
int main(){ _
// freopen("file.in","r",stdin);
// freopen("file.out","w",stdout);
lli t; cin >> t;
while(t--){
lli n; cin >> n;
vector<ii> v;
vi comp;
fore(i, 0, n){
lli h, w; cin >> h >> w;
v.pb({h, w});
comp.pb(h); comp.pb(w);
}
comp.pb(0);
sort(all(comp));
comp.erase(unique(all(comp)), end(comp));
lli N = sz(comp);
Segtree st(N + 1);
lli ind = 1;
for(auto &[h, w] : v){
h = lower_bound(all(comp), h) - begin(comp);
w = lower_bound(all(comp), w) - begin(comp);
st.update(0, 0, N, h, {min(st.query(0, 0, N, h, h).mn.f, w), ind});
st.update(0, 0, N, w, {min(st.query(0, 0, N, w, w).mn.f, h), ind});
ind++;
}
ind = 0;
for(auto [h, w] : v){
ind++;
auto foo = st.query(0, 0, N, 0, h - 1);
if(foo.mn.f < w and foo.mn.s != ind){
cout << foo.mn.s << ' ';
continue;
}
if(foo.mx.f < w and foo.mx.s != ind){
cout << foo.mx.s << ' ';
continue;
}
foo = st.query(0, 0, N, 0, w - 1);
if(foo.mn.f < h and foo.mn.s != ind){
cout << foo.mn.s << ' ';
continue;
}
if(foo.mx.f < h and foo.mx.s != ind){
cout << foo.mx.s << ' ';
continue;
}
cout << -1 << ' ';
}
cout << ENDL;
}
return 0;
}