Submission #1010553

# Submission time Handle Problem Language Result Execution time Memory
1010553 2024-06-29T08:02:44 Z dozer Fish 3 (JOI24_fish3) C++14
0 / 100
634 ms 43216 KB
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n";
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 200005
#define LL node * 2
#define RR node * 2 + 1
#define mid (l + r) / 2
#define LOGN 20
#define S 2
#define int long long

const int modulo = 1e9 + 7;
const long long INF = 2e18 + 7;

vector<int> gaps[N], pre[N];
int last[N], ans[N];

int32_t main()
{
	fastio();


	int n, d;
	cin>>n>>d;
	vector<int> arr(n + 5, 0);
	for (int i = 1; i <= n; i++)
		cin>>arr[i];

	for (int i = 1; i <= n; i++){
		if (i % S == 0){
			int curr = arr[i];
			for (int j = i - 1; j > i - S; j--){
				int diff = max((int)0, arr[j] - curr);
				int cnt = (diff + d - 1) / d;
				ans[i] += cnt;
				int tmp = arr[j] - cnt * d;
				int gap = (curr - tmp) / d;
				if (gaps[i].empty()) gaps[i].pb(gap);
				else gaps[i].pb(gaps[i].back() + gap);
				if (pre[i].empty()) pre[i].pb(gaps[i].back());
				else pre[i].pb(pre[i].back() + gaps[i].back());
				curr = tmp;
			}
			last[i] = curr;
			reverse(gaps[i].begin(), gaps[i].end());
		}
	}

	auto solve = [&](int l, int r){
		int curr = arr[r];
		int res = 0;
		while(r >= l && r % S != 0){
			int diff = max((int)0, arr[r] - curr);
			int cnt = (diff + d - 1) / d;
			int tmp = arr[r] - d * cnt;
			res += cnt;
			curr = tmp;
			r--;
		}

		//cout<<r<<sp<<res<<endl;
		while(r - S >= l){
			res += ans[r];
			if (curr >= arr[r]){
				curr = last[r];
				r -= S;
			}
			else{
				int diff = arr[r] - curr;
				int cnt = (diff + d - 1) / d;
				res += cnt;
				int pos = lower_bound(gaps[r].begin(), gaps[r].end(), cnt) - gaps[r].begin();
				curr = last[r];

				if (pos == gaps[r].size()){
					pos--;
					curr -= (cnt - gaps[r][pos]) * d;
				}
				int sz = pos + 1;
				res += cnt * sz;
				res -= pre[r][pos];
				r -= S;
			}
		}

		//cout<<r<<sp<<res<<sp<<curr<<endl;
		while(r >= l){
			int diff = max((int)0, arr[r] - curr);
			int cnt = (diff + d - 1) / d;
			res += cnt;
			curr = arr[r] - cnt * d;
			r--;
		}

		if (curr < 0) return (int)-1;
		return res;
	};


	int q;
	cin>>q;
	while(q--){
		int l, r;
		cin>>l>>r;
		cout<<solve(l, r)<<endl;
	}

	cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " seconds\n";
}

Compilation message

Main.cpp: In lambda function:
Main.cpp:81:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     if (pos == gaps[r].size()){
      |         ~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 3 ms 9820 KB Output is correct
3 Correct 4 ms 9828 KB Output is correct
4 Incorrect 15 ms 9956 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 33 ms 43216 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 634 ms 14616 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 33 ms 42576 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 3 ms 9820 KB Output is correct
3 Correct 4 ms 9828 KB Output is correct
4 Incorrect 15 ms 9956 KB Output isn't correct
5 Halted 0 ms 0 KB -