昨天作的多校題目。
題目大概就是說有n*m的一個方格,其中k個格子裏放了數字。而後進行q個操做,1是交換列,2是交換行,3是查詢當前x,y有什麼數字,沒有輸出0。node
題目不難,可是寫起來有點彆扭。主要的思想是hash。ios
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define maxn 100010 struct N { int x, y, val; } node[maxn]; int x[maxn], y[maxn]; int xx[maxn], yy[maxn]; int changex[maxn], changey[maxn]; int n, m, k; void solve1() { int q; cin >> q; while(q--) { int a, b, c; scanf("%d%d%d", &a, &b, &c); if(a == 3) puts("0"); } return ; } int find(int qq[], int q, int tk) { // for(int i = 0; i < tk; i ++) // cout << qq[i] << endl; int l = lower_bound(qq, qq+tk, q) - qq; int r = upper_bound(qq, qq+tk, q) - qq; // cout << qq[l] << " " << qq[r] << " " << q << endl; if(l > r) return -1; if(l == r-1) return l; else while(1) l++; } int solve(int qx, int qy) { qx = changex[qx]; qy = changey[qy]; int l = lower_bound(x, x+k, xx[qx]) - x; int r = upper_bound(x, x+k, xx[qx]) - x; r--; qy = yy[qy]; while(l <= r) { int m = (l + r) / 2; if(node[m].y > qy) r = m-1; else if(node[m].y < qy) l = m+1; else return node[m].val; } return 0; } bool cmp(N a, N b) { if(a.x == b.x) return a.y < b.y; return a.x < b.x; } int main() { // f//reopen("in.txt", "r", stdin); //freopen("out(2).txt", "w", stdout); int t; cin >> t; for(int Ca = 1; Ca <= t; Ca++) { scanf("%d%d%d", &n, &m, &k); for(int i = 0; i < k; i++) { scanf("%d%d%d", &node[i].x, &node[i].y, &node[i].val); x[i] = node[i].x; y[i] = node[i].y; } printf("Case #%d:\n", Ca); sort(node, node + k, cmp); sort(x, x + k); sort(y, y + k); if(!k) { solve1(); continue; } xx[0] = x[0]; int sumx = 1; for(int i = 1; i < k; i++) if(x[i] != x[i-1]) xx[sumx++] = x[i]; yy[0] =y[0]; int sumy = 1; for(int i = 1; i < k; i++) if(y[i] != y[i-1]) yy[sumy++] = y[i]; for(int i = 0; i < sumx; i++) changex[i] = i; for(int i = 0; i < sumy; i++) changey[i] = i; int q; cin >> q; while(q--) { int a, b, c; scanf("%d%d%d", &a, &b, &c); //cout << a << " " << b << " " << c << endl; if(a == 2) { b = find(yy, b, sumy); c = find(yy, c, sumy); if(b == -1 && c == -1) continue; swap(changey[b], changey[c]); } else if(a == 1) { b = find(xx, b, sumx); c = find(xx, c, sumx); //cout << a << " " << b << " " << c << endl; if(b == -1 && c == -1) continue; swap(changex[b], changex[c]); } else { b = find(xx, b, sumx); c = find(yy, c, sumy); if(b == -1 || c == -1) puts("0"); else printf("%d\n", solve(b, c)); } } } }
開始的時候WA了幾發,由於upper_bound(), 返回的是大於鍵值的第一個位置,我覺得是小於等於鍵值的第一個位置。。。
真心跪了,我這基礎是有多不紮實啊。ide