码迷,mamicode.com
首页 > 其他好文 > 详细

HDU多校联合赛(1007 Magical Forest)模拟题

时间:2014-08-12 18:34:54      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   数据   for   

题目:

 

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
Hint
No two fruits at the same location.
做法:本来想写二维线段树的。。敲了两分钟发现sb了。。首先离散是必须的,然后你会发现只用维护这个矩阵的下标就行。。
效率:Q(q)
注意多组数据T_T...
Codes
  1 #include<set>
  2 #include<map>
  3 #include<queue>
  4 #include<cstdio>
  5 #include<cstdlib>
  6 #include<cstring>
  7 #include<iostream>
  8 #include<algorithm>
  9 using namespace std;
 10 const int N = 100010;
 11 #define For(i,n) for(int i=1;i<=n;i++)
 12 #define Rep(i,l,r) for(int i=l;i<=r;i++)
 13 int q,n,m,op,k,row[N],line[N],Row[N],Line[N];
 14 int T,x,y,c,tx,ty;
 15 map< pair<int,int> , int > Ans;
 16 
 17 struct querys{
 18     int x,y,c;
 19 }Query[N];
 20 
 21 int find(int x,int upper,int A[]){
 22     int L = 1 , R = upper;
 23     while(R-L>1){
 24         int Mid = (L+R)>>1;
 25         if(A[Mid] > x) R = Mid;
 26         else           L = Mid;
 27     }
 28     if(A[R]==x) return R;
 29     else if(A[L]==x) return L;
 30     else        return -1;
 31 }
 32 
 33 void read(int &v){
 34     int num = 0; char ch = getchar();
 35     while(ch>9||ch<0) ch = getchar();
 36     while(ch>=0&&ch<=9){
 37         num = num * 10 + ch - 0;
 38         ch = getchar();
 39     }
 40     v = num;
 41 }
 42 
 43 bool cmp(int A,int B){
 44     return A < B;
 45 }
 46 
 47 bool cmp2(int A,int B){
 48     return (A==B);
 49 }
 50 
 51 void init(){
 52     Ans.clear();
 53     read(n);read(m);read(k);
 54     For(i,k){
 55         read(x);read(y);read(c);
 56         Query[i].x = x ; Query[i].y = y; Query[i].c = c;
 57         Row[i] = x;Line[i] = y;
 58     }
 59     sort(Row+1,Row+k+1,cmp);
 60     sort(Line+1,Line+k+1,cmp);
 61     n = unique(Row+1,Row+k+1,cmp2) - Row - 1;
 62     m = unique(Line+1,Line+k+1,cmp2) - Line - 1;
 63     For(i,n) row[i] = i;
 64     For(i,m) line[i] = i;
 65     For(i,k){
 66         int x = find(Query[i].x,n,Row) , y = find(Query[i].y,m,Line);
 67         Ans.insert(make_pair(make_pair(x,y),Query[i].c));
 68     }
 69 }
 70 
 71 void solve(){
 72     read(q);
 73     For(i,q){
 74         read(op);read(tx);read(ty);
 75         if(op==1){
 76             x = find(tx,n,Row); y = find(ty,n,Row);
 77         }
 78         if(op==2){
 79             x = find(tx,m,Line); y = find(ty,m,Line);
 80         }
 81         if(op==3){
 82             x = find(tx,n,Row); y = find(ty,m,Line);
 83         }
 84         if(x==-1||y==-1) {
 85             if(op==3) puts("0");
 86             continue;
 87         }
 88         if(op==1) swap(row[x],row[y]);
 89         if(op==2) swap(line[x],line[y]);
 90         if(op==3){     
 91             map< pair<int,int> , int >::iterator tans = Ans.find(make_pair(row[x],line[y]));
 92             if(tans==Ans.end())                     printf("0\n");
 93             else                                    printf("%d\n",tans->second);
 94         }
 95     }
 96 }
 97 
 98 int main(){
 99     scanf("%d",&T);
100     For(i,T) {
101         printf("Case #%d:\n",i);
102         init();
103         solve();
104     }
105     return 0;
106 }

 

HDU多校联合赛(1007 Magical Forest)模拟题,布布扣,bubuko.com

HDU多校联合赛(1007 Magical Forest)模拟题

标签:des   style   blog   color   os   io   数据   for   

原文地址:http://www.cnblogs.com/kjerome/p/3907807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!