Codeforces -878D. Magic Breeding(搜索)

D. Magic Breeding

Time limit4000 msMemory limit1048576 kB

Description
Nikita and Sasha play a computer game where you have to breed some magical creatures. Initially, they have k creatures numbered from 1 to k. Creatures have n different characteristics.

Sasha has a spell that allows to create a new creature from two given creatures. Each of its characteristics will be equal to the maximum of the corresponding characteristics of used creatures. Nikita has a similar spell, but in his spell, each characteristic of the new creature is equal to the minimum of the corresponding characteristics of used creatures. A new creature gets the smallest unused number.

They use their spells and are interested in some characteristics of their new creatures. Help them find out these characteristics.

Input
The first line contains integers n, k and q (1 ≤ n ≤ 105, 1 ≤ k ≤ 12, 1 ≤ q ≤ 105) — number of characteristics, creatures and queries.

Next k lines describe original creatures. The line i contains n numbers ai1, ai2, …, ain (1 ≤ aij ≤ 109) — characteristics of the i-th creature.

Each of the next q lines contains a query. The i-th of these lines contains numbers ti, xi and yi (1 ≤ ti ≤ 3). They denote a query:

ti = 1 means that Sasha used his spell to the creatures xi and yi.
ti = 2 means that Nikita used his spell to the creatures xi and yi.
ti = 3 means that they want to know the yi-th characteristic of the xi-th creature. In this case 1 ≤ yi ≤ n.
It’s guaranteed that all creatures’ numbers are valid, that means that they are created before any of the queries involving them.

Output
For each query with ti = 3 output the corresponding characteristic.

input
2 2 4
1 2
2 1
1 1 2
2 1 2
3 3 1
3 4 2
output
2
1
input
5 3 8
1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
1 1 2
1 2 3
2 4 5
3 6 1
3 6 2
3 6 3
3 6 4
3 6 5
output
5
2
2
3
4
Note
In the first sample, Sasha makes a creature with number 3 and characteristics (2, 2). Nikita makes a creature with number 4 and characteristics (1, 1). After that they find out the first characteristic for the creature 3 and the second characteristic for the creature 4.

题意:开始有k个生物,有n个属性,支持3种操作:
1.将两个生物合并成一个新的生物,所有属性值为原来的较大值
2.将两个生物合并成一个新的生物,所有属性值为原来的较小值
3.询问i生物的第j个属性值
对于每次询问输出一行表示答案

根据题意,保存每次更新的生物,之后再查询即可,但是会MLE,所以用dfs搜索,合并生物时将其祖先和合并方法保存,下次查询直接找祖先就行。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <complex>
using namespace std;
const int N=1e5;

int node[15][N+5];
int n, k, q;
struct Ele
{
    int x, y, com=0;
}ele[N+15];//至少要加上12,12个初始生物+1e5个操作全是合并

int findd(int x, int y)
{
    if(x<=k)//如果是原先的k个生物,直接返回
        return node[x][y];
    int t1, t2;
    t1=findd(ele[x].x, y);
    t2=findd(ele[x].y, y);
    if(ele[x].com==1)
        return max(t1, t2);
    else
        return min(t1, t2);
}

int main()
{
    while(scanf("%d%d%d", &n, &k, &q)!=EOF)
    {
        memset(node, 0, sizeof node);
        for(int i=1; i<=k; i++)
            for(int j=1; j<=n; j++)
                scanf("%d", &node[i][j]);
        int com, x, y, cnt=k+1;//cnt从1开始也行,感觉这个方便
        for(int i=1; i<=q; i++)
        {
            scanf("%d%d%d", &com, &x, &y);
            if(com==1)
            {
                ele[cnt].x=x, ele[cnt].y=y, ele[cnt++].com=com;//将父节点和合并方式保存
            }
            else if(com==2)
            {
                ele[cnt].x=x, ele[cnt].y=y, ele[cnt++].com=com;
            }
            else
            {
                printf("%d\n", findd(x,y));
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值