Submission #1165668

#TimeUsernameProblemLanguageResultExecution timeMemory
1165668Zero_OPMeetings (IOI18_meetings)C++20
100 / 100
3435 ms498624 KiB
#include <bits/stdc++.h>

#ifndef LOCAL
      #include "meetings.h"
#endif //nLOCAL

using namespace std;

#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r) - 1; i >= (l); --i)

#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second

#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define pb push_back
#define eb emplace_back
#define sz(v) (int)v.size()
#define sum_of(v) accumulate(all(v), 0ll)
#define compact(v) v.erase(unique(all(v)), end(v))

#define dbg(x) "[" #x " = " << (x) << "]"

template<typename T>
      bool minimize(T& a, const T& b){
            if(a > b) return a = b, true;
            return false;
      }

template<typename T>
      bool maximize(T& a, const T& b){
            if(a < b) return a = b, true;
            return false;
      }

using ll = long long;
using db = double;
using ld = long double;
using ull = unsigned long long;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;

using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vl = vector<ll>;
using vd = vector<db>;

using vpi = vector<pi>;
using vpl = vector<pl>;

/*

Extended Li-chao Tree features :
- 1 l r a b : for all i in [l, r] : v[i] = min(v[i], a * i + b)
- 2 l r a b : for all i in [l, r] : v[i] += a * i + b
- 3 p : return a[p]

*/

struct line{
      ll a, b;
      line(ll a, ll b) : a(a), b(b) {}

      ll operator()(ll x) const  { return a * x + b; }
      ll slope(){ return a; }
      ll intercept(){ return b; }

      line& operator += (const line& o){
            a += o.a; b += o.b;
            return *this;
      }

      friend line operator + (line a, const line& b){ return a += b; }
};

const ll inf = 1e17;

struct MinExtendedLichaoTree{
      vector<line> st, lazy;

      MinExtendedLichaoTree(int n) : st(n << 2, line(0, 0)), lazy(n << 2, line(0, 0)) {}

      void apply_increase(int id, line f){
            if(st[id].a != 0 || st[id].b != inf) {
                  st[id] += f;
            }
            lazy[id] += f;
      }

      void push_down(int id){
            if(lazy[id].a != 0 || lazy[id].b != 0){
                  apply_increase(id << 1, lazy[id]);
                  apply_increase(id << 1 | 1, lazy[id]);
                  lazy[id].a = lazy[id].b = 0;
            }
      }

      void add_line(int id, int l, int r, line f){
            if(l == r){
                  if(st[id](l) > f(l)) st[id] = f;
            } else{
                  int mid = l + r >> 1;
                  push_down(id);
                  if(st[id](mid) > f(mid)) swap(st[id], f);
                  if(f.a > st[id].a) add_line(id << 1, l, mid, f);
                  if(f.a < st[id].a) add_line(id << 1 | 1, mid + 1, r, f);
            }
      }

      void push_line(int id, int l, int r, int mid){
            if(st[id].b != inf){
                  add_line(id << 1, l, mid, st[id]);
                  add_line(id << 1 | 1, mid + 1, r, st[id]);
                  st[id].a = 0;
                  st[id].b = inf;
            }
      }

      void update_add_line(int id, int l, int r, int u, int v, line f){
            if(u <= l && r <= v){
                  add_line(id, l, r, f);
            } else{
                  int mid = l + r >> 1;
                  push_down(id);
                  if(u <= mid) update_add_line(id << 1, l, mid, u, v, f);
                  if(mid < v)  update_add_line(id << 1 | 1, mid + 1, r, u, v, f);
            }
      }

      void update_increase(int id, int l, int r, int u, int v, line f){
            if(u <= l && r <= v) {
                  apply_increase(id, f);
            }
            else{
                  int mid = l + r >> 1;
                  push_down(id);
                  push_line(id, l, r, mid);
                  if(u <= mid) update_increase(id << 1, l, mid, u, v, f);
                  if(mid < v)  update_increase(id << 1 | 1, mid + 1, r, u, v, f);
            }
      }

      ll query_point(int id, int l, int r, int pos){
            ll cur = st[id](pos);
            if(l == r) return cur;
            int mid = l + r >> 1;
            push_down(id);
            if(pos <= mid) return min(cur, query_point(id << 1, l, mid, pos));
            else return min(cur, query_point(id << 1 | 1, mid + 1, r, pos));
      }
};

