QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#51053#859. CivilizationstricyzhkxRE 5ms15588kbC++142.8kb2022-09-30 12:57:102022-09-30 12:57:10

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-30 12:57:10]
  • 评测
  • 测评结果:RE
  • 用时:5ms
  • 内存:15588kb
  • [2022-09-30 12:57:10]
  • 提交

answer

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,mx[250010],mn[250010],a[510][510],p[510][510],sum[250010],cnt[250010],len[250010],t[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
multiset<int> st[250010];
struct Arr
{
	vector<int> legal;
	int tot,c[250010],pos[250010];
	void init(int t)
	{
		tot=t;legal.clear();
		for(int i=0;i<=tot;i++) pos[i]=-1,c[i]=0;
	}
	void Inc(int i)
	{
		if(!c[i]) pos[i]=legal.size(),legal.push_back(i);
		c[i]++;
	}
	void Dec(int i)
	{
		if(c[i]==1)
		{
			swap(legal[pos[i]],legal.back());pos[legal[pos[i]]]=i;
			legal.pop_back();pos[i]=-1;
		}
		c[i]--;
	}
}Ar;
void pushup(int i)
{
	if(st[i].empty()) mn[i]=1e9,mx[i]=-1e9;
	else mn[i]=*st[i].begin(),mx[i]=*--st[i].end();
}
void Insert(int i)
{
	if(!cnt[i]) return;
	Ar.Inc(len[i]);st[len[i]].insert(sum[i]);
	pushup(len[i]);
}
void Erase(int i)
{
	if(!cnt[i]) return;
	Ar.Dec(len[i]);st[len[i]].erase(st[len[i]].find(sum[i]));
	pushup(len[i]);
}
ll query(ll A,ll B,ll C)
{
	ll ans=-1e18;
	for(int l:Ar.legal)
	{
		if(st[l].empty()) continue;
		ll k=A+C*l,b=B*l;
		ans=max(ans,max(k*mn[l]+b,k*mx[l]+b));
	}
	return ans;
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				scanf("%d",&a[i][j]);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				scanf("%d",&p[i][j]),cnt[p[i][j]]++,sum[p[i][j]]+=a[i][j];
		for(int i=1;i<n;i++)
			for(int j=1;j<=n;j++)
				if(p[i][j]!=p[i+1][j])
					len[p[i][j]]++,len[p[i+1][j]]++;
		for(int i=1;i<=n;i++)
			for(int j=1;j<n;j++)
				if(p[i][j]!=p[i][j+1])
					len[p[i][j]]++,len[p[i][j+1]]++;
		Ar.init(n*n);
		for(int i=0;i<=n*n;i++) st[i].clear(),mn[i]=1e9,mx[i]=-1e9;
		for(int i=0;i<n*n;i++) Insert(i);
		int q;
		scanf("%d",&q);
		while(q--)
		{
			int x,y,z;
			ll A,B,C;
			scanf("%d%d%d%lld%lld%lld",&x,&y,&z,&A,&B,&C);
			vector<int> vec={p[x][y],z};
			for(int i=0;i<4;i++)
			{
				int tx=x+t[i][0],ty=y+t[i][1];
				if(tx>=1 && tx<=n && ty>=1 && ty<=n) vec.push_back(p[tx][ty]);
			}
			sort(vec.begin(),vec.end());
			vec.erase(unique(vec.begin(),vec.end()),vec.end());
			for(int i:vec) Erase(i);
			for(int i=0;i<4;i++)
			{
				int tx=x+t[i][0],ty=y+t[i][1];
				if(tx<1 || tx>n || ty<1 || ty>n) continue;
				if(p[tx][ty]!=p[x][y]) len[p[tx][ty]]--,len[p[x][y]]--;
			}
			sum[p[x][y]]-=a[x][y];cnt[p[x][y]]--;
			p[x][y]=z;
			sum[p[x][y]]+=a[x][y];cnt[p[x][y]]++;
			for(int i=0;i<4;i++)
			{
				int tx=x+t[i][0],ty=y+t[i][1];
				if(tx<1 || tx>n || ty<1 || ty>n) continue;
				if(p[tx][ty]!=p[x][y]) len[p[tx][ty]]++,len[p[x][y]]++;
			}
			for(int i:vec) Insert(i);
			printf("%lld%c",query(A,B,C)," \n"[!q]);
		}
		for(int i=0;i<=n*n;i++) sum[i]=cnt[i]=len[i]=0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 15588kb

input:

1
2
1 2
3 4
1 1
2 2
6
2 2 1 1 -1 0
1 2 2 1 2 -1
2 1 3 0 1 -1
1 2 3 2 0 0
1 1 3 1 1 1
2 2 3 -1 -1 -1

output:

5 -7 -2 10 20 -10

result:

ok 6 numbers

Test #2:

score: -100
Runtime Error

input:

5000
13
58 69 -65 -29 -100 3 26 -29 73 -29 -93 33 73
-77 -76 69 19 -77 -61 59 -15 85 81 -20 73 72
60 -46 -100 -59 -79 -74 -94 41 -24 -28 -5 36 70
-49 -59 -11 44 26 38 -73 92 -16 -37 -84 86 90
-19 9 -30 19 -24 88 46 -80 98 -75 14 -77 55
-41 22 -71 -75 78 -97 9 -99 95 30 41 -30 72
-31 -15 14 99 -98 -1...

output:


result: