Codeforces Round #443 (Div. 1) D. Magic Breeding 位運算

D. Magic Breeding

http://codeforces.com/contest/878/problem/Dc++

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.this

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.spa

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

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.code

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

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:get

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.input

Output

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

Examples

input

2 2 4
1 2
2 1
1 1 2
2 1 2
3 3 1
3 4 2io

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個屬性。
而後如今你有三個操做:
第一個操做是用x和y怪物生成一個新的怪物,這個怪物的屬性是x和y的最大值
第二個操做是用x和y怪物生成一個新的怪物,這個怪物的屬性是x和y的最小值
第三個操做是詢問第x個怪物的第y個屬性是多少。

題解

對於每一個屬性而言,就k種狀況。
考慮最簡單的狀況,屬性只有0和1。那麼咱們暴力能夠用一個數字來表示怪獸的每一個屬性的取值。
取最大就是取|,最小就是取&,而後最後看是0是1便可。

可是如今屬性是1e9範圍的,咱們也能夠這樣作,咱們用1表示這個位置的屬性至少爲這麼大。
而後和以前同樣作,最後輸出最大的合法屬性便可。

代碼

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
bitset<4096>S[maxn];
int a[12][maxn];
int n,k,q,tot;
int main(){
    scanf("%d%d%d",&n,&k,&q);
    tot=k;
    for(int i=0;i<k;i++){
        for(int j=0;j<4096;j++){
            if(j&(1<<i))S[i].set(j);
        }
        for(int j=0;j<n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(int qq=0;qq<q;qq++){
        int op,x,y;
        scanf("%d%d%d",&op,&x,&y);
        x--,y--;
        if(op==1)S[tot++]=S[x]&S[y];
        if(op==2)S[tot++]=S[x]|S[y];
        if(op==3){
            vector<pair<int,int> >Q;
            for(int i=0;i<k;i++){
                Q.push_back(make_pair(a[i][y],i));
            }
            sort(Q.begin(),Q.end());
            int b = 0;
            for(int i=0;i<k;i++){
                b|=(1<<Q[i].second);
                if(S[x][b]){
                    cout<<Q[i].first<<endl;
                    break;
                }
            }
        }
    }
}
相關文章
相關標籤/搜索