#include <iostream> #include <cstdio> #include <queue> #include <set> #include <tuple> using namespace std; typedef long long LL; typedef long double LD; template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; LL t[200001]; bool dead[200001]; int nxt[200001]; int prv[200001]; int main() { int n, m; scanf("%i%i", &n, &m); for(int i = 1; i <= n; i++) scanf("%lli", t+i); min_heap<pair<LD, int>> q; vector<pair<LD, int>> timeline; for(int i = 1; i <= n; i++) { q.emplace(t[i]-t[i-1], i); prv[i] = i-1; } for(int i = 0; i < n; i++) nxt[i] = i+1; while(!q.empty()) { auto evt = q.top(); q.pop(); //sprawdź czy event jest aktualny if(dead[evt.second]) continue; if(nxt[evt.second]) q.emplace(LD(t[nxt[evt.second]]-t[prv[evt.second]]) / (nxt[evt.second]-prv[evt.second]), nxt[evt.second]); dead[evt.second] = true; prv[nxt[evt.second]] = prv[evt.second]; nxt[prv[evt.second]] = nxt[evt.second]; timeline.emplace_back(evt.first, evt.second - prv[evt.second]); } LD sum = 0, h = 0; set<tuple<LD, LL, int>> s = {make_tuple((LD)0, (LL)0, 0)}; for(int i = 0; i < (int)timeline.size(); i++) { //cout << i.first << " " << i.second << endl; LD dt = timeline[i].first; if(i > 0) dt -= timeline[i-1].first; h += sum*dt; sum += timeline[i].second; s.emplace(timeline[i].first, h, sum); } for(int i = 0; i < m; i++) { int a; scanf("%i", &a); auto it = s.upper_bound(make_tuple(a, 0, 0)); --it; //cout << get<0>(*it) << " " << get<1>(*it) << " " << get<2>(*it) << endl; LD ans = get<1>(*it) + get<2>(*it)*(a-get<0>(*it)); printf("%lli\n", LL(ans)); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <iostream> #include <cstdio> #include <queue> #include <set> #include <tuple> using namespace std; typedef long long LL; typedef long double LD; template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; LL t[200001]; bool dead[200001]; int nxt[200001]; int prv[200001]; int main() { int n, m; scanf("%i%i", &n, &m); for(int i = 1; i <= n; i++) scanf("%lli", t+i); min_heap<pair<LD, int>> q; vector<pair<LD, int>> timeline; for(int i = 1; i <= n; i++) { q.emplace(t[i]-t[i-1], i); prv[i] = i-1; } for(int i = 0; i < n; i++) nxt[i] = i+1; while(!q.empty()) { auto evt = q.top(); q.pop(); //sprawdź czy event jest aktualny if(dead[evt.second]) continue; if(nxt[evt.second]) q.emplace(LD(t[nxt[evt.second]]-t[prv[evt.second]]) / (nxt[evt.second]-prv[evt.second]), nxt[evt.second]); dead[evt.second] = true; prv[nxt[evt.second]] = prv[evt.second]; nxt[prv[evt.second]] = nxt[evt.second]; timeline.emplace_back(evt.first, evt.second - prv[evt.second]); } LD sum = 0, h = 0; set<tuple<LD, LL, int>> s = {make_tuple((LD)0, (LL)0, 0)}; for(int i = 0; i < (int)timeline.size(); i++) { //cout << i.first << " " << i.second << endl; LD dt = timeline[i].first; if(i > 0) dt -= timeline[i-1].first; h += sum*dt; sum += timeline[i].second; s.emplace(timeline[i].first, h, sum); } for(int i = 0; i < m; i++) { int a; scanf("%i", &a); auto it = s.upper_bound(make_tuple(a, 0, 0)); --it; //cout << get<0>(*it) << " " << get<1>(*it) << " " << get<2>(*it) << endl; LD ans = get<1>(*it) + get<2>(*it)*(a-get<0>(*it)); printf("%lli\n", LL(ans)); } } |