别看了,每一列保存 排序,upper_bound 降复杂度
慢慢写吧
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
typedef long long ll;
const int maxn = 10000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int n, m, len;
vector<int> a[maxn];
int ans[maxn];
void init() {
scanf("%d %d %d", &n, &m, &len);
for(int i = 0; i <= n; ++i)
a[i].clear();
for(int i = 0; i < m; ++i) {
int r, c;
scanf("%d %d", &r, &c);
a[r].push_back(c);
}
for(int i = 0; i < n; ++i) {
sort(a[i].begin(), a[i].end());
//cout << " i " << i << " = ";
//for(int j = 0; j < a[i].size(); ++j)
// cout << a[i][j] << " ";
//cout << endl;
}
}
void solve() {
if(n == 1) {
cout << 0 << endl;
return;
}
for(int i = 0; i < n; ++i) {
ans[i] = i;
int pos = 0;
while(1) {
//cout << ans[i] << " ++= " << pos << endl;
if(ans[i] == 0) {
int t = upper_bound(a[0].begin(), a[0].end(), pos) - a[0].begin();
if(t >= a[0].size()) break;
else {
pos = a[0][t];
ans[i] = 1;
}
} else if(ans[i] == n-1) {
int t = upper_bound(a[n-2].begin(), a[n-2].end(), pos) - a[n-2].begin();
if(t >= a[n-2].size()) break;
else {
//cout << t << " " << a[n-2][t] << " ==== 23333333" << endl;
pos = a[n-2][t];
ans[i] = ans[i]-1;
}
} else {
int t1 = upper_bound(a[ans[i]-1].begin(), a[ans[i]-1].end(), pos) - a[ans[i]-1].begin();
int t2 = upper_bound(a[ans[i]].begin(), a[ans[i]].end(), pos) - a[ans[i]].begin();
if(t1 < a[ans[i]-1].size() && t2 < a[ans[i]].size()) {
if(a[ans[i]-1][t1] < a[ans[i]][t2]) {pos = a[ans[i]-1][t1];
ans[i] = ans[i]-1;
}
else {//cout << a[ans[i]-1][t1] << " ------------------------------ " << a[ans[i]][t2] << endl;
pos = a[ans[i]][t2];
ans[i] = ans[i]+1;
//cout << ans[i] << " +++ 4654765 " << pos << endl;
}
//cout << " 2333 " << endl;
}
else if(t1 < a[ans[i]-1].size()) {
pos = a[ans[i]-1][t1];
ans[i] = ans[i]-1; //cout << " ++++++++++++++++++++++++ " << endl;
}
else if(t2 < a[ans[i]].size()) {pos = a[ans[i]][t2];
ans[i] = ans[i]+1; //cout << " +++++++==============+++++++++++ " << endl;
}
else {
break;
}
}
}
//cout << ans[i] << endl;
printf("%d\n", ans[i]);
}
}
int main() {
//int b[4] = {0,1,1,2};
//int tt = upper_bound(b,b+4, 5) - b;
//cout << tt << endl;
int T;
scanf("%d", &T);
while(T--) {
init();
solve();
}
return 0;
}