zoukankan      html  css  js  c++  java
  • hdu 4941

    Magical Forest

    Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 2016    Accepted Submission(s): 893


    Problem Description
    There is a forest can be seen as N * M grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location(Xi, Yi) and the energy can be provided Ci.

    However, the forest will make the following change sometimes:
    1. Two rows of forest exchange.
    2. Two columns of forest exchange.
    Fortunately, two rows(columns) can exchange only if both of them contain fruits or none of them contain fruits.

    Your superior attach importance to these magical fruit, he needs to know this forest information at any time, and you as his best programmer, you need to write a program in order to ask his answers quick every time.
     
    Input
    The input consists of multiple test cases.

    The first line has one integer W. Indicates the case number.(1<=W<=5)

    For each case, the first line has three integers N, M, K. Indicates that the forest can be seen as maps N rows, M columns, there are K fruits on the map.(1<=N, M<=2*10^9, 0<=K<=10^5)

    The next K lines, each line has three integers X, Y, C, indicates that there is a fruit with C energy in X row, Y column. (0<=X<=N-1, 0<=Y<=M-1, 1<=C<=1000)

    The next line has one integer T. (0<=T<=10^5)
    The next T lines, each line has three integers Q, A, B.
    If Q = 1 indicates that this is a map of the row switching operation, the A row and B row exchange.
    If Q = 2 indicates that this is a map of the column switching operation, the A column and B column exchange.
    If Q = 3 means that it is time to ask your boss for the map, asked about the situation in (A, B).
    (Ensure that all given A, B are legal. )
     
    Output
    For each case, you should output "Case #C:" first, where C indicates the case number and counts from 1.

    In each case, for every time the boss asked, output an integer X, if asked point have fruit, then the output is the energy of the fruit, otherwise the output is 0.
     
    Sample Input
    1
    3 3 2
    1 1 1
    2 2 2
    5
    3 1 1
    1 1 2
    2 1 2
    3 1 1
    3 2 2
     
    Sample Output
    Case #1:
    1
    2
    1
     
    析:一个n*m的网格,其中某些格子中有魔法水果,每个水果都有一个能力值,现有3种操作:
    1.交换任意两行A与B
    2.交换任意两列A与B
    3.查询位置(A, B)位置上的状态
    n与m均太大,不可直接存储,但水果个数仅有1e5个,故可开两个map,rw[x]保存每次交换后该行的原来行标,另一个cl[y]表示列号,与之类似
    并另用一个map,val[x][y]存储原位置的实际值
     
    #include <map>
    #include <queue>
    #include <math.h>
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #define ll long long
    #define ull unsigned ll
    #define Inf 0x7fffffff
    #define maxn 50005
    #define maxm 100005
    #define P pair<int, int>
    #define eps 1e-6
    #define mod 10000
    using namespace std;
    const int N = 205;
    int t, n, m, len, ans;
    int x, y, c, k;
    map<int, int>rw, cl;
    map<P, int>val;
    int main(){
        int tot = 1;
        scanf("%d", &t);
        while(t--){
            scanf("%d%d%d", &n, &m, &k);
            for(int i = 1; i <= k; i ++){
                scanf("%d%d%d", &x, &y, &c);
                val[P(x, y)] = c;
            }
            int q, xx, yy, res;
            scanf("%d", &q);
            printf("Case #%d:
    ", tot++);
            while(q--){
                scanf("%d%d%d", &c, &x, &y);
                xx = x, yy = y;
                if(!--c){
                    if(rw.count(x))
                        xx = rw[x];
                    if(rw.count(y))
                        yy = rw[y];
                    rw[x] = yy, rw[y] = xx;
                }else if(c < 2){
                    if(cl.count(x))
                        xx = cl[x];
                    if(cl.count(y))
                        yy = cl[y];
                    cl[x] = yy, cl[y] = xx;
                }else{
                    res = 0;
                    if(rw.count(x))
                        xx = rw[x];
                    if(cl.count(y))
                        yy = cl[y];
                    if(val.count(P(xx, yy)))
                        res = val[P(xx, yy)];
                    printf("%d
    ", res);
                }
            }
            rw.clear(), cl.clear(), val.clear();
        }
        return 0;
    }
    View Code
  • 相关阅读:
    三角函数
    第十七次作业
    第十六次作业
    第15次作业
    第13次java作业
    第十二次java作业
    第十一次java作业
    第十次java作业
    第九次java
    第八次java作业
  • 原文地址:https://www.cnblogs.com/microcodes/p/12795154.html
Copyright © 2011-2022 走看看