답안 #257543

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
257543 2020-08-04T11:51:29 Z BThero Bigger segments (IZhO19_segments) C++17
73 / 100
289 ms 148216 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)1e5 + 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 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 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 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 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 0 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 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 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 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 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 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 2 ms 896 KB Output is correct
18 Correct 2 ms 1024 KB Output is correct
19 Correct 2 ms 1024 KB Output is correct
20 Correct 2 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 1 ms 384 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 1 ms 512 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 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 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 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 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 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 2 ms 896 KB Output is correct
18 Correct 2 ms 1024 KB Output is correct
19 Correct 2 ms 1024 KB Output is correct
20 Correct 2 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 1 ms 384 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 1 ms 512 KB Output is correct
31 Correct 6 ms 2688 KB Output is correct
32 Correct 8 ms 3328 KB Output is correct
33 Correct 8 ms 3968 KB Output is correct
34 Correct 8 ms 3840 KB Output is correct
35 Correct 9 ms 4736 KB Output is correct
36 Correct 8 ms 4608 KB Output is correct
37 Correct 9 ms 3968 KB Output is correct
38 Correct 6 ms 3200 KB Output is correct
39 Correct 5 ms 896 KB Output is correct
40 Correct 9 ms 4480 KB Output is correct
41 Correct 5 ms 896 KB Output is correct
42 Correct 6 ms 1536 KB Output is correct
43 Correct 5 ms 896 KB Output is correct
44 Correct 5 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 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 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 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 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 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 2 ms 896 KB Output is correct
18 Correct 2 ms 1024 KB Output is correct
19 Correct 2 ms 1024 KB Output is correct
20 Correct 2 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 1 ms 384 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 1 ms 512 KB Output is correct
31 Correct 6 ms 2688 KB Output is correct
32 Correct 8 ms 3328 KB Output is correct
33 Correct 8 ms 3968 KB Output is correct
34 Correct 8 ms 3840 KB Output is correct
35 Correct 9 ms 4736 KB Output is correct
36 Correct 8 ms 4608 KB Output is correct
37 Correct 9 ms 3968 KB Output is correct
38 Correct 6 ms 3200 KB Output is correct
39 Correct 5 ms 896 KB Output is correct
40 Correct 9 ms 4480 KB Output is correct
41 Correct 5 ms 896 KB Output is correct
42 Correct 6 ms 1536 KB Output is correct
43 Correct 5 ms 896 KB Output is correct
44 Correct 5 ms 2176 KB Output is correct
45 Correct 5 ms 2176 KB Output is correct
46 Correct 146 ms 54448 KB Output is correct
47 Correct 178 ms 62328 KB Output is correct
48 Correct 241 ms 124664 KB Output is correct
49 Correct 266 ms 134008 KB Output is correct
50 Correct 289 ms 148216 KB Output is correct
51 Correct 246 ms 145916 KB Output is correct
52 Correct 121 ms 73208 KB Output is correct
53 Correct 76 ms 44028 KB Output is correct
54 Correct 119 ms 20564 KB Output is correct
55 Correct 265 ms 140920 KB Output is correct
56 Correct 183 ms 65144 KB Output is correct
57 Correct 165 ms 54136 KB Output is correct
58 Correct 177 ms 59512 KB Output is correct
59 Correct 203 ms 84344 KB Output is correct
60 Correct 208 ms 83792 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 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 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 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 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 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 2 ms 896 KB Output is correct
18 Correct 2 ms 1024 KB Output is correct
19 Correct 2 ms 1024 KB Output is correct
20 Correct 2 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 1 ms 384 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 1 ms 512 KB Output is correct
31 Correct 6 ms 2688 KB Output is correct
32 Correct 8 ms 3328 KB Output is correct
33 Correct 8 ms 3968 KB Output is correct
34 Correct 8 ms 3840 KB Output is correct
35 Correct 9 ms 4736 KB Output is correct
36 Correct 8 ms 4608 KB Output is correct
37 Correct 9 ms 3968 KB Output is correct
38 Correct 6 ms 3200 KB Output is correct
39 Correct 5 ms 896 KB Output is correct
40 Correct 9 ms 4480 KB Output is correct
41 Correct 5 ms 896 KB Output is correct
42 Correct 6 ms 1536 KB Output is correct
43 Correct 5 ms 896 KB Output is correct
44 Correct 5 ms 2176 KB Output is correct
45 Correct 5 ms 2176 KB Output is correct
46 Correct 146 ms 54448 KB Output is correct
47 Correct 178 ms 62328 KB Output is correct
48 Correct 241 ms 124664 KB Output is correct
49 Correct 266 ms 134008 KB Output is correct
50 Correct 289 ms 148216 KB Output is correct
51 Correct 246 ms 145916 KB Output is correct
52 Correct 121 ms 73208 KB Output is correct
53 Correct 76 ms 44028 KB Output is correct
54 Correct 119 ms 20564 KB Output is correct
55 Correct 265 ms 140920 KB Output is correct
56 Correct 183 ms 65144 KB Output is correct
57 Correct 165 ms 54136 KB Output is correct
58 Correct 177 ms 59512 KB Output is correct
59 Correct 203 ms 84344 KB Output is correct
60 Correct 208 ms 83792 KB Output is correct
61 Execution timed out 32 ms 4480 KB Time limit exceeded (wall clock)
62 Halted 0 ms 0 KB -