답안 #200860

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
200860 2020-02-08T10:24:15 Z a1_N Bigger segments (IZhO19_segments) C++14
73 / 100
705 ms 262148 KB
#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const int N = (int)5e5 + 5;
const long long M = (long long)5e14 + 1;
    
int n,a[N];

long long pref[N];

pair<int,long long> dp[N];

struct tree{
	tree *left,*right;
	pair<int,long long> mx;
	tree(): left(NULL), right(NULL), mx(make_pair(0,0LL)) {}
}; 

tree *root = new tree();

pair<int,long long> get_mx(tree *t){
	return (t != NULL) ? (t->mx) : make_pair(0,0LL);
}

void upd(tree *t,long long tl,long long tr,long long pos,pair<int,long long> val){
	if(tl == tr){
		t->mx = val;
		return;
	}
	long long tm = (tl + tr) / 2;
	if(pos <= tm){
		if(t->left == NULL) t->left = new tree();
		upd(t->left,tl,tm,pos,val);		
	}
	else{
		if(t->right == NULL) t->right = new tree();
		upd(t->right,tm + 1,tr,pos,val);	
	}
	t->mx = max(get_mx(t->left),get_mx(t->right));
}

pair<int,long long> get(tree *t,long long tl,long long tr,long long l,long long r){
	if(l <= tl && tr <= r) return t->mx;
	if(l > tr || r < tl) return make_pair(0,0LL);
	long long tm = (tl + tr) / 2;
	pair<int,long long> mx1 = (t->left != NULL) ? get(t->left,tl,tm,l,r) : make_pair(0,0LL);
	pair<int,long long> mx2 = (t->right != NULL) ? get(t->right,tm + 1,tr,l,r) : make_pair(0,0LL);
	return max(mx1,mx2);
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		pref[i] = pref[i - 1] + a[i];
	}
	dp[1] = make_pair(1,a[1]);
	upd(root,1,M,pref[1] + dp[1].S,make_pair(dp[1].F,1));
	for(int i = 2; i <= n; i++){
		dp[i] = make_pair(dp[i - 1].F,dp[i - 1].S + a[i]);
		pair<int,long long> cur = get(root,1,M,1,pref[i]);
		if(cur.F > 0) dp[i] = make_pair(cur.F + 1,pref[i] - pref[cur.S]);
		/* pref[i] - pref[j] >= dp[j].S 
			pref[i] >= pref[j] + dp[j].S

			1) we update value in position pref[j] + dp[j].S
			2) when we want to find answer satisfying condition for this position we use segment tree
		*/
		upd(root,1,M,pref[i] + dp[i].S,make_pair(dp[i].F,i));	
	}
	cout << dp[n].F;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 4 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 7 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 4 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 7 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 760 KB Output is correct
