#include<bits/stdc++.h> using namespace std; #define REP(i, M) for(int i = 0; i < M; i++) #define R(p, k) for(int i = p; i <= k; i++) #define RI(j, p, k) for(int j = p; j <= k; j++) #define PB push_back #define FI first #define SE second #define LL long long #define DEB 1 #if DEB #define CEN cerr << endl; #define CEL(a) cerr << #a << ": " << a << " "; #define CES(s) cerr << s << endl; #else #define CEN #define CEL(a) #define CES(s) #endif const LL INF = (LL)1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 5; const int MN = 200 * 1000 + 5; long long t[MN]; pair<LL, LL> d[MN]; int n, m; long long result[MN], res, squares, nex[MN], sizek[MN]; bool merged[MN]; struct sp{ LL v; }; bool operator<(const sp& a, const sp& b){ LL al = t[nex[a.v]] - t[a.v]; LL bl = t[nex[b.v]] - t[b.v]; // compare al / ak and bl / bk return al * sizek[b.v] >= bl * sizek[a.v]; } sp spojne[MN]; priority_queue<sp> q; LL l_square(LL a){ return a * (a - 1) / 2; } sp merge_nex(sp s, LL delta){ sp nex_sp; int v = s.v; int w = nex[v]; squares -= l_square(sizek[v]); squares -= l_square(sizek[w]); merged[w] = 1; nex_sp.v = v; nex[v] = nex[w]; sizek[v] += sizek[w]; //cerr << "next sp: " << v << " " << nex[v] << " " << sizek[v] << endl; squares += l_square(sizek[v]); res += delta * sizek[w]; return nex_sp; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for(int i = 1; i<=n; i++) cin >> t[i]; t[0] = 0; n++; t[n] = INF; nex[n] = n + 1; squares = 0; sort(t, t + n); REP(i, m){ cin >> d[i].first; d[i].second = i; } sort(d, d + m); REP(i, n){ sp s; s.v = i; nex[i] = i + 1; sizek[i] = 1; q.push(s); } LL ld = 0; REP(j, m){ LL dist = d[j].FI; //cerr << "rozwazam piec d= " << dist << endl; res += (dist - ld) * squares; ld = dist; while(!q.empty()){ sp s = q.top(); if(merged[s.v]){ q.pop(); continue; } if(t[s.v] + dist * sizek[s.v] > t[nex[s.v]]){ //cerr << "wyjmuje " << s.v << endl; q.pop(); sp ns = merge_nex(s, t[s.v] + dist * sizek[s.v] - t[nex[s.v]]); q.push(ns); } else{ break; } } result[d[j].SE] = res; } //sp s = q.top(); //cerr << s.v << " " << nex[s.v] << " " << t[nex[s.v]] - t[s.v] << endl; REP(i, m){ cout << result[i] << endl;; } return 0; }
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #include<bits/stdc++.h> using namespace std; #define REP(i, M) for(int i = 0; i < M; i++) #define R(p, k) for(int i = p; i <= k; i++) #define RI(j, p, k) for(int j = p; j <= k; j++) #define PB push_back #define FI first #define SE second #define LL long long #define DEB 1 #if DEB #define CEN cerr << endl; #define CEL(a) cerr << #a << ": " << a << " "; #define CES(s) cerr << s << endl; #else #define CEN #define CEL(a) #define CES(s) #endif const LL INF = (LL)1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 5; const int MN = 200 * 1000 + 5; long long t[MN]; pair<LL, LL> d[MN]; int n, m; long long result[MN], res, squares, nex[MN], sizek[MN]; bool merged[MN]; struct sp{ LL v; }; bool operator<(const sp& a, const sp& b){ LL al = t[nex[a.v]] - t[a.v]; LL bl = t[nex[b.v]] - t[b.v]; // compare al / ak and bl / bk return al * sizek[b.v] >= bl * sizek[a.v]; } sp spojne[MN]; priority_queue<sp> q; LL l_square(LL a){ return a * (a - 1) / 2; } sp merge_nex(sp s, LL delta){ sp nex_sp; int v = s.v; int w = nex[v]; squares -= l_square(sizek[v]); squares -= l_square(sizek[w]); merged[w] = 1; nex_sp.v = v; nex[v] = nex[w]; sizek[v] += sizek[w]; //cerr << "next sp: " << v << " " << nex[v] << " " << sizek[v] << endl; squares += l_square(sizek[v]); res += delta * sizek[w]; return nex_sp; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for(int i = 1; i<=n; i++) cin >> t[i]; t[0] = 0; n++; t[n] = INF; nex[n] = n + 1; squares = 0; sort(t, t + n); REP(i, m){ cin >> d[i].first; d[i].second = i; } sort(d, d + m); REP(i, n){ sp s; s.v = i; nex[i] = i + 1; sizek[i] = 1; q.push(s); } LL ld = 0; REP(j, m){ LL dist = d[j].FI; //cerr << "rozwazam piec d= " << dist << endl; res += (dist - ld) * squares; ld = dist; while(!q.empty()){ sp s = q.top(); if(merged[s.v]){ q.pop(); continue; } if(t[s.v] + dist * sizek[s.v] > t[nex[s.v]]){ //cerr << "wyjmuje " << s.v << endl; q.pop(); sp ns = merge_nex(s, t[s.v] + dist * sizek[s.v] - t[nex[s.v]]); q.push(ns); } else{ break; } } result[d[j].SE] = res; } //sp s = q.top(); //cerr << s.v << " " << nex[s.v] << " " << t[nex[s.v]] - t[s.v] << endl; REP(i, m){ cout << result[i] << endl;; } return 0; } |