/*
-------------- | /
| | /
| | /
| * |/ | | ------ *
| | | | / \
| | |\ | | | |\ |
\ | | | \ | | | | \ |
\ | | | \ | | \ / \ |
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;
assert(*lower_bound(v.begin(), v.end(), l) == l);
assert(*lower_bound(v.begin(), v.end(), r) == r);
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();
assert(v[it] == u);
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:183: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]
183 | if(r < v.size()) {
| ~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:202:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
202 | while(it < que.size() && que[it].p == i) {
| ~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
16720 KB |
Output is correct |
2 |
Correct |
13 ms |
16748 KB |
Output is correct |
3 |
Correct |
11 ms |
16500 KB |
Output is correct |
4 |
Correct |
14 ms |
16712 KB |
Output is correct |
5 |
Correct |
12 ms |
16720 KB |
Output is correct |
6 |
Correct |
12 ms |
16488 KB |
Output is correct |
7 |
Correct |
15 ms |
16616 KB |
Output is correct |
8 |
Correct |
15 ms |
16720 KB |
Output is correct |
9 |
Correct |
10 ms |
16504 KB |
Output is correct |
10 |
Correct |
14 ms |
16720 KB |
Output is correct |
11 |
Correct |
13 ms |
16720 KB |
Output is correct |
12 |
Correct |
12 ms |
16500 KB |
Output is correct |
13 |
Correct |
15 ms |
16736 KB |
Output is correct |
14 |
Correct |
14 ms |
16700 KB |
Output is correct |
15 |
Correct |
13 ms |
16728 KB |
Output is correct |
16 |
Correct |
13 ms |
16672 KB |
Output is correct |
17 |
Correct |
14 ms |
16564 KB |
Output is correct |
18 |
Correct |
14 ms |
16592 KB |
Output is correct |
19 |
Correct |
12 ms |
16456 KB |
Output is correct |
20 |
Correct |
12 ms |
16600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
26600 KB |
Output is correct |
2 |
Correct |
130 ms |
26740 KB |
Output is correct |
3 |
Correct |
120 ms |
27624 KB |
Output is correct |
4 |
Correct |
108 ms |
24636 KB |
Output is correct |
5 |
Correct |
157 ms |
26720 KB |
Output is correct |
6 |
Correct |
460 ms |
57612 KB |
Output is correct |
7 |
Correct |
569 ms |
56984 KB |
Output is correct |
8 |
Correct |
597 ms |
62148 KB |
Output is correct |
9 |
Correct |
449 ms |
50480 KB |
Output is correct |
10 |
Correct |
484 ms |
59044 KB |
Output is correct |
11 |
Correct |
491 ms |
58484 KB |
Output is correct |
12 |
Correct |
396 ms |
47908 KB |
Output is correct |
13 |
Correct |
606 ms |
53712 KB |
Output is correct |
14 |
Correct |
593 ms |
53980 KB |
Output is correct |
15 |
Correct |
366 ms |
57896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
648 ms |
58180 KB |
Output is correct |
2 |
Correct |
532 ms |
62628 KB |
Output is correct |
3 |
Correct |
421 ms |
50140 KB |
Output is correct |
4 |
Correct |
583 ms |
60024 KB |
Output is correct |
5 |
Correct |
467 ms |
60180 KB |
Output is correct |
6 |
Correct |
530 ms |
61784 KB |
Output is correct |
7 |
Correct |
506 ms |
54024 KB |
Output is correct |
8 |
Correct |
435 ms |
59252 KB |
Output is correct |
9 |
Correct |
319 ms |
46812 KB |
Output is correct |
10 |
Correct |
374 ms |
58944 KB |
Output is correct |
11 |
Correct |
541 ms |
58908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
16720 KB |
Output is correct |
2 |
Correct |
13 ms |
16748 KB |
Output is correct |
3 |
Correct |
11 ms |
16500 KB |
Output is correct |
4 |
Correct |
14 ms |
16712 KB |
Output is correct |
5 |
Correct |
12 ms |
16720 KB |
Output is correct |
6 |
Correct |
12 ms |
16488 KB |
Output is correct |
7 |
Correct |
15 ms |
16616 KB |
Output is correct |
8 |
Correct |
15 ms |
16720 KB |
Output is correct |
9 |
Correct |
10 ms |
16504 KB |
Output is correct |
10 |
Correct |
14 ms |
16720 KB |
Output is correct |
11 |
Correct |
13 ms |
16720 KB |
Output is correct |
12 |
Correct |
12 ms |
16500 KB |
Output is correct |
13 |
Correct |
15 ms |
16736 KB |
Output is correct |
14 |
Correct |
14 ms |
16700 KB |
Output is correct |
15 |
Correct |
13 ms |
16728 KB |
Output is correct |
16 |
Correct |
13 ms |
16672 KB |
Output is correct |
17 |
Correct |
14 ms |
16564 KB |
Output is correct |
18 |
Correct |
14 ms |
16592 KB |
Output is correct |
19 |
Correct |
12 ms |
16456 KB |
Output is correct |
20 |
Correct |
12 ms |
16600 KB |
Output is correct |
21 |
Correct |
115 ms |
26600 KB |
Output is correct |
22 |
Correct |
130 ms |
26740 KB |
Output is correct |
23 |
Correct |
120 ms |
27624 KB |
Output is correct |
24 |
Correct |
108 ms |
24636 KB |
Output is correct |
25 |
Correct |
157 ms |
26720 KB |
Output is correct |
26 |
Correct |
460 ms |
57612 KB |
Output is correct |
27 |
Correct |
569 ms |
56984 KB |
Output is correct |
28 |
Correct |
597 ms |
62148 KB |
Output is correct |
29 |
Correct |
449 ms |
50480 KB |
Output is correct |
30 |
Correct |
484 ms |
59044 KB |
Output is correct |
31 |
Correct |
491 ms |
58484 KB |
Output is correct |
32 |
Correct |
396 ms |
47908 KB |
Output is correct |
33 |
Correct |
606 ms |
53712 KB |
Output is correct |
34 |
Correct |
593 ms |
53980 KB |
Output is correct |
35 |
Correct |
366 ms |
57896 KB |
Output is correct |
36 |
Correct |
648 ms |
58180 KB |
Output is correct |
37 |
Correct |
532 ms |
62628 KB |
Output is correct |
38 |
Correct |
421 ms |
50140 KB |
Output is correct |
39 |
Correct |
583 ms |
60024 KB |
Output is correct |
40 |
Correct |
467 ms |
60180 KB |
Output is correct |
41 |
Correct |
530 ms |
61784 KB |
Output is correct |
42 |
Correct |
506 ms |
54024 KB |
Output is correct |
43 |
Correct |
435 ms |
59252 KB |
Output is correct |
44 |
Correct |
319 ms |
46812 KB |
Output is correct |
45 |
Correct |
374 ms |
58944 KB |
Output is correct |
46 |
Correct |
541 ms |
58908 KB |
Output is correct |
47 |
Correct |
658 ms |
55348 KB |
Output is correct |
48 |
Correct |
617 ms |
63476 KB |
Output is correct |
49 |
Correct |
442 ms |
47444 KB |
Output is correct |
50 |
Correct |
617 ms |
60208 KB |
Output is correct |
51 |
Correct |
500 ms |
56780 KB |
Output is correct |
52 |
Correct |
531 ms |
60752 KB |
Output is correct |
53 |
Correct |
569 ms |
54212 KB |
Output is correct |
54 |
Correct |
511 ms |
60068 KB |
Output is correct |
55 |
Correct |
424 ms |
46956 KB |
Output is correct |
56 |
Correct |
398 ms |
59120 KB |
Output is correct |
57 |
Correct |
580 ms |
58856 KB |
Output is correct |