水题也要优化效率嘛
1.用两个数组单独记录下标的更新
2.用STL中lower_bound来进行二分查找.
要注意lower_bound的返回值意义 是大于等于val的第一个,所以返回值要进行判断才可以利用
#include <iostream> #include <cstdio> #include <algorithm> #include <cstdlib> using namespace std; struct Point { int x; int y; int z; }; int n; const int MAXN = 100000; //int data[MAXN][MAXN]; Point points[MAXN]; int curx[MAXN]; int cury[MAXN]; void init(){ cin>>n; for (int i = 0; i < n; ++i){ //int x,y,z; //scanf("%d %d %d",&x,&y,&z); //data[x][y] = z; scanf("%d %d %d",&points[i].x,&points[i].y,&points[i].z); } for (int i = 0; i < MAXN; ++i) curx[i]=i; for (int i = 0; i < MAXN; ++i) cury[i]=i; } bool cmpPoint(const Point& a,const Point& b){ if(a.x!=b.x){ return a.x<b.x; }else return a.y<b.y; } int find(int x,int y){ Point tofind; tofind.x = x; tofind.y = y; Point* f = lower_bound(points,points+n,tofind,cmpPoint); //注意lower_bound的返回值是大于等于val的第一个 不一定正好是它 if(f != points+n and f->x==x and f->y==y) return f->z; return 0; } void exe(){ int m; cin>>m; sort(points,points+n,cmpPoint); for (int i = 0; i < m; ++i) { int op,x,y; scanf("%d %d %d",&op,&x,&y); if(op==0){ swap(curx[x],curx[y]); }else if(op==1) swap(cury[x],cury[y]); else{ //printf("%d ", data[curx[x]][cury[y]]); printf("%d ", find(curx[x],cury[y])); } } } int main(int argc, char const *argv[]) { init(); exe(); return 0; }