#include <bits/stdc++.h>
#include "nile.h"
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vll>;
const ll INF = 1e9;
vll EMPTY = {};
vll a, b, w;
struct Sum {
ll n;
vll sum;
//Sum();
Sum(vll a) :n(a.size()), sum(n + 1) {
for (ll i = 0; i < n; i++) {
sum[i + 1] = sum[i] + a[i];
}
}
ll qu(ll l, ll r) {
return sum[r] - sum[l];
}
};
struct Seg {
ll n;
vll a;
//Seg();
Seg(vll& v) :n(v.size()), a(2 * n) {
for (ll i = 0; i < n; i++) {
a[i + n] = v[i];
}
for (ll i = n - 1; i > 0; i--) {
a[i] = max(a[i * 2], a[i * 2 + 1]);
}
}
void up(ll i, ll x) {
i += n;
a[i] = x;
while (i > 1) {
i /= 2;
a[i] = max(a[i * 2], a[i * 2 + 1]);
}
}
ll qu(ll l, ll r) {
ll ans = -INF;
for (l += n, r += n; l < r; l /= 2, r /= 2) {
if (l % 2) {
ans = max(ans,a[l++]);
}
if (r % 2) {
ans = max(ans,a[--r]);
}
}
return ans;
}
};
Sum sumall(EMPTY);
Seg segall(EMPTY), segeven(EMPTY), segodd(EMPTY);
ll calc(ll l, ll r) {
if ((r - l) % 2 == 0) {
return sumall.qu(l, r);
}
if (l % 2 == 0) {
return sumall.qu(l, r) - max(segall.qu(l, r), segeven.qu(l, r));
}
return sumall.qu(l, r) - max(segall.qu(l, r), segodd.qu(l, r));
}
std::vector<long long> calculate_costs(std::vector<int> W, std::vector<int> A, std::vector<int> B, std::vector<int> E) {
ll n = A.size(), q = E.size();
vector<pair<ll, pll>> ord(n);
for (ll i = 0; i < n; i++) {
ord[i] = { W[i],{A[i],B[i]} };
}
sort(ord.begin(), ord.end());
//a = A, b = B, w = W; in correct order:
a = b = w = vll(n);
for (ll i = 0; i < n; i++) {
w[i] = ord[i].first;
a[i] = ord[i].second.first;
b[i] = ord[i].second.second;
}
//setup rmq rsq:
sumall = Sum(b);
vll temp = b, rem;
for (ll i = 0; i < n; i++) {
temp[i] -= a[i];
}
rem = temp;
segall = Seg(temp);
for (ll i = 0; i < n; i += 2) {
temp[i] = -INF;
}
segodd = Seg(temp);
temp = rem;
for (ll i = 1; i < n; i += 2) {
temp[i] = -INF;
}
segeven = Seg(temp);
temp = rem;
//setup ev:
vector<pair<ll, pll>> ev;// {len,{type,inx}}. type: 0 - query,1 - next disconnect,2 - disconnect two steps
for (ll i = 0; i < q; i++) {
ev.push_back({ E[i]+1,{0,i} });//?
}
for (ll i = 0; i < n - 1; i++) {
ev.push_back({ w[i + 1] - w[i],{1,i} });//+1?
}
for (ll i = 0; i < n - 2; i++) {
ev.push_back({ w[i + 2] - w[i],{2,i} });//+1?
}
sort(ev.begin(), ev.end());
reverse(ev.begin(), ev.end());
// solve:
vll ans(q);
ll sum = calc(0, n);
set<pll> segs = { { 0ll,n } };
for (auto k : ev) {
ll type = k.second.first, inx = k.second.second;
if (type == 0) {
ans[inx] = sum;
}
if (type == 1) {
auto p = segs.upper_bound({ inx,INF });
p--;
ll l = (*p).first, r = (*p).second;
sum -= calc(l, r);
segs.erase(p);
segs.insert({ l, inx + 1 });
segs.insert({inx + 1, r});
sum += calc( l, inx + 1 );
sum += calc( inx + 1, r );
}
if (type == 2) {
auto p = segs.upper_bound({ inx,INF });
p--;
ll l = (*p).first, r = (*p).second;
sum -= calc(l, r);
segall.up(inx+1, -INF);//?+1
sum += calc(l, r);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1020 KB |
Output is correct |
2 |
Correct |
2 ms |
960 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
29356 KB |
Output is correct |
2 |
Correct |
120 ms |
29412 KB |
Output is correct |
3 |
Correct |
115 ms |
29216 KB |
Output is correct |
4 |
Correct |
109 ms |
29296 KB |
Output is correct |
5 |
Correct |
117 ms |
29232 KB |
Output is correct |
6 |
Correct |
113 ms |
29356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
28076 KB |
Output is correct |
2 |
Correct |
100 ms |
28112 KB |
Output is correct |
3 |
Correct |
110 ms |
28084 KB |
Output is correct |
4 |
Correct |
117 ms |
28080 KB |
Output is correct |
5 |
Correct |
109 ms |
28336 KB |
Output is correct |
6 |
Correct |
115 ms |
28080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1020 KB |
Output is correct |
2 |
Correct |
2 ms |
960 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
3 ms |
956 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
3 ms |
852 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1020 KB |
Output is correct |
2 |
Correct |
2 ms |
960 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
7 |
Correct |
128 ms |
29356 KB |
Output is correct |
8 |
Correct |
120 ms |
29412 KB |
Output is correct |
9 |
Correct |
115 ms |
29216 KB |
Output is correct |
10 |
Correct |
109 ms |
29296 KB |
Output is correct |
11 |
Correct |
117 ms |
29232 KB |
Output is correct |
12 |
Correct |
113 ms |
29356 KB |
Output is correct |
13 |
Correct |
103 ms |
28076 KB |
Output is correct |
14 |
Correct |
100 ms |
28112 KB |
Output is correct |
15 |
Correct |
110 ms |
28084 KB |
Output is correct |
16 |
Correct |
117 ms |
28080 KB |
Output is correct |
17 |
Correct |
109 ms |
28336 KB |
Output is correct |
18 |
Correct |
115 ms |
28080 KB |
Output is correct |
19 |
Correct |
2 ms |
852 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
3 ms |
956 KB |
Output is correct |
22 |
Correct |
3 ms |
852 KB |
Output is correct |
23 |
Correct |
3 ms |
852 KB |
Output is correct |
24 |
Correct |
3 ms |
852 KB |
Output is correct |
25 |
Correct |
116 ms |
29616 KB |
Output is correct |
26 |
Correct |
114 ms |
29616 KB |
Output is correct |
27 |
Correct |
121 ms |
29620 KB |
Output is correct |
28 |
Correct |
150 ms |
29612 KB |
Output is correct |
29 |
Correct |
102 ms |
29608 KB |
Output is correct |
30 |
Correct |
121 ms |
29620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
28076 KB |
Output is correct |
2 |
Correct |
100 ms |
28112 KB |
Output is correct |
3 |
Correct |
110 ms |
28084 KB |
Output is correct |
4 |
Correct |
117 ms |
28080 KB |
Output is correct |
5 |
Correct |
109 ms |
28336 KB |
Output is correct |
6 |
Correct |
115 ms |
28080 KB |
Output is correct |
7 |
Correct |
128 ms |
32024 KB |
Output is correct |
8 |
Correct |
129 ms |
32424 KB |
Output is correct |
9 |
Correct |
140 ms |
32436 KB |
Output is correct |
10 |
Correct |
149 ms |
31392 KB |
Output is correct |
11 |
Correct |
147 ms |
31224 KB |
Output is correct |
12 |
Correct |
151 ms |
31144 KB |
Output is correct |
13 |
Correct |
151 ms |
31128 KB |
Output is correct |
14 |
Correct |
128 ms |
30892 KB |
Output is correct |
15 |
Correct |
154 ms |
31236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
1020 KB |
Output is correct |
3 |
Correct |
2 ms |
960 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
3 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
128 ms |
29356 KB |
Output is correct |
9 |
Correct |
120 ms |
29412 KB |
Output is correct |
10 |
Correct |
115 ms |
29216 KB |
Output is correct |
11 |
Correct |
109 ms |
29296 KB |
Output is correct |
12 |
Correct |
117 ms |
29232 KB |
Output is correct |
13 |
Correct |
113 ms |
29356 KB |
Output is correct |
14 |
Correct |
103 ms |
28076 KB |
Output is correct |
15 |
Correct |
100 ms |
28112 KB |
Output is correct |
16 |
Correct |
110 ms |
28084 KB |
Output is correct |
17 |
Correct |
117 ms |
28080 KB |
Output is correct |
18 |
Correct |
109 ms |
28336 KB |
Output is correct |
19 |
Correct |
115 ms |
28080 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
852 KB |
Output is correct |
22 |
Correct |
3 ms |
956 KB |
Output is correct |
23 |
Correct |
3 ms |
852 KB |
Output is correct |
24 |
Correct |
3 ms |
852 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
116 ms |
29616 KB |
Output is correct |
27 |
Correct |
114 ms |
29616 KB |
Output is correct |
28 |
Correct |
121 ms |
29620 KB |
Output is correct |
29 |
Correct |
150 ms |
29612 KB |
Output is correct |
30 |
Correct |
102 ms |
29608 KB |
Output is correct |
31 |
Correct |
121 ms |
29620 KB |
Output is correct |
32 |
Correct |
128 ms |
32024 KB |
Output is correct |
33 |
Correct |
129 ms |
32424 KB |
Output is correct |
34 |
Correct |
140 ms |
32436 KB |
Output is correct |
35 |
Correct |
149 ms |
31392 KB |
Output is correct |
36 |
Correct |
147 ms |
31224 KB |
Output is correct |
37 |
Correct |
151 ms |
31144 KB |
Output is correct |
38 |
Correct |
151 ms |
31128 KB |
Output is correct |
39 |
Correct |
128 ms |
30892 KB |
Output is correct |
40 |
Correct |
154 ms |
31236 KB |
Output is correct |
41 |
Correct |
153 ms |
32420 KB |
Output is correct |
42 |
Correct |
135 ms |
33952 KB |
Output is correct |
43 |
Correct |
158 ms |
33784 KB |
Output is correct |
44 |
Correct |
142 ms |
33956 KB |
Output is correct |
45 |
Correct |
158 ms |
33948 KB |
Output is correct |
46 |
Correct |
152 ms |
33816 KB |
Output is correct |
47 |
Correct |
148 ms |
33956 KB |
Output is correct |
48 |
Correct |
135 ms |
33560 KB |
Output is correct |
49 |
Correct |
152 ms |
33960 KB |
Output is correct |