HDU 4941 Magical Forest 离散化

来源:互联网 发布:windows update在哪里 编辑:程序博客网 时间:2024/06/04 18:13

题意: 一个n * m 的图, 某些点有苹果,每个苹果有价值, 然后现在交换某些行或者列, 或者查询某个点的苹果的价值(没有苹果就是0)。

还是比较好想的。。x[],y[]数组存当前状态下的各行各列存的是初始时的哪一行哪一列,交换的时候交换值就行了, 可能复杂就在离散化吧,不过实际上用map离散化也挺easy的样子。。虽然我用的最传统的vector、sort、 unique、resize然后upper_bound、、

题目给了12s实际上3s完全够了。。

#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <string>#include <cctype>#include <cstdio>#include <vector>#include <cstdlib>#include <cstring>#include <iomanip>#include <sstream>#include <iostream>#include <algorithm>using namespace std;#define ls id<<1,l,mid#define rs id<<1|1,mid+1,r#define OFF(x) memset(x,-1,sizeof x)#define CLR(x) memset(x,0,sizeof x)#define MEM(x) memset(x,0x3f,sizeof x)typedef long long ll ;typedef pair<int,int> pii ;const int maxn = 3e5+50 ;const int inf = 0x3f3f3f3f ;const int MOD = 1e9+7 ;struct pnt{    int x, y ,c;    pnt(int x, int y, int c):x(x),y(y),c(c){}};int N, M, n, m, k, q, T;vector<pnt> fruit, query;vector<int> Vx, Vy;int mx[maxn], my[maxn];map<pii, int> mp;void init() {    sort(Vx.begin(), Vx.end());    sort(Vy.begin(), Vy.end());    Vx.resize(unique(Vx.begin(), Vx.end()) - Vx.begin());    Vy.resize(unique(Vy.begin(), Vy.end()) - Vy.begin());    for (pnt e : fruit) {        int x = e.x, y = e.y;        x = upper_bound(Vx.begin(), Vx.end(), x) - Vx.begin();        y = upper_bound(Vy.begin(), Vy.end(), y) - Vy.begin();        mp[pii(x,y)] = e.c;    }    n = Vx.size();m = Vy.size();}inline int read() {    char c = getchar();    while (!isdigit(c)) c = getchar();    int x = 0;    while (isdigit(c)) {        x = x * 10 + c - '0' ;        c = getchar();    }    return x;}int main () {#ifdef LOCALfreopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);//      freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);#endif    scanf("%d", &T);    int cas = 1;    while (T--) {        printf("Case #%d:\n",cas++);        scanf("%d%d%d", &n, &m, &k);        Vx.clear();Vy.clear();        fruit.clear();query.clear();        mp.clear();        Vx.push_back(n);Vy.push_back(m);        Vx.push_back(0);Vy.push_back(0);        int x, y, c;        while (k--) {            x = read(), y = read(), c = read();            Vx.push_back(x);            Vy.push_back(y);            fruit.push_back(pnt(x,y,c));        }        q = read();        while (q--) {            c = read(), x = read(), y = read();            if (c == 1) {                Vx.push_back(x);                Vx.push_back(y);            } else if (c == 2) {                Vy.push_back(x);                Vy.push_back(y);            } else {                Vx.push_back(x);                Vy.push_back(y);            }            query.push_back(pnt(x, y, c));        }        init();        for (int i = 1; i <= n; i++) mx[i] = i;        for (int i = 1; i <= m; i++) my[i] = i;        for (pnt q : query) {            int op = q.c;            if (op == 3) {                int x = upper_bound(Vx.begin(), Vx.end(), q.x) - Vx.begin();                int y = upper_bound(Vy.begin(), Vy.end(), q.y) - Vy.begin();                x = mx[x], y = my[y];                if (mp.count(pii(x,y))) printf("%d\n",mp[pii(x,y)]);                else puts("0");            } else if (op == 1) {                int x1 = upper_bound(Vx.begin(), Vx.end(), q.x) - Vx.begin();                int x2 = upper_bound(Vx.begin(), Vx.end(), q.y) - Vx.begin();                swap(mx[x1], mx[x2]);            } else {                int y1 = upper_bound(Vy.begin(), Vy.end(), q.x) - Vy.begin();                int y2 = upper_bound(Vy.begin(), Vy.end(), q.y) - Vy.begin();                swap(my[y1], my[y2]);            }        }    }return 0;}

0 0