Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <ostream>
- #include <istream>
- #include <set>
- #include <unordered_set>
- #include <map>
- #include <unordered_map>
- #include <bitset>
- #include <vector>
- #include <string>
- #include <stack>
- #include <queue>
- #include <deque>
- #include <array>
- #include <algorithm>
- #include <functional>
- #include <cmath>
- #include <time.h>
- #include <random>
- #include <chrono>
- #include <cassert>
- #include <cstring>
- #include <limits>
- using namespace std;
- // #define int long long
- #define pb push_back
- #define ppb pop_back
- #define all(a) (a).begin(), (a).end()
- #define pii pair<int, int>
- #define ld long double
- #define ll long long
- istream& operator>> (istream& in, pii& b) {
- in >> b.first >> b.second;
- return in;
- }
- ostream& operator<< (ostream& out, const pii& b) {
- out << "{" << b.first << ", " << b.second << "}";
- return out;
- }
- template<typename T> ostream& operator<< (ostream& out, const vector<T>& a) {
- for (auto k : a) out << k << " ";
- return out;
- }
- template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
- template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
- #ifdef LOCAL
- #define dbg(x) cout << #x << " : " << (x) << endl;
- const long long INF = 1e18;
- // const long long mod = 2600000069;
- // const long long p = 10;
- #else
- #define dbg(x) 57
- const long long INF = 1e18;
- // const long long mod = 2600000069;
- // const long long p = 179;
- #endif
- const ld PI = 4 * atan(1);
- #define time clock() / (double) CLOCKS_PER_SEC
- #pragma GCC optimize("Ofast,no-stack-protector")
- // #pragma GCC target("sse,sse2,sse3,sse3,sse4")
- #pragma GCC optimize("unroll-loops")
- // #pragma GCC optimize("fast-math")
- // #pragma GCC target("avx2")
- mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
- // /** Begin fast allocation */
- // const int MAX_MEM = 1e8;
- // int mpos = 0;
- // char mem[MAX_MEM];
- // inline void * operator new ( size_t n ) {
- // assert((mpos += n) <= MAX_MEM);
- // return (void *)(mem + mpos - n);
- // }
- // inline void operator delete ( void * ) noexcept { } // must have!
- // /** End fast allocation */
- #define getchar_unlocked _getchar_nolock
- inline ll readInt()
- {
- char x;
- while (x = getchar_unlocked())
- {
- if (x >= '0' && x <= '9')
- {
- ll ans = x - '0';
- while (x = getchar_unlocked())
- {
- if (!(x >= '0' && x <= '9')) return ans;
- ans = ans * 10 + x - '0';
- }
- }
- }
- }
- const int N = 2e6 + 10;
- int n, Q;
- ll a[N];
- // vector<int> g[N];
- int g[N];
- int ptr1[N];
- int szz[N], ptr[N];
- int ans;
- int path[N];
- int sz;
- int q[N];
- int qsz;
- bool f = 0;
- unordered_map<int, int> wans;
- void solve() {
- ll b;
- cin >> b;
- if (wans.count(b)) {
- cout << wans[b] << "\n";
- return;
- }
- int r = 0;
- ll sum = 0;
- for (int i = 0; i < n + n; ++i) {
- while (r < n + n - 1 && sum + a[r] <= b) {
- sum += a[r];
- r++;
- }
- if (r > i) {
- // g[r].pb(i);
- szz[r]++;
- }
- sum -= a[i];
- }
- sum = 0;
- for (int i = 0; i < n; i++) {
- sum += a[i];
- }
- int mn = (sum + b - 1) / b;
- sum = 0, r = 0;
- for (int i = 1; i < n + n; ++i) {
- ptr1[i] = ptr1[i - 1] + szz[i - 1];
- }
- ans = 0;
- int start = gen() % n;
- int rofl = start;
- for (int i = 0; i < n + n; ++i) {
- while (r < n + n - 1 && sum + a[r] <= b) {
- sum += a[r];
- r++;
- }
- if (rofl < start + n && rofl == i) {
- rofl = r;
- ans++;
- }
- if (r > i) {
- g[ptr1[r]++] = i;
- }
- sum -= a[i];
- }
- for (int i = 1; i < n + n; ++i) {
- ptr1[i] = ptr1[i - 1] + szz[i - 1];
- }
- qsz = 0;
- q[qsz++] = n + n - 1;
- sz = 0;
- while (qsz > 0 && ans > mn) {
- int v = q[qsz - 1];
- if (ptr[v] == 0) {
- path[sz++] = v;
- if (ans > mn && v < n) while (sz < ans || path[sz - ans] >= v + n) {ans--;}
- }
- if (ptr[v] < szz[v]) {
- q[qsz++] = g[ptr1[v] + ptr[v]];
- ptr[v]++;
- } else {
- sz--;
- qsz--;
- }
- }
- cout << ans << "\n";
- wans[b] = ans;
- for (int i = 0; i < n + n; ++i) {
- ptr[i] = szz[i] = 0;
- }
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- n = readInt();
- Q = readInt();
- for (int i = 0; i < n; i++) {
- a[i] = readInt();
- a[n + i] = a[i];
- }
- while (Q--) {
- solve();
- }
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment