This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define N 1505
using namespace std;
typedef long long ll;
int n,x,y,d,a[N][N],ddp[N][N],tree[N][N],s[N],e[N];
ll ans;
char c;
// [s,e] add v
void add(int x,int s,int e,int v)
{
for(;s<=n;s+=s&-s)
tree[x][s]+=v;
for(;e<=n;e+=e&-e)
tree[x][e]-=v;
}
int dp(int x,int y)
{
int res=ddp[x][y];
for(;y;y-=y&-y)
res+=tree[x][y];
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
ddp[i][j]=a[i][j]+max(ddp[i-1][j],ddp[i][j-1]);
ans+=ddp[i][j];
}
}
cout<<ans<<'\n';
for(int q=1;q<=n;q++){
cin>>c>>x>>y;
d=(c=='U'?1:-1);
for(int i=1;i<=n;i++)
s[i]=n+1,e[i]=0;
s[x]=e[x]=y;
for(int xx=x,yy=y;;){
if(yy<n&&max(dp(xx-1,yy+1),dp(xx,yy))+d==max(dp(xx-1,yy+1),dp(xx,yy)+d))
yy++;
else
xx++;
if(xx>n)
break;
e[xx]=yy;
}
for(int xx=x,yy=y;;){
if(xx<n&&max(dp(xx+1,yy-1),dp(xx,yy))+d==max(dp(xx+1,yy-1),dp(xx,yy)+d)){
xx++;
}
else
yy++;
if(yy>n||e[xx]<yy)
break;
s[xx]=min(s[xx],yy);
}
for(int i=x;i<=n;i++){
if(s[i]<=e[i]){
ans+=(e[i]-s[i]+1)*d;
add(i,s[i],e[i]+1,d);
}
}
cout<<ans<<'\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |