/*
-------------- | /
| | /
| | /
| * |/ | | ------ *
| | | | / \
| | |\ | | | |\ |
\ | | | \ | | | | \ |
\ | | | \ | | \ / \ |
V | | \ \__/| ----- \ |
*/
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << "\e[1;31m" << #x << " = " << (x) << "\e[0m\n"
#define print(x) emilia_mata_tenshi(#x, begin(x), end(x))
template<typename T> void emilia_mata_tenshi(const char *s, T l, T r) {
cerr << "\e[1;33m" << s << " = [";
while(l != r) {
cerr << *l;
cerr << (++l == r ? ']' : ',');
}
cerr << "\e[0m\n";
}
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS = 1e-7;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9+7;
static auto Lamy_is_cute = []() {
EmiliaMyWife
return 48763;
}();
/*--------------------------------------------------------------------------------------*/
const int N = 1e6 + 25;
struct fenwicktree {
ll arr[N];
int n;
void init(int _n) { n = _n; }
void edt(int p, ll v) {
for(p++; p <= n; p += p & -p)
arr[p] += v;
}
ll que(int p) {
ll res = 0;
for(p++; p; p -= p & -p)
res += arr[p];
return res;
}
} slope, intercept;
struct segtree {
ll arr[N << 1];
int n;
void init(vector<ll> &a) {
n = a.size();
for(int i = 0; i < n; i++)
arr[i + n] = a[i];
for(int i = n - 1; i; i--)
arr[i] = max(arr[i << 1], arr[i << 1 | 1]);
}
ll que(int l, int r) {
ll res = 0;
for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
if(l & 1)
res = max(res, arr[l++]);
if(r & 1)
res = max(res, arr[--r]);
}
return res;
}
} tree;
struct segtree2 {
pair<int, int> arr[N << 1];
int n;
void init(vector<int> &a) {
n = a.size();
for(int i = 0; i < n; i++)
arr[i + n] = {a[i], i};
for(int i = n - 1; i; i--)
arr[i] = min(arr[i << 1], arr[i << 1 | 1]);
}
int que(int l, int r) {
pair<int, int> res = {INF, 0};
for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
if(l & 1)
res = min(res, arr[l++]);
if(r & 1)
res = min(res, arr[--r]);
}
return res.second;
}
} pos_tree;
struct query {
int p, u, id, v;
bool operator<(query b) {
return p > b.p;
}
};
signed main() {
int n, m;
cin >> n >> m;
vector<ll> pos(n + 1);
vector<int> cost(n + 1), r(n, -1);
vector<vector<int>> is(n);
for(int i = 1; i <= n; i++)
cin >> pos[i];
tree.init(pos);
for(int i = 1; i <= n; i++)
pos[i] += pos[i - 1];
for(int i = 0; i < n; i++)
cin >> cost[i];
pos_tree.init(cost);
auto pos_get = [&](ll l, ll r) {
l = lower_bound(pos.begin(), pos.end(), l) - pos.begin();
r = lower_bound(pos.begin(), pos.end(), r) - pos.begin();
return pos_tree.que(l, r);
};
vector<query> que(m * 2);
vector<ll> v = {0, LINF}, ans(m);
for(int i = 0, a, b, c; i < m; i++) {
cin >> a >> b >> c;
a--;
b--;
if(tree.que(a + 1, b + 1) > c)
ans[i] = -1;
else {
v.push_back(c);
int t = pos_get(max(pos[a], pos[b] - c), pos[b]);
ans[i] += (pos[b] - pos[t]) * cost[t];
que[i * 2] = {a, c, i, 1};
que[i * 2 + 1] = {t, c, i, -1};
}
}
sort(que.begin(), que.end());
stack<int> has;
has.push(n);
for(int i = n - 1; ~i; i--) {
while(!has.empty() && cost[has.top()] >= cost[i]) {
v.push_back(pos[has.top()] - pos[i]);
is[i].push_back(has.top());
has.pop();
}
for(int j : is[i])
v.push_back(pos[r[j]] - pos[i]);
if(!has.empty()) {
r[i] = has.top();
v.push_back(pos[r[i]] - pos[i]);
}
has.push(i);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
slope.init(v.size());
intercept.init(v.size());
auto edt = [&](ll l, ll r, ll m, ll k) { // [l, r)
if(l > r)
return;
ll x = l;
l = lower_bound(v.begin(), v.end(), l) - v.begin();
r = lower_bound(v.begin(), v.end(), r) - v.begin();
slope.edt(l, m);
intercept.edt(l, k - x * m);
if(r < v.size()) {
slope.edt(r, -m);
intercept.edt(r, -(k - x * m));
}
};
auto get = [&](ll u) {
int it = lower_bound(v.begin(), v.end(), u) - v.begin();
return slope.que(it) * u + intercept.que(it);
};
for(int i = n - 1, it = 0; ~i; i--) {
for(int j : is[i]) {
edt(pos[j] - pos[i], pos[r[j]] - pos[i], -cost[j], 0);
edt(pos[r[j]] - pos[i], LINF, 0, -cost[j] * (pos[r[j]] - pos[j]));
}
edt(0, pos[r[i]] - pos[i], cost[i], 0);
edt(pos[r[i]] - pos[i], LINF, 0, cost[i] * (pos[r[i]] - pos[i]));
while(it < que.size() && que[it].p == i) {
ans[que[it].id] += get(que[it].u) * que[it].v;
it++;
}
}
for(int i = 0; i < m; i++) {
if(ans[i] < 0)
cout << "-1\n";
else
cout << ans[i] << '\n';
}
}
Compilation message
Main.cpp: In lambda function:
Main.cpp:182:8: warning: comparison of integer expressions of different signedness: 'll' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
182 | if(r < v.size()) {
| ~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:200:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
200 | while(it < que.size() && que[it].p == i) {
| ~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
16844 KB |
Output is correct |
2 |
Correct |
16 ms |
16708 KB |
Output is correct |
3 |
Correct |
12 ms |
16468 KB |
Output is correct |
4 |
Correct |
15 ms |
16712 KB |
Output is correct |
5 |
Correct |
14 ms |
16632 KB |
Output is correct |
6 |
Correct |
11 ms |
16500 KB |
Output is correct |
7 |
Correct |
14 ms |
16716 KB |
Output is correct |
8 |
Correct |
13 ms |
16716 KB |
Output is correct |
9 |
Correct |
15 ms |
16484 KB |
Output is correct |
10 |
Correct |
14 ms |
16696 KB |
Output is correct |
11 |
Correct |
16 ms |
16752 KB |
Output is correct |
12 |
Correct |
14 ms |
16492 KB |
Output is correct |
13 |
Correct |
14 ms |
16680 KB |
Output is correct |
14 |
Correct |
16 ms |
16716 KB |
Output is correct |
15 |
Correct |
12 ms |
16652 KB |
Output is correct |
16 |
Correct |
13 ms |
16712 KB |
Output is correct |
17 |
Correct |
14 ms |
16588 KB |
Output is correct |
18 |
Correct |
12 ms |
16628 KB |
Output is correct |
19 |
Correct |
11 ms |
16460 KB |
Output is correct |
20 |
Correct |
11 ms |
16588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
26588 KB |
Output is correct |
2 |
Correct |
118 ms |
26720 KB |
Output is correct |
3 |
Correct |
113 ms |
27700 KB |
Output is correct |
4 |
Correct |
91 ms |
24656 KB |
Output is correct |
5 |
Correct |
138 ms |
26772 KB |
Output is correct |
6 |
Correct |
445 ms |
57596 KB |
Output is correct |
7 |
Correct |
478 ms |
57124 KB |
Output is correct |
8 |
Correct |
492 ms |
62240 KB |
Output is correct |
9 |
Correct |
467 ms |
50556 KB |
Output is correct |
10 |
Correct |
477 ms |
59004 KB |
Output is correct |
11 |
Correct |
513 ms |
58412 KB |
Output is correct |
12 |
Correct |
404 ms |
47820 KB |
Output is correct |
13 |
Correct |
545 ms |
53720 KB |
Output is correct |
14 |
Correct |
539 ms |
53716 KB |
Output is correct |
15 |
Correct |
310 ms |
57928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
542 ms |
58184 KB |
Output is correct |
2 |
Correct |
434 ms |
62708 KB |
Output is correct |
3 |
Correct |
377 ms |
50024 KB |
Output is correct |
4 |
Correct |
497 ms |
60328 KB |
Output is correct |
5 |
Correct |
417 ms |
60224 KB |
Output is correct |
6 |
Correct |
502 ms |
61716 KB |
Output is correct |
7 |
Correct |
429 ms |
54132 KB |
Output is correct |
8 |
Correct |
398 ms |
59120 KB |
Output is correct |
9 |
Correct |
312 ms |
46708 KB |
Output is correct |
10 |
Correct |
338 ms |
59036 KB |
Output is correct |
11 |
Correct |
440 ms |
58912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
16844 KB |
Output is correct |
2 |
Correct |
16 ms |
16708 KB |
Output is correct |
3 |
Correct |
12 ms |
16468 KB |
Output is correct |
4 |
Correct |
15 ms |
16712 KB |
Output is correct |
5 |
Correct |
14 ms |
16632 KB |
Output is correct |
6 |
Correct |
11 ms |
16500 KB |
Output is correct |
7 |
Correct |
14 ms |
16716 KB |
Output is correct |
8 |
Correct |
13 ms |
16716 KB |
Output is correct |
9 |
Correct |
15 ms |
16484 KB |
Output is correct |
10 |
Correct |
14 ms |
16696 KB |
Output is correct |
11 |
Correct |
16 ms |
16752 KB |
Output is correct |
12 |
Correct |
14 ms |
16492 KB |
Output is correct |
13 |
Correct |
14 ms |
16680 KB |
Output is correct |
14 |
Correct |
16 ms |
16716 KB |
Output is correct |
15 |
Correct |
12 ms |
16652 KB |
Output is correct |
16 |
Correct |
13 ms |
16712 KB |
Output is correct |
17 |
Correct |
14 ms |
16588 KB |
Output is correct |
18 |
Correct |
12 ms |
16628 KB |
Output is correct |
19 |
Correct |
11 ms |
16460 KB |
Output is correct |
20 |
Correct |
11 ms |
16588 KB |
Output is correct |
21 |
Correct |
103 ms |
26588 KB |
Output is correct |
22 |
Correct |
118 ms |
26720 KB |
Output is correct |
23 |
Correct |
113 ms |
27700 KB |
Output is correct |
24 |
Correct |
91 ms |
24656 KB |
Output is correct |
25 |
Correct |
138 ms |
26772 KB |
Output is correct |
26 |
Correct |
445 ms |
57596 KB |
Output is correct |
27 |
Correct |
478 ms |
57124 KB |
Output is correct |
28 |
Correct |
492 ms |
62240 KB |
Output is correct |
29 |
Correct |
467 ms |
50556 KB |
Output is correct |
30 |
Correct |
477 ms |
59004 KB |
Output is correct |
31 |
Correct |
513 ms |
58412 KB |
Output is correct |
32 |
Correct |
404 ms |
47820 KB |
Output is correct |
33 |
Correct |
545 ms |
53720 KB |
Output is correct |
34 |
Correct |
539 ms |
53716 KB |
Output is correct |
35 |
Correct |
310 ms |
57928 KB |
Output is correct |
36 |
Correct |
542 ms |
58184 KB |
Output is correct |
37 |
Correct |
434 ms |
62708 KB |
Output is correct |
38 |
Correct |
377 ms |
50024 KB |
Output is correct |
39 |
Correct |
497 ms |
60328 KB |
Output is correct |
40 |
Correct |
417 ms |
60224 KB |
Output is correct |
41 |
Correct |
502 ms |
61716 KB |
Output is correct |
42 |
Correct |
429 ms |
54132 KB |
Output is correct |
43 |
Correct |
398 ms |
59120 KB |
Output is correct |
44 |
Correct |
312 ms |
46708 KB |
Output is correct |
45 |
Correct |
338 ms |
59036 KB |
Output is correct |
46 |
Correct |
440 ms |
58912 KB |
Output is correct |
47 |
Correct |
574 ms |
55468 KB |
Output is correct |
48 |
Correct |
575 ms |
63452 KB |
Output is correct |
49 |
Correct |
440 ms |
47408 KB |
Output is correct |
50 |
Correct |
556 ms |
59960 KB |
Output is correct |
51 |
Correct |
500 ms |
56892 KB |
Output is correct |
52 |
Correct |
518 ms |
60892 KB |
Output is correct |
53 |
Correct |
509 ms |
54176 KB |
Output is correct |
54 |
Correct |
453 ms |
59796 KB |
Output is correct |
55 |
Correct |
381 ms |
46716 KB |
Output is correct |
56 |
Correct |
344 ms |
58776 KB |
Output is correct |
57 |
Correct |
493 ms |
57984 KB |
Output is correct |