結果

問題 No.2505 matriX cOnstRuction
ユーザー kotatsugamekotatsugame
提出日時 2023-10-13 23:29:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,500 ms
コード長 2,146 bytes
コンパイル時間 830 ms
コンパイル使用メモリ 80,624 KB
実行使用メモリ 72,824 KB
最終ジャッジ日時 2023-10-13 23:29:30
合計ジャッジ時間 6,592 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 18 ms
4,348 KB
testcase_02 AC 15 ms
4,348 KB
testcase_03 AC 15 ms
4,348 KB
testcase_04 AC 16 ms
4,348 KB
testcase_05 AC 15 ms
4,348 KB
testcase_06 AC 18 ms
4,348 KB
testcase_07 AC 10 ms
4,348 KB
testcase_08 AC 10 ms
4,348 KB
testcase_09 AC 10 ms
4,348 KB
testcase_10 AC 10 ms
4,348 KB
testcase_11 AC 10 ms
4,348 KB
testcase_12 AC 10 ms
4,348 KB
testcase_13 AC 10 ms
4,348 KB
testcase_14 AC 10 ms
4,348 KB
testcase_15 AC 10 ms
4,348 KB
testcase_16 AC 10 ms
4,348 KB
testcase_17 AC 10 ms
4,352 KB
testcase_18 AC 10 ms
4,348 KB
testcase_19 AC 10 ms
4,356 KB
testcase_20 AC 10 ms
4,352 KB
testcase_21 AC 10 ms
4,348 KB
testcase_22 AC 10 ms
4,348 KB
testcase_23 AC 10 ms
4,348 KB
testcase_24 AC 10 ms
4,348 KB
testcase_25 AC 10 ms
4,352 KB
testcase_26 AC 10 ms
4,348 KB
testcase_27 AC 10 ms
4,348 KB
testcase_28 AC 10 ms
4,348 KB
testcase_29 AC 10 ms
4,348 KB
testcase_30 AC 9 ms
4,352 KB
testcase_31 AC 9 ms
4,348 KB
testcase_32 AC 9 ms
4,352 KB
testcase_33 AC 9 ms
4,348 KB
testcase_34 AC 14 ms
4,944 KB
testcase_35 AC 8 ms
4,348 KB
testcase_36 AC 13 ms
4,896 KB
testcase_37 AC 12 ms
5,076 KB
testcase_38 AC 17 ms
8,720 KB
testcase_39 AC 40 ms
21,060 KB
testcase_40 AC 16 ms
8,944 KB
testcase_41 AC 188 ms
70,264 KB
testcase_42 AC 17 ms
8,008 KB
testcase_43 AC 189 ms
72,824 KB
testcase_44 AC 192 ms
70,620 KB
testcase_45 AC 194 ms
72,412 KB
testcase_46 AC 189 ms
69,656 KB
testcase_47 AC 193 ms
72,244 KB
testcase_48 AC 63 ms
20,740 KB
testcase_49 AC 193 ms
69,840 KB
testcase_50 AC 195 ms
69,644 KB
testcase_51 AC 282 ms
4,352 KB
testcase_52 AC 136 ms
39,792 KB
testcase_53 AC 130 ms
37,148 KB
testcase_54 AC 132 ms
39,248 KB
testcase_55 AC 15 ms
4,352 KB
testcase_56 AC 15 ms
4,348 KB
testcase_57 AC 15 ms
4,352 KB
testcase_58 AC 18 ms
5,076 KB
testcase_59 AC 20 ms
6,332 KB
testcase_60 AC 20 ms
6,752 KB
testcase_61 AC 13 ms
4,348 KB
testcase_62 AC 13 ms
4,348 KB
testcase_63 AC 10 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cassert>
using namespace std;
#include<array>
#include<vector>
#include<cassert>
template<unsigned int sz>
struct binarytrie{
	using Bit=typename conditional<sz<=32,unsigned int,unsigned long long>::type;
	struct node{
		long val;
		array<int,2>nxt;
		node():val(0L),nxt({-1,-1}){}
	};
	vector<node>v;
	binarytrie(){v.emplace_back();}
	int nxt(int u,int i)
	{
		if(v[u].nxt[i]==-1)
		{
			v[u].nxt[i]=v.size();
			v.emplace_back();
		}
		return v[u].nxt[i];
	}
	void range_add(Bit a,Bit r,long w)
	{
		assert(0<=a&&(a>>sz)==0);
		if(r==((Bit)1<<sz))
		{
			v[0].val+=w;
			return;
		}
		assert(0<=r&&(r>>sz)==0);
		int p=0;
		for(int i=sz;i--;)
		{
			int t=a>>i&1;
			if(r>>i&1)
			{
				v[nxt(p,t)].val+=w;
				p=nxt(p,1-t);
			}
			else p=nxt(p,t);
		}
		v[p].val+=w;
	}
	long get_min(int u)
	{
		long ret=1e18;
		for(int t:v[u].nxt)ret=min(ret,t==-1?0L:get_min(t));
		ret+=v[u].val;
		return ret;
	}
	long get_min(){return get_min(0);}
};
int N,M;
int R[50505],C[50505];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;cin>>T;
	for(;T--;)
	{
		cin>>N>>M;
		for(int i=0;i<N;i++)cin>>R[i];
		for(int i=0;i<M;i++)cin>>C[i];
		vector<vector<int> >A(N,vector<int>(M));
		for(int i=0;i<N;i++)for(int j=0;j<M;j++)cin>>A[i][j];
		{//check
			bool ok=true;
			for(int i=0;i<N;i++)
			{
				int v=i==0?0:A[i][0]^A[0][0];
				for(int j=0;j<M;j++)
				{
					if(A[i][j]!=(v^A[0][j]))ok=false;
				}
			}
			if(!ok)
			{
				cout<<"-1\n";
				continue;
			}
		}
		binarytrie<30>BT;
		long inf=1e9;
		for(int j=0;j<M;j++)
		{
			//x^A[0][j], C[j]
			int v=0;
			while(C[j]>>v)v++;
			BT.range_add(A[0][j],1<<30,inf);
			BT.range_add(A[0][j],(1<<v)-1,2-inf);
			BT.range_add(A[0][j],C[j],-1);
			BT.range_add(A[0][j],0,-1);
		}
		for(int i=0;i<N;i++)
		{
			//x^A[0][0]^A[i][0], R[i]
			int v=0;
			while(R[i]>>v)v++;
			BT.range_add(A[0][0]^A[i][0],1<<30,inf);
			BT.range_add(A[0][0]^A[i][0],(1<<v)-1,2-inf);
			BT.range_add(A[0][0]^A[i][0],R[i],-1);
			BT.range_add(A[0][0]^A[i][0],0,-1);
		}
		long t=BT.get_min();
		if(t<inf)cout<<t<<"\n";
		else cout<<"-1\n";
	}
}
0