#include <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define M_PI 3.14159265358979323846 template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template<class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',')cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class T> ostream& operator<<(ostream& os, set<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #endif int sumaStartow = 0; int sumaMnoznikow = 0; struct interval { int start; int count; bool deleted; interval * right; interval * left; interval(int _start, int _count) { deleted = 0; right = left = 0; start = _start; count = _count; } interval * append(interval * rhs) { right = rhs; rhs->left = this; return rhs; } void mergeRight(int t) { if (deleted) return; while (tryMergeRight(t)); } bool tryMergeRight(int t) { if (right->count == -1) { return false; } if (getEnd(t) >= right->getBegin(t)) { right->del(); del(); deleted = false; count += right->count; append(right->right); sumaStartow += getSumaStartow(); sumaMnoznikow += getMnoznik(); return true; } else { return false; } } void del() { deleted = true; sumaStartow -= getSumaStartow(); sumaMnoznikow -= getMnoznik(); } bool operator<(const interval & rhs) const { return tie(start, count) < tie(rhs.start, rhs.count); } int getBegin(int p) const { return start - p; } int getEnd(int p) const { return start + (count - 1) * p; } int getMnoznik() const { return (count) * (count - 1) / 2; } int getSumaStartow() const { return start * count; } }; int top(int a, int b) { return (a + b - 1) / b; } int mergeTime(const interval & i1, const interval & i2) { return top(i2.start - i1.start, i1.count); } int32_t main(int32_t argc, char ** argv) { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; VI klienci(n); int sum = 0; REP(i, n) { cin >> klienci[i]; sum += klienci[i]; } const int mxN = 1e6; VI wynik(mxN + 1); interval * last = new interval(klienci[0], 1); interval * dummy = new interval(-1, -1); interval * tail = dummy; FOR(i, 1, n - 1) { if (last->start + last->count >= klienci[i]) { last->count++; } else { tail = tail->append(last); last = new interval(klienci[i], 1); } } tail = tail->append(last); tail = tail->append(dummy); { interval * i = dummy->right; while (i != dummy) { sumaMnoznikow += i->getMnoznik(); sumaStartow += i->getSumaStartow(); i = i->right; } } vector<set<interval*>> toUpdate(mxN + 1); { interval *lhs = dummy->right; interval *rhs = dummy->right->right; while (rhs != dummy) { int czas = mergeTime(*lhs, *rhs); if (czas <= mxN) { toUpdate[czas].insert(lhs); } lhs = lhs->right; rhs = rhs->right; } } interval * fst = dummy->right; FOR(i, 1, mxN) { int diff = min(0LL, fst->getBegin(i)); sumaStartow -= fst->getSumaStartow(); fst->start -= diff; sumaStartow += fst->getSumaStartow(); fst->mergeRight(i); for (interval* inter : toUpdate[i]) { if (inter->deleted) { continue; } inter->mergeRight(i); if (inter->left != dummy) { int czas = mergeTime(*(inter->left), *inter); if (czas <= mxN && czas > i) toUpdate[czas].insert(inter->left); } if (inter->right != dummy) { int czas = mergeTime(*inter, *(inter->right)); if (czas <= mxN && czas > i) toUpdate[czas].insert(inter); } } toUpdate[i] = {}; wynik[i] = sumaStartow - sum + sumaMnoznikow * i; } REP(i, m) { int a; cin >> a; cout << wynik[a] << "\n"; } }
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 | #include <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define M_PI 3.14159265358979323846 template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template<class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',')cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class T> ostream& operator<<(ostream& os, set<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } template<class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #endif int sumaStartow = 0; int sumaMnoznikow = 0; struct interval { int start; int count; bool deleted; interval * right; interval * left; interval(int _start, int _count) { deleted = 0; right = left = 0; start = _start; count = _count; } interval * append(interval * rhs) { right = rhs; rhs->left = this; return rhs; } void mergeRight(int t) { if (deleted) return; while (tryMergeRight(t)); } bool tryMergeRight(int t) { if (right->count == -1) { return false; } if (getEnd(t) >= right->getBegin(t)) { right->del(); del(); deleted = false; count += right->count; append(right->right); sumaStartow += getSumaStartow(); sumaMnoznikow += getMnoznik(); return true; } else { return false; } } void del() { deleted = true; sumaStartow -= getSumaStartow(); sumaMnoznikow -= getMnoznik(); } bool operator<(const interval & rhs) const { return tie(start, count) < tie(rhs.start, rhs.count); } int getBegin(int p) const { return start - p; } int getEnd(int p) const { return start + (count - 1) * p; } int getMnoznik() const { return (count) * (count - 1) / 2; } int getSumaStartow() const { return start * count; } }; int top(int a, int b) { return (a + b - 1) / b; } int mergeTime(const interval & i1, const interval & i2) { return top(i2.start - i1.start, i1.count); } int32_t main(int32_t argc, char ** argv) { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; VI klienci(n); int sum = 0; REP(i, n) { cin >> klienci[i]; sum += klienci[i]; } const int mxN = 1e6; VI wynik(mxN + 1); interval * last = new interval(klienci[0], 1); interval * dummy = new interval(-1, -1); interval * tail = dummy; FOR(i, 1, n - 1) { if (last->start + last->count >= klienci[i]) { last->count++; } else { tail = tail->append(last); last = new interval(klienci[i], 1); } } tail = tail->append(last); tail = tail->append(dummy); { interval * i = dummy->right; while (i != dummy) { sumaMnoznikow += i->getMnoznik(); sumaStartow += i->getSumaStartow(); i = i->right; } } vector<set<interval*>> toUpdate(mxN + 1); { interval *lhs = dummy->right; interval *rhs = dummy->right->right; while (rhs != dummy) { int czas = mergeTime(*lhs, *rhs); if (czas <= mxN) { toUpdate[czas].insert(lhs); } lhs = lhs->right; rhs = rhs->right; } } interval * fst = dummy->right; FOR(i, 1, mxN) { int diff = min(0LL, fst->getBegin(i)); sumaStartow -= fst->getSumaStartow(); fst->start -= diff; sumaStartow += fst->getSumaStartow(); fst->mergeRight(i); for (interval* inter : toUpdate[i]) { if (inter->deleted) { continue; } inter->mergeRight(i); if (inter->left != dummy) { int czas = mergeTime(*(inter->left), *inter); if (czas <= mxN && czas > i) toUpdate[czas].insert(inter->left); } if (inter->right != dummy) { int czas = mergeTime(*inter, *(inter->right)); if (czas <= mxN && czas > i) toUpdate[czas].insert(inter); } } toUpdate[i] = {}; wynik[i] = sumaStartow - sum + sumaMnoznikow * i; } REP(i, m) { int a; cin >> a; cout << wynik[a] << "\n"; } } |