#include <algorithm> #include <cstdio> #include <set> #include <vector> using namespace std; #define LL long long #define PB push_back #define GEN_LVLS 6 struct Block { LL s; // pozycja końcowa pierwszego elementu bloku LL n; // ilość elementów w bloku bool valid; }; struct Invalidate { int idx; LL K; }; struct Invalidate_cmp { bool operator()(const Invalidate& a, const Invalidate& b) { if (a.K != b.K) { return a.K < b.K; } return a.idx < b.idx; } }; int last_block; Block blocks[201000]; int b_next[201000]; // pozycja następnego poprawnego bloku set<Invalidate, Invalidate_cmp> invs; int N, M, MAX_D; int D[201000]; LL SUM_NS; // suma B.N * B.S po poprawnych blokach B LL SUM_NN; // suma B.N * (B.N - 1) po poprawnych blokach B LL SUM_T; // suma czasów przyjścia // results[K] = SUM_NS + K * SUM_NN / 2 - SUM_T; LL T[201000]; // czas przyjścia i-tego klienta (i € {1, …, N}) LL results[1001000]; void input(); void preprocessing(); void solve(); int main() { input(); preprocessing(); solve(); } void input() { if (scanf("%d %d", &N, &M)) {} T[0] = 0; // strażnik for (int i = 1; i <= N; ++i) { if (scanf("%lld", &T[i])) {} SUM_T += T[i]; } for (int i = 0; i < M; ++i) { if (scanf("%d", &D[i])) {} MAX_D = max(MAX_D, D[i]); } } Invalidate memoize_invalidation(int idx) { if (!blocks[idx].valid || b_next[idx] == idx) { Invalidate inv = { .idx = -1, .K = -1 }; return inv; } Block& P = blocks[idx]; Block& Q = blocks[b_next[idx]]; LL K = Q.s - P.s; if (K % P.n == 0) K = K / P.n; else K = K / P.n + 1; Invalidate inv = { .idx = idx, .K = K }; invs.insert(inv); return inv; } bool process_invalidation(Invalidate inv, LL K) { int idx = inv.idx; if (!blocks[idx].valid || b_next[idx] == idx) { return false; } Block& P = blocks[idx]; Block& Q = blocks[b_next[idx]]; LL lp_P = P.s + K * (P.n - 1); LL fp_Q = Q.s - K + 1; if (lp_P + 1 < fp_Q) { // nie powinno się zdarzyć return false; } if (b_next[b_next[idx]] == b_next[idx]) { b_next[idx] = idx; } else { b_next[idx] = b_next[b_next[idx]]; } Q.valid = false; SUM_NS -= Q.s * Q.n; SUM_NN -= Q.n * (Q.n - 1); SUM_NS -= P.s * P.n; SUM_NN -= P.n * (P.n - 1); P.n += Q.n; SUM_NS += P.s * P.n; SUM_NN += P.n * (P.n - 1); memoize_invalidation(idx); return true; } LL calculate_result(LL K) { return SUM_NS + K * SUM_NN / 2 - SUM_T; } void preprocessing() { // bloki dla K = 0: last_block = 0; //blocks[last_block] jest ostatni blocks[0] = { .s = 0, .n = 1, .valid = true}; b_next[0] = 0; for (int i = 1; i <= N; ++i) { if (T[i] == blocks[last_block].s) { blocks[last_block].n++; } else { last_block++; blocks[last_block] = { .s = T[i], .n = 1, .valid = true}; b_next[last_block - 1] = b_next[last_block] = last_block; } } for (int i = 0; i <= last_block; ++i) { SUM_NS += blocks[i].n * blocks[i].s; SUM_NN += blocks[i].n * (blocks[i].n - 1); if (i < last_block) { memoize_invalidation(i); } } /// for (int K = 1; K <= MAX_D; ++K) { int idx = 0; while (invs.size() > 0) { Invalidate inv = *(invs.begin()); if (inv.K > K) { break; } invs.erase(invs.begin()); process_invalidation(inv, K); } results[K] = calculate_result(K); } } void solve() { for (int i = 0; i < M; ++i) { printf("%lld\n", results[D[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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #include <algorithm> #include <cstdio> #include <set> #include <vector> using namespace std; #define LL long long #define PB push_back #define GEN_LVLS 6 struct Block { LL s; // pozycja końcowa pierwszego elementu bloku LL n; // ilość elementów w bloku bool valid; }; struct Invalidate { int idx; LL K; }; struct Invalidate_cmp { bool operator()(const Invalidate& a, const Invalidate& b) { if (a.K != b.K) { return a.K < b.K; } return a.idx < b.idx; } }; int last_block; Block blocks[201000]; int b_next[201000]; // pozycja następnego poprawnego bloku set<Invalidate, Invalidate_cmp> invs; int N, M, MAX_D; int D[201000]; LL SUM_NS; // suma B.N * B.S po poprawnych blokach B LL SUM_NN; // suma B.N * (B.N - 1) po poprawnych blokach B LL SUM_T; // suma czasów przyjścia // results[K] = SUM_NS + K * SUM_NN / 2 - SUM_T; LL T[201000]; // czas przyjścia i-tego klienta (i € {1, …, N}) LL results[1001000]; void input(); void preprocessing(); void solve(); int main() { input(); preprocessing(); solve(); } void input() { if (scanf("%d %d", &N, &M)) {} T[0] = 0; // strażnik for (int i = 1; i <= N; ++i) { if (scanf("%lld", &T[i])) {} SUM_T += T[i]; } for (int i = 0; i < M; ++i) { if (scanf("%d", &D[i])) {} MAX_D = max(MAX_D, D[i]); } } Invalidate memoize_invalidation(int idx) { if (!blocks[idx].valid || b_next[idx] == idx) { Invalidate inv = { .idx = -1, .K = -1 }; return inv; } Block& P = blocks[idx]; Block& Q = blocks[b_next[idx]]; LL K = Q.s - P.s; if (K % P.n == 0) K = K / P.n; else K = K / P.n + 1; Invalidate inv = { .idx = idx, .K = K }; invs.insert(inv); return inv; } bool process_invalidation(Invalidate inv, LL K) { int idx = inv.idx; if (!blocks[idx].valid || b_next[idx] == idx) { return false; } Block& P = blocks[idx]; Block& Q = blocks[b_next[idx]]; LL lp_P = P.s + K * (P.n - 1); LL fp_Q = Q.s - K + 1; if (lp_P + 1 < fp_Q) { // nie powinno się zdarzyć return false; } if (b_next[b_next[idx]] == b_next[idx]) { b_next[idx] = idx; } else { b_next[idx] = b_next[b_next[idx]]; } Q.valid = false; SUM_NS -= Q.s * Q.n; SUM_NN -= Q.n * (Q.n - 1); SUM_NS -= P.s * P.n; SUM_NN -= P.n * (P.n - 1); P.n += Q.n; SUM_NS += P.s * P.n; SUM_NN += P.n * (P.n - 1); memoize_invalidation(idx); return true; } LL calculate_result(LL K) { return SUM_NS + K * SUM_NN / 2 - SUM_T; } void preprocessing() { // bloki dla K = 0: last_block = 0; //blocks[last_block] jest ostatni blocks[0] = { .s = 0, .n = 1, .valid = true}; b_next[0] = 0; for (int i = 1; i <= N; ++i) { if (T[i] == blocks[last_block].s) { blocks[last_block].n++; } else { last_block++; blocks[last_block] = { .s = T[i], .n = 1, .valid = true}; b_next[last_block - 1] = b_next[last_block] = last_block; } } for (int i = 0; i <= last_block; ++i) { SUM_NS += blocks[i].n * blocks[i].s; SUM_NN += blocks[i].n * (blocks[i].n - 1); if (i < last_block) { memoize_invalidation(i); } } /// for (int K = 1; K <= MAX_D; ++K) { int idx = 0; while (invs.size() > 0) { Invalidate inv = *(invs.begin()); if (inv.K > K) { break; } invs.erase(invs.begin()); process_invalidation(inv, K); } results[K] = calculate_result(K); } } void solve() { for (int i = 0; i < M; ++i) { printf("%lld\n", results[D[i]]); } } |