878D - Magic Breeding - CodeForces Solution


bitmasks *2900

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define rg register
#define file(x)freopen(x".in","r",stdin);freopen(x".out","w",stdout)

using namespace std;

const int maxn=1e5+10;
const int maxK=12+1;

int n,K,Q;
int A[maxn][maxK];
int Rk[maxK][maxn];
int Value[maxK][maxn*maxK];

int Stand[maxn*maxK];

bitset<(1<<12)> Sta[maxn<<1];
inline int Query(int x,int Column)
{
	Column=Stand[Column];
	return Sta[x][Column];
}

int main()
{
	scanf("%d %d %d",&n,&K,&Q);
	for(rg int i=1;i<=K;i+=1)
		for(rg int j=1;j<=n;j+=1)
			scanf("%d",A[j]+i);
	for(rg int i=1;i<=n;i+=1)
	{
		static int P[maxK];
		for(rg int j=1;j<=K;j+=1)P[j]=j;
		sort(P+1,P+K+1,[&](auto x,auto y){return A[i][x]<A[i][y];});
		for(rg int j=1;j<=K;j+=1)Rk[P[j]][i]=j;
		sort(A[i]+1,A[i]+K+1);
	}
	for(rg int i=1;i<=K;i+=1)
	{
		int id=0;
		for(rg int j=1;j<=n;j+=1)
		{
			for(rg int k=1;k<=Rk[i][j];k+=1)Value[i][++id]=1;
			for(rg int k=Rk[i][j]+1;k<=K;k+=1)Value[i][++id]=0;
		}
	}
	for(rg int i=1;i<=n*K;i+=1)
	{
		int &p=Stand[i];
		p=0;
		for(rg int j=K;j;j-=1)
		{
			p<<=1;
			p+=Value[j][i];
		}
	}
	for(rg int i=0;i<(1<<K);i+=1)
		for(rg int j=1;j<=K;j+=1)
			Sta[j][i]=(i>>(j-1))&1;
	int tnt=K;
	while(Q--)
	{
		int opt,x,y;scanf("%d %d %d",&opt,&x,&y);
		if(opt==1)Sta[++tnt]=Sta[x]|Sta[y];
		else if(opt==2)Sta[++tnt]=Sta[x]&Sta[y];
		else
		{
			int Be=(y-1)*K+1,End=y*K;
			int pos=Be;
			while(pos<End&&Query(x,pos+1))pos+=1;
			pos=pos-Be+1;
			printf("%d\n",A[y][pos]);
		}
	}
	return 0;
}
//ggggg
 	 	 	 	 		 			 	 		 	 	 		 			


Comments

Submit
0 Comments
More Questions

1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game