题目:https://ac.nowcoder.com/acm/contest/890/E
题意:定位Hibert Curve中的某个坐标出现的时间次序。
思路:思维有些不正常,写的比较麻烦。先声明数组a[][] = {{1, 4}, {2, 3}}是刚开始图形的顺序,分别表示左上、左下、右下、右上。把大图形分成四个小的,每次看询问坐标位于哪个小方块内,并且找对应的a[][]值,如果值为1,交换a[0][1]和a[1][0],如果值为0,交换a[0][0]和a[1][1]。
代码:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn=1e6+10;
struct node{
LL x,y,id;
bool operator <(const node &a) const{return id<a.id;}
}p[maxn];
bool lefttop(LL x, LL y, LL sz){return x<=sz && y <= sz;}
bool leftbottom(LL x, LL y, LL sz){return x<=sz && y>sz;}
bool righttop(LL x, LL y, LL sz){return x>sz && y<=sz;}
bool rightbottom(LL x, LL y, LL sz){return x>sz && y>sz;}
int a[][3] = {{1, 4}, {2, 3}};
int n,k;
LL dfs(LL siz, LL y, LL x){
LL ans = 0; int d = 0;
while(siz){
if(siz == 1) {ans ++; break;}
siz >>= 1; LL sum = siz*siz;
if(lefttop(x, y, siz)) d = 1;
else if(leftbottom(x, y, siz)) d = 2, y-=siz;
else if(rightbottom(x, y, siz)) d = 3, x-=siz, y-=siz;
else if(righttop(x, y, siz)) d = 4, x-=siz;
if(d == 1) ans += sum*(LL)(a[0][0]-1);
if(d == 2) ans += sum*(LL)(a[1][0]-1);
if(d == 3) ans += sum*(LL)(a[1][1]-1);
if(d == 4) ans += sum*(LL)(a[0][1]-1);
if((d==1&&a[0][0]==1) || (d==2&&a[1][0]==1) || (d==3&&a[1][1]==1) || (d==4&&a[0][1]==1)) swap(a[1][0], a[0][1]);
else if((d==1&&a[0][0]==4) || (d==2&&a[1][0]==4) || (d==3&&a[1][1]==4) || (d==4&&a[0][1]==4)) swap(a[1][1], a[0][0]);
}
return ans;
}
int main()
{
// freopen("D:\\in.txt", "r", stdin);
// freopen("D:\\out2.txt", "w", stdout);
while(~scanf("%d%d",&n,&k)) {
for(int i=1;i<=n;i++){
scanf("%lld%lld",&p[i].x,&p[i].y);
int d=0;
a[0][0] = 1, a[1][0] = 2, a[0][1] = 4, a[1][1] = 3;
p[i].id = dfs(1LL<<k, p[i].x, p[i].y);
}
sort(p+1, p+n+1);
for(int i=1; i<=n; i++) printf("%lld %lld\n", p[i].x, p[i].y);
}
return 0;
}
/**
1 1
2 1
2 2
1 2
1 3
1 4
2 4
2 3
3 3
3 4
4 4
4 3
4 2
3 2
3 1
4 1
5 1
5 2
6 2
6 1
7 1
8 1
8 2
7 2
7 3
8 3
8 4
7 4
6 4
6 3
5 3
5 4
5 5
5 6
6 6
6 5
7 5
8 5
8 6
7 6
7 7
8 7
8 8
7 8
6 8
6 7
5 7
5 8
4 8
3 8
3 7
4 7
4 6
4 5
3 5
3 6
2 6
2 5
1 5
1 6
1 7
2 7
2 8
1 8
*/
正常代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
struct node{
ll x,y,id;
}p[N];
bool cmp(node a,node b){
return a.id<b.id;
}
ll f(ll n,ll x,ll y){
if(n==0) return 1;
ll m=1<<(n-1);
if(x<=m&&y<=m) return f(n-1,y,x);
else if(x>m&&y<=m) return 3LL*m*m+f(n-1,m-y+1,m*2-x+1);
else if(x<=m&&y>m) return 1LL*m*m+f(n-1,x,y-m);
else return 2LL*m*m+f(n-1,x-m,y-m);
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%lld%lld",&p[i].x,&p[i].y);
p[i].id=f(k,p[i].y,p[i].x);
}
sort(p+1,p+1+n,cmp);
for(int i=1;i<=n;i++){
printf("%lld %lld\n",p[i].x,p[i].y);
}
return 0;
}