QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#858315 | #859. Civilizations | peimuda | AC ✓ | 1490ms | 18924kb | C++11 | 2.7kb | 2025-01-16 16:03:42 | 2025-01-16 16:03:53 |
Judging History
answer
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#define pr pair
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pll pr<ll,ll>
#define pii pr<int,int>
#define piii pr<int,pii>
using namespace std;
int a[502][502];
int bl[502][502];
int b[502][502];
int nx[250005];
int pf[250005];
int st;
int mx[502];
int mi[502];
multiset<int> hv[502];
int lg[250005];
int wt[250005];
int ar[250005];
int n;
void upd(int x)
{
if(hv[x].size())
{
mi[x]=*hv[x].begin();
mx[x]=*--hv[x].end();
}
}
void ch(int x,int dl,int dw=0,int dz=0)
{
// cout<<"Ch "<<x<<' '<<dl<<' '<<dw<<endl;
if(ar[x]) if(lg[x]<=n)
{
hv[lg[x]].erase(hv[lg[x]].find(wt[x]));
upd(lg[x]);
}
else
{
pf[nx[x]]=pf[x];
nx[pf[x]]=nx[x];
}
lg[x]+=dl;
wt[x]+=dw;
ar[x]+=dz;
if(ar[x]) if(lg[x]<=n)
{
hv[lg[x]].insert(wt[x]);
upd(lg[x]);
}
else
{
nx[x]=nx[st];
pf[nx[st]]=x;
nx[st]=x;
pf[x]=st;
}
}
void add(int i,int j,int x)
{
int sd=0;
if(i&&bl[i-1][j]!=x)
{
ch(bl[i-1][j],1);
sd++;
}
if(j&&bl[i][j-1]!=x)
{
ch(bl[i][j-1],1);
sd++;
}
if(i<n-1&&bl[i+1][j]!=x)
{
ch(bl[i+1][j],1);
sd++;
}
if(j<n-1&&bl[i][j+1]!=x)
{
ch(bl[i][j+1],1);
sd++;
}
ch(x,sd,a[i][j],1);
bl[i][j]=x;
}
void del(int i,int j)
{
int x=bl[i][j];
int sd=0;
if(i&&bl[i-1][j]!=x)
{
ch(bl[i-1][j],-1);
sd++;
}
if(j&&bl[i][j-1]!=x)
{
ch(bl[i][j-1],-1);
sd++;
}
if(i<n-1&&bl[i+1][j]!=x)
{
ch(bl[i+1][j],-1);
sd++;
}
if(j<n-1&&bl[i][j+1]!=x)
{
ch(bl[i][j+1],-1);
sd++;
}
ch(x,-sd,-a[i][j],-1);
}
void sl()
{
cin>>n;
st=n*n;
nx[st]=st;
pf[st]=st;
for(int i=0;i<n*n;i++) lg[i]=0,wt[i]=0,ar[i]=0;
for(int i=0;i<=n;i++) hv[i].clear();
for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a[i][j];
for(int i=0;i<n;i++) for(int j=0;j<n;j++) bl[i][j]=0;
for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>b[i][j];
for(int i=0;i<n;i++) for(int j=0;j<n;j++) if((i+j)%2==0) bl[i][j]=b[i][j],ch(b[i][j],0,a[i][j],1);
for(int i=0;i<n;i++) for(int j=0;j<n;j++) if((i+j)%2==1) add(i,j,b[i][j]);
int q;
cin>>q;
ll a,b,c;
int x,y,w;
while(q--)
{
cin>>x>>y>>w>>a>>b>>c;
x--;
y--;
del(x,y);
add(x,y,w);
ll ans=-1e18;
for(int i=0;i<=n;i++) if(hv[i].size())
{
ans=max(ans,a*mi[i]+b*i+c*i*mi[i]);
ans=max(ans,a*mx[i]+b*i+c*i*mx[i]);
}
// for(int i=0;i<=n;i++) if(hv[i].size())
// {
// cout<<"Hav "<<i<<" ["<<mi[i]<<','<<mx[i]<<"]\n";
// }
for(int i=nx[st];i!=st;i=nx[i])
{
// cout<<"Fd "<<i<<' '<<wt[i]<<' '<<lg[i]<<endl;
ans=max(ans,a*wt[i]+b*lg[i]+c*lg[i]*wt[i]);
}
cout<<ans<<'\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
int t;
cin>>t;
while(t--) sl();
return 0;
}/*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
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 9828kb
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: 0
Accepted
time: 244ms
memory: 9856kb
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...
result:
ok 174760 numbers
Test #3:
score: 0
Accepted
time: 1490ms
memory: 18924kb
input:
2 500 34 47 -56 82 -99 2 82 11 39 -97 60 -10 -100 -55 -35 10 -40 60 -28 -77 -6 50 -44 -89 54 -41 -10 -97 -99 84 32 70 -4 8 65 -63 -35 82 49 -54 14 86 8 -89 -80 53 39 88 0 -21 66 -64 -95 13 22 -71 55 100 -47 25 -99 -48 20 96 -51 -36 -69 -25 -75 12 -52 32 90 -15 2 0 80 -16 -6 -37 -4 86 77 1 99 -45 98 ...
output:
3803944835096 14643945957494 86844324396 14134081642428 23365258674231 14366240146112 16003972712236 -1312148321120 15052574522629 -1038900390882 -1844962440900 -1547048075848 -709863397534 -933621408796 18767756935516 8688763765136 -1159725720058 -975612623034 -516745232422 -657752127188 1274638936...
result:
ok 200000 numbers
Test #4:
score: 0
Accepted
time: 1015ms
memory: 11620kb
input:
2 500 -32 97 -40 -3 -56 71 59 -46 -65 -35 34 96 31 23 -13 -49 -36 -52 -26 -25 -47 -5 -100 -15 87 -31 -56 -67 62 -98 -91 21 73 -98 7 100 -37 93 67 100 -84 -15 -87 -37 24 41 97 -66 -62 72 76 87 -56 -5 77 51 -41 -37 -38 -23 -46 -9 46 -84 100 82 75 -69 -31 54 50 93 75 0 72 -21 31 73 95 -48 -49 28 72 87 ...
output:
-760483926664 -1131478165059 1078736685982509 -1579623130444 140300847961300 1308301091342367 356660126273 -1621398232494 -540496041768 -1250956947650 103312925756 1704298676270655 1570063066892932 -1595534318544 506841370045292 -302881470126 1137224507971245 1271191760711178 721644233650750 1874110...
result:
ok 200000 numbers
Test #5:
score: 0
Accepted
time: 1190ms
memory: 11464kb
input:
2 500 57 88 82 38 56 82 23 15 39 39 30 61 4 52 8 77 25 14 64 88 76 59 12 48 7 87 17 9 77 20 51 17 52 50 63 45 41 12 3 90 63 33 81 96 4 60 77 6 62 24 38 81 91 5 73 13 14 41 70 59 28 12 65 53 20 8 26 80 11 22 10 41 65 39 67 56 74 76 88 32 82 26 63 46 21 99 13 82 83 63 9 61 38 96 85 85 75 63 20 43 100 ...
output:
986726997524044 510026146409654 1381034794876828 -925451281152 492247257135690 -1514433152120 23920898919391 174527347582808 -2435233882210 -3120004262886 -1833248353986 1027045228680394 1303004696907326 -1618235558654 -1044009435046 1322885462977946 8628545303575 1238031457650414 -2973521363059 -20...
result:
ok 200000 numbers