#include <cstdio> #include <vector> #include <set> #include <algorithm> #define MAX 1000009 using namespace std; using type = long long; int n, m; void readData(); void printResult(); void solve(); void moveAllChunksUp(type newStep); void removeChunk(type i); void addChunk(type i); void insertEvent(type elem, type step); type removeEvent(type elem); bool addEvent(type elem); type getMatchStep(type f); void manageEvent(type elem, type step, bool insert); void eraseEvent(type elem, type step); void initChunk(int i); set<type> activeChunks, wantedValues; vector<set<type>> events; type chunks[MAX], size[MAX], start[MAX], nextChunk[MAX], parent[MAX]; type t[MAX], d[MAX], results[MAX], chunkValues[MAX]; type currentSum, currentStep, maxStep; type find(type a) { if (a != parent[a]) { parent[a] = find(parent[a]); } return parent[a]; } type getChunkValue(type i) { return i * (i + 1) / 2; } type merge(type a) { a = find(a); type b = find(nextChunk[a]); parent[b] = a; removeEvent(a); removeEvent(b); type sA = size[a], sB = size[b]; removeChunk(sB); removeChunk(sA); nextChunk[a] = nextChunk[b]; addChunk(sA + sB); size[a] = sA + sB; currentSum -= (start[b] - start[a]) * sB; if (nextChunk[b] <= n) addEvent(a); } bool addEvent(type elem) { type step = getMatchStep(elem); insertEvent(elem, step); } type getMatchStep(type f) { type siz = size[f]; type start = t[f] + chunkValues[siz]; type end = t[nextChunk[f]]; type dist = end - start; return currentStep + (dist - currentStep) / siz; } type removeEvent(type elem) { type step = getMatchStep(elem); eraseEvent(elem, step); return step; } int main() { readData(); solve(); printResult(); } void solve() { for (type step : wantedValues) { moveAllChunksUp(step); currentStep = step; while (!events[step].empty()) { set<type> currentEvents = events[step]; events[step].clear(); for (type event : currentEvents) { merge(event); } } results[step] = currentSum; } } void printResult() { for (int i=0; i<m; i++) { printf("%lld\n", results[d[i]]); } } void readData() { scanf("%d%d", &n, &m); for (int i=1; i<=n; i++) { scanf("%lld", t + i); chunkValues[i] = getChunkValue(i-1); parent[i] = i; initChunk(i); } chunkValues[n+1] = getChunkValue(n); initChunk(0); for (int i=0; i<m; i++) { scanf("%lld", d + i); wantedValues.insert(d[i]); maxStep = max(maxStep, d[i]); } events = vector<set<type>>(maxStep + 1); for (int i=0; i<n; i++) addEvent(i); } void initChunk(int i) { size[i] = 1; start[i] = t[i]; nextChunk[i] = i+1; addChunk(0); } void addChunk(type i) { if (!chunks[i]) { activeChunks.insert(i); } currentSum += chunkValues[i] * currentStep; chunks[i]++; } void removeChunk(type i) { chunks[i]--; currentSum -= chunkValues[i] * currentStep; if (!chunks[i]) { activeChunks.erase(i); } } void moveAllChunksUp(type newStep) { for (type v : activeChunks) { currentSum += chunkValues[v] * chunks[v] * (newStep - currentStep); } } void insertEvent(type elem, type step) { manageEvent(elem, step, true); } void eraseEvent(type elem, type step) { manageEvent(elem, step, false); } void manageEvent(type elem, type step, bool insert) { if (step >= maxStep) return; auto newStep = wantedValues.lower_bound(step); if (newStep != wantedValues.end()) { step = *newStep; } if (insert) events[step].insert(elem); else events[step].erase(elem); }
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 | #include <cstdio> #include <vector> #include <set> #include <algorithm> #define MAX 1000009 using namespace std; using type = long long; int n, m; void readData(); void printResult(); void solve(); void moveAllChunksUp(type newStep); void removeChunk(type i); void addChunk(type i); void insertEvent(type elem, type step); type removeEvent(type elem); bool addEvent(type elem); type getMatchStep(type f); void manageEvent(type elem, type step, bool insert); void eraseEvent(type elem, type step); void initChunk(int i); set<type> activeChunks, wantedValues; vector<set<type>> events; type chunks[MAX], size[MAX], start[MAX], nextChunk[MAX], parent[MAX]; type t[MAX], d[MAX], results[MAX], chunkValues[MAX]; type currentSum, currentStep, maxStep; type find(type a) { if (a != parent[a]) { parent[a] = find(parent[a]); } return parent[a]; } type getChunkValue(type i) { return i * (i + 1) / 2; } type merge(type a) { a = find(a); type b = find(nextChunk[a]); parent[b] = a; removeEvent(a); removeEvent(b); type sA = size[a], sB = size[b]; removeChunk(sB); removeChunk(sA); nextChunk[a] = nextChunk[b]; addChunk(sA + sB); size[a] = sA + sB; currentSum -= (start[b] - start[a]) * sB; if (nextChunk[b] <= n) addEvent(a); } bool addEvent(type elem) { type step = getMatchStep(elem); insertEvent(elem, step); } type getMatchStep(type f) { type siz = size[f]; type start = t[f] + chunkValues[siz]; type end = t[nextChunk[f]]; type dist = end - start; return currentStep + (dist - currentStep) / siz; } type removeEvent(type elem) { type step = getMatchStep(elem); eraseEvent(elem, step); return step; } int main() { readData(); solve(); printResult(); } void solve() { for (type step : wantedValues) { moveAllChunksUp(step); currentStep = step; while (!events[step].empty()) { set<type> currentEvents = events[step]; events[step].clear(); for (type event : currentEvents) { merge(event); } } results[step] = currentSum; } } void printResult() { for (int i=0; i<m; i++) { printf("%lld\n", results[d[i]]); } } void readData() { scanf("%d%d", &n, &m); for (int i=1; i<=n; i++) { scanf("%lld", t + i); chunkValues[i] = getChunkValue(i-1); parent[i] = i; initChunk(i); } chunkValues[n+1] = getChunkValue(n); initChunk(0); for (int i=0; i<m; i++) { scanf("%lld", d + i); wantedValues.insert(d[i]); maxStep = max(maxStep, d[i]); } events = vector<set<type>>(maxStep + 1); for (int i=0; i<n; i++) addEvent(i); } void initChunk(int i) { size[i] = 1; start[i] = t[i]; nextChunk[i] = i+1; addChunk(0); } void addChunk(type i) { if (!chunks[i]) { activeChunks.insert(i); } currentSum += chunkValues[i] * currentStep; chunks[i]++; } void removeChunk(type i) { chunks[i]--; currentSum -= chunkValues[i] * currentStep; if (!chunks[i]) { activeChunks.erase(i); } } void moveAllChunksUp(type newStep) { for (type v : activeChunks) { currentSum += chunkValues[v] * chunks[v] * (newStep - currentStep); } } void insertEvent(type elem, type step) { manageEvent(elem, step, true); } void eraseEvent(type elem, type step) { manageEvent(elem, step, false); } void manageEvent(type elem, type step, bool insert) { if (step >= maxStep) return; auto newStep = wantedValues.lower_bound(step); if (newStep != wantedValues.end()) { step = *newStep; } if (insert) events[step].insert(elem); else events[step].erase(elem); } |