This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <tuple>
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
long long tree_squares[524288], tree_parallels[1048576];
long long lazy_squares[524288], lazy_parallels[1048576];
long long mul_triangles[200006], add_triangles[200006];
long long query_triangles(int x) {
x++;
int st = x;
long long mul = 0, add = 0;
while (x > 0) {
mul += mul_triangles[x];
add += add_triangles[x];
x -= x & -x;
}
return st * mul + add;
}
long long query_triangles(int l, int r) {
if (l > r) return 0;
return query_triangles(r) - query_triangles(l - 1);
}
void update_triangles_int(int x, long long mul, long long add) {
while (x < 200005) {
mul_triangles[x] += mul;
add_triangles[x] += add;
x += x & -x;
}
}
void update_triangles(int l, int r, long long v) {
l++, r++;
if (l > r) return;
update_triangles_int(l, v, -v * (l - 1));
update_triangles_int(r + 1, -v, v * r);
}
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_triangles(0, a - 1, b);
}
int k = 0, l = 0;
for (int i = 0; i < q; i++) {
auto [ a, b, c, d ] = queries[i];
while (k < (int)part_squares.size() && get<0>(part_squares[k]) <= a) {
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]) <= a) {
auto [ a, b, c, d ] = part_parallels[l];
update(tree_parallels, lazy_parallels, 0, 0, 524287, 262143 + b, 262143 + c, d);
l++;
}
res[d] += query_triangles(max(a, 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 - a, 262143 + c - a);
}
for (int i = 0; i < q; i++) printf("%lld\n", res[i]);
}
Compilation message (stderr)
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:116:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:117:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | for (int i = 0; i < n; i++) scanf("%d", a + i);
| ~~~~~^~~~~~~~~~~~~
ho_t5.cpp:139:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | scanf("%d%d%d", &t, &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |