제출 #606466

#제출 시각아이디문제언어결과실행 시간메모리
606466wiwiho모임들 (IOI18_meetings)C++14
41 / 100
3551 ms18932 KiB
#include "meetings.h" #include <bits/stdc++.h> #include <bits/extc++.h> #define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define lsort(a) sort(iter(a)) #define gsort(a) sort(riter(a)) #define pb(a) push_back(a) #define eb(a) emplace_back(a) #define pf(a) push_front(a) #define ef(a) emplace_front(a) #define pob pop_back() #define pof pop_front() #define mp(a, b) make_pair(a, b) #define F first #define S second #define mt make_tuple #define gt(t, i) get<i>(t) #define tomax(a, b) ((a) = max((a), (b))) #define tomin(a, b) ((a) = min((a), (b))) #define topos(a) ((a) = (((a) % MOD + MOD) % MOD)) #define uni(a) a.resize(unique(iter(a)) - a.begin()) #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ b << "\n";} using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef long double ld; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using tiii = tuple<int, int, int>; const ll MOD = 1000000007; const ll MAX = 2147483647; template<typename A, typename B> ostream& operator<<(ostream& o, pair<A, B> p){ return o << '(' << p.F << ',' << p.S << ')'; } ll ifloor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a < 0) return (a - b + 1) / b; else return a / b; } ll iceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a > 0) return (a + b - 1) / b; else return a / b; } int n; struct Line{ ll a = 0, b = 0; int l = 1, r = n; ll operator()(ll x){ return a * x + b; } }; ostream& operator<<(ostream& o, Line ln){ return o << '(' << ln.a << ',' << ln.b << ',' << ln.l << ',' << ln.r << ')'; } int intersection(Line l1, Line l2){ return iceil(l1.b - l2.b, l2.a - l1.a); } struct DynamicHull{ deque<Line> dq; void insert(Line ln){ while(!dq.empty() && dq.back()(dq.back().l) >= ln(dq.back().l)){ dq.pob; } if(dq.empty()){ dq.eb(ln); return; } if(dq.back()(dq.back().r) < ln(dq.back().r)){ if(dq.back().r < n){ ln.l = dq.back().r + 1; if(ln.l > ln.r) return; dq.eb(ln); } } if(dq.back().a == ln.a) return; int mid = intersection(dq.back(), ln); dq.back().r = mid - 1; ln.l = mid; if(ln.l > ln.r) return; dq.eb(ln); } ll query(int x){ while(!dq.empty() && dq.front().r < x) dq.pof; if(dq.empty()) return 1LL << 60; return dq.front()(x); } }; #define lc 2 * id + 1 #define rc 2 * id + 2 struct SegmentTree{ vector<DynamicHull> st; void init(){ st.resize(4 * n); } void modify(int x, Line ln, int L = 1, int R = n, int id = 0){ st[id].insert(ln); if(L == R){ return; } int M = (L + R) / 2; if(x <= M) modify(x, ln, L, M, lc); else modify(x, ln, M + 1, R, rc); } ll query(int l, int r, int x, int L = 1, int R = n, int id = 0){ if(l <= L && R <= r){ return st[id].query(x); } int M = (L + R) / 2; if(r <= M) return min(st[id].query(x), query(l, r, x, L, M, lc)); else if(l > M) return min(st[id].query(x), query(l, r, x, M + 1, R, rc)); else{ return min({st[id].query(x), query(l, r, x, L, M, lc), query(l, r, x, M + 1, R, rc)}); } } }; vector<ll> minimum_costs(vector<int> H, vector<int> L, vector<int> R){ n = H.size(); int q = L.size(); vector<ll> h(n + 1); for(int i = 0; i < n; i++) h[i + 1] = H[i]; for(int i = 0; i < q; i++) L[i]++, R[i]++; vector<ll> sum(n + 1), lsum(n + 2), rsum(n + 2); vector<int> lp(n + 1), rp(n + 1); vector<vector<int>> qry(n + 1); vector<ll> ans(q, MAX); vector<int> lb(q, -1), rb(q, -1); for(int i = 0; i < q; i++){ qry[R[i]].eb(i); } auto init = [&](int v, vector<ll>& ssum, vector<int>& p){ deque<int> dq; dq.eb(v == 1 ? 0 : n + 1); ll tmp = 0; for(int i = v == 1 ? 1 : n; 1 <= i && i <= n; i += v){ while(dq.size() > 1 && h[dq.back()] < h[i]){ int t = dq.back(); dq.pob; tmp -= abs(t - dq.back()) * h[t]; } tmp += abs(i - dq.back()) * h[i]; ssum[i] = tmp; p[i] = dq.back(); dq.eb(i); } }; init(1, lsum, lp); init(-1, rsum, rp); for(int i = 1; i <= n; i++){ sum[i] = lsum[i] + rsum[i] - h[i]; } auto solve = [&](){ vector<ll> tans = sum; deque<int> dq; /*SegmentTree st; st.init();*/ vector<Line> ln(n + 2); for(int i = 1; i <= n; i++){ while(!dq.empty() && h[dq.back()] <= h[i]){ tans[i] = min(tans[i], tans[dq.back()]); dq.pob; } dq.eb(i); int t = rp[i]; ln[i] = Line({h[i], tans[i] - t * h[i] - rsum[t] + h[i], 0, t - 1}); // st.modify(i, Line({h[i], tans[i] - t * h[i] - rsum[t] + h[i], 0, t - 1})); for(int j : qry[i]){ int l = L[j]; rb[j] = *lower_bound(iter(dq), l); if(rb[j] == i) continue; for(int k = rb[j] + 1; k <= i; k++){ if(ln[k].r < i) continue; ans[j] = min(ans[j], ln[k](i) - lsum[rb[j]] + (rb[j] - l + 1) * h[rb[j]]); } /*ll tmp = st.query(rb[j] + 1, i, i) - lsum[rb[j]] + (rb[j] - l + 1) * h[rb[j]]; ans[j] = min(ans[j], tmp);*/ } } }; solve(); reverse(h.begin() + 1, h.begin() + n + 1); reverse(sum.begin() + 1, sum.begin() + n + 1); reverse(lsum.begin() + 1, lsum.begin() + n + 1); reverse(rsum.begin() + 1, rsum.begin() + n + 1); lsum.swap(rsum); rp.swap(lp); for(int i = 1; i <= n; i++) rp[i] = n - rp[i] + 1; reverse(rp.begin() + 1, rp.begin() + n + 1); fill(iter(qry), vector<int>()); L.swap(R); for(int i = 0; i < q; i++){ L[i] = n - L[i] + 1; R[i] = n - R[i] + 1; qry[R[i]].eb(i); } lb.swap(rb); solve(); lsum.swap(rsum); reverse(h.begin() + 1, h.begin() + n + 1); reverse(sum.begin() + 1, sum.begin() + n + 1); reverse(lsum.begin() + 1, lsum.begin() + n + 1); reverse(rsum.begin() + 1, rsum.begin() + n + 1); for(int i = 0; i < q; i++){ L[i] = n - L[i] + 1; R[i] = n - R[i] + 1; } L.swap(R); lb.swap(rb); for(int i = 0; i < q; i++) lb[i] = n - lb[i] + 1; fill(iter(qry), vector<int>()); for(int i = 0; i < q; i++) qry[rb[i]].eb(i); { vector<int> st; for(int i = 1; i <= n; i++){ while(!st.empty() && sum[st.back()] >= sum[i]) st.pob; st.eb(i); for(int j : qry[i]){ int l = lb[j]; int id = *lower_bound(iter(st), l); ll tmp = sum[id] - lsum[l] + h[l] * (l - L[j] + 1) - rsum[i] + h[i] * (R[j] - i + 1); ans[j] = min(ans[j], tmp); } } } return ans; }
#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...