QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#560474 | #859. Civilizations | rotcar07 | RE | 0ms | 10024kb | C++20 | 3.0kb | 2024-09-12 15:51:46 | 2024-09-12 15:51:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int w[505][505],p[505][505];
int n,q;
int sum[250005],l[250005],cnt[250005];
vector<int> sb[250005];
typedef long long ll;
inline void solve(){
cin>>n;
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++) cin>>w[i][j];
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++) cin>>p[i][j],p[i][j]++;
for(int i=1;i<=n*n;i++) sum[i]=l[i]=cnt[i]=0,sb[i].clear();
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
sum[p[i][j]]+=w[i][j];cnt[p[i][j]]++;
if(i<n)
if(p[i][j]!=p[i+1][j]) l[p[i][j]]++,l[p[i+1][j]]++;
if(j<n)
if(p[i][j]!=p[i][j+1]) l[p[i][j]]++,l[p[i][j+1]]++;
}cin>>q;
unordered_set<int> v;
for(int i=1;i<=n*n;i++)if(cnt[i])v.insert(l[i]),sb[l[i]].push_back(sum[i]);
for(auto x:v) sort(sb[x].begin(),sb[x].end());
while(q--){
int x,y,z;ll A,B,C;cin>>x>>y>>z>>A>>B>>C;z++;
auto del=[&](int z){
int a=l[z],b=sum[z];
if(!cnt[z]) return;
// cout<<"DEL "<<z<<' '<<a<<' '<<b<<'\n';
assert(!sb[a].empty());
assert(v.count(a));
sb[a].erase(lower_bound(sb[a].begin(),sb[a].end(),b));
if(sb[a].empty()) v.erase(a);
};
auto add=[&](int z){
int a=l[z],b=sum[z];
if(!cnt[z]) return;
// cout<<"ADD "<<z<<' '<<a<<' '<<b<<'\n';
sb[a].insert(upper_bound(sb[a].begin(),sb[a].end(),b),b);
if(sb[a].size()==1) v.insert(a);
};
if(z!=p[x][y]){
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int w=p[x][y];
for(int d=0;d<4;d++){
int xx=x+dx[d],yy=y+dy[d];
if(xx<1||yy<1||xx>n||yy>n) continue;
del(p[xx][yy]);
if(w!=p[xx][yy])l[p[xx][yy]]--;
if(z!=p[xx][yy])l[p[xx][yy]]++;
add(p[xx][yy]);
}
del(w),del(z);
for(int d=0;d<4;d++){
int xx=x+dx[d],yy=y+dy[d];
if(xx<1||yy<1||xx>n||yy>n) continue;
if(w!=p[xx][yy])l[w]--;
if(z!=p[xx][yy])l[z]++;
}cnt[w]--,cnt[z]++;
sum[w]-=::w[x][y];
add(w);
p[x][y]=z;
sum[z]+=::w[x][y];
add(z);
}
ll ans=-1e18;
// for(int i=1;i<=n*n;i++) cout<<i<<' '<<l[i]<<' '<<sum[i]<<' '<<cnt[i]<<'\n';
for(auto x:v){
ll d=A+C*x;
// cout<<x<<' '<<sb[x].size()<<'\n';
// for(auto z:sb[x]) cout<<z<<' ';cout<<'\n';
assert(!sb[x].empty());
if(d<0)ans=max(ans,B*x+d*sb[x][0]);
else ans=max(ans,B*x+d*sb[x].back());
}
cout<<ans<<'\n';
}
}
int main(){
// string name="A";freopen((name+".in").c_str(),"r",stdin);//freopen((name+".out").c_str(),"w",stdout);
std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t;cin>>t;
while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10024kb
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:
42103747013412 35680188991650 -6713336103808 -11059164282756 -1668545181184 -8186426524853 39582599038173 38802048647760 5920723833015 -21684163594486 -16595070672095 -11701923512925 -3008229643548 -18937722770187 56407046298711 71796328172997 -6315561061725 -24088994837750 -22482313580650 545583443...