首页 > 代码库 > HDU4941Magical Forest (二分+链表优化)

HDU4941Magical Forest (二分+链表优化)

Magical Forest

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


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.

Author
UESTC

Source
2014 Multi-University Training Contest 7 
用时1156MS。
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;

typedef struct nnn
{
    int id,valu;
    struct nnn *next;
}node;
typedef struct nnnnn
{
    int x,y,v;
}Friut;
Friut friut[100005];
node *map[100005];//最初水果的位置
int R[100005],rn,L[100005],ln,fn,row[100005],colum[100005];//row[i]表示交换后的第i行在最初行的位置是row[i],列则同理

int cmp(int a,int b){ return a<b;}
int RtwoFind(int ans)
{
    int l,r,m;
    l=1; r=rn;
    while(l<=r)
    {
        m=(l+r)/2;
        if(ans==R[m])return m;
        if(ans<R[m])r=m-1;
        else l=m+1;
    }
    return 0;
}
int LtwoFind(int ans)
{
    int l,r,m;
    l=1; r=ln;
    while(l<=r)
    {
        m=(l+r)/2;
        if(ans==L[m])return m;
        if(ans<L[m])r=m-1;
        else l=m+1;
    }
    return 0;
}

void buildeMap()
{
    for(int i=1;i<=rn;i++)
    {
        map[i]=new node; map[i]->next=NULL;
    }
    node *p;
    for(int i=1;i<=fn;i++)
    {
        int l,r;
        r=RtwoFind(friut[i].x);
        l=LtwoFind(friut[i].y);
        row[r]=r; colum[l]=l;//当前行列下标指向当前的行列
        p=new node;
        p->id=l; p->valu=friut[i].v;
        p->next=map[r]->next; map[r]->next=p;
    }
}
void reSortR()
{
    sort(R+1,R+rn+1,cmp);
    int j=1;
    for(int i=2;i<=rn;i++)
    if(R[j]!=R[i])
    R[++j]=R[i];
    rn=j;
}
void reSortL()
{
    sort(L+1,L+ln+1,cmp);
    int j=1;
    for(int i=2;i<=ln;i++)
    if(L[j]!=L[i])
    L[++j]=L[i];
    ln=j;
}
void exchgR(int r1,int r2)
{
    r1=RtwoFind(r1);
    r2=RtwoFind(r2);
    if(!r1||!r2) return ;
    int tem=row[r1];
    row[r1]=row[r2]; row[r2]=tem;
}
void exchgL(int l1,int l2)
{
    l1=LtwoFind(l1);
    l2=LtwoFind(l2);
    if(!l1||!l2) return ;
    int tem=colum[l1];
    colum[l1]=colum[l2];
    colum[l2]=tem;
}
int query(int x,int y)
{
    int tx,ty;
    node *p;
    tx=RtwoFind(x); ty=LtwoFind(y);
    if(tx==0||ty==0)return 0;
    tx=row[tx]; ty=colum[ty];
    p=map[tx]->next;
    while(p&&p->id!=ty)p=p->next;
    if(p)return p->valu;
    return 0;
}
int main()
{
    int N,M,t,cas=0,Q,b,a;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case #%d:\n",++cas);
        scanf("%d%d%d",&N,&M,&fn);
        rn=ln=0;
        for(int i=1;i<=fn;i++)
        {
            scanf("%d%d%d",&friut[i].x,&friut[i].y,&friut[i].v);
            R[++rn]=friut[i].x; L[++ln]=friut[i].y;
        }
        reSortR();
        reSortL();
        buildeMap();
        scanf("%d",&M);
        while(M--)
        {
            scanf("%d%d%d",&Q,&a,&b);
            if(Q==1)exchgR(a,b);
            else if(Q==2)exchgL(a,b);
            else printf("%d\n",query(a,b));
        }
    }
}