## Submission #1084145

# Submission time Handle Problem Language Result Execution time Memory
1084145 2024-09-05T11:12:17 Z Mike_Vu Fish 3 (JOI24_fish3) C++14
100 / 100
235 ms 38048 KB
```#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dou;
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second

bool maximize(ll &x, ll y ){
if (x < y) {x = y; return true;};
return false;
}
bool minimize(ll &x, ll y){
if (x > y) {x = y; return true;}
return false;
}

struct BIT{
int n;
vector<ll> bit;
BIT(int _n = 0) {
n = _n;
bit.assign(n+1, 0);
}
void update(int k, ll x) {
while (k <= n) {
bit[k] += x;
k += k & (-k);
}
}
ll getsum(int k) {
ll res = 0;
while (k > 0) {
res += bit[k];
k -= k & (-k);
}
return res;
}
ll query(int l, int r) {
return getsum(r)-getsum(l-1);
}
} bitcost, bitsum;
struct query{
int l, r, id;
ll ans;
query(int u, int v, int i) {
l = u;
r = v;
id = i;
}
};
bool cmp1(query a, query b) {
return a.r < b.r;
}
bool cmp2(query a, query b) {
return a.id < b.id;
}

const int maxn = 3e5+5;
int n, q;
ll d, c[maxn], a[maxn];
vector<query> qu;

void input() {
cin >> n >> d;
c[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> c[i];
//manghieu
a[i] = c[i] - c[i-1];
}
cin >> q;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
qu.pb(query(l, r, i));
}
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
//input
input();
//init
bitsum = BIT(n);
bitcost = BIT(n);
sort(qu.begin(), qu.end(), cmp1);
//vector<cou+pos>
vector<pair<ll, int>> avai;
int j = 0;
for (int i = 1; i <= n; i++) {
if (a[i] >= d) {
avai.push_back({a[i]/d, i});
}
else if (a[i] < 0) {
ll x = (-a[i]-1)/d+1;
//need x
//bit update
bitsum.update(i, -x);
bitcost.update(i, (-x)*(n-i+1));
//fix x
while (!avai.empty() && x > 0) {
ll &cou = avai.back().first;
int pos = avai.back().se;
ll decval = min(cou, x);
cou -= decval;
x -= decval;
//bit
bitsum.update(pos, decval);
bitcost.update(pos, decval*(n-pos+1));
if (cou == 0) avai.pop_back();
}
}
//        for (int i = 1; i <= n; i++) {
//            cout << bitsum.query(i, i);
//        }
//        cout << "\n";
//        for (int i = 1; i <= n; i++) {
//            cout << bitcost.query(i, i);
//        }
//        cout << "\n\n";
//solve queries offline
while (j < q && qu[j].r == i) {
int l = qu[j].l, r = qu[j].r;
ll &ans = qu[j].ans;
++j;
ll sum = bitsum.query(l+1, r);
//start
ans = 0;
if (sum < 0) {
ll can = max(0LL, c[l]/d);
//need: -sum
if (can+sum<0) {
ans = -1;
continue;
}
ans += (-sum)*(r-l+1);
}
ans += bitcost.query(l+1, r)-sum*(n-r);
}
}
sort(qu.begin(), qu.end(), cmp2);
for (int i = 0; i < q; i++) {
cout << qu[i].ans << "\n";
}
}
/*
4 2
3 1 2 1
1
1 3

4 2
0 1 0 1
3
1 2
2 3
1 1

5 1
3 1 4 1 5
3
1 5
2 4
3 5

6 3
16 14 13 8 6 5
4
1 4
2 5
3 3
1 6
*/
```

#### Subtask #1 9.0 / 9.0

# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 772 KB Output is correct
13 Correct 3 ms 604 KB Output is correct

#### Subtask #2 7.0 / 7.0

# Verdict Execution time Memory Grader output
1 Correct 182 ms 22944 KB Output is correct
2 Correct 176 ms 22504 KB Output is correct
3 Correct 125 ms 15288 KB Output is correct
4 Correct 178 ms 22180 KB Output is correct
5 Correct 178 ms 22260 KB Output is correct
6 Correct 170 ms 21928 KB Output is correct
7 Correct 168 ms 22084 KB Output is correct

#### Subtask #3 28.0 / 28.0

