답안 #257544

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
257544 2020-08-04T11:52:05 Z BThero Bigger segments (IZhO19_segments) C++17
73 / 100
746 ms 262148 KB
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " = " << x << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = (int)5e5 + 5;
const ll INF = (ll)1e16;

struct Node {
  Node *c1, *c2;
  pair<int, ll> mx;
  
  Node() {
    c1 = c2 = NULL;
    mx = mp(0, 0);
  }
} *root;

pair<int, ll> dp[MAXN];
ll pref[MAXN];
int arr[MAXN];
int n;

void recalc(Node *&v) {
  v -> mx = mp(0, 0);
  
  if (v -> c1) {
    v -> mx = max(v -> mx, v -> c1 -> mx);
  }
  
  if (v -> c2) {
    v -> mx = max(v -> mx, v -> c2 -> mx);
  }
}

void update(Node *&v, ll tl, ll tr, ll p, pair<int, ll> x) {
  if (!v) {
    v = new Node();
  }
  
  if (tl == tr) {
    v -> mx = max(v -> mx, x);
    return;
  }
  
  ll mid = (tl + tr) >> 1;
  
  if (p <= mid) {
    update(v -> c1, tl, mid, p, x);
  }
  else {
    update(v -> c2, mid + 1, tr, p, x);
  }
  
  recalc(v);
}

pair<int, ll> segMax(Node *&v, ll tl, ll tr, ll l, ll r) {
  if (!v || l > r || tl > r || tr < l) {
    return mp(0, 0);
  }
  
  if (l <= tl && tr <= r) {
    return v -> mx;
  }
  
  ll mid = (tl + tr) >> 1;
  return max(segMax(v -> c1, tl, mid, l, r), segMax(v -> c2, mid + 1, tr, l, r));
}

void solve() {
  scanf("%d", &n);
  
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &arr[i]);
    pref[i] = pref[i - 1] + arr[i];
  }
  
  for (int i = 1; i <= n; ++i) {
    dp[i] = mp(0, INF);
  }
  
  dp[0] = mp(0, 0);
  update(root, 0, INF, 0, mp(0, 0));
  
  for (int i = 1; i <= n; ++i) {
    dp[i] = segMax(root, 0, INF, 0, pref[i]);
    dp[i].fi++;
    dp[i].se = pref[i] - dp[i].se;
    update(root, 0, INF, dp[i].se + pref[i], mp(dp[i].fi, pref[i]));
  }
  
  printf("%d\n", dp[n].fi);
}

int main() {
  int tt = 1;
  
  while (tt--) {
    solve();
  }

  return 0;
}

Compilation message

segments.cpp: In function 'void solve()':
segments.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
   ~~~~~^~~~~~~~~~
segments.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &arr[i]);
     ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 768 KB Output is correct
