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

题目:

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)模拟题

时间: 2024-08-09 02:20:21

HDU多校联合赛(1007 Magical Forest)模拟题的相关文章

Wow! Such Sequence! HDU多校联合赛第三场1007

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It's

HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是把区间 (l,r) 中大于x的数跟 x 做gcd操作. 线段树区间更新的题目,每个节点保存一个最大和最小值,当该节点的最大值和最小值相等的时候表示这个区间所有的数字都是相同的,可以直接对这个区间进行1或2操作, 进行1操作时,当还没有到达要操作的区间但已经出现了节点的最大值跟最小值相等的情况时,说明

hdu5336 多校联合第四场1010 模拟+bfs优先队列

http://acm.hdu.edu.cn/showproblem.php?pid=5336 Problem Description XYZ is playing an interesting game called "drops". It is played on a r?c grid. Each grid cell is either empty, or occupied by a waterdrop. Each waterdrop has a property "siz

HDU 4930 Fighting the Landlords(扯淡模拟题)

Fighting the Landlords 大意: 斗地主....   分别给出两把手牌,肯定都合法.每张牌大小顺序是Y (i.e. colored Joker) > X (i.e. Black & White Joker) > 2 > A (Ace) > K (King) > Q (Queen) > J (Jack) > T (10) > 9 > 8 > 7 > 6 > 5 > 4 > 3. 给你8种组合:1.

HDU 4028 The time of a day STL 模拟题

暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define ll __int64 #define N 42 ll n,m,ans;

HDU 4772 Zhuge Liang&#39;s Password (简单模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1404    Accepted Submission(s): 926 Problem Description In the anc

hdu多校(二) 1004 1007 1010

Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Alice and Bob are playing a game.The game is played on a set of positive integers from 1 t

HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第一种是行交换操作,就是把矩阵的两行进行交换,另一种是列交换操作,注意两种操作都要求行或列至少要有一个水果,第三种操作是查找,询问第A行B列的水果的能量值,如果查询的位置没有水果,则输出0. 因为n和m都很大,达到了2*10^9,但水果最多一共只有10^5个,我的做法是直接用结构体存了之后排序,然后m

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分相同,第一部分与第二部分对称. 现在给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法,求出以第i个点为中心的回文串长度,记录到数组p中 要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样. 因为我们已经记录下来以