# Verdict Execution time Memory Grader output
1 Correct 145 ms 15548 KB Output is correct
2 Correct 218 ms 29856 KB Output is correct
3 Correct 224 ms 29860 KB Output is correct
4 Correct 215 ms 29860 KB Output is correct
5 Correct 207 ms 29860 KB Output is correct
6 Correct 184 ms 23280 KB Output is correct
7 Correct 185 ms 23460 KB Output is correct
8 Correct 214 ms 26528 KB Output is correct
9 Correct 203 ms 26532 KB Output is correct
10 Correct 199 ms 37852 KB Output is correct
11 Correct 189 ms 37928 KB Output is correct
12 Correct 211 ms 32160 KB Output is correct
13 Correct 211 ms 32164 KB Output is correct

#### Subtask #4 20.0 / 20.0

# Verdict Execution time Memory Grader output
1 Correct 168 ms 21248 KB Output is correct
2 Correct 176 ms 23204 KB Output is correct
3 Correct 156 ms 17856 KB Output is correct
4 Correct 195 ms 26512 KB Output is correct
5 Correct 228 ms 28068 KB Output is correct
6 Correct 219 ms 29600 KB Output is correct
7 Correct 208 ms 26788 KB Output is correct
8 Correct 235 ms 29604 KB Output is correct
9 Correct 171 ms 22432 KB Output is correct
10 Correct 192 ms 22176 KB Output is correct
11 Correct 185 ms 26272 KB Output is correct
12 Correct 188 ms 25680 KB Output is correct
13 Correct 227 ms 29340 KB Output is correct
14 Correct 189 ms 25248 KB Output is correct
15 Correct 226 ms 29348 KB Output is correct
16 Correct 186 ms 25468 KB Output is correct

#### Subtask #5 36.0 / 36.0

# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 772 KB Output is correct
13 Correct 3 ms 604 KB Output is correct
14 Correct 182 ms 22944 KB Output is correct
15 Correct 176 ms 22504 KB Output is correct
16 Correct 125 ms 15288 KB Output is correct
17 Correct 178 ms 22180 KB Output is correct
18 Correct 178 ms 22260 KB Output is correct
19 Correct 170 ms 21928 KB Output is correct
20 Correct 168 ms 22084 KB Output is correct
21 Correct 145 ms 15548 KB Output is correct
22 Correct 218 ms 29856 KB Output is correct
23 Correct 224 ms 29860 KB Output is correct
24 Correct 215 ms 29860 KB Output is correct
25 Correct 207 ms 29860 KB Output is correct
26 Correct 184 ms 23280 KB Output is correct
27 Correct 185 ms 23460 KB Output is correct
28 Correct 214 ms 26528 KB Output is correct
29 Correct 203 ms 26532 KB Output is correct
30 Correct 199 ms 37852 KB Output is correct
31 Correct 189 ms 37928 KB Output is correct
32 Correct 211 ms 32160 KB Output is correct
33 Correct 211 ms 32164 KB Output is correct
34 Correct 168 ms 21248 KB Output is correct
35 Correct 176 ms 23204 KB Output is correct
36 Correct 156 ms 17856 KB Output is correct
37 Correct 195 ms 26512 KB Output is correct
38 Correct 228 ms 28068 KB Output is correct
39 Correct 219 ms 29600 KB Output is correct
40 Correct 208 ms 26788 KB Output is correct
41 Correct 235 ms 29604 KB Output is correct
42 Correct 171 ms 22432 KB Output is correct
43 Correct 192 ms 22176 KB Output is correct
44 Correct 185 ms 26272 KB Output is correct
45 Correct 188 ms 25680 KB Output is correct
46 Correct 227 ms 29340 KB Output is correct
47 Correct 189 ms 25248 KB Output is correct
48 Correct 226 ms 29348 KB Output is correct
49 Correct 186 ms 25468 KB Output is correct
50 Correct 215 ms 29732 KB Output is correct
51 Correct 185 ms 23460 KB Output is correct
52 Correct 219 ms 26784 KB Output is correct
53 Correct 184 ms 38048 KB Output is correct
54 Correct 203 ms 32236 KB Output is correct
55 Correct 210 ms 29668 KB Output is correct
56 Correct 185 ms 25252 KB Output is correct
57 Correct 172 ms 22176 KB Output is correct
58 Correct 167 ms 22168 KB Output is correct
59 Correct 204 ms 27556 KB Output is correct
60 Correct 189 ms 25328 KB Output is correct
61 Correct 184 ms 38048 KB Output is correct
62 Correct 177 ms 25124 KB Output is correct
63 Correct 200 ms 31908 KB Output is correct
64 Correct 186 ms 25252 KB Output is correct