被虐了一下午。。离散化,开一个rr[mnx], cc[mnx],初始化为rr[i] = i, cc[i],然后换的时候就换这两个数组就好了。。
然后就是不断的lower_bound
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdio> 5 #include<string> 6 #include<queue> 7 #include<cmath> 8 #include<vector> 9 10 using namespace std; 11 12 #define mnx 104000 13 #define ll long long 14 #define inf 0x3f3f3f3f 15 #define lson l, m, rt << 1 16 #define rson m+1, r, rt << 1 | 1 17 18 int cx[mnx], cy[mnx], rr[mnx], cc[mnx], sx, sy; 19 struct point{ 20 int x, y, c; 21 point( int x = 0, int y = 0, int c = 0 ) : x(x), y(y), c(c) {} 22 bool operator < ( const point & b ) const{ 23 if( x != b.x ) return x < b.x; 24 if( y != b.y ) return y < b.y; 25 return c < b.c; 26 } 27 }p[mnx]; 28 void init( int n ){ 29 for( int i = 0; i <= n; i++ ){ 30 rr[i] = i, cc[i] = i; 31 } 32 } 33 int main(){ 34 int cas, cnt = 1; 35 scanf( "%d", &cas ); 36 while( cas-- ){ 37 int n, m, k, x, y, c; 38 scanf( "%d %d %d", &n, &m, &k ); 39 for( int i = 0; i < k; i++ ){ 40 scanf( "%d %d %d", &x, &y, &c ); 41 p[i].x = x, p[i].y = y, p[i].c = c; 42 cx[i] = x, cy[i] = y; 43 } 44 sort( p, p + k ); 45 sort( cx, cx + k ); 46 sx = unique( cx, cx + k ) - cx; 47 sort( cy, cy + k ); 48 sy = unique( cy, cy + k ) - cy; 49 init( k ); 50 int q; 51 scanf( "%d", &q ); 52 printf( "Case #%d:\n", cnt++ ); 53 while( q-- ){ 54 scanf( "%d %d %d", &c, &x, &y ); 55 if( c == 1 ){ 56 int a = lower_bound( cx, cx + sx, x ) - cx; 57 int b = lower_bound( cx, cx + sx, y ) - cx; 58 if( cx[a] != x ) continue; 59 swap( rr[a], rr[b] ); 60 } 61 if( c == 2 ){ 62 int a = lower_bound( cy, cy + sy, x ) - cy; 63 int b = lower_bound( cy, cy + sy, y ) - cy; 64 if( cy[a] != x ) continue; 65 swap( cc[a], cc[b] ); 66 } 67 if( c == 3 ){ 68 int a = lower_bound( cx, cx + sx, x ) - cx; 69 int b = lower_bound( cy, cy + sy, y ) - cy; 70 if( x != cx[a] || y != cy[b] ){ 71 puts( "0" ); 72 } 73 a = rr[a], b = cc[b]; 74 a = cx[a], b = cy[b]; 75 int ans = lower_bound( p, p + k, point( a, b, 0 ) ) - p; 76 if( p[ans].x == a && p[ans].y == b ){ 77 printf( "%d\n", p[ans].c ); 78 } 79 else puts( "0" ); 80 } 81 } 82 } 83 return 0; 84 }
hdu 4941 Magical Forest,布布扣,bubuko.com
时间: 2024-10-13 22:28:25