답안 #244652

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
244652 2020-07-04T13:37:17 Z Yera Bigger segments (IZhO19_segments) C++17
100 / 100
483 ms 52344 KB
// In The Name Of God
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <bitset>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
 
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sagyndym_seni ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
 
const ll N = 5e5+5, p1 = 911382323, p2 = 972663749, INF = 1e18;
 
inline int read(){
	char c = getchar();
	bool minus = 0;
	while (c < '0' || '9' < c){
		if(c == '-'){ minus = 1;}
		c = getchar();
		if(c == '-'){ minus = 1;}
	}
	int res = 0;
	while ('0' <= c && c <= '9') {
		res = (res << 3) + (res << 1) + c - '0';
		c = getchar();
	}
	if(minus){ res *= -1;}
	return res;
}
 
ll a[N], p[N];
int n;
pair<ll, ll> dp[N];
// {num of blocks, sum in last block}

struct node{
	ll block, own_sum, pref_sum, y;
	pair<ll, ll> maxVal;
	node *left, *right;
	node(ll bl, ll own, ll pref){
		left = nullptr; right = nullptr;
		block = bl; own_sum = own; pref_sum = pref;
		maxVal = {bl, pref};
		y = (rand() << 16) | rand();
	}
};

node *root = nullptr;

inline pair<ll, ll> get_maxVal(node *v){
	return !v ? mp(-INF, -INF) : v->maxVal;
}

inline void update(node *v){
	if(!v){ return;}
	v->maxVal = max(mp(v->block, v->pref_sum), max(get_maxVal(v->left), get_maxVal(v->right)));
}

node *merge(node *t1, node *t2){
	if(!t1){ return t2;}
	if(!t2){ return t1;}
	if(t1->y < t2->y){
		t2->left = merge(t1, t2->left);
		update(t2); return t2;
	}else{
		t1->right = merge(t1->right, t2);
		update(t1); return t1;
	}
}
//[0...k]-[k + 1...n]
pair<node*, node*> split(node *v, ll k){
	if(!v){ return {v, v};}
	ll cur_node = v->pref_sum + v->own_sum;
	if(k < cur_node){
		pair<node*, node*> temp = split(v->left, k);
		v->left = temp.s; update(v);
		return {temp.f, v};
	}else{
		pair<node*, node*> temp = split(v->right, k);
		v->right = temp.f; update(v);
		return {v, temp.s};
	}
}

pair<ll, ll> max_val(ll key){
	pair<node*, node*> temp = split(root, key);
	pair<ll, ll> ans = get_maxVal(temp.f);
	root = merge(temp.f, temp.s);
	return ans;
}

void insert(ll bl, ll own, ll pref){
	pair<node*, node*> temp = split(root, pref + own);
	root = merge(temp.f, merge(new node(bl, own, pref), temp.s));
}

