[C++] 纯文本查看 复制代码
A题签到
#include<bits/stdc++.h>
#include<array>
using namespace std;
using ll = long long;
using info = array<ll, 2>;
struct Tree{
int l, r, id;
ll val;
};
struct segTree {
int n;
vector<Tree> tree;
segTree(int size, const vector<array<ll, 3>>& b): n(size), tree(size << 2){
build(b, 1, size, 1);
}
void push_up(int i) {
if(tree[i << 1].val < tree[i << 1 | 1].val) {
tree[i].val = tree[i << 1 | 1].val;
tree[i].id = tree[i << 1 | 1].id;
}else {
tree[i].val = tree[i << 1].val;
tree[i].id = tree[i << 1].id;
}
}
void build(const vector<array<ll, 3>>& b, int l, int r, int x) {
tree[x].l = l;
tree[x].r = r;
if(l == r) {
tree[x].val = b[l][1];
tree[x].id = b[l][2];
return;
}
int mid = (l + r) >> 1;
build(b, l, mid, x << 1);
build(b, mid + 1, r, x << 1 | 1);
push_up(x);
}
// 重置mu的值
void update(int mu, int x) {
if(tree[x].l == tree[x].r) {
tree[x].val = 0;
tree[x].id = -1;
return;
}
if(mu <= tree[x << 1].r) {
update(mu, x << 1);
}else {
update(mu, x << 1 | 1);
}
push_up(x);
}
info get_max(int l, int r, int x) {
if(l <= tree[x].l && tree[x].r <= r) {
return {tree[x].val, tree[x].id};
}
info ans = {0, -1};
if(l <= tree[x << 1].r) {
info a = get_max(l, r, x << 1);
ans = max(ans, a);
}
if(r >= tree[x << 1 | 1].l) {
info b = get_max(l, r, x << 1 | 1);
ans = max(ans, b);
}
return ans;
}
};
void solve() {
int n, total;
cin >> n;
total = 3 * n;
vector<info> a(total + 1);
vector<array<ll, 3>> b(total + 1);
int cnt = 0;
for(int i = 1; i <= total; i++) {
cin >> a[i][0] >> b[i][0];
a[i][1] = i;
b[i][1] = a[i][0];
b[i][2] = i;
}
sort(a.begin() + 1, a.end());
sort(b.begin() + 1, b.end());
// 更据b排序后建线段树
segTree Tree(total, b);
vector<bool> visd(total + 1);
vector<array<ll, 3>> ans;
vector<int> id_b(total + 1);
for(int i = 1; i <= total; i++) {
id_b[b[i][2]] = i;
}
int cur = total;
for(int i = total; i >= 1; i--) {
if(visd[a[i][1]]) continue;
ll fi = a[i][0];
visd[a[i][1]] = true;
Tree.update(id_b[a[i][1]], 1);
int find = 2;
array<ll, 3> temp{a[i][1], 0, 0};
while(find) {
ll l = lower_bound(b.begin() + 1, b.end(), array<ll, 3>{fi, 0, 0}) - b.begin();
if (l == b.size()) {
// 没有可以在一起的了
cout << "No" << endl;
return;
}
info it = Tree.get_max(l, total, 1);
if(it[1] == -1) {
cout << "No" << endl;
return;
}
visd[it[1]] = true;
Tree.update(id_b[it[1]], 1);
temp[find] = it[1];
find--;
}
if(find != 0) {
cout << "No" << endl;
return;
}
ans.push_back(temp);
}
cout << "Yes" << endl;
for(auto [x, y, z] : ans) {
cout << x << ' ' << y << ' ' << z << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
while(t--) {
solve();
}
return 0;
}