// Skarbonka.cpp : Defines the entry point for the console application. // //#include "stdafx.h" #include <cassert> #include <iostream> #include <istream> #include <fstream> #include <algorithm> #include <cstdio> #include <complex> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> #include <string> #include <cstdlib> #include <memory> #include <ctime> #include <bitset> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <bitset> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define REP(i,n) for(int i = 0; i < (n); ++i) #define REPD(i, a, b) for(int i = (a); i > (b); i--) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define LL long long #define ULL unsigned long long #define PII pair<int,int> #define VI vector<int> #define VLL vector<long long> #define VULL vector<unsigned long long> #define VPII vector<pair<int,int> > #define PLL pair<long long,long long> #define VPLL vector<pair<long long,long long> > #define F first #define S second #define forIt(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define forRev(it, a) for(__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++) #define tr(container, it) for (typeof(container.begin()) it = container.begin(); it != container.end(); it++) #define present(container, element) (container.find(element) != container.end()) #define ft(a) __typeof((a).begin()) #define ld long double #define fi first #define se second #define mk make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define bitcount(n) __builtin_popcountll(n) #define pii pair<int, int> using namespace std; #define SORT(v) sort((v).begin(),(v).end()) #define UN(v) SORT(v),(v).erase(unique((v).begin(),(v).end()),(v).end()) const int MOD = 1e9 + 7; const int SIZE = 1e6 + 10; const double EPS = 1e-12; const double PI = acos(-1); class Pair { public: LL val, pos, res; //val == size static bool compPos(const Pair &p1, const Pair &p2) { return p1.pos < p2.pos; } static bool compVal(const Pair &p1, const Pair &p2) { return p1.val < p2.val; } }; class Zapiekanki { private: int n; int m; static const int N = 200001; static const int M = 200001; static const int D = 1000001; LL t[N + 1]; LL d[M + 1]; LL vals[D + 1]; vector<Pair> vec; public: void Read() { cin >> n >> m; std::fill_n(t, n+1, 0); std::fill_n(d, m+1, 0); for (int i = 0; i < n; i++) { cin >> t[i]; } for (int i = 0; i < m; i++) { cin >> d[i]; Pair pair; pair.val = d[i]; pair.pos = i; pair.res = -1; vec.push_back(pair); } } LL Calc(LL size) { LL sum = 0; if (vals[size] == -1) { LL last = 0; for (int j = 0; j < n; j++) { if (t[j] - size >= last) { //no wait last = t[j]; } else { last += size; LL wait = last - t[j]; sum = sum + wait; } } vals[size] = sum; } return vals[size];//memoization } void Fill(int begin, int end) { if (begin < end) { if (vec.at(begin).res == -1) { vec.at(begin).res = Calc(vec.at(begin).val); } if (vec.at(end).res == -1) { vec.at(end).res = Calc(vec.at(end).val); } if (vec.at(begin).res == vec.at(end).res) { for (int i = begin + 1; i < end; i++) { vec.at(i).res = vec.at(begin).res; } } else { //b.res < e.res if (begin + 1 < end) { int middle = (int)((begin + end) / 2); Fill(begin, middle); Fill(middle + 1, end); } //else fine } } else { //begin == end if (vec.at(begin).res == -1) { vec.at(begin).res = Calc(vec.at(begin).val); } } } void Solve() { std::fill_n(vals, D+1, -1); sort(vec.begin(), vec.end(), Pair::compVal); int begin = 0; int end = vec.size(); Fill(0, m-1); sort(vec.begin(), vec.end(), Pair::compPos); for (int i = 0; i < m; i++) { cout << vec.at(i).res << endl; } } }; int main()//int argc, _TCHAR* argv[]) { Zapiekanki *p = new Zapiekanki(); p->Read(); p->Solve(); delete p; 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 | // Skarbonka.cpp : Defines the entry point for the console application. // //#include "stdafx.h" #include <cassert> #include <iostream> #include <istream> #include <fstream> #include <algorithm> #include <cstdio> #include <complex> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> #include <string> #include <cstdlib> #include <memory> #include <ctime> #include <bitset> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <bitset> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define REP(i,n) for(int i = 0; i < (n); ++i) #define REPD(i, a, b) for(int i = (a); i > (b); i--) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define LL long long #define ULL unsigned long long #define PII pair<int,int> #define VI vector<int> #define VLL vector<long long> #define VULL vector<unsigned long long> #define VPII vector<pair<int,int> > #define PLL pair<long long,long long> #define VPLL vector<pair<long long,long long> > #define F first #define S second #define forIt(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define forRev(it, a) for(__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++) #define tr(container, it) for (typeof(container.begin()) it = container.begin(); it != container.end(); it++) #define present(container, element) (container.find(element) != container.end()) #define ft(a) __typeof((a).begin()) #define ld long double #define fi first #define se second #define mk make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define bitcount(n) __builtin_popcountll(n) #define pii pair<int, int> using namespace std; #define SORT(v) sort((v).begin(),(v).end()) #define UN(v) SORT(v),(v).erase(unique((v).begin(),(v).end()),(v).end()) const int MOD = 1e9 + 7; const int SIZE = 1e6 + 10; const double EPS = 1e-12; const double PI = acos(-1); class Pair { public: LL val, pos, res; //val == size static bool compPos(const Pair &p1, const Pair &p2) { return p1.pos < p2.pos; } static bool compVal(const Pair &p1, const Pair &p2) { return p1.val < p2.val; } }; class Zapiekanki { private: int n; int m; static const int N = 200001; static const int M = 200001; static const int D = 1000001; LL t[N + 1]; LL d[M + 1]; LL vals[D + 1]; vector<Pair> vec; public: void Read() { cin >> n >> m; std::fill_n(t, n+1, 0); std::fill_n(d, m+1, 0); for (int i = 0; i < n; i++) { cin >> t[i]; } for (int i = 0; i < m; i++) { cin >> d[i]; Pair pair; pair.val = d[i]; pair.pos = i; pair.res = -1; vec.push_back(pair); } } LL Calc(LL size) { LL sum = 0; if (vals[size] == -1) { LL last = 0; for (int j = 0; j < n; j++) { if (t[j] - size >= last) { //no wait last = t[j]; } else { last += size; LL wait = last - t[j]; sum = sum + wait; } } vals[size] = sum; } return vals[size];//memoization } void Fill(int begin, int end) { if (begin < end) { if (vec.at(begin).res == -1) { vec.at(begin).res = Calc(vec.at(begin).val); } if (vec.at(end).res == -1) { vec.at(end).res = Calc(vec.at(end).val); } if (vec.at(begin).res == vec.at(end).res) { for (int i = begin + 1; i < end; i++) { vec.at(i).res = vec.at(begin).res; } } else { //b.res < e.res if (begin + 1 < end) { int middle = (int)((begin + end) / 2); Fill(begin, middle); Fill(middle + 1, end); } //else fine } } else { //begin == end if (vec.at(begin).res == -1) { vec.at(begin).res = Calc(vec.at(begin).val); } } } void Solve() { std::fill_n(vals, D+1, -1); sort(vec.begin(), vec.end(), Pair::compVal); int begin = 0; int end = vec.size(); Fill(0, m-1); sort(vec.begin(), vec.end(), Pair::compPos); for (int i = 0; i < m; i++) { cout << vec.at(i).res << endl; } } }; int main()//int argc, _TCHAR* argv[]) { Zapiekanki *p = new Zapiekanki(); p->Read(); p->Solve(); delete p; return 0; } |