#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <vector> #include <list> #include <set> #include <map> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PI; typedef vector<PI> VPI; typedef long long LL; typedef vector<LL> VLL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define ST first #define ND second const int INF = 1000000001; const double EPS = 10e-9; class Mowing { public: int beginning; int end; LL day; LL height; Mowing() { beginning = 0; end = 0; day = 0; height = 0; } Mowing(int size) : Mowing() { end = size; } Mowing(int p, int q, LL d, LL h) { beginning = p; end = q; day = d; height = h; } bool contains(int position) { return position >= beginning && position <= end; } int size() { return end - beginning + 1; } }; class Field { private: int n; vector<Mowing> mowings; vector<int> grass; vector<LL> grassSum; int findMowingBeginning(LL day, LL height) { int position = 0, count = n; while (count > 0) { int step = count / 2; LL grassHeight = computeGrassHeight(position + step, day); if (height >= grassHeight) { position += step + 1; count -= step + 1; } else { count = step; } } return position; } Mowing findMowing(int position) { int p = 0, q = mowings.size() - 1, s; while (p < q) { s = (p + q) / 2; Mowing mowing = mowings[s]; if (mowing.contains(position)) { return mowing; } else if (mowing.beginning > position) { q = s - 1; } else { p = s + 1; } } return mowings[p]; } void addMowing(int beginning, LL day, LL height) { while (!mowings.empty()) { Mowing last = mowings.back(); mowings.pop_back(); if (last.beginning < beginning) { last.end = beginning - 1; mowings.push_back(last); break; } } mowings.push_back(Mowing(beginning, n - 1, day, height)); } LL computeGrassHeight(int position, LL day) { Mowing mowing = findMowing(position); return mowing.height + grass[position] * (day - mowing.day); } LL growthInInterval(int beginning, int end) { LL valBeginning = beginning == 0 ? 0 : grassSum[beginning - 1]; return grassSum[end] - valBeginning; } LL computeMowingWeight(Mowing mowing, LL day, LL height) { int fieldSize = mowing.size(); LL oldMass = fieldSize * mowing.height; LL growth = growthInInterval(mowing.beginning, mowing.end) * (day - mowing.day); LL newMass = oldMass + growth; LL weight = newMass - height * fieldSize; return weight; } LL computeMowingWeight(int mowingBeginning, LL day, LL height) { LL result = 0; int index = mowings.size() - 1; while (index >= 0) { Mowing mowing = mowings[index--]; if (mowing.end < mowingBeginning) { break; } if (mowing.beginning < mowingBeginning) { result += computeMowingWeight(Mowing(mowingBeginning, mowing.end, mowing.day, mowing.height), day, height); return result; } else { result += computeMowingWeight(mowing, day, height); } } return result; } public: Field(vector<int>& vect) { this->grass = vect; n = grass.size(); sort(ALL(grass)); mowings.push_back(Mowing(n - 1)); grassSum.reserve(n); grassSum.push_back(grass[0]); for (int i = 1; i < n; ++i) { grassSum.push_back(grassSum.back() + grass[i]); } } LL mow(LL day, LL height) { int mowingBeginning = findMowingBeginning(day, height); if (mowingBeginning == n) { return 0; } else { LL growth = computeMowingWeight(mowingBeginning, day, height); addMowing(mowingBeginning, day, height); return growth; } } }; int main() { int n, m; scanf("%d%d", &n, &m); vector<int> grass(n); for (int i = 0; i < n; ++i) { scanf("%d", &grass[i]); } Field field(grass); for (int i = 0; i < m; ++i) { LL d, h; scanf("%lld %lld", &d, &h); printf("%lld\n", field.mow(d, h)); } 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 231 232 233 234 235 236 | #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <vector> #include <list> #include <set> #include <map> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PI; typedef vector<PI> VPI; typedef long long LL; typedef vector<LL> VLL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define ST first #define ND second const int INF = 1000000001; const double EPS = 10e-9; class Mowing { public: int beginning; int end; LL day; LL height; Mowing() { beginning = 0; end = 0; day = 0; height = 0; } Mowing(int size) : Mowing() { end = size; } Mowing(int p, int q, LL d, LL h) { beginning = p; end = q; day = d; height = h; } bool contains(int position) { return position >= beginning && position <= end; } int size() { return end - beginning + 1; } }; class Field { private: int n; vector<Mowing> mowings; vector<int> grass; vector<LL> grassSum; int findMowingBeginning(LL day, LL height) { int position = 0, count = n; while (count > 0) { int step = count / 2; LL grassHeight = computeGrassHeight(position + step, day); if (height >= grassHeight) { position += step + 1; count -= step + 1; } else { count = step; } } return position; } Mowing findMowing(int position) { int p = 0, q = mowings.size() - 1, s; while (p < q) { s = (p + q) / 2; Mowing mowing = mowings[s]; if (mowing.contains(position)) { return mowing; } else if (mowing.beginning > position) { q = s - 1; } else { p = s + 1; } } return mowings[p]; } void addMowing(int beginning, LL day, LL height) { while (!mowings.empty()) { Mowing last = mowings.back(); mowings.pop_back(); if (last.beginning < beginning) { last.end = beginning - 1; mowings.push_back(last); break; } } mowings.push_back(Mowing(beginning, n - 1, day, height)); } LL computeGrassHeight(int position, LL day) { Mowing mowing = findMowing(position); return mowing.height + grass[position] * (day - mowing.day); } LL growthInInterval(int beginning, int end) { LL valBeginning = beginning == 0 ? 0 : grassSum[beginning - 1]; return grassSum[end] - valBeginning; } LL computeMowingWeight(Mowing mowing, LL day, LL height) { int fieldSize = mowing.size(); LL oldMass = fieldSize * mowing.height; LL growth = growthInInterval(mowing.beginning, mowing.end) * (day - mowing.day); LL newMass = oldMass + growth; LL weight = newMass - height * fieldSize; return weight; } LL computeMowingWeight(int mowingBeginning, LL day, LL height) { LL result = 0; int index = mowings.size() - 1; while (index >= 0) { Mowing mowing = mowings[index--]; if (mowing.end < mowingBeginning) { break; } if (mowing.beginning < mowingBeginning) { result += computeMowingWeight(Mowing(mowingBeginning, mowing.end, mowing.day, mowing.height), day, height); return result; } else { result += computeMowingWeight(mowing, day, height); } } return result; } public: Field(vector<int>& vect) { this->grass = vect; n = grass.size(); sort(ALL(grass)); mowings.push_back(Mowing(n - 1)); grassSum.reserve(n); grassSum.push_back(grass[0]); for (int i = 1; i < n; ++i) { grassSum.push_back(grassSum.back() + grass[i]); } } LL mow(LL day, LL height) { int mowingBeginning = findMowingBeginning(day, height); if (mowingBeginning == n) { return 0; } else { LL growth = computeMowingWeight(mowingBeginning, day, height); addMowing(mowingBeginning, day, height); return growth; } } }; int main() { int n, m; scanf("%d%d", &n, &m); vector<int> grass(n); for (int i = 0; i < n; ++i) { scanf("%d", &grass[i]); } Field field(grass); for (int i = 0; i < m; ++i) { LL d, h; scanf("%lld %lld", &d, &h); printf("%lld\n", field.mow(d, h)); } return 0; } |