Submission #1010596

# Submission time Handle Problem Language Result Execution time Memory
1010596 2024-06-29T08:24:51 Z dozer Fish 3 (JOI24_fish3) C++14
9 / 100
578 ms 43116 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 3
#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 4 ms 9816 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 10092 KB Output is correct
5 Correct 5 ms 10072 KB Output is correct
6 Correct 10 ms 9960 KB Output is correct
7 Correct 4 ms 9920 KB Output is correct
8 Correct 5 ms 9820 KB Output is correct
9 Correct 7 ms 10032 KB Output is correct
10 Correct 11 ms 9860 KB Output is correct
11 Correct 6 ms 10072 KB Output is correct
12 Correct 6 ms 10076 KB Output is correct
13 Correct 6 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 46 ms 39764 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 578 ms 17112 KB Output is correct
2 Runtime error 56 ms 43116 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 34 ms 38992 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 4 ms 9820 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 13 ms 10092 KB Output is correct
5 Correct 5 ms 10072 KB Output is correct
6 Correct 10 ms 9960 KB Output is correct
7 Correct 4 ms 9920 KB Output is correct
8 Correct 5 ms 9820 KB Output is correct
9 Correct 7 ms 10032 KB Output is correct
10 Correct 11 ms 9860 KB Output is correct
11 Correct 6 ms 10072 KB Output is correct
12 Correct 6 ms 10076 KB Output is correct
13 Correct 6 ms 9984 KB Output is correct
14 Runtime error 46 ms 39764 KB Execution killed with signal 11
15 Halted 0 ms 0 KB -