tdog2026暑期集训Day2T4题解
pengcheng_ · · 题解
思路
考虑到国力和
由于
代码
时间复杂度
#include<bits/stdc++.h>
using namespace std;
const int dix[4]={1,0,-1,0};
const int diy[4]={0,1,0,-1};
long long n,q,v[505][505],p[505][505],w[250005],l[250005],d[250005],ag[1000005],Map[1000005],INF=4e18;
vector<int> G,Gx;
multiset<long long> Set[1000005];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>v[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>p[i][j];
d[p[i][j]]++;
w[p[i][j]]+=v[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int dx=0;dx<4;dx++){
int nowx=i+dix[dx];
int nowy=j+diy[dx];
if(0<=nowx&&nowx<n&&0<=nowy&&nowy<n&&p[i][j]!=p[nowx][nowy]){
l[p[i][j]]++;
}
}
}
}
for(int i=0;i<n*n;i++){
if(d[i])Set[l[i]].insert(w[i]),ag[l[i]]++;
}
for(int i=0;i<4*n*n;i++){
if(ag[i])G.push_back(i);
}
cin>>q;
while(q--){
long long x,y,px,A,B,C;
cin>>x>>y>>px>>A>>B>>C;
x--;
y--;
if(p[x][y]!=px){
d[px]++;
d[p[x][y]]--;
if(d[px]==1){
Set[l[px]].insert(w[px]);
ag[l[px]]++;
G.push_back(l[px]);
}
auto it=Set[l[px]].lower_bound(w[px]);
Set[l[px]].erase(it);
w[px]+=v[x][y];
Set[l[px]].insert(w[px]);
it=Set[l[p[x][y]]].lower_bound(w[p[x][y]]);
Set[l[p[x][y]]].erase(it);
w[p[x][y]]-=v[x][y];
Set[l[p[x][y]]].insert(w[p[x][y]]);
for(int i=0;i<4;i++){
int nowx=x+dix[i];
int nowy=y+diy[i];
if(0<=nowx&&nowx<n&&0<=nowy&&nowy<n&&p[x][y]!=p[nowx][nowy]){
it=Set[l[p[x][y]]].lower_bound(w[p[x][y]]);
Set[l[p[x][y]]].erase(it);
ag[l[p[x][y]]]--;
l[p[x][y]]--;
G.push_back(l[p[x][y]]);
ag[l[p[x][y]]]++;
Set[l[p[x][y]]].insert(w[p[x][y]]);
it=Set[l[p[nowx][nowy]]].lower_bound(w[p[nowx][nowy]]);
Set[l[p[nowx][nowy]]].erase(it);
ag[l[p[nowx][nowy]]]--;
l[p[nowx][nowy]]--;
G.push_back(l[p[nowx][nowy]]);
ag[l[p[nowx][nowy]]]++;
Set[l[p[nowx][nowy]]].insert(w[p[nowx][nowy]]);
}
if(0<=nowx&&nowx<n&&0<=nowy&&nowy<n&&px!=p[nowx][nowy]){
it=Set[l[px]].lower_bound(w[px]);
Set[l[px]].erase(it);
ag[l[px]]--;
l[px]++;
ag[l[px]]++;
G.push_back(l[px]);
Set[l[px]].insert(w[px]);
it=Set[l[p[nowx][nowy]]].lower_bound(w[p[nowx][nowy]]);
Set[l[p[nowx][nowy]]].erase(it);
ag[l[p[nowx][nowy]]]--;
l[p[nowx][nowy]]++;
G.push_back(l[p[nowx][nowy]]);
ag[l[p[nowx][nowy]]]++;
Set[l[p[nowx][nowy]]].insert(w[p[nowx][nowy]]);
}
}
if(d[p[x][y]]==0){
it=Set[l[p[x][y]]].lower_bound(w[p[x][y]]);
Set[l[p[x][y]]].erase(it);
ag[l[p[x][y]]]--;
}
p[x][y]=px;
}
long long mx=-INF;
int L=G.size();
for(int li=0;li<L;li++){
int i=G[li];
if(ag[i]&&!Map[i]){
Map[i]=1;
if(C*i+A>0){
auto it=Set[i].end();
it--;
mx=max(mx,(C*i+A)*(*it)+B*i);
}else if(C*i+A==0){
mx=max(mx,B*i);
}else{
auto it=Set[i].begin();
mx=max(mx,(C*i+A)*(*it)+B*i);
}
Gx.push_back(i);
}
}
G.clear();
L=Gx.size();
for(int i=0;i<L;i++)G.push_back(Gx[i]),Map[Gx[i]]=0;
Gx.clear();
cout<<mx<<' ';
}
return 0;
}
::::