int main(){
	n = read();
	for(int i = 1; i <= n; i++){
		a[i] = read();
		p[i] = a[i] + p[i - 1];
	}
	insert(0, 0, 0);
	for(int r = 1; r <= n; r++){
		pair<ll, ll> temp = max_val(p[r]);
		insert(temp.f + 1, p[r] - temp.s, p[r]);
	}
	cout<<get_maxVal(root).f;
	return 0;
}
/* TIMUS: 292220YC*/
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 6 ms 640 KB Output is correct
32 Correct 6 ms 640 KB Output is correct
33 Correct 7 ms 640 KB Output is correct
34 Correct 6 ms 640 KB Output is correct
35 Correct 6 ms 640 KB Output is correct
36 Correct 7 ms 640 KB Output is correct
37 Correct 6 ms 512 KB Output is correct
38 Correct 6 ms 512 KB Output is correct
39 Correct 6 ms 640 KB Output is correct
40 Correct 7 ms 640 KB Output is correct
41 Correct 5 ms 640 KB Output is correct
42 Correct 5 ms 640 KB Output is correct
43 Correct 6 ms 640 KB Output is correct
44 Correct 6 ms 640 KB Output is correct
45 Correct 6 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 6 ms 640 KB Output is correct
32 Correct 6 ms 640 KB Output is correct
33 Correct 7 ms 640 KB Output is correct
34 Correct 6 ms 640 KB Output is correct
35 Correct 6 ms 640 KB Output is correct
36 Correct 7 ms 640 KB Output is correct
37 Correct 6 ms 512 KB Output is correct
38 Correct 6 ms 512 KB Output is correct
39 Correct 6 ms 640 KB Output is correct
40 Correct 7 ms 640 KB Output is correct
41 Correct 5 ms 640 KB Output is correct
42 Correct 5 ms 640 KB Output is correct
43 Correct 6 ms 640 KB Output is correct
44 Correct 6 ms 640 KB Output is correct
45 Correct 6 ms 640 KB Output is correct
46 Correct 55 ms 9752 KB Output is correct
47 Correct 82 ms 10232 KB Output is correct
48 Correct 78 ms 10616 KB Output is correct
49 Correct 67 ms 10616 KB Output is correct
50 Correct 50 ms 10744 KB Output is correct
51 Correct 83 ms 10744 KB Output is correct
52 Correct 45 ms 5500 KB Output is correct
53 Correct 27 ms 3456 KB Output is correct
54 Correct 73 ms 9976 KB Output is correct
55 Correct 83 ms 10720 KB Output is correct
56 Correct 43 ms 10232 KB Output is correct
57 Correct 57 ms 10104 KB Output is correct
58 Correct 56 ms 10104 KB Output is correct
59 Correct 47 ms 10240 KB Output is correct
60 Correct 67 ms 10232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 6 ms 640 KB Output is correct
32 Correct 6 ms 640 KB Output is correct
33 Correct 7 ms 640 KB Output is correct
34 Correct 6 ms 640 KB Output is correct
35 Correct 6 ms 640 KB Output is correct
36 Correct 7 ms 640 KB Output is correct
37 Correct 6 ms 512 KB Output is correct
38 Correct 6 ms 512 KB Output is correct
39 Correct 6 ms 640 KB Output is correct
40 Correct 7 ms 640 KB Output is correct
41 Correct 5 ms 640 KB Output is correct
42 Correct 5 ms 640 KB Output is correct
43 Correct 6 ms 640 KB Output is correct
44 Correct 6 ms 640 KB Output is correct
45 Correct 6 ms 640 KB Output is correct
46 Correct 55 ms 9752 KB Output is correct
47 Correct 82 ms 10232 KB Output is correct
48 Correct 78 ms 10616 KB Output is correct
49 Correct 67 ms 10616 KB Output is correct
50 Correct 50 ms 10744 KB Output is correct
51 Correct 83 ms 10744 KB Output is correct
52 Correct 45 ms 5500 KB Output is correct
53 Correct 27 ms 3456 KB Output is correct
54 Correct 73 ms 9976 KB Output is correct
55 Correct 83 ms 10720 KB Output is correct
56 Correct 43 ms 10232 KB Output is correct
57 Correct 57 ms 10104 KB Output is correct
58 Correct 56 ms 10104 KB Output is correct
59 Correct 47 ms 10240 KB Output is correct
60 Correct 67 ms 10232 KB Output is correct
61 Correct 300 ms 49272 KB Output is correct
62 Correct 337 ms 49360 KB Output is correct
63 Correct 483 ms 49656 KB Output is correct
64 Correct 411 ms 51832 KB Output is correct
65 Correct 274 ms 52216 KB Output is correct
66 Correct 431 ms 52216 KB Output is correct
67 Correct 357 ms 41848 KB Output is correct
68 Correct 175 ms 21112 KB Output is correct
69 Correct 444 ms 48376 KB Output is correct
70 Correct 453 ms 52344 KB Output is correct
71 Correct 245 ms 49272 KB Output is correct
72 Correct 271 ms 49660 KB Output is correct
73 Correct 284 ms 50296 KB Output is correct
74 Correct 254 ms 50536 KB Output is correct
75 Correct 302 ms 50552 KB Output is correct