17 Correct 6 ms 888 KB Output is correct
18 Correct 6 ms 1016 KB Output is correct
19 Correct 6 ms 1016 KB Output is correct
20 Correct 6 ms 1016 KB Output is correct
21 Correct 6 ms 1144 KB Output is correct
22 Correct 6 ms 1016 KB Output is correct
23 Correct 6 ms 760 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 6 ms 1016 KB Output is correct
26 Correct 5 ms 504 KB Output is correct
27 Correct 5 ms 504 KB Output is correct
28 Correct 5 ms 508 KB Output is correct
29 Correct 5 ms 632 KB Output is correct
30 Correct 6 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 4 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 7 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 760 KB Output is correct
17 Correct 6 ms 888 KB Output is correct
18 Correct 6 ms 1016 KB Output is correct
19 Correct 6 ms 1016 KB Output is correct
20 Correct 6 ms 1016 KB Output is correct
21 Correct 6 ms 1144 KB Output is correct
22 Correct 6 ms 1016 KB Output is correct
23 Correct 6 ms 760 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 6 ms 1016 KB Output is correct
26 Correct 5 ms 504 KB Output is correct
27 Correct 5 ms 504 KB Output is correct
28 Correct 5 ms 508 KB Output is correct
29 Correct 5 ms 632 KB Output is correct
30 Correct 6 ms 632 KB Output is correct
31 Correct 9 ms 2680 KB Output is correct
32 Correct 10 ms 3320 KB Output is correct
33 Correct 13 ms 3960 KB Output is correct
34 Correct 12 ms 3832 KB Output is correct
35 Correct 13 ms 4732 KB Output is correct
36 Correct 12 ms 4728 KB Output is correct
37 Correct 11 ms 3960 KB Output is correct
38 Correct 9 ms 3192 KB Output is correct
39 Correct 7 ms 1016 KB Output is correct
40 Correct 12 ms 4472 KB Output is correct
41 Correct 8 ms 1020 KB Output is correct
42 Correct 8 ms 1656 KB Output is correct
43 Correct 8 ms 888 KB Output is correct
44 Correct 9 ms 2168 KB Output is correct
45 Correct 10 ms 2168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 4 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 7 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 760 KB Output is correct
17 Correct 6 ms 888 KB Output is correct
18 Correct 6 ms 1016 KB Output is correct
19 Correct 6 ms 1016 KB Output is correct
20 Correct 6 ms 1016 KB Output is correct
21 Correct 6 ms 1144 KB Output is correct
22 Correct 6 ms 1016 KB Output is correct
23 Correct 6 ms 760 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 6 ms 1016 KB Output is correct
26 Correct 5 ms 504 KB Output is correct
27 Correct 5 ms 504 KB Output is correct
28 Correct 5 ms 508 KB Output is correct
29 Correct 5 ms 632 KB Output is correct
30 Correct 6 ms 632 KB Output is correct
31 Correct 9 ms 2680 KB Output is correct
32 Correct 10 ms 3320 KB Output is correct
33 Correct 13 ms 3960 KB Output is correct
34 Correct 12 ms 3832 KB Output is correct
35 Correct 13 ms 4732 KB Output is correct
36 Correct 12 ms 4728 KB Output is correct
37 Correct 11 ms 3960 KB Output is correct
38 Correct 9 ms 3192 KB Output is correct
39 Correct 7 ms 1016 KB Output is correct
40 Correct 12 ms 4472 KB Output is correct
41 Correct 8 ms 1020 KB Output is correct
42 Correct 8 ms 1656 KB Output is correct
43 Correct 8 ms 888 KB Output is correct
44 Correct 9 ms 2168 KB Output is correct
45 Correct 10 ms 2168 KB Output is correct
46 Correct 134 ms 54584 KB Output is correct
47 Correct 162 ms 61944 KB Output is correct
48 Correct 219 ms 123896 KB Output is correct
49 Correct 242 ms 133192 KB Output is correct
50 Correct 279 ms 147448 KB Output is correct
51 Correct 243 ms 144888 KB Output is correct
52 Correct 132 ms 72568 KB Output is correct
53 Correct 77 ms 43768 KB Output is correct
54 Correct 107 ms 20600 KB Output is correct
55 Correct 258 ms 140148 KB Output is correct
56 Correct 176 ms 64764 KB Output is correct
57 Correct 160 ms 53624 KB Output is correct
58 Correct 164 ms 59056 KB Output is correct
59 Correct 188 ms 83704 KB Output is correct
60 Correct 185 ms 83192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 4 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 7 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 760 KB Output is correct
17 Correct 6 ms 888 KB Output is correct
18 Correct 6 ms 1016 KB Output is correct
19 Correct 6 ms 1016 KB Output is correct
20 Correct 6 ms 1016 KB Output is correct
21 Correct 6 ms 1144 KB Output is correct
22 Correct 6 ms 1016 KB Output is correct
23 Correct 6 ms 760 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 6 ms 1016 KB Output is correct
26 Correct 5 ms 504 KB Output is correct
27 Correct 5 ms 504 KB Output is correct
28 Correct 5 ms 508 KB Output is correct
29 Correct 5 ms 632 KB Output is correct
30 Correct 6 ms 632 KB Output is correct
31 Correct 9 ms 2680 KB Output is correct
32 Correct 10 ms 3320 KB Output is correct
33 Correct 13 ms 3960 KB Output is correct
34 Correct 12 ms 3832 KB Output is correct
35 Correct 13 ms 4732 KB Output is correct
36 Correct 12 ms 4728 KB Output is correct
37 Correct 11 ms 3960 KB Output is correct
38 Correct 9 ms 3192 KB Output is correct
39 Correct 7 ms 1016 KB Output is correct
40 Correct 12 ms 4472 KB Output is correct
41 Correct 8 ms 1020 KB Output is correct
42 Correct 8 ms 1656 KB Output is correct
43 Correct 8 ms 888 KB Output is correct
44 Correct 9 ms 2168 KB Output is correct
45 Correct 10 ms 2168 KB Output is correct
46 Correct 134 ms 54584 KB Output is correct
47 Correct 162 ms 61944 KB Output is correct
48 Correct 219 ms 123896 KB Output is correct
49 Correct 242 ms 133192 KB Output is correct
50 Correct 279 ms 147448 KB Output is correct
51 Correct 243 ms 144888 KB Output is correct
52 Correct 132 ms 72568 KB Output is correct
53 Correct 77 ms 43768 KB Output is correct
54 Correct 107 ms 20600 KB Output is correct
55 Correct 258 ms 140148 KB Output is correct
56 Correct 176 ms 64764 KB Output is correct
57 Correct 160 ms 53624 KB Output is correct
58 Correct 164 ms 59056 KB Output is correct
59 Correct 188 ms 83704 KB Output is correct
60 Correct 185 ms 83192 KB Output is correct
61 Correct 680 ms 252324 KB Output is correct
62 Correct 653 ms 244380 KB Output is correct
63 Runtime error 705 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
64 Halted 0 ms 0 KB -