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;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
const int mxN = 1505;
int N, A[mxN][mxN], dp[mxN][mxN];
struct Fenwick {
int ft[mxN];
Fenwick() {
memset(ft,0,sizeof ft);
}
void update(int a, int b) {
for (; a <= N; a += a&-a) ft[a] += b;
}
void update(int a, int b, int c) {
update(a,c);
update(b+1,-c);
}
long long query(int a) {
long long r = 0;
for (; a; a -= a&-a) r += ft[a];
return r;
}
} ft[mxN];
long long S = 0;
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N;
FOR(i,1,N){
FOR(j,1,N){
cin >> A[i][j];
dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + A[i][j];
ft[i].update(j,j,dp[i][j]);
S += dp[i][j];
}
}
cout << S << '\n';
FOR(i,1,N){
char op;
int R, C, V;
cin >> op >> R >> C;
V = (op == 'U' ? 1 : -1);
A[R][C] += V;
int s = C, e = C+1;
while (R <= N) {
while (s < e) {
int ov = ft[R].query(s);
int nv = max(ft[R-1].query(s), ft[R].query(s-1)) + A[R][s];
//TRACE(R _ s _ ov _ nv);
if (nv != ov) break;
++s;
}
if (s < e) {
while (e <= N) {
int ov = ft[R].query(e);
int nv = max(ft[R-1].query(e), ft[R].query(e-1)+V) + A[R][e];
//TRACE(R _ e _ ov _ nv);
if (nv == ov) break;
++e;
}
ft[R].update(s,e-1,V);
//TRACE(R _ "UPDATE [)" _ s _ e);
S += (e-s) * V;
++R;
} else break;
}
cout << S << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |