# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1010607 |
2024-06-29T08:26:59 Z |
dozer |
Fish 3 (JOI24_fish3) |
C++14 |
|
2000 ms |
42324 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 400005
#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 |
10 ms |
19084 KB |
Output is correct |
2 |
Correct |
9 ms |
19260 KB |
Output is correct |
3 |
Correct |
10 ms |
19036 KB |
Output is correct |
4 |
Correct |
19 ms |
19436 KB |
Output is correct |
5 |
Correct |
26 ms |
19440 KB |
Output is correct |
6 |
Correct |
20 ms |
19416 KB |
Output is correct |
7 |
Correct |
19 ms |
19376 KB |
Output is correct |
8 |
Correct |
25 ms |
19292 KB |
Output is correct |
9 |
Correct |
27 ms |
19412 KB |
Output is correct |
10 |
Correct |
17 ms |
19452 KB |
Output is correct |
11 |
Correct |
25 ms |
19444 KB |
Output is correct |
12 |
Correct |
16 ms |
19264 KB |
Output is correct |
13 |
Correct |
18 ms |
19464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1714 ms |
37144 KB |
Output is correct |
2 |
Correct |
1323 ms |
36688 KB |
Output is correct |
3 |
Correct |
1052 ms |
22832 KB |
Output is correct |
4 |
Execution timed out |
2032 ms |
31868 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
884 ms |
26196 KB |
Output is correct |
2 |
Execution timed out |
2047 ms |
42324 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1702 ms |
35288 KB |
Output is correct |
2 |
Execution timed out |
2050 ms |
37460 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19084 KB |
Output is correct |
2 |
Correct |
9 ms |
19260 KB |
Output is correct |
3 |
Correct |
10 ms |
19036 KB |
Output is correct |
4 |
Correct |
19 ms |
19436 KB |
Output is correct |
5 |
Correct |
26 ms |
19440 KB |
Output is correct |
6 |
Correct |
20 ms |
19416 KB |
Output is correct |
7 |
Correct |
19 ms |
19376 KB |
Output is correct |
8 |
Correct |
25 ms |
19292 KB |
Output is correct |
9 |
Correct |
27 ms |
19412 KB |
Output is correct |
10 |
Correct |
17 ms |
19452 KB |
Output is correct |
11 |
Correct |
25 ms |
19444 KB |
Output is correct |
12 |
Correct |
16 ms |
19264 KB |
Output is correct |
13 |
Correct |
18 ms |
19464 KB |
Output is correct |
14 |
Correct |
1714 ms |
37144 KB |
Output is correct |
15 |
Correct |
1323 ms |
36688 KB |
Output is correct |
16 |
Correct |
1052 ms |
22832 KB |
Output is correct |
17 |
Execution timed out |
2032 ms |
31868 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |