#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef pair<int, int> PII; typedef vector<pair<int, int> > VPII; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; //813437586 #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define VAR(v, n) auto v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " /*************************** END OF TEMPLATE ***************************/ int n, m; const int maxn = 200042; pair<LL, LL> t[maxn]; LL anst[maxn]; map<LL, LL> g; multimap<LL, LL> e; LL ans; LL inc; int main() { scanf("%d%d", &n, &m); FOR(i, 1, n) { LL a; scanf("%lld", &a); if(g.find(a) == g.end()) { g[a] = 1; } else { g[a] = g[a] + 1; inc += g[a] - 1; } } n++; if(g.find(0) == g.end()) g[0] = 1; else { g[0] = g[0] + 1; inc += g[0] - 1; } auto i = g.begin(); LL last = i->ST; LL lnum = i->ND; i++; while(i != g.end()) { //cout << last << endl; e.insert(MP((i->ST - last)/lnum + 1, last)); last = i->ST; lnum = i->ND; i++; } FOR(i, 1, m) { t[i].ND = i; scanf("%lld", &(t[i].ST)); } sort(t+1, t+1+m); FOR(z, 1, m) { //cout << "*** " << t[z].ST << endl; ans += inc * (t[z].ST - t[z-1].ST); //cout << "inc " << inc << endl; while(!e.empty() && e.begin() -> ST <= t[z].ST) { LL p = e.begin()->ND; /* cout << "wyj " << p << endl; if(p > 1000) cout << "wtf" << endl; for(auto i : e) cout << '\t' << i.ST << ' ' << i.ND << endl; */ e.erase(e.begin()); if(g.find(p) != g.end()) { auto it = g.find(p); LL lnum = it->ND; //cout << "lnum " << lnum << endl; it++; if(it != g.end()) { inc += it->ND*lnum; ans += it->ND * (p + lnum * t[z].ST - it->ST); //cout << ans << endl; lnum = it->ND; g.erase(it); g[p] = g[p] + lnum; lnum = g[p]; it = g.find(p); it++; if(it != g.end()) { //cout << "wsadzam z czasem " << (it->ST - p)/lnum + 1 << endl; e.insert(MP((it->ST - p)/lnum + 1, p)); } } } } anst[t[z].ND] = ans; //printf("%lld\n", ans); } FOR(i, 1, m) { printf("%lld\n", anst[i]); } }
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 140 141 142 143 144 145 146 147 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef pair<int, int> PII; typedef vector<pair<int, int> > VPII; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; //813437586 #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define VAR(v, n) auto v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " /*************************** END OF TEMPLATE ***************************/ int n, m; const int maxn = 200042; pair<LL, LL> t[maxn]; LL anst[maxn]; map<LL, LL> g; multimap<LL, LL> e; LL ans; LL inc; int main() { scanf("%d%d", &n, &m); FOR(i, 1, n) { LL a; scanf("%lld", &a); if(g.find(a) == g.end()) { g[a] = 1; } else { g[a] = g[a] + 1; inc += g[a] - 1; } } n++; if(g.find(0) == g.end()) g[0] = 1; else { g[0] = g[0] + 1; inc += g[0] - 1; } auto i = g.begin(); LL last = i->ST; LL lnum = i->ND; i++; while(i != g.end()) { //cout << last << endl; e.insert(MP((i->ST - last)/lnum + 1, last)); last = i->ST; lnum = i->ND; i++; } FOR(i, 1, m) { t[i].ND = i; scanf("%lld", &(t[i].ST)); } sort(t+1, t+1+m); FOR(z, 1, m) { //cout << "*** " << t[z].ST << endl; ans += inc * (t[z].ST - t[z-1].ST); //cout << "inc " << inc << endl; while(!e.empty() && e.begin() -> ST <= t[z].ST) { LL p = e.begin()->ND; /* cout << "wyj " << p << endl; if(p > 1000) cout << "wtf" << endl; for(auto i : e) cout << '\t' << i.ST << ' ' << i.ND << endl; */ e.erase(e.begin()); if(g.find(p) != g.end()) { auto it = g.find(p); LL lnum = it->ND; //cout << "lnum " << lnum << endl; it++; if(it != g.end()) { inc += it->ND*lnum; ans += it->ND * (p + lnum * t[z].ST - it->ST); //cout << ans << endl; lnum = it->ND; g.erase(it); g[p] = g[p] + lnum; lnum = g[p]; it = g.find(p); it++; if(it != g.end()) { //cout << "wsadzam z czasem " << (it->ST - p)/lnum + 1 << endl; e.insert(MP((it->ST - p)/lnum + 1, p)); } } } } anst[t[z].ND] = ans; //printf("%lld\n", ans); } FOR(i, 1, m) { printf("%lld\n", anst[i]); } } |