17 Correct 2 ms 896 KB Output is correct
18 Correct 3 ms 1024 KB Output is correct
19 Correct 2 ms 1024 KB Output is correct
20 Correct 3 ms 1024 KB Output is correct
21 Correct 2 ms 1024 KB Output is correct
22 Correct 2 ms 896 KB Output is correct
23 Correct 1 ms 768 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 2 ms 1024 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 2 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 768 KB Output is correct
17 Correct 2 ms 896 KB Output is correct
18 Correct 3 ms 1024 KB Output is correct
19 Correct 2 ms 1024 KB Output is correct
20 Correct 3 ms 1024 KB Output is correct
21 Correct 2 ms 1024 KB Output is correct
22 Correct 2 ms 896 KB Output is correct
23 Correct 1 ms 768 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 2 ms 1024 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 2 ms 640 KB Output is correct
31 Correct 5 ms 2688 KB Output is correct
32 Correct 6 ms 3328 KB Output is correct
33 Correct 8 ms 3968 KB Output is correct
34 Correct 7 ms 3840 KB Output is correct
35 Correct 9 ms 4736 KB Output is correct
36 Correct 9 ms 4608 KB Output is correct
37 Correct 8 ms 3992 KB Output is correct
38 Correct 10 ms 3200 KB Output is correct
39 Correct 4 ms 896 KB Output is correct
40 Correct 9 ms 4480 KB Output is correct
41 Correct 4 ms 896 KB Output is correct
42 Correct 6 ms 1536 KB Output is correct
43 Correct 4 ms 896 KB Output is correct
44 Correct 7 ms 2176 KB Output is correct
45 Correct 5 ms 2176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 768 KB Output is correct
17 Correct 2 ms 896 KB Output is correct
18 Correct 3 ms 1024 KB Output is correct
19 Correct 2 ms 1024 KB Output is correct
20 Correct 3 ms 1024 KB Output is correct
21 Correct 2 ms 1024 KB Output is correct
22 Correct 2 ms 896 KB Output is correct
23 Correct 1 ms 768 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 2 ms 1024 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 2 ms 640 KB Output is correct
31 Correct 5 ms 2688 KB Output is correct
32 Correct 6 ms 3328 KB Output is correct
33 Correct 8 ms 3968 KB Output is correct
34 Correct 7 ms 3840 KB Output is correct
35 Correct 9 ms 4736 KB Output is correct
36 Correct 9 ms 4608 KB Output is correct
37 Correct 8 ms 3992 KB Output is correct
38 Correct 10 ms 3200 KB Output is correct
39 Correct 4 ms 896 KB Output is correct
40 Correct 9 ms 4480 KB Output is correct
41 Correct 4 ms 896 KB Output is correct
42 Correct 6 ms 1536 KB Output is correct
43 Correct 4 ms 896 KB Output is correct
44 Correct 7 ms 2176 KB Output is correct
45 Correct 5 ms 2176 KB Output is correct
46 Correct 231 ms 54392 KB Output is correct
47 Correct 168 ms 61904 KB Output is correct
48 Correct 255 ms 123896 KB Output is correct
49 Correct 257 ms 133112 KB Output is correct
50 Correct 281 ms 147132 KB Output is correct
51 Correct 242 ms 145016 KB Output is correct
52 Correct 120 ms 72568 KB Output is correct
53 Correct 72 ms 43640 KB Output is correct
54 Correct 126 ms 20216 KB Output is correct
55 Correct 264 ms 139896 KB Output is correct
56 Correct 180 ms 64784 KB Output is correct
57 Correct 159 ms 53624 KB Output is correct
58 Correct 172 ms 59128 KB Output is correct
59 Correct 204 ms 83704 KB Output is correct
60 Correct 192 ms 83192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 768 KB Output is correct
17 Correct 2 ms 896 KB Output is correct
18 Correct 3 ms 1024 KB Output is correct
19 Correct 2 ms 1024 KB Output is correct
20 Correct 3 ms 1024 KB Output is correct
21 Correct 2 ms 1024 KB Output is correct
22 Correct 2 ms 896 KB Output is correct
23 Correct 1 ms 768 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 2 ms 1024 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 2 ms 640 KB Output is correct
31 Correct 5 ms 2688 KB Output is correct
32 Correct 6 ms 3328 KB Output is correct
33 Correct 8 ms 3968 KB Output is correct
34 Correct 7 ms 3840 KB Output is correct
35 Correct 9 ms 4736 KB Output is correct
36 Correct 9 ms 4608 KB Output is correct
37 Correct 8 ms 3992 KB Output is correct
38 Correct 10 ms 3200 KB Output is correct
39 Correct 4 ms 896 KB Output is correct
40 Correct 9 ms 4480 KB Output is correct
41 Correct 4 ms 896 KB Output is correct
42 Correct 6 ms 1536 KB Output is correct
43 Correct 4 ms 896 KB Output is correct
44 Correct 7 ms 2176 KB Output is correct
45 Correct 5 ms 2176 KB Output is correct
46 Correct 231 ms 54392 KB Output is correct
47 Correct 168 ms 61904 KB Output is correct
48 Correct 255 ms 123896 KB Output is correct
49 Correct 257 ms 133112 KB Output is correct
50 Correct 281 ms 147132 KB Output is correct
51 Correct 242 ms 145016 KB Output is correct
52 Correct 120 ms 72568 KB Output is correct
53 Correct 72 ms 43640 KB Output is correct
54 Correct 126 ms 20216 KB Output is correct
55 Correct 264 ms 139896 KB Output is correct
56 Correct 180 ms 64784 KB Output is correct
57 Correct 159 ms 53624 KB Output is correct
58 Correct 172 ms 59128 KB Output is correct
59 Correct 204 ms 83704 KB Output is correct
60 Correct 192 ms 83192 KB Output is correct
61 Correct 671 ms 254680 KB Output is correct
62 Correct 746 ms 244728 KB Output is correct
63 Runtime error 739 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
64 Halted 0 ms 0 KB -