Submission #1010603

# Submission time Handle Problem Language Result Execution time Memory
1010603 2024-06-29T08:26:21 Z dozer Fish 3 (JOI24_fish3) C++14
9 / 100
833 ms 40532 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 500
#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;
		}
	}

	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 + 1 >= l && curr >= 0){
			if (last[r] < 0){
				return (int)-1;	
			} 
			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];
				if (gaps[r][pos] > cnt) res += gaps[r][pos] - cnt;
				r -= S;
			}

			if (curr < 0) return (int)-1;
		}
	
	//	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--;
		}

		//cout<<"-- "<<curr<<endl;
		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:84: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]
   84 |     if (pos == gaps[r].size()){
      |         ~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9820 KB Output is correct
2 Correct 4 ms 9820 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 13 ms 10076 KB Output is correct
5 Correct 21 ms 9816 KB Output is correct
6 Correct 13 ms 10020 KB Output is correct
7 Correct 13 ms 10072 KB Output is correct
8 Correct 20 ms 9820 KB Output is correct
9 Correct 20 ms 9820 KB Output is correct
10 Correct 11 ms 9820 KB Output is correct
11 Correct 20 ms 10044 KB Output is correct
12 Correct 12 ms 10044 KB Output is correct
13 Correct 12 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 35 ms 37968 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 833 ms 16984 KB Output is correct
2 Runtime error 46 ms 40532 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 37212 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9820 KB Output is correct
2 Correct 4 ms 9820 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 13 ms 10076 KB Output is correct
5 Correct 21 ms 9816 KB Output is correct
6 Correct 13 ms 10020 KB Output is correct
7 Correct 13 ms 10072 KB Output is correct
8 Correct 20 ms 9820 KB Output is correct
9 Correct 20 ms 9820 KB Output is correct
10 Correct 11 ms 9820 KB Output is correct
11 Correct 20 ms 10044 KB Output is correct
12 Correct 12 ms 10044 KB Output is correct
13 Correct 12 ms 9820 KB Output is correct
14 Runtime error 35 ms 37968 KB Execution killed with signal 11
15 Halted 0 ms 0 KB -