답안 #331606

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331606 2020-11-29T06:32:58 Z oolimry 조개 줍기 (KOI17_shell) C++14
100 / 100
1157 ms 44652 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int,int> ii;
typedef long long lint;

int n, Q;
struct segment{
	int tree[3005];
	int N;
	void init(int _N){
		N = _N;
		fill(tree,tree+2*N, 0);
	}
	void update(int l, int r, int v){
		for(l += N, r += N+1;l < r;l >>= 1, r >>= 1){
			if(l&1) tree[l++] += v;
			if(r&1) tree[--r] += v;
		}
	}
	int query(int i){
		int res = 0;
		for(i += N;i;i >>= 1) res += tree[i];
		return res;
	}
} seg[1505];

int arr[1505][1505];
int dp[1505][1505];

lint ans = 0;


void update(int R, int C, int v){
	int s = C, e = C;
	arr[R][C] += v;
	
	for(int r = R;r <= n;r++){
		while(s < e){
			int recalc = max(seg[r].query(s-1), seg[r-1].query(s)) + arr[r][s];
			int prev = seg[r].query(s);
			
			if(recalc != prev) break;
			s++;
		}
		seg[r].update(s, n, v);
		
		while(e <= n){
			int recalc = max(seg[r].query(e-1), seg[r-1].query(e)) + arr[r][e];
			int prev = seg[r].query(e) - v;
						
			if(recalc == prev) break;
			e++;
		}
		
		seg[r].update(e, n, -v);
		
		ans += v*(e-s);
		
		if(s == e) break;
	}
}

int main(){
	cin >> n; Q = n;
	
	for(int r = 1;r <= n;r++){
		for(int c = 1;c <= n;c++){
			cin >> arr[r][c];
			dp[r][c] = arr[r][c] + max(dp[r-1][c], dp[r][c-1]);
			ans += dp[r][c];
		}
	} 
		
	for(int r = 0;r <= n;r++) seg[r].init(n+1);
	for(int r = 1;r <= n;r++){
		for(int c = 1;c <= n;c++){
			seg[r].update(c,c,dp[r][c]);
		}
	}
	
	cout << ans << "\n";
	while(Q--){
		char cmd; int R, C; cin >> cmd >> R >> C;
		if(cmd == 'U') update(R, C, 1);
		if(cmd == 'D') update(R, C, -1);
		cout << ans << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1772 KB Output is correct
2 Correct 5 ms 1900 KB Output is correct
3 Correct 4 ms 1772 KB Output is correct
4 Correct 7 ms 1772 KB Output is correct
5 Correct 5 ms 1772 KB Output is correct
6 Correct 5 ms 1772 KB Output is correct
7 Correct 4 ms 1792 KB Output is correct
8 Correct 4 ms 1772 KB Output is correct
9 Correct 4 ms 1772 KB Output is correct
10 Correct 4 ms 1772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 422 ms 40064 KB Output is correct
2 Correct 416 ms 40300 KB Output is correct
3 Correct 434 ms 40320 KB Output is correct
4 Correct 400 ms 40268 KB Output is correct
5 Correct 419 ms 40428 KB Output is correct
6 Correct 413 ms 40172 KB Output is correct
7 Correct 470 ms 40172 KB Output is correct
8 Correct 435 ms 40288 KB Output is correct
9 Correct 421 ms 40300 KB Output is correct
10 Correct 439 ms 40172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1772 KB Output is correct
2 Correct 5 ms 1900 KB Output is correct
3 Correct 4 ms 1772 KB Output is correct
4 Correct 7 ms 1772 KB Output is correct
5 Correct 5 ms 1772 KB Output is correct
6 Correct 5 ms 1772 KB Output is correct
7 Correct 4 ms 1792 KB Output is correct
8 Correct 4 ms 1772 KB Output is correct
9 Correct 4 ms 1772 KB Output is correct
10 Correct 422 ms 40064 KB Output is correct
11 Correct 416 ms 40300 KB Output is correct
12 Correct 434 ms 40320 KB Output is correct
13 Correct 400 ms 40268 KB Output is correct
14 Correct 419 ms 40428 KB Output is correct
15 Correct 413 ms 40172 KB Output is correct
16 Correct 470 ms 40172 KB Output is correct
17 Correct 435 ms 40288 KB Output is correct
18 Correct 421 ms 40300 KB Output is correct
19 Correct 439 ms 40172 KB Output is correct
20 Correct 575 ms 44440 KB Output is correct
21 Correct 587 ms 44396 KB Output is correct
22 Correct 587 ms 44268 KB Output is correct
23 Correct 1135 ms 44268 KB Output is correct
24 Correct 1072 ms 44360 KB Output is correct
25 Correct 1121 ms 44268 KB Output is correct
26 Correct 412 ms 40256 KB Output is correct
27 Correct 442 ms 40160 KB Output is correct
28 Correct 569 ms 44524 KB Output is correct
29 Correct 558 ms 44652 KB Output is correct
30 Correct 1063 ms 44564 KB Output is correct
31 Correct 1157 ms 44524 KB Output is correct
32 Correct 451 ms 40172 KB Output is correct
33 Correct 401 ms 40044 KB Output is correct
34 Correct 4 ms 1772 KB Output is correct