Submission #578208

# Submission time Handle Problem Language Result Execution time Memory
578208 2022-06-16T08:20:09 Z slime Bigger segments (IZhO19_segments) C++14
100 / 100
265 ms 99156 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
#ifdef ONLINE_JUDGE
const int MAXN = 3e5 + 10;
#endif
#ifndef ONLINE_JUDGE
const int MAXN = 1029;
#endif
const int MOD = 998244353;
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { int x; cin >> x; return (ll)x; }
long long bm(long long b, long long p) {
  if(p==0) return 1 % MOD;
  long long r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
long long inv(long long b) { 
  return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) { 
  long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
  ans *= inv(f[n-r]); ans %= MOD; return ans;
}
long long fib[MAXN], lucas[MAXN];
void precomp() { 
  for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD); 
  lucas[0] = 2;
  lucas[1] = 1;
  for(int i=2; i<MAXN; i++) lucas[i] = (lucas[i-2] + lucas[i-1]) % MOD;
  fib[0] = 0;
  fib[1] = 1;
  for(int i=2; i<MAXN; i++) fib[i] = (fib[i-2] + fib[i-1]) % MOD;
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void gay(int i) { cout << "Case #" << i << ": "; }
int csb(int l, int r, int k) { // count number of [l, r] such that i & 2^k > 0
  if(l > r) return 0;
  if(l == 0) {
    int s = r / (1ll << (k+1)); // number of complete cycles
    int t = r % (1ll << (k+1));
    int ans = s * (1ll << k);
    ans += (t >= (1ll << k) ? t - (1ll << k) + 1 : 0);
    return ans;
  }
  else return csb(0, r, k) - csb(0, l - 1, k);
}
int lis(vector<int> a) {
  int n = a.size();
  int bucket[n+1];
  for(int i=1; i<=n; i++) bucket[i] = 1e18;
  int ans = 1;
  for(int x: a) {
    auto it = lower_bound(bucket + 1, bucket +n +1, x);
    int d = distance(bucket, it);
    ans = max(ans, d);
    bucket[d] = min(bucket[d], x);
  }
  return ans;
}
// WARNING: This template lacks testing for query 5, 6, 7
// WARNING: This template is over 700 lines long
// NOTE: 0 is allowed as index in this segtree version
struct segtree_basic {
  //Segtree that supports all basic operations
  //Updates: Range :=, Range +=
  //Basic queries: Range min, Range max, Range sum
  //Advanced queries: Range first >= v, Range first <= v, Range last >= v, Range last <= v
  //https://codeforces.com/edu/course/2/lesson/5/2/practice Problem A, D, E, F
  //https://codeforces.com/edu/course/2/lesson/5/3/practice Problem C
  //https://codeforces.com/edu/course/2/lesson/5/4/practice Problem A
  //#define int long long please
  struct node {
    int upd = 0;
    char type = '?'; 
    int mi = 0, ma = 0, sum = 0;
  };
  int stok;
  vector<node> st;
  void u1(int l, int r, int constl, int constr, int idx, int val) { // Range :=
    if(l <= constl && constr <= r) {
      st[idx].upd = val;
      st[idx].type = ':';
      st[idx].mi = val;
      st[idx].ma = val;
      st[idx].sum = val * (constr - constl + 1);
      return;
    }
    int mid = (constl + constr) >> 1;
    if(st[idx].type != '?') {
      if(st[idx].type == ':') {
        st[2*idx+1].upd = st[idx].upd;
        st[2*idx+1].type = ':';
        st[2*idx+1].mi = st[idx].upd;
        st[2*idx+1].ma = st[idx].upd;
        st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd = st[idx].upd;
        st[2*idx+2].type = ':';
        st[2*idx+2].mi = st[idx].upd;
        st[2*idx+2].ma = st[idx].upd;
        st[2*idx+2].sum = st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
      else {
        st[2*idx+1].upd += st[idx].upd;
        if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
        st[2*idx+1].mi += st[idx].upd;
        st[2*idx+1].ma += st[idx].upd;
        st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd += st[idx].upd;
        if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
        st[2*idx+2].mi += st[idx].upd;
        st[2*idx+2].ma += st[idx].upd;
        st[2*idx+2].sum += st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
    }
    if(mid<l || r<constl) u1(l, r, mid + 1, constr, 2*idx+2, val);
    else if(constr<l || r<mid+1) u1(l, r, constl, mid, 2*idx+1, val);
    else {
      u1(l, r, constl, mid, 2*idx+1, val); u1(l, r, mid + 1, constr, 2*idx+2, val);
    }
    st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
    st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
    st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
  }
  void u2(int l, int r, int constl, int constr, int idx, int val) { // Range +=
    if(l <= constl && constr <= r) {
      st[idx].upd += val;
      if(st[idx].type == '?') st[idx].type = '+';
      st[idx].mi += val;
      st[idx].ma += val;
      st[idx].sum += val * (constr - constl + 1);
      return;
    }
    int mid = (constl + constr) >> 1;
    if(st[idx].type != '?') {
      if(st[idx].type == ':') {
        st[2*idx+1].upd = st[idx].upd;
        st[2*idx+1].type = ':';
        st[2*idx+1].mi = st[idx].upd;
        st[2*idx+1].ma = st[idx].upd;
        st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd = st[idx].upd;
        st[2*idx+2].type = ':';
        st[2*idx+2].mi = st[idx].upd;
        st[2*idx+2].ma = st[idx].upd;
        st[2*idx+2].sum = st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
      else {
        st[2*idx+1].upd += st[idx].upd;
        if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
        st[2*idx+1].mi += st[idx].upd;
        st[2*idx+1].ma += st[idx].upd;
        st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd += st[idx].upd;
        if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
        st[2*idx+2].mi += st[idx].upd;
        st[2*idx+2].ma += st[idx].upd;
        st[2*idx+2].sum += st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
    }
    if(mid<l || r<constl) u2(l, r, mid + 1, constr, 2*idx+2, val);
    else if(constr<l || r<mid+1) u2(l, r, constl, mid, 2*idx+1, val);
    else {
      u2(l, r, constl, mid, 2*idx+1, val); u2(l, r, mid + 1, constr, 2*idx+2, val);
    }
    st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
    st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
    st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
  }
  int qu1(int l, int r, int constl, int constr, int idx) { // Range min
    if(l<=constl && constr<=r) return st[idx].mi;
    int mid = (constl + constr) >> 1;
    if(st[idx].type != '?') {
      if(st[idx].type == ':') {
        st[2*idx+1].upd = st[idx].upd;
        st[2*idx+1].type = ':';
        st[2*idx+1].mi = st[idx].upd;
        st[2*idx+1].ma = st[idx].upd;
        st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd = st[idx].upd;
        st[2*idx+2].type = ':';
        st[2*idx+2].mi = st[idx].upd;
        st[2*idx+2].ma = st[idx].upd;
        st[2*idx+2].sum = st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
      else {
        st[2*idx+1].upd += st[idx].upd;
        if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
        st[2*idx+1].mi += st[idx].upd;
        st[2*idx+1].ma += st[idx].upd;
        st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd += st[idx].upd;
        if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
        st[2*idx+2].mi += st[idx].upd;
        st[2*idx+2].ma += st[idx].upd;
        st[2*idx+2].sum += st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
    }
    if(mid<l || r<constl) return qu1(l, r, mid+1, constr, 2*idx+2);
    else if(constr<l || r<mid+1) return qu1(l, r, constl, mid, 2*idx+1);
    return min(qu1(l, r, constl, mid, 2*idx+1), qu1(l, r, mid+1, constr, 2*idx+2));
  }
  int qu2(int l, int r, int constl, int constr, int idx) { // Range max
    if(l<=constl && constr<=r) return st[idx].ma;
    int mid = (constl + constr) >> 1;
    if(st[idx].type != '?') {
      if(st[idx].type == ':') {
        st[2*idx+1].upd = st[idx].upd;
        st[2*idx+1].type = ':';
        st[2*idx+1].mi = st[idx].upd;
        st[2*idx+1].ma = st[idx].upd;
        st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd = st[idx].upd;
        st[2*idx+2].type = ':';
        st[2*idx+2].mi = st[idx].upd;
        st[2*idx+2].ma = st[idx].upd;
        st[2*idx+2].sum = st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
      else {
        st[2*idx+1].upd += st[idx].upd;
        if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
        st[2*idx+1].mi += st[idx].upd;
        st[2*idx+1].ma += st[idx].upd;
        st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd += st[idx].upd;
        if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
        st[2*idx+2].mi += st[idx].upd;
        st[2*idx+2].ma += st[idx].upd;
        st[2*idx+2].sum += st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
    }
    if(mid<l || r<constl) return qu2(l, r, mid+1, constr, 2*idx+2);
    else if(constr<l || r<mid+1) return qu2(l, r, constl, mid, 2*idx+1);
    return max(qu2(l, r, constl, mid, 2*idx+1), qu2(l, r, mid+1, constr, 2*idx+2));
  }
  int qu3(int l, int r, int constl, int constr, int idx) { // Range sum
    if(l<=constl && constr<=r) return st[idx].sum;
    int mid = (constl + constr) >> 1;
    if(st[idx].type != '?') {
      if(st[idx].type == ':') {
        st[2*idx+1].upd = st[idx].upd;
        st[2*idx+1].type = ':';
        st[2*idx+1].mi = st[idx].upd;
        st[2*idx+1].ma = st[idx].upd;
        st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd = st[idx].upd;
        st[2*idx+2].type = ':';
        st[2*idx+2].mi = st[idx].upd;
        st[2*idx+2].ma = st[idx].upd;
        st[2*idx+2].sum = st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
      else {
        st[2*idx+1].upd += st[idx].upd;
        if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
        st[2*idx+1].mi += st[idx].upd;
        st[2*idx+1].ma += st[idx].upd;
        st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd += st[idx].upd;
        if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
        st[2*idx+2].mi += st[idx].upd;
        st[2*idx+2].ma += st[idx].upd;
        st[2*idx+2].sum += st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
    }
    if(mid<l || r<constl) return qu3(l, r, mid + 1, constr, 2*idx + 2);
    else if(constr < l || r <mid+ 1) return qu3(l, r, constl, mid, 2*idx + 1);
    else {
      return qu3(l, r, constl, mid, 2*idx + 1) + qu3(l, r, mid + 1, constr, 2*idx + 2);
    }
  }
  int qu4(int l, int r, int constl, int constr, int idx, int val) { // First element ≥ v
    if(l<=constl && constr<=r) {
      if(st[idx].ma < val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        if(st[idx].type != '?') {
          if(st[idx].type == ':') {
            st[2*idx+1].upd = st[idx].upd;
            st[2*idx+1].type = ':';
            st[2*idx+1].mi = st[idx].upd;
            st[2*idx+1].ma = st[idx].upd;
            st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

            st[2*idx+2].upd = st[idx].upd;
            st[2*idx+2].type = ':';
            st[2*idx+2].mi = st[idx].upd;
            st[2*idx+2].ma = st[idx].upd;
            st[2*idx+2].sum = st[idx].upd * (constr - mid);

            st[idx].upd = 0;
            st[idx].type = '?';
            st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
            st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
            st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
          }
          else {
            st[2*idx+1].upd += st[idx].upd;
            if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
            st[2*idx+1].mi += st[idx].upd;
            st[2*idx+1].ma += st[idx].upd;
            st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

            st[2*idx+2].upd += st[idx].upd;
            if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
            st[2*idx+2].mi += st[idx].upd;
            st[2*idx+2].ma += st[idx].upd;
            st[2*idx+2].sum += st[idx].upd * (constr - mid);

            st[idx].upd = 0;
            st[idx].type = '?';
            st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
            st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
            st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
          }
        }
        if(st[2*idx+1].ma >= val) idx = 2*idx + 1, constr = mid;
        else idx = 2*idx+2, constl = mid + 1;
      }
      return constl;
    }
    int mid = (constl+constr) >> 1;
    if(st[idx].type != '?') {
      if(st[idx].type == ':') {
        st[2*idx+1].upd = st[idx].upd;
        st[2*idx+1].type = ':';
        st[2*idx+1].mi = st[idx].upd;
        st[2*idx+1].ma = st[idx].upd;
        st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd = st[idx].upd;
        st[2*idx+2].type = ':';
        st[2*idx+2].mi = st[idx].upd;
        st[2*idx+2].ma = st[idx].upd;
        st[2*idx+2].sum = st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
      else {
        st[2*idx+1].upd += st[idx].upd;
        if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
        st[2*idx+1].mi += st[idx].upd;
        st[2*idx+1].ma += st[idx].upd;
        st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd += st[idx].upd;
        if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
        st[2*idx+2].mi += st[idx].upd;
        st[2*idx+2].ma += st[idx].upd;
        st[2*idx+2].sum += st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
    }
    if(mid<l || r<constl) return qu4(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr<l || r<mid+1) return qu4(l, r, constl, mid, 2*idx+1, val);
    else {
      int oho = qu4(l, r, constl, mid, 2*idx+1, val);
      if(oho != -1) return oho;
      return qu4(l, r, mid+1, constr, 2*idx+2, val);
    }
  }
  int qu5(int l, int r, int constl, int constr, int idx, int val) { // First element ≤ v
    if(l<=constl && constr<=r) {
      if(st[idx].mi > val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        if(st[idx].type != '?') {
          if(st[idx].type == ':') {
            st[2*idx+1].upd = st[idx].upd;
            st[2*idx+1].type = ':';
            st[2*idx+1].mi = st[idx].upd;
            st[2*idx+1].ma = st[idx].upd;
            st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

            st[2*idx+2].upd = st[idx].upd;
            st[2*idx+2].type = ':';
            st[2*idx+2].mi = st[idx].upd;
            st[2*idx+2].ma = st[idx].upd;
            st[2*idx+2].sum = st[idx].upd * (constr - mid);

            st[idx].upd = 0;
            st[idx].type = '?';
            st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
            st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
            st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
          }
          else {
            st[2*idx+1].upd += st[idx].upd;
            if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
            st[2*idx+1].mi += st[idx].upd;
            st[2*idx+1].ma += st[idx].upd;
            st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

            st[2*idx+2].upd += st[idx].upd;
            if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
            st[2*idx+2].mi += st[idx].upd;
            st[2*idx+2].ma += st[idx].upd;
            st[2*idx+2].sum += st[idx].upd * (constr - mid);

            st[idx].upd = 0;
            st[idx].type = '?';
            st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
            st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
            st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
          }
        }
        if(st[2*idx+1].mi <= val) idx = 2*idx + 1, constr = mid;
        else idx = 2*idx+2, constl = mid + 1;
      }
      return constl;
    }
    int mid = (constl+constr) >> 1;
    if(st[idx].type != '?') {
      if(st[idx].type == ':') {
        st[2*idx+1].upd = st[idx].upd;
        st[2*idx+1].type = ':';
        st[2*idx+1].mi = st[idx].upd;
        st[2*idx+1].ma = st[idx].upd;
        st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd = st[idx].upd;
        st[2*idx+2].type = ':';
        st[2*idx+2].mi = st[idx].upd;
        st[2*idx+2].ma = st[idx].upd;
        st[2*idx+2].sum = st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
      else {
        st[2*idx+1].upd += st[idx].upd;
        if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
        st[2*idx+1].mi += st[idx].upd;
        st[2*idx+1].ma += st[idx].upd;
        st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd += st[idx].upd;
        if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
        st[2*idx+2].mi += st[idx].upd;
        st[2*idx+2].ma += st[idx].upd;
        st[2*idx+2].sum += st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
    }
    if(mid<l || r<constl) return qu5(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr<l || r<mid+1) return qu5(l, r, constl, mid, 2*idx+1, val);
    else {
      int oho = qu5(l, r, constl, mid, 2*idx+1, val);
      if(oho != -1) return oho;
      return qu5(l, r, mid+1, constr, 2*idx+2, val);
    }
  }
  int qu6(int l, int r, int constl, int constr, int idx, int val) { // Last element ≥ v
    if(l<=constl && constr<=r) {
      if(st[idx].ma < val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        if(st[idx].type != '?') {
          if(st[idx].type == ':') {
            st[2*idx+1].upd = st[idx].upd;
            st[2*idx+1].type = ':';
            st[2*idx+1].mi = st[idx].upd;
            st[2*idx+1].ma = st[idx].upd;
            st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

            st[2*idx+2].upd = st[idx].upd;
            st[2*idx+2].type = ':';
            st[2*idx+2].mi = st[idx].upd;
            st[2*idx+2].ma = st[idx].upd;
            st[2*idx+2].sum = st[idx].upd * (constr - mid);

            st[idx].upd = 0;
            st[idx].type = '?';
            st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
            st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
            st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
          }
          else {
            st[2*idx+1].upd += st[idx].upd;
            if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
            st[2*idx+1].mi += st[idx].upd;
            st[2*idx+1].ma += st[idx].upd;
            st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

            st[2*idx+2].upd += st[idx].upd;
            if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
            st[2*idx+2].mi += st[idx].upd;
            st[2*idx+2].ma += st[idx].upd;
            st[2*idx+2].sum += st[idx].upd * (constr - mid);

            st[idx].upd = 0;
            st[idx].type = '?';
            st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
            st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
            st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
          }
        }
        if(st[2*idx+2].ma >= val) idx = 2*idx + 2, constl = mid + 1;
        else idx = 2*idx + 1, constr = mid;
      }
      return constl;
    }
    int mid = (constl+constr) >> 1;
    if(st[idx].type != '?') {
      if(st[idx].type == ':') {
        st[2*idx+1].upd = st[idx].upd;
        st[2*idx+1].type = ':';
        st[2*idx+1].mi = st[idx].upd;
        st[2*idx+1].ma = st[idx].upd;
        st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd = st[idx].upd;
        st[2*idx+2].type = ':';
        st[2*idx+2].mi = st[idx].upd;
        st[2*idx+2].ma = st[idx].upd;
        st[2*idx+2].sum = st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
      else {
        st[2*idx+1].upd += st[idx].upd;
        if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
        st[2*idx+1].mi += st[idx].upd;
        st[2*idx+1].ma += st[idx].upd;
        st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd += st[idx].upd;
        if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
        st[2*idx+2].mi += st[idx].upd;
        st[2*idx+2].ma += st[idx].upd;
        st[2*idx+2].sum += st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
    }
    if(mid<l || r<constl) return qu6(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr<l || r<mid+1) return qu6(l, r, constl, mid, 2*idx+1, val);
    else {
      int oho = qu6(l, r, mid+1, constr, 2*idx+2, val);
      if(oho != -1) return oho;
      return qu6(l, r, constl, mid, 2*idx+1, val);
    }
  }
  int qu7(int l, int r, int constl, int constr, int idx, int val) { // Last element ≤ v
    if(l<=constl && constr<=r) {
      if(st[idx].mi > val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        if(st[idx].type != '?') {
          if(st[idx].type == ':') {
            st[2*idx+1].upd = st[idx].upd;
            st[2*idx+1].type = ':';
            st[2*idx+1].mi = st[idx].upd;
            st[2*idx+1].ma = st[idx].upd;
            st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

            st[2*idx+2].upd = st[idx].upd;
            st[2*idx+2].type = ':';
            st[2*idx+2].mi = st[idx].upd;
            st[2*idx+2].ma = st[idx].upd;
            st[2*idx+2].sum = st[idx].upd * (constr - mid);

            st[idx].upd = 0;
            st[idx].type = '?';
            st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
            st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
            st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
          }
          else {
            st[2*idx+1].upd += st[idx].upd;
            if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
            st[2*idx+1].mi += st[idx].upd;
            st[2*idx+1].ma += st[idx].upd;
            st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

            st[2*idx+2].upd += st[idx].upd;
            if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
            st[2*idx+2].mi += st[idx].upd;
            st[2*idx+2].ma += st[idx].upd;
            st[2*idx+2].sum += st[idx].upd * (constr - mid);

            st[idx].upd = 0;
            st[idx].type = '?';
            st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
            st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
            st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
          }
        }
        if(st[2*idx+2].mi <= val) idx = 2*idx + 2, constl = mid + 1;
        else idx = 2*idx + 1, constr = mid;
      }
      return constl;
    }
    int mid = (constl+constr) >> 1;
    if(st[idx].type != '?') {
      if(st[idx].type == ':') {
        st[2*idx+1].upd = st[idx].upd;
        st[2*idx+1].type = ':';
        st[2*idx+1].mi = st[idx].upd;
        st[2*idx+1].ma = st[idx].upd;
        st[2*idx+1].sum = st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd = st[idx].upd;
        st[2*idx+2].type = ':';
        st[2*idx+2].mi = st[idx].upd;
        st[2*idx+2].ma = st[idx].upd;
        st[2*idx+2].sum = st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
      else {
        st[2*idx+1].upd += st[idx].upd;
        if(st[2*idx+1].type == '?') st[2*idx+1].type = '+';
        st[2*idx+1].mi += st[idx].upd;
        st[2*idx+1].ma += st[idx].upd;
        st[2*idx+1].sum += st[idx].upd * (mid - constl + 1);

        st[2*idx+2].upd += st[idx].upd;
        if(st[2*idx+2].type == '?') st[2*idx+2].type = '+';
        st[2*idx+2].mi += st[idx].upd;
        st[2*idx+2].ma += st[idx].upd;
        st[2*idx+2].sum += st[idx].upd * (constr - mid);

        st[idx].upd = 0;
        st[idx].type = '?';
        st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
        st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
        st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
      }
    }
    if(mid<l || r<constl) return qu7(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr<l || r<mid+1) return qu7(l, r, constl, mid, 2*idx+1, val);
    else {
      int oho = qu7(l, r, mid+1, constr, 2*idx+2, val);
      if(oho != -1) return oho;
      return qu7(l, r, constl, mid, 2*idx+1, val);
    }
  }
  public:
  void resize(int k) {
    stok = k;
    st.resize(4 * k + 10);
  }
  void range_assign(int l, int r, int v) {
    u1(l, r, 0, stok, 0, v);
  }
  void range_add(int l, int r, int v) {
    u2(l, r, 0, stok, 0, v);
  }
  int query_min(int l, int r) {
    return qu1(l, r, 0, stok, 0);
  }
  int query_max(int l, int r) {
    return qu2(l, r, 0, stok, 0);
  }
  int query_sum(int l, int r) {
    return qu3(l, r, 0, stok, 0);
  }
  int query_firstAtLeast(int l, int r, int v) {
    return qu4(l, r, 0, stok, 0, v);
  }
  int query_firstAtMost(int l, int r, int v) {
    return qu5(l, r, 0, stok, 0, v);
  }
  int query_lastAtLeast(int l, int r, int v) {
    return qu6(l, r, 0, stok, 0, v);
  }
  int query_lastAtMost(int l, int r, int v) {
    return qu7(l, r, 0, stok, 0, v);
  }
};
void solve(int tc) {
  int n;
  cin >> n;
  int a[n+1];
  int dp[n+1];
  int prv[n+1];
  dp[0] = 0;
  prv[0] = 0;
  int ps[n+1];
  ps[0] = 0;
  segtree_basic stb;
  stb.resize(n);
  for(int i=1; i<=n; i++) {
    cin >> a[i];
    ps[i] = ps[i-1] + a[i];
  }
  for(int i=1; i<=n; i++) {
    dp[i] = 1e18;
    int opt = stb.query_lastAtMost(0, i-1, ps[i]);
    prv[i] = opt;
    dp[i] = ps[i] - ps[opt];
    stb.range_assign(i, i, dp[i] + ps[i]);
  }
  int ct = 0;
  int cur = n;
  while(cur != 0) {
    ct++;
    cur = prv[cur];
  }
  cout << ct << "\n";
}

int32_t main() {
  precomp();
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
// I don't know geometry.
// Teaming is unfair.
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 852 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 2 ms 852 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 1 ms 852 KB Output is correct
36 Correct 2 ms 852 KB Output is correct
37 Correct 2 ms 724 KB Output is correct
38 Correct 2 ms 724 KB Output is correct
39 Correct 2 ms 852 KB Output is correct
40 Correct 2 ms 896 KB Output is correct
41 Correct 2 ms 852 KB Output is correct
42 Correct 3 ms 852 KB Output is correct
43 Correct 2 ms 852 KB Output is correct
44 Correct 1 ms 852 KB Output is correct
45 Correct 1 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 852 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 2 ms 852 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 1 ms 852 KB Output is correct
36 Correct 2 ms 852 KB Output is correct
37 Correct 2 ms 724 KB Output is correct
38 Correct 2 ms 724 KB Output is correct
39 Correct 2 ms 852 KB Output is correct
40 Correct 2 ms 896 KB Output is correct
41 Correct 2 ms 852 KB Output is correct
42 Correct 3 ms 852 KB Output is correct
43 Correct 2 ms 852 KB Output is correct
44 Correct 1 ms 852 KB Output is correct
45 Correct 1 ms 852 KB Output is correct
46 Correct 53 ms 19140 KB Output is correct
47 Correct 47 ms 19540 KB Output is correct
48 Correct 49 ms 19940 KB Output is correct
49 Correct 46 ms 20052 KB Output is correct
50 Correct 47 ms 20004 KB Output is correct
51 Correct 48 ms 20040 KB Output is correct
52 Correct 24 ms 10120 KB Output is correct
53 Correct 18 ms 6248 KB Output is correct
54 Correct 44 ms 19332 KB Output is correct
55 Correct 52 ms 20052 KB Output is correct
56 Correct 44 ms 19556 KB Output is correct
57 Correct 48 ms 19540 KB Output is correct
58 Correct 44 ms 19520 KB Output is correct
59 Correct 42 ms 19668 KB Output is correct
60 Correct 45 ms 19648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 852 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 2 ms 852 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 1 ms 852 KB Output is correct
36 Correct 2 ms 852 KB Output is correct
37 Correct 2 ms 724 KB Output is correct
38 Correct 2 ms 724 KB Output is correct
39 Correct 2 ms 852 KB Output is correct
40 Correct 2 ms 896 KB Output is correct
41 Correct 2 ms 852 KB Output is correct
42 Correct 3 ms 852 KB Output is correct
43 Correct 2 ms 852 KB Output is correct
44 Correct 1 ms 852 KB Output is correct
45 Correct 1 ms 852 KB Output is correct
46 Correct 53 ms 19140 KB Output is correct
47 Correct 47 ms 19540 KB Output is correct
48 Correct 49 ms 19940 KB Output is correct
49 Correct 46 ms 20052 KB Output is correct
50 Correct 47 ms 20004 KB Output is correct
51 Correct 48 ms 20040 KB Output is correct
52 Correct 24 ms 10120 KB Output is correct
53 Correct 18 ms 6248 KB Output is correct
54 Correct 44 ms 19332 KB Output is correct
55 Correct 52 ms 20052 KB Output is correct
56 Correct 44 ms 19556 KB Output is correct
57 Correct 48 ms 19540 KB Output is correct
58 Correct 44 ms 19520 KB Output is correct
59 Correct 42 ms 19668 KB Output is correct
60 Correct 45 ms 19648 KB Output is correct
61 Correct 260 ms 96140 KB Output is correct
62 Correct 244 ms 96204 KB Output is correct
63 Correct 248 ms 96564 KB Output is correct
64 Correct 250 ms 98776 KB Output is correct
65 Correct 247 ms 99136 KB Output is correct
66 Correct 265 ms 99020 KB Output is correct
67 Correct 211 ms 79328 KB Output is correct
68 Correct 106 ms 39824 KB Output is correct
69 Correct 234 ms 95300 KB Output is correct
70 Correct 265 ms 99156 KB Output is correct
71 Correct 221 ms 96204 KB Output is correct
72 Correct 226 ms 96604 KB Output is correct
73 Correct 230 ms 97208 KB Output is correct
74 Correct 229 ms 97680 KB Output is correct
75 Correct 230 ms 97588 KB Output is correct