#include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long int lli; const int MAXN = 200005; const lli INF = 1e13; lli t[MAXN]; pair<lli, lli> d[MAXN]; struct Inter { lli x; // punkt zaczepienia - pierwszy koleś lli n; // liczba przedziałów Inter(){} Inter(lli _x, lli _n) : x(_x), n(_n) {} void print() { printf("%lld %lld\n", x, n); } }; bool operator<(const Inter &a, const Inter &b) { return a.x < b.x; } struct Join { lli x1, x2; lli tim; Join() {} Join(lli _x1, lli _x2, lli _tim) : x1(_x1), x2(_x2), tim(_tim) {} void print() { printf("%lld %lld, %lld\n", x1, x2, tim); } }; bool operator<(const Join &a, const Join &b) { if (a.tim != b.tim) return a.tim < b.tim; return a.x1 < b.x1; } set<Inter> state; set<Join> toJoin; lli ans = 0; lli curt = 1; lli sumsq = 0; lli sum = 0; lli toprint[MAXN]; Join predict(const Inter &a, const Inter &b) { lli dif = (b.x-curt) - (a.x + (a.n-1)*curt); return Join(a.x, b.x, curt+dif/a.n); } void printToJoin() { printf("toJoin\n"); for (auto zd : toJoin) zd.print(); printf("\n"); } void printState() { printf("state\n"); for (auto p : state) p.print(); printf("\n"); } void printAll() { printState(); printToJoin(); } void proceedTo(lli tar) { // printf("proceed to %lld\n", tar); if (curt < tar) { ans += (tar-curt) * (sumsq-sum)/2; curt = tar; } } void onion(const Join &zd) { // printf("onion %lld %lld, %lld\n", zd.x1, zd.x2, zd.tim); lli x1 = zd.x1, x2 = zd.x2; auto it1 = state.find(Inter(x1, 0)); sumsq -= it1->n*it1->n; auto it2 = state.find(Inter(x2, 0)); sumsq -= it2->n*it2->n; auto it3 = it2; it3++; if (it3 != state.end()) { toJoin.erase(toJoin.find(predict(*it2, *it3))); } //TODO auto p = Inter(x1, it1->n + it2->n); lli dif = (it2->x-curt) - (it1->x + (it1->n-1)*curt); ans -= dif*it2->n; state.erase(it1); state.erase(it2); state.insert(p); sumsq += p.n*p.n; if (it3 == state.end()) return; toJoin.insert(predict(p, *it3)); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i=0; i<n; i++) { scanf("%lld", &t[i]); } for (int i=0; i<m; i++) { scanf("%lld", &d[i].st); d[i].nd = i; } // printf("read\n"); sort(t, t+n); sort(d, d+m); lli last = 0; state.insert(Inter(0, 1)); for (int i=0; i<n; i++) { if (t[i] <= last+1) { last++; ans += last-t[i]; auto p = *state.rbegin(); state.erase(state.find(p)); p.n++; state.insert(p); } else { last = t[i]; state.insert(Inter(t[i], 1)); } } // for (auto p : state) { // p.print(); // } for (auto it = state.begin(); it != state.end(); it++) { sum += it->n; sumsq += it->n*it->n; auto nex = it; nex++; if (nex == state.end()) break; auto p1 = *it; auto p2 = *nex; toJoin.insert(predict(p1, p2)); // predict(p1, p2).print(); } // printf("loop\n"); for (int i=0; i<m; i++) { // printf("%d\n", i); lli tar = d[i].st; if (curt == tar) { toprint[d[i].nd] = ans; continue; } while (true) { if (toJoin.empty()) break; auto zd = *toJoin.begin(); if (zd.tim >= tar) break; toJoin.erase(toJoin.begin()); proceedTo(zd.tim); onion(zd); // printAll(); } proceedTo(tar); // printAll(); // printf("%lld %lld %lld\n", curt, ans, d[i].nd); toprint[d[i].nd] = ans; } for (int i=0; i<m; i++) { printf("%lld\n", toprint[i]); } 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long int lli; const int MAXN = 200005; const lli INF = 1e13; lli t[MAXN]; pair<lli, lli> d[MAXN]; struct Inter { lli x; // punkt zaczepienia - pierwszy koleś lli n; // liczba przedziałów Inter(){} Inter(lli _x, lli _n) : x(_x), n(_n) {} void print() { printf("%lld %lld\n", x, n); } }; bool operator<(const Inter &a, const Inter &b) { return a.x < b.x; } struct Join { lli x1, x2; lli tim; Join() {} Join(lli _x1, lli _x2, lli _tim) : x1(_x1), x2(_x2), tim(_tim) {} void print() { printf("%lld %lld, %lld\n", x1, x2, tim); } }; bool operator<(const Join &a, const Join &b) { if (a.tim != b.tim) return a.tim < b.tim; return a.x1 < b.x1; } set<Inter> state; set<Join> toJoin; lli ans = 0; lli curt = 1; lli sumsq = 0; lli sum = 0; lli toprint[MAXN]; Join predict(const Inter &a, const Inter &b) { lli dif = (b.x-curt) - (a.x + (a.n-1)*curt); return Join(a.x, b.x, curt+dif/a.n); } void printToJoin() { printf("toJoin\n"); for (auto zd : toJoin) zd.print(); printf("\n"); } void printState() { printf("state\n"); for (auto p : state) p.print(); printf("\n"); } void printAll() { printState(); printToJoin(); } void proceedTo(lli tar) { // printf("proceed to %lld\n", tar); if (curt < tar) { ans += (tar-curt) * (sumsq-sum)/2; curt = tar; } } void onion(const Join &zd) { // printf("onion %lld %lld, %lld\n", zd.x1, zd.x2, zd.tim); lli x1 = zd.x1, x2 = zd.x2; auto it1 = state.find(Inter(x1, 0)); sumsq -= it1->n*it1->n; auto it2 = state.find(Inter(x2, 0)); sumsq -= it2->n*it2->n; auto it3 = it2; it3++; if (it3 != state.end()) { toJoin.erase(toJoin.find(predict(*it2, *it3))); } //TODO auto p = Inter(x1, it1->n + it2->n); lli dif = (it2->x-curt) - (it1->x + (it1->n-1)*curt); ans -= dif*it2->n; state.erase(it1); state.erase(it2); state.insert(p); sumsq += p.n*p.n; if (it3 == state.end()) return; toJoin.insert(predict(p, *it3)); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i=0; i<n; i++) { scanf("%lld", &t[i]); } for (int i=0; i<m; i++) { scanf("%lld", &d[i].st); d[i].nd = i; } // printf("read\n"); sort(t, t+n); sort(d, d+m); lli last = 0; state.insert(Inter(0, 1)); for (int i=0; i<n; i++) { if (t[i] <= last+1) { last++; ans += last-t[i]; auto p = *state.rbegin(); state.erase(state.find(p)); p.n++; state.insert(p); } else { last = t[i]; state.insert(Inter(t[i], 1)); } } // for (auto p : state) { // p.print(); // } for (auto it = state.begin(); it != state.end(); it++) { sum += it->n; sumsq += it->n*it->n; auto nex = it; nex++; if (nex == state.end()) break; auto p1 = *it; auto p2 = *nex; toJoin.insert(predict(p1, p2)); // predict(p1, p2).print(); } // printf("loop\n"); for (int i=0; i<m; i++) { // printf("%d\n", i); lli tar = d[i].st; if (curt == tar) { toprint[d[i].nd] = ans; continue; } while (true) { if (toJoin.empty()) break; auto zd = *toJoin.begin(); if (zd.tim >= tar) break; toJoin.erase(toJoin.begin()); proceedTo(zd.tim); onion(zd); // printAll(); } proceedTo(tar); // printAll(); // printf("%lld %lld %lld\n", curt, ans, d[i].nd); toprint[d[i].nd] = ans; } for (int i=0; i<m; i++) { printf("%lld\n", toprint[i]); } return 0; } |