Submission #257648

# Submission time Handle Problem Language Result Execution time Memory
257648 2020-08-04T13:59:29 Z BThero Bigger segments (IZhO19_segments) C++17
100 / 100
207 ms 25080 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;

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

void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = INF;
    return;
  }
  
  int mid = (tl + tr) >> 1;
  int c1 = (v << 1), c2 = (c1 | 1);
  
  build(c1, tl, mid);
  build(c2, mid + 1, tr);
  
  t[v] = min(t[c1], t[c2]);
}

void update(int v, int tl, int tr, int p, ll x) {
  if (tl == tr) {
    t[v] = x;
    return;
  }
  
  int mid = (tl + tr) >> 1;
  int c1 = (v << 1), c2 = (c1 | 1);
  
  if (p <= mid) {
    update(c1, tl, mid, p, x);
  }
  else {
    update(c2, mid + 1, tr, p, x);
  }
  
  t[v] = min(t[c1], t[c2]);
}

int query(int v, int tl, int tr, ll lim) {
  if (tl == tr) {
    return tl;
  }
  
  int mid = (tl + tr) >> 1;
  int c1 = (v << 1), c2 = (c1 | 1);
  
  if (t[c2] <= lim) {
    return query(c2, mid + 1, tr, lim);
  }
  else {
    return query(c1, tl, mid, lim);
  }
}

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);
  build(1, 0, n);
  update(1, 0, n, 0, 0);
    
  for (int i = 1; i <= n; ++i) {
    int j = query(1, 0, n, pref[i]);
    dp[i].fi = dp[j].fi + 1;
    dp[i].se = pref[i] - pref[j];
    update(1, 0, n, i, dp[i].se + 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:77:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
   ~~~~~^~~~~~~~~~
segments.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &arr[i]);
     ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 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 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 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 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 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 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 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 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 0 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 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 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 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 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 0 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 1 ms 512 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 1 ms 512 KB Output is correct
35 Correct 1 ms 512 KB Output is correct
36 Correct 3 ms 512 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
38 Correct 1 ms 512 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 1 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 1 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 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 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 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 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 0 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 1 ms 512 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 1 ms 512 KB Output is correct
35 Correct 1 ms 512 KB Output is correct
36 Correct 3 ms 512 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
38 Correct 1 ms 512 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 1 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 1 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 28 ms 5632 KB Output is correct
47 Correct 31 ms 5632 KB Output is correct
48 Correct 33 ms 5888 KB Output is correct
49 Correct 41 ms 6008 KB Output is correct
50 Correct 44 ms 6136 KB Output is correct
51 Correct 41 ms 6136 KB Output is correct
52 Correct 21 ms 3200 KB Output is correct
53 Correct 12 ms 1920 KB Output is correct
54 Correct 32 ms 5368 KB Output is correct
55 Correct 44 ms 6136 KB Output is correct
56 Correct 37 ms 5632 KB Output is correct
57 Correct 31 ms 5504 KB Output is correct
58 Correct 32 ms 5632 KB Output is correct
59 Correct 50 ms 5752 KB Output is correct
60 Correct 33 ms 5760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 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 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 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 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 0 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 1 ms 512 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 1 ms 512 KB Output is correct
35 Correct 1 ms 512 KB Output is correct
36 Correct 3 ms 512 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
38 Correct 1 ms 512 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 1 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 1 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 28 ms 5632 KB Output is correct
47 Correct 31 ms 5632 KB Output is correct
48 Correct 33 ms 5888 KB Output is correct
49 Correct 41 ms 6008 KB Output is correct
50 Correct 44 ms 6136 KB Output is correct
51 Correct 41 ms 6136 KB Output is correct
52 Correct 21 ms 3200 KB Output is correct
53 Correct 12 ms 1920 KB Output is correct
54 Correct 32 ms 5368 KB Output is correct
55 Correct 44 ms 6136 KB Output is correct
56 Correct 37 ms 5632 KB Output is correct
57 Correct 31 ms 5504 KB Output is correct
58 Correct 32 ms 5632 KB Output is correct
59 Correct 50 ms 5752 KB Output is correct
60 Correct 33 ms 5760 KB Output is correct
61 Correct 151 ms 24228 KB Output is correct
62 Correct 154 ms 24184 KB Output is correct
63 Correct 163 ms 24696 KB Output is correct
64 Correct 205 ms 24952 KB Output is correct
65 Correct 207 ms 24952 KB Output is correct
66 Correct 201 ms 25080 KB Output is correct
67 Correct 159 ms 22136 KB Output is correct
68 Correct 83 ms 11896 KB Output is correct
69 Correct 160 ms 23288 KB Output is correct
70 Correct 203 ms 24952 KB Output is correct
71 Correct 169 ms 24312 KB Output is correct
72 Correct 164 ms 24572 KB Output is correct
73 Correct 168 ms 24936 KB Output is correct
74 Correct 171 ms 24952 KB Output is correct
75 Correct 180 ms 24952 KB Output is correct