#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long) (1e16);
const int N = (int) (4e7);
int root, tsz, ch[N][2];
pair<long long, long long> st[N];
void Modify(int& x, long long l, long long r, long long ll, long long rr, int a, long long b) {
if (!x) {
x = ++tsz;
}
if (ll <= l && r <= rr) {
st[x].first += a;
st[x].second += b;
return;
}
long long mid = (l + r) >> 1;
if (rr <= mid) {
Modify(ch[x][0], l, mid, ll, rr, a, b);
} else if (ll > mid) {
Modify(ch[x][1], mid + 1, r, ll, rr, a, b);
} else {
Modify(ch[x][0], l, mid, ll, rr, a, b);
Modify(ch[x][1], mid + 1, r, ll, rr, a, b);
}
}
long long Query(int x, long long l, long long r, int i) {
if (l == r) {
return st[x].first * 1LL * i + st[x].second;
}
long long mid = (l + r) >> 1;
if (i <= mid) {
return st[x].first * 1LL * i + st[x].second + Query(ch[x][0], l, mid, i);
} else {
return st[x].first * 1LL * i + st[x].second + Query(ch[x][1], mid + 1, r, i);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b(n);
for (int i = 0; i < n; i++) {
cin >> b[i];
}
vector<int> L(n), R(n);
{
vector<int> stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && b[i] < b[stk.back()]) {
stk.pop_back();
}
L[i] = stk.empty() ? -1 : stk.back();
stk.push_back(i);
}
}
{
vector<int> stk;
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && b[i] <= b[stk.back()]) {
stk.pop_back();
}
R[i] = stk.empty() ? n : stk.back();
stk.push_back(i);
}
}
vector<long long> pref(n + 1);
for (int i = 0; i < n; i++) {
pref[i + 1] = pref[i] + a[i];
}
vector<int> s(m), t(m), u(m);
for (int i = 0; i < m; i++) {
cin >> s[i] >> t[i] >> u[i];
--s[i]; --t[i]; --t[i];
}
const int LOG = 20;
vector<vector<int>> mx(n, vector<int>(LOG));
vector<vector<int>> mn(n, vector<int>(LOG));
for (int i = 0; i < n; i++) {
mx[i][0] = a[i];
mn[i][0] = i;
}
auto Cmp = [&](int i, int j) {
return b[i] < b[j] ? i : j;
};
for (int j = 1; j < LOG; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
mn[i][j] = Cmp(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
}
}
vector<int> logs(n + 1);
for (int i = 2; i <= n; i++) {
logs[i] = logs[i >> 1] + 1;
}
auto GetMax = [&](int l, int r) {
int k = logs[r - l + 1];
return max(mx[l][k], mx[r - (1 << k) + 1][k]);
};
auto GetMin = [&](int l, int r) {
int k = logs[r - l + 1];
return Cmp(mn[l][k], mn[r - (1 << k) + 1][k]);
};
vector<vector<array<long long, 4>>> qc(n);
for (int i = 0; i < n; i++) {
{
// after L_i
long long d = pref[R[i]] - pref[i];
qc[i].push_back({0, d, b[i], 0});
qc[i].push_back({d + 1, inf, 0, d * b[i]});
if (L[i] != -1) {
qc[L[i]].push_back({0, d, -b[i], 0});
qc[L[i]].push_back({d + 1, inf, 0, -d * b[i]});
}
}
if (L[i] != -1) {
// before L_i
vector<long long> v;
v.push_back(0);
v.push_back(inf);
v.push_back(pref[R[i]] - pref[i]);
v.push_back(pref[i] - pref[L[i]]);
v.push_back(pref[R[i]] - pref[L[i]]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int j = 0; j + 1 < (int) v.size(); j++) {
if (v[j] >= pref[R[i]] - pref[L[i]]) {
break;
}
pair<long long, long long> p, q;
if (pref[i] + v[j] < pref[R[i]]) {
p = {1, pref[i]};
} else {
p = {0, pref[R[i]]};
}
if (pref[i] > pref[L[i]] + v[j]) {
q = {0, pref[i]};
} else {
q = {1, pref[L[i]]};
}
pair<long long, long long> r;
r.first = p.first - q.first;
r.second = p.second - q.second;
qc[L[i]].push_back({v[j], v[j + 1] - 1, r.first * b[i], r.second * b[i]});
}
}
}
vector<vector<int>> qs(n);
vector<long long> res(m);
for (int i = 0; i < m; i++) {
if (GetMax(s[i], t[i]) > u[i]) {
res[i] = -1;
continue;
}
int low = s[i], high = t[i], pos = t[i];
while (low <= high) {
int mid = (low + high) >> 1;
if (pref[t[i] + 1] - pref[mid] <= u[i]) {
pos = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
pos = GetMin(pos, t[i]);
qs[s[i]].push_back(i);
qs[pos].push_back(~i);
res[i] += (pref[t[i] + 1] - pref[pos]) * 1LL * b[pos];
}
for (int i = n - 1; i >= 0; i--) {
for (auto& p : qc[i]) {
Modify(root, 0, inf, p[0], p[1], p[2], p[3]);
}
for (int id : qs[i]) {
if (id >= 0) {
res[id] += Query(root, 0, inf, u[id]);
} else {
id = ~id;
res[id] -= Query(root, 0, inf, u[id]);
}
}
}
for (int i = 0; i < m; i++) {
cout << res[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5212 KB |
Output is correct |
2 |
Correct |
11 ms |
5240 KB |
Output is correct |
3 |
Correct |
4 ms |
1884 KB |
Output is correct |
4 |
Correct |
9 ms |
5212 KB |
Output is correct |
5 |
Correct |
11 ms |
5212 KB |
Output is correct |
6 |
Correct |
6 ms |
4188 KB |
Output is correct |
7 |
Correct |
13 ms |
4852 KB |
Output is correct |
8 |
Correct |
8 ms |
5212 KB |
Output is correct |
9 |
Correct |
5 ms |
1884 KB |
Output is correct |
10 |
Correct |
10 ms |
5212 KB |
Output is correct |
11 |
Correct |
9 ms |
5596 KB |
Output is correct |
12 |
Correct |
6 ms |
4188 KB |
Output is correct |
13 |
Correct |
10 ms |
5344 KB |
Output is correct |
14 |
Correct |
8 ms |
5076 KB |
Output is correct |
15 |
Correct |
10 ms |
4964 KB |
Output is correct |
16 |
Correct |
8 ms |
5212 KB |
Output is correct |
17 |
Correct |
8 ms |
4640 KB |
Output is correct |
18 |
Correct |
8 ms |
4960 KB |
Output is correct |
19 |
Correct |
4 ms |
1884 KB |
Output is correct |
20 |
Correct |
9 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
73556 KB |
Output is correct |
2 |
Correct |
154 ms |
50800 KB |
Output is correct |
3 |
Correct |
178 ms |
79552 KB |
Output is correct |
4 |
Correct |
76 ms |
29720 KB |
Output is correct |
5 |
Correct |
181 ms |
51220 KB |
Output is correct |
6 |
Correct |
644 ms |
282784 KB |
Output is correct |
7 |
Correct |
596 ms |
167972 KB |
Output is correct |
8 |
Correct |
752 ms |
303568 KB |
Output is correct |
9 |
Correct |
360 ms |
105408 KB |
Output is correct |
10 |
Correct |
718 ms |
241388 KB |
Output is correct |
11 |
Correct |
715 ms |
235776 KB |
Output is correct |
12 |
Correct |
373 ms |
96444 KB |
Output is correct |
13 |
Correct |
624 ms |
146544 KB |
Output is correct |
14 |
Correct |
627 ms |
146408 KB |
Output is correct |
15 |
Correct |
587 ms |
224308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
699 ms |
152804 KB |
Output is correct |
2 |
Correct |
667 ms |
270440 KB |
Output is correct |
3 |
Correct |
326 ms |
97472 KB |
Output is correct |
4 |
Correct |
588 ms |
162124 KB |
Output is correct |
5 |
Correct |
575 ms |
274104 KB |
Output is correct |
6 |
Correct |
640 ms |
259548 KB |
Output is correct |
7 |
Correct |
595 ms |
137116 KB |
Output is correct |
8 |
Correct |
612 ms |
233948 KB |
Output is correct |
9 |
Correct |
284 ms |
86256 KB |
Output is correct |
10 |
Correct |
548 ms |
219320 KB |
Output is correct |
11 |
Correct |
645 ms |
281480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5212 KB |
Output is correct |
2 |
Correct |
11 ms |
5240 KB |
Output is correct |
3 |
Correct |
4 ms |
1884 KB |
Output is correct |
4 |
Correct |
9 ms |
5212 KB |
Output is correct |
5 |
Correct |
11 ms |
5212 KB |
Output is correct |
6 |
Correct |
6 ms |
4188 KB |
Output is correct |
7 |
Correct |
13 ms |
4852 KB |
Output is correct |
8 |
Correct |
8 ms |
5212 KB |
Output is correct |
9 |
Correct |
5 ms |
1884 KB |
Output is correct |
10 |
Correct |
10 ms |
5212 KB |
Output is correct |
11 |
Correct |
9 ms |
5596 KB |
Output is correct |
12 |
Correct |
6 ms |
4188 KB |
Output is correct |
13 |
Correct |
10 ms |
5344 KB |
Output is correct |
14 |
Correct |
8 ms |
5076 KB |
Output is correct |
15 |
Correct |
10 ms |
4964 KB |
Output is correct |
16 |
Correct |
8 ms |
5212 KB |
Output is correct |
17 |
Correct |
8 ms |
4640 KB |
Output is correct |
18 |
Correct |
8 ms |
4960 KB |
Output is correct |
19 |
Correct |
4 ms |
1884 KB |
Output is correct |
20 |
Correct |
9 ms |
4972 KB |
Output is correct |
21 |
Correct |
154 ms |
73556 KB |
Output is correct |
22 |
Correct |
154 ms |
50800 KB |
Output is correct |
23 |
Correct |
178 ms |
79552 KB |
Output is correct |
24 |
Correct |
76 ms |
29720 KB |
Output is correct |
25 |
Correct |
181 ms |
51220 KB |
Output is correct |
26 |
Correct |
644 ms |
282784 KB |
Output is correct |
27 |
Correct |
596 ms |
167972 KB |
Output is correct |
28 |
Correct |
752 ms |
303568 KB |
Output is correct |
29 |
Correct |
360 ms |
105408 KB |
Output is correct |
30 |
Correct |
718 ms |
241388 KB |
Output is correct |
31 |
Correct |
715 ms |
235776 KB |
Output is correct |
32 |
Correct |
373 ms |
96444 KB |
Output is correct |
33 |
Correct |
624 ms |
146544 KB |
Output is correct |
34 |
Correct |
627 ms |
146408 KB |
Output is correct |
35 |
Correct |
587 ms |
224308 KB |
Output is correct |
36 |
Correct |
699 ms |
152804 KB |
Output is correct |
37 |
Correct |
667 ms |
270440 KB |
Output is correct |
38 |
Correct |
326 ms |
97472 KB |
Output is correct |
39 |
Correct |
588 ms |
162124 KB |
Output is correct |
40 |
Correct |
575 ms |
274104 KB |
Output is correct |
41 |
Correct |
640 ms |
259548 KB |
Output is correct |
42 |
Correct |
595 ms |
137116 KB |
Output is correct |
43 |
Correct |
612 ms |
233948 KB |
Output is correct |
44 |
Correct |
284 ms |
86256 KB |
Output is correct |
45 |
Correct |
548 ms |
219320 KB |
Output is correct |
46 |
Correct |
645 ms |
281480 KB |
Output is correct |
47 |
Correct |
608 ms |
146636 KB |
Output is correct |
48 |
Correct |
843 ms |
302528 KB |
Output is correct |
49 |
Correct |
338 ms |
95964 KB |
Output is correct |
50 |
Correct |
635 ms |
173112 KB |
Output is correct |
51 |
Correct |
568 ms |
222364 KB |
Output is correct |
52 |
Correct |
664 ms |
240536 KB |
Output is correct |
53 |
Correct |
607 ms |
144980 KB |
Output is correct |
54 |
Correct |
675 ms |
247880 KB |
Output is correct |
55 |
Correct |
324 ms |
95048 KB |
Output is correct |
56 |
Correct |
525 ms |
225104 KB |
Output is correct |
57 |
Correct |
665 ms |
288984 KB |
Output is correct |