template<typename T>
struct MaxSparseTable{
      vector<vector<T>> rmq;

      MaxSparseTable(vector<T> v) : rmq(){
            rmq.pb(v);
            int N = sz(v);
            for(int i = 1; (1 << i) <= N; ++i){
                  rmq.pb(vector<T>(N - (1 << i) + 1));
                  FOR(j, 0, N - (1 << i) + 1){
                        rmq[i][j] = max(rmq[i-1][j], rmq[i-1][j + (1 << (i-1))]);
                  }
            }
      }

      T query(int l, int r){
            int k = 31 - __builtin_clz(r - l + 1);
            return max(rmq[k][l], rmq[k][r - (1 << k) + 1]);
      }
};

vl minimum_costs(vi H, vi L, vi R){
      int N = sz(H);
      int Q = sz(L);

      vpi v(N);
      FOR(i, 0, N){
            v[i] = mp(H[i], i);
      }

      MaxSparseTable<pi> ST(v);

      vector<vi> pos(N);
      FOR(i, 0, Q){
            int p = ST.query(L[i], R[i]).ss;
            pos[p].pb(i);
      }

      MinExtendedLichaoTree le(N), re(N);
      vl ans(Q, -1);

      function<void(int, int)> solve = [&](int l, int r){
            int M = ST.query(l, r).ss;
            if(l < M) solve(l, M-1);
            if(M < r) solve(M+1, r);

            //le : opt[L][M-1], opt[L+1][M-1], ..., opt[M-1][M-1] ; opt[M+1][R],   opt[M+2][R],   ..., opt[R][R]
            //re : opt[L][L],   opt[L][L+1],   ..., opt[L][M-1]   ; opt[M+1][M+1], opt[M+1][M+2], ..., opt[M+1][R] ;

            for(auto id : pos[M]){
                  ans[id] = min(le.query_point(1, 0, N-1, L[id]) + 1LL * (R[id] - M + 1) * H[M],
                                re.query_point(1, 0, N-1, R[id]) + 1LL * (M - L[id] + 1) * H[M]);
            }

            //opt[x][R] = min(opt[x][M-1] + (R - M + 1) * H[M], opt[M+1][R] + M * H[M] + H[M] - x * H[M])
            //opt[L][x] = min(opt[L][M-1] + x * H[M] - M * H[M] + H[M], opt[M+1][x] + (M - L + 1) * H[M])

            ll optRight = (M<r ? le.query_point(1, 0, N-1, M+1) : 0);
            le.update_increase(1, 0, N-1, l, M, line(0, 1LL * (r - M + 1) * H[M]));
            le.update_add_line(1, 0, N-1, l, M, line(-H[M], 1LL * M * H[M] + H[M] + optRight));

            ll optLeft = (l<M ? re.query_point(1, 0, N-1, M-1) : 0);
            re.update_increase(1, 0, N-1, M, r, line(0, 1LL * (M - l + 1) * H[M]));
            re.update_add_line(1, 0, N-1, M, r, line(H[M], -1LL * M * H[M] + H[M] + optLeft));
      };

      solve(0, N-1);
      return ans;
}

#ifdef LOCAL
void setIO(){
      ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
      freopen("task.inp", "r", stdin);
      freopen("task.out", "w", stdout);
#endif // LOCAL
}

int main(){
      setIO();

      int N, Q;
      cin >> N >> Q;
      vi H(N);
      FOR(i, 0, N) cin >> H[i];

      vi L(Q), R(Q);
      FOR(i, 0, Q){
            cin >> L[i] >> R[i];
      }

      vl result = minimum_costs(H, L, R);
      FOR(i, 0, Q) cout << result[i] << '\n';

      return 0;
}
#endif //LOCAL

/*

Let f[L][R] = the answer for query [L, R]

Consider query [L, R] :
- Let (H[p], p) become the maximum (H[i], i) for L <= i <= R
- f[L][R] = min(f[L][p-1] + (R - p + 1) * H[p], f[p+1][R] + (p - L + 1) * H[p])

Let recursively solve f[L][p-1] and f[p+1][R]

Assume that we had calculated f[1][p-1], f[L][p-1], ..., f[p-1][p-1] :
- for every p < R : f[L][R] = f[L][p-1] + (R - p + 1) * H[p]
- then f[L][R] = min(f[L][R], f[p+1][R] + (p - L + 1) * H[p])

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...