Submission #425851

#TimeUsernameProblemLanguageResultExecution timeMemory
425851MarcoMeijerMeetings (IOI18_meetings)C++14
19 / 100
491 ms392260 KiB
#include "meetings.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<ii> vii;

#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define RE(a,b) REP(a,0,b)
#define RE1(a,b) REP(a,1,b+1)
#define FOR(a,b) for(auto& a : b)
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(), a.end()

const int MXH = 21;
const int N = (1<<17);

struct T {
  ll resl[MXH], resr[MXH];
  ll minl[MXH], minr[MXH];
  int maxh;
};
T combine(const T& l, const T& r) {
  T res;
  res.maxh = max(l.maxh, r.maxh);
  RE(i,MXH) res.minl[i] = l.minl[i] + r.minl[l.maxh];
  RE(i,MXH) res.minr[i] = r.minr[i] + l.minr[r.maxh];
  RE(i,MXH) res.resl[i] = res.resr[i] = 1e18;
  RE(i,MXH) {
    res.resl[i     ] = min(res.resl[i], l.resl[i] + r.minl[l.maxh]);
    res.resr[i     ] = min(res.resr[i], r.resr[i] + l.minr[r.maxh]);
    res.resl[l.maxh] = min(res.resl[l.maxh], l.resr[i] + r.minl[i]);
    res.resr[r.maxh] = min(res.resr[r.maxh], r.resl[i] + l.minr[i]);
    if(l.maxh == res.maxh && r.maxh != res.maxh)
      res.resr[r.maxh] = min(res.resr[r.maxh], l.resr[i] + r.minl[i]);
    if(r.maxh == res.maxh && l.maxh != res.maxh)
      res.resl[l.maxh] = min(res.resl[l.maxh], r.resl[i] + l.minr[i]);
  }
  return res;
}
T createSingle(int x) {
  T res;
  res.maxh = x;
  RE(i,MXH) res.resl[i]=res.resr[i]=1e18;
  res.resl[x] = res.resr[x] = x;
  RE(i,MXH) res.minl[i]=res.minr[i]=max(x,i);
  return res;
}
T createEmpty() {
  T res;
  res.maxh = 0;
  RE(i,MXH) res.minl[i]=res.minr[i]=res.resl[i]=res.resr[i] = 0;
  return res;
}

T seg[N*2];
int h[N];
void buildSeg(int p=1, int l=0, int r=N-1) {
  if(l == r) {
    seg[p] = createSingle(h[l]);
    return;
  }
  int m=(l+r)/2;
  buildSeg(p*2  ,l  ,m);
  buildSeg(p*2+1,m+1,r);
  seg[p] = combine(seg[p*2], seg[p*2+1]);
}
T getSeg(int i, int j, int p=1, int l=0, int r=N-1) {
  if(j < l || i > r) return createEmpty();
  if(i <= l && j >= r) return seg[p];
  int m=(l+r)/2;
  T a = getSeg(i,j,p*2  ,l  ,m);
  T b = getSeg(i,j,p*2+1,m+1,r);
  return combine(a,b);
}

vll minimum_costs(vi H, vi L, vi R) {
  int n = H.size();
  int q = L.size();

  if(n <= 5000 && q <= 5000) {
    vector<vll> costL, costR;
    RE(i,2) (i?costL:costR).assign(n,vll(n,0));
    RE(i,n) {
      ll mx = 0;
      ll res = 0;
      REP(j,i,n) {
        mx = max<ll>(mx,H[j]);
        res += mx;
        costR[j][i] = res;
      }
      mx = res = 0;
      REV(j,0,i+1) {
        mx = max<ll>(mx,H[j]);
        res += mx;
        costL[j][i] = res;
      }
    }

    vll C(q,1e18);
    RE(cq,q) {
      int l=L[cq], r=R[cq];
      REP(i,l,r+1) {
        C[cq] = min(C[cq], costL[l][i] + costR[r][i] - H[i]);
      }
    }
    return C;
  } else {
    RE(i,n) h[i]=H[i];
    buildSeg();
    vll C(q,1e18);
    RE(cq,q) {
      int l=L[cq], r=R[cq];
      T res = getSeg(l,r);
      RE(i,MXH) {
        C[cq] = min(C[cq], res.resl[i]);
        C[cq] = min(C[cq], res.resr[i]);
      }
    }
    return C;
  }
}
#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...