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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1500;
int N;
ll A[MAXN+10][MAXN+10], B[MAXN+10][MAXN+10], ans;
struct BIT
{
ll tree[MAXN+10];
void update(int i, ll val) { for(; i<=N; i+=(i&-i)) tree[i]+=val; }
ll query(int i) { ll ret=0; for(; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
};
BIT bit[MAXN+10];
ll query(int y, int x, int val)
{
int i, j;
int s=x, e=x;
//printf("=========\n");
//for(i=1; i<=N; i++) { for(j=1; j<=N; j++) printf("%d ", bit[i].query(j)); printf("\n"); }
//printf("=========\n");
bit[y].update(x, val);
for(i=x+1; i<=N; i++)
{
ll t=max(bit[y].query(i-1), bit[y-1].query(i))+A[y][i];
if(t==bit[y].query(i)-val) break;
}
e=i;
bit[y].update(e, -val);
ans+=val*(e-s);
//printf("!%d %d\n", s, e);
for(i=y+1; i<=N; i++)
{
while(s<e)
{
ll t=max(bit[i].query(s-1), bit[i-1].query(s))+A[i][s];
if(t!=bit[i].query(s)) break;
s++;
}
bit[i].update(s, val);
while(e<=N)
{
ll t=max(bit[i].query(e-1), bit[i-1].query(e))+A[i][e];
if(t==bit[i].query(e)-val) break;
e++;
}
bit[i].update(e, -val);
ans+=val*(e-s);
//printf("!%d %d\n", s, e);
if(s==e) break;
}
A[y][x]+=val;
return ans;
}
int main()
{
int i, j;
scanf("%d", &N);
for(i=1; i<=N; i++) for(j=1; j<=N; j++) scanf("%lld", &A[i][j]);
for(i=1; i<=N; i++) for(j=1; j<=N; j++) B[i][j]=A[i][j]+max(B[i-1][j], B[i][j-1]);
for(i=1; i<=N; i++) for(j=1; j<=N; j++) ans+=B[i][j];
printf("%lld\n", ans);
for(i=1; i<=N; i++) for(j=1; j<=N; j++) bit[i].update(j, B[i][j]-B[i][j-1]);
for(i=1; i<=N; i++)
{
char t;
int y, x;
scanf("\n%c%d%d", &t, &y, &x);
if(t=='U') printf("%lld\n", query(y, x, 1));
else printf("%lld\n", query(y, x, -1));
}
}
Compilation message (stderr)
shell.cpp: In function 'll query(int, int, int)':
shell.cpp:23:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
shell.cpp: In function 'int main()':
shell.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
shell.cpp:72:50: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) for(j=1; j<=N; j++) scanf("%lld", &A[i][j]);
~~~~~^~~~~~~~~~~~~~~~~~
shell.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("\n%c%d%d", &t, &y, &x);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |