#include <tuple>
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
long long tree_triangles[524288], tree_squares[524288], tree_parallels[1048576];
long long lazy_triangles[524288], lazy_squares[524288], lazy_parallels[1048576];
void propagate(long long *tree, long long *lazy, int i, int b, int e) {
if (!lazy[i]) return;
if (b != e) {
lazy[i * 2 + 1] += lazy[i];
lazy[i * 2 + 2] += lazy[i];
}
tree[i] += (e - b + 1) * lazy[i];
lazy[i] = 0;
}
long long query(long long *tree, long long *lazy, int i, int b, int e, int l, int r) {
propagate(tree, lazy, i, b, e);
if (r < l || r < b || e < l) return 0;
if (l <= b && e <= r) return tree[i];
int m = (b + e) / 2;
return query(tree, lazy, i * 2 + 1, b, m, l, r) + query(tree, lazy, i * 2 + 2, m + 1, e, l, r);
}
long long update(long long *tree, long long *lazy, int i, int b, int e, int l, int r, long long v) {
propagate(tree, lazy, i, b, e);
if (r < l || r < b || e < l) return tree[i];
if (l <= b && e <= r) {
lazy[i] += v;
propagate(tree, lazy, i, b, e);
return tree[i];
}
int m = (b + e) / 2;
return tree[i] = update(tree, lazy, i * 2 + 1, b, m, l, r, v) + update(tree, lazy, i * 2 + 2, m + 1, e, l, r, v);
}
int n, q, a[200006], lt[200006], rt[200006];
vector<int> st;
vector<pair<int, int>> triangles;
vector<tuple<int, int, int, int>> queries, part_squares, part_parallels;
vector<tuple<int, int, int, int, int>> squares, parallels;
long long res[200006];
void triangle(int x, int y, int l, int v) {
if (x == y) {
if (!x) triangles.push_back({ l, v });
else {
triangles.push_back({ x + l, v });
triangles.push_back({ x - 1, -v });
squares.push_back({ 0, x, x - 1, x, -v });
}
} else if (x < y) {
if (!x) {
triangles.push_back({ l - 1, v });
squares.push_back({ 0, l - 1, l, y + 1, v });
parallels.push_back({ 0, 0, l, y, -v });
} else {
triangles.push_back({ x + l - 1, v });
triangles.push_back({ x - 1, -v });
squares.push_back({ 0, x + l - 1, x + l, y - x + 1, v });
squares.push_back({ 0, x - 1, x, y - x + l + 1, -v });
parallels.push_back({ x, x, l, y - x, -v });
}
} else {
triangles.push_back({ x + l, v });
triangles.push_back({ x, -v });
squares.push_back({ 0, x, x, l, -v });
squares.push_back({ x, y + l, l, x - y, -v });
parallels.push_back({ x, y, l, x - y, v });
}
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++) scanf("%d", a + i);
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.back()] < a[i]) st.pop_back();
if (st.empty()) lt[i] = -1;
else lt[i] = st.back();
st.push_back(i);
} st.clear();
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.back()] <= a[i]) st.pop_back();
if (st.empty()) rt[i] = n;
else rt[i] = st.back();
st.push_back(i);
} st.clear();
for (int i = 0; i < n; i++) {
int height = n + 1, width = rt[i] - i;
if (lt[i] != -1) height = i - lt[i];
squares.push_back({ 0, i, height, width, a[i] });
triangle(0, i + 1, width - 1, -a[i]);
triangle(height, i + 1, width - 1, a[i]);
}
for (int i = 0; i < q; i++) {
int t, l, r;
scanf("%d%d%d", &t, &l, &r);
l--, r--;
queries.push_back({ t, l, r, i });
}
sort(queries.begin(), queries.end());
for (auto &i: squares) {
auto [ a, b, c, d, e ] = i;
part_squares.push_back({ a, b, b + d - 1, e });
part_squares.push_back({ a + c, b, b + d - 1, -e });
}
for (auto &i: parallels) {
auto [ a, b, c, d, e ] = i;
part_parallels.push_back({ a, b - a, b - a + d - 1, e });
part_parallels.push_back({ a + c, b - a, b - a + d - 1, -e });
}
sort(part_squares.begin(), part_squares.end());
sort(part_parallels.begin(), part_parallels.end());
for (auto &i: triangles) {
auto [ a, b ] = i;
update(tree_triangles, lazy_triangles, 0, 0, 262143, 0, a - 1, b);
}
int k = 0, l = 0, m = 0;
for (int i = 0; i <= n; i++) {
while (k < (int)part_squares.size() && get<0>(part_squares[k]) <= i) {
auto [ a, b, c, d ] = part_squares[k];
update(tree_squares, lazy_squares, 0, 0, 262143, b, c, d);
k++;
}
while (l < (int)part_parallels.size() && get<0>(part_parallels[l]) <= i) {
auto [ a, b, c, d ] = part_parallels[l];
update(tree_parallels, lazy_parallels, 0, 0, 524287, 262143 + b, 262143 + c, d);
l++;
}
while (m < q && get<0>(queries[m]) <= i) {
auto [ a, b, c, d ] = queries[m];
res[d] += query(tree_triangles, lazy_triangles, 0, 0, 262143, max(i, b), c);
res[d] += query(tree_squares, lazy_squares, 0, 0, 262143, b, c);
res[d] += query(tree_parallels, lazy_parallels, 0, 0, 524287, 262143 + b - i, 262143 + c - i);
m++;
}
}
for (int i = 0; i < q; i++) printf("%lld\n", res[i]);
}
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:79:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | for (int i = 0; i < n; i++) scanf("%d", a + i);
| ~~~~~^~~~~~~~~~~~~
ho_t5.cpp:101:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | scanf("%d%d%d", &t, &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
4 ms |
716 KB |
Output is correct |
3 |
Correct |
3 ms |
716 KB |
Output is correct |
4 |
Correct |
3 ms |
680 KB |
Output is correct |
5 |
Correct |
3 ms |
716 KB |
Output is correct |
6 |
Correct |
4 ms |
768 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
688 KB |
Output is correct |
11 |
Correct |
3 ms |
716 KB |
Output is correct |
12 |
Correct |
3 ms |
716 KB |
Output is correct |
13 |
Correct |
3 ms |
716 KB |
Output is correct |
14 |
Correct |
3 ms |
716 KB |
Output is correct |
15 |
Correct |
2 ms |
716 KB |
Output is correct |
16 |
Correct |
3 ms |
716 KB |
Output is correct |
17 |
Correct |
3 ms |
684 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
19 |
Correct |
3 ms |
716 KB |
Output is correct |
20 |
Correct |
3 ms |
716 KB |
Output is correct |
21 |
Correct |
3 ms |
716 KB |
Output is correct |
22 |
Correct |
4 ms |
716 KB |
Output is correct |
23 |
Correct |
4 ms |
716 KB |
Output is correct |
24 |
Correct |
3 ms |
716 KB |
Output is correct |
25 |
Correct |
3 ms |
716 KB |
Output is correct |
26 |
Correct |
3 ms |
684 KB |
Output is correct |
27 |
Correct |
3 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
3 ms |
716 KB |
Output is correct |
30 |
Correct |
3 ms |
716 KB |
Output is correct |
31 |
Correct |
3 ms |
716 KB |
Output is correct |
32 |
Correct |
3 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Execution timed out |
1087 ms |
98744 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Execution timed out |
1066 ms |
97968 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1085 ms |
103128 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
4 ms |
716 KB |
Output is correct |
3 |
Correct |
3 ms |
716 KB |
Output is correct |
4 |
Correct |
3 ms |
680 KB |
Output is correct |
5 |
Correct |
3 ms |
716 KB |
Output is correct |
6 |
Correct |
4 ms |
768 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
688 KB |
Output is correct |
11 |
Correct |
3 ms |
716 KB |
Output is correct |
12 |
Correct |
3 ms |
716 KB |
Output is correct |
13 |
Correct |
3 ms |
716 KB |
Output is correct |
14 |
Correct |
3 ms |
716 KB |
Output is correct |
15 |
Correct |
2 ms |
716 KB |
Output is correct |
16 |
Correct |
3 ms |
716 KB |
Output is correct |
17 |
Correct |
3 ms |
684 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
19 |
Correct |
3 ms |
716 KB |
Output is correct |
20 |
Correct |
3 ms |
716 KB |
Output is correct |
21 |
Correct |
3 ms |
716 KB |
Output is correct |
22 |
Correct |
4 ms |
716 KB |
Output is correct |
23 |
Correct |
4 ms |
716 KB |
Output is correct |
24 |
Correct |
3 ms |
716 KB |
Output is correct |
25 |
Correct |
3 ms |
716 KB |
Output is correct |
26 |
Correct |
3 ms |
684 KB |
Output is correct |
27 |
Correct |
3 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
3 ms |
716 KB |
Output is correct |
30 |
Correct |
3 ms |
716 KB |
Output is correct |
31 |
Correct |
3 ms |
716 KB |
Output is correct |
32 |
Correct |
3 ms |
716 KB |
Output is correct |
33 |
Execution timed out |
1087 ms |
98744 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |