Submission #131771

#TimeUsernameProblemLanguageResultExecution timeMemory
131771PeppaPigHoliday (IOI14_holiday)C++14
47 / 100
482 ms65540 KiB
#include "holiday.h" #include <bits/stdc++.h> #define long long long using namespace std; const int N = 1e5+5; vector<long> t1, t2, L, R; int newleaf(long sum, long cnt) { t1.emplace_back(sum), t2.emplace_back(cnt); L.emplace_back(-1), R.emplace_back(-1); return t1.size() - 1; } int newpar(int l, int r) { t1.emplace_back(t1[l] + t1[r]), t2.emplace_back(t2[l] + t2[r]); L.emplace_back(l), R.emplace_back(r); return t1.size() - 1; } int n, d, start, a[N], ver[N]; long ans = -1; vector<int> coord; map<int, int> M; int build(int l = 1, int r = n) { if(l == r) return newleaf(0, 0); int mid = (l + r) >> 1; return newpar(build(l, mid), build(mid+1, r)); } int update(int x, long k, int p, int l = 1, int r = n) { if(l == r) return newleaf(k, 1); int mid = (l + r) >> 1; if(x <= mid) return newpar(update(x, k, L[p], l, mid), R[p]); else return newpar(L[p], update(x, k, R[p], mid+1, r)); } long query(int k, int pl, int pr, int l = 1, int r = n) { if(!k) return 0; if(l == r) return t1[pr] - t1[pl]; long sum_r = t1[R[pr]] - t1[R[pl]], cnt_r = t2[R[pr]] - t2[R[pl]]; int mid = (l + r) >> 1; if(cnt_r >= k) return query(k, R[pl], R[pr], mid+1, r); else return sum_r + query(k - cnt_r, L[pl], L[pr], l, mid); } void solve(int l, int r, int optl, int optr) { if(l > r) return; long now = -1, idx = optl; int mid = (l + r) >> 1; for(int i = max(mid, optl); i <= min(mid + d - 1, optr); i++) { int cost = i - mid + min(abs(start - i), abs(start - mid)); if(d - cost - 2 < 0) continue; long q = query(d - cost - 2, ver[mid], ver[i-1]) + a[mid] + a[i]; if(q > now) now = q, idx = i; } ans = max(ans, now); solve(l, mid-1, optl, idx), solve(mid+1, r, idx, optr); } long findMaxAttraction(int _n, int _start, int _d, int _a[]) { n = _n, start = _start + 1, d = _d; for(int i = 1; i <= n; i++) { a[i] = _a[i-1]; coord.emplace_back(a[i]); } if(d == 1) return a[start]; sort(coord.begin(), coord.end()); ver[0] = build(); for(int i = 1; i <= n; i++) { int idx = lower_bound(coord.begin(), coord.end(), a[i]) - coord.begin() + ++M[a[i]]; ver[i] = update(idx, a[i], ver[i-1]); } solve(max(1, start - d + 1), min(n, start + d - 1), 1, n); 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...