lalalalalalalaalalla

Untitled

Jul 8th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ostream>
  4. #include <istream>
  5. #include <set>
  6. #include <unordered_set>
  7. #include <map>
  8. #include <unordered_map>
  9. #include <bitset>
  10. #include <vector>
  11. #include <string>
  12. #include <stack>
  13. #include <queue>
  14. #include <deque>
  15. #include <array>
  16. #include <algorithm>
  17. #include <functional>
  18. #include <cmath>
  19. #include <time.h>
  20. #include <random>
  21. #include <chrono>
  22. #include <cassert>
  23. #include <cstring>
  24. #include <limits>
  25.  
  26. using namespace std;
  27.  
  28. // #define int long long
  29. #define pb push_back
  30. #define ppb pop_back
  31. #define all(a) (a).begin(), (a).end()
  32. #define pii pair<int, int>
  33. #define ld long double
  34. #define ll long long
  35.  
  36. istream& operator>> (istream& in, pii& b) {
  37. in >> b.first >> b.second;
  38. return in;
  39. }
  40.  
  41. ostream& operator<< (ostream& out, const pii& b) {
  42. out << "{" << b.first << ", " << b.second << "}";
  43. return out;
  44. }
  45.  
  46. template<typename T> ostream& operator<< (ostream& out, const vector<T>& a) {
  47. for (auto k : a) out << k << " ";
  48. return out;
  49. }
  50.  
  51. template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
  52. template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
  53.  
  54. #ifdef LOCAL
  55. #define dbg(x) cout << #x << " : " << (x) << endl;
  56. const long long INF = 1e18;
  57. // const long long mod = 2600000069;
  58. // const long long p = 10;
  59. #else
  60. #define dbg(x) 57
  61. const long long INF = 1e18;
  62. // const long long mod = 2600000069;
  63. // const long long p = 179;
  64. #endif
  65.  
  66. const ld PI = 4 * atan(1);
  67.  
  68. #define time clock() / (double) CLOCKS_PER_SEC
  69.  
  70. #pragma GCC optimize("Ofast,no-stack-protector")
  71. // #pragma GCC target("sse,sse2,sse3,sse3,sse4")
  72. #pragma GCC optimize("unroll-loops")
  73. // #pragma GCC optimize("fast-math")
  74. // #pragma GCC target("avx2")
  75.  
  76. mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
  77.  
  78.  
  79. // /** Begin fast allocation */
  80. // const int MAX_MEM = 1e8;
  81. // int mpos = 0;
  82. // char mem[MAX_MEM];
  83. // inline void * operator new ( size_t n ) {
  84. // assert((mpos += n) <= MAX_MEM);
  85. // return (void *)(mem + mpos - n);
  86. // }
  87. // inline void operator delete ( void * ) noexcept { } // must have!
  88. // /** End fast allocation */
  89.  
  90. #define getchar_unlocked _getchar_nolock
  91.  
  92. inline ll readInt()
  93.  
  94. {
  95.  
  96. char x;
  97.  
  98. while (x = getchar_unlocked())
  99.  
  100. {
  101.  
  102. if (x >= '0' && x <= '9')
  103.  
  104. {
  105.  
  106. ll ans = x - '0';
  107.  
  108. while (x = getchar_unlocked())
  109.  
  110. {
  111.  
  112. if (!(x >= '0' && x <= '9')) return ans;
  113.  
  114. ans = ans * 10 + x - '0';
  115.  
  116. }
  117.  
  118. }
  119.  
  120. }
  121.  
  122. }
  123.  
  124. const int N = 2e6 + 10;
  125.  
  126. int n, Q;
  127. ll a[N];
  128. // vector<int> g[N];
  129. int g[N];
  130. int ptr1[N];
  131. int szz[N], ptr[N];
  132.  
  133. int ans;
  134. int path[N];
  135. int sz;
  136.  
  137. int q[N];
  138. int qsz;
  139.  
  140. bool f = 0;
  141.  
  142. unordered_map<int, int> wans;
  143.  
  144. void solve() {
  145. ll b;
  146. cin >> b;
  147. if (wans.count(b)) {
  148. cout << wans[b] << "\n";
  149. return;
  150. }
  151. int r = 0;
  152. ll sum = 0;
  153. for (int i = 0; i < n + n; ++i) {
  154. while (r < n + n - 1 && sum + a[r] <= b) {
  155. sum += a[r];
  156. r++;
  157. }
  158. if (r > i) {
  159. // g[r].pb(i);
  160. szz[r]++;
  161. }
  162. sum -= a[i];
  163. }
  164. sum = 0;
  165. for (int i = 0; i < n; i++) {
  166. sum += a[i];
  167. }
  168. int mn = (sum + b - 1) / b;
  169. sum = 0, r = 0;
  170. for (int i = 1; i < n + n; ++i) {
  171. ptr1[i] = ptr1[i - 1] + szz[i - 1];
  172. }
  173. ans = 0;
  174. int start = gen() % n;
  175. int rofl = start;
  176. for (int i = 0; i < n + n; ++i) {
  177. while (r < n + n - 1 && sum + a[r] <= b) {
  178. sum += a[r];
  179. r++;
  180. }
  181. if (rofl < start + n && rofl == i) {
  182. rofl = r;
  183. ans++;
  184. }
  185. if (r > i) {
  186. g[ptr1[r]++] = i;
  187. }
  188. sum -= a[i];
  189. }
  190. for (int i = 1; i < n + n; ++i) {
  191. ptr1[i] = ptr1[i - 1] + szz[i - 1];
  192. }
  193. qsz = 0;
  194. q[qsz++] = n + n - 1;
  195. sz = 0;
  196. while (qsz > 0 && ans > mn) {
  197. int v = q[qsz - 1];
  198. if (ptr[v] == 0) {
  199. path[sz++] = v;
  200. if (ans > mn && v < n) while (sz < ans || path[sz - ans] >= v + n) {ans--;}
  201. }
  202. if (ptr[v] < szz[v]) {
  203. q[qsz++] = g[ptr1[v] + ptr[v]];
  204. ptr[v]++;
  205. } else {
  206. sz--;
  207. qsz--;
  208. }
  209. }
  210. cout << ans << "\n";
  211. wans[b] = ans;
  212. for (int i = 0; i < n + n; ++i) {
  213. ptr[i] = szz[i] = 0;
  214. }
  215. }
  216.  
  217. signed main() {
  218. ios_base::sync_with_stdio(0);
  219. cin.tie(0);
  220. cout.tie(0);
  221. n = readInt();
  222. Q = readInt();
  223. for (int i = 0; i < n; i++) {
  224. a[i] = readInt();
  225. a[n + i] = a[i];
  226. }
  227. while (Q--) {
  228. solve();
  229. }
  230. }
  231. /*
  232.  
  233. */
  234.  
Advertisement
Add Comment
Please, Sign In to add comment