#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
struct fenwick {
vector<long long> tree;
int n;
fenwick(int n) : tree(n + 1), n(n) {}
void update(int a, long long x) {
for ( ; a <= n; a += a & -a) {
tree[a] += x;
}
}
long long query(int a) {
long long sum = 0;
for ( ; a > 0; a -= a & -a) {
sum += tree[a];
}
return sum;
}
};
vector<array<long long, 3>> queries[N];
vector<array<long long, 2>> updates[N];
long long s[N], ans[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
}
for (int i = 1; i <= q; ++i) {
int t, l, r;
cin >> t >> l >> r;
queries[l - 1].push_back({t, -1, i});
queries[r].push_back({t, 1, i});
}
vector<int> stack;
for (int i = 1; i <= n; ++i) {
while (!stack.empty() && s[stack.back()] <= s[i]) {
int j = stack.back();
stack.pop_back();
if (!stack.empty()) {
int k = stack.back();
updates[i].push_back({i - k, s[j] - s[k]});
}
}
if (!stack.empty()) {
int j = stack.back();
updates[i].push_back({i - j, s[j] - s[i]});
}
stack.push_back(i);
}
long long prefix = 0;
fenwick add_q(n), add_l(n), sub_q(n), sub_l(n);
for (int i = 1; i <= n; ++i) {
for (auto [j, k] : updates[i]) {
add_l.update(i - j, k);
add_q.update(i - j, (n - i + j) * k);
if (j > 1) {
sub_l.update(j - 1, k);
sub_q.update(j - 1, (j - 1) * k);
}
}
prefix += s[i];
for (auto [j, k, l] : queries[i]) {
long long sum = prefix;
sum += add_q.query(i) - add_q.query(i - j);
sum -= (n - i) * (add_l.query(i) - add_l.query(i - j));
sum += j * add_l.query(i - j);
sum -= sub_q.query(j);
sum -= j * (sub_l.query(n) - sub_l.query(j));
ans[l] += k * sum;
}
}
for (int i = 1; i <= q; ++i) {
cout << ans[i] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9716 KB |
Output is correct |
2 |
Correct |
7 ms |
9680 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9696 KB |
Output is correct |
5 |
Correct |
7 ms |
9712 KB |
Output is correct |
6 |
Correct |
7 ms |
9720 KB |
Output is correct |
7 |
Correct |
7 ms |
9736 KB |
Output is correct |
8 |
Correct |
7 ms |
9736 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9676 KB |
Output is correct |
11 |
Correct |
7 ms |
9700 KB |
Output is correct |
12 |
Correct |
8 ms |
9712 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
8 ms |
9676 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9672 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9712 KB |
Output is correct |
19 |
Correct |
7 ms |
9716 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9720 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9668 KB |
Output is correct |
25 |
Correct |
6 ms |
9676 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
7 ms |
9676 KB |
Output is correct |
29 |
Correct |
7 ms |
9676 KB |
Output is correct |
30 |
Correct |
7 ms |
9804 KB |
Output is correct |
31 |
Correct |
7 ms |
9716 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9716 KB |
Output is correct |
2 |
Correct |
338 ms |
50156 KB |
Output is correct |
3 |
Correct |
321 ms |
49984 KB |
Output is correct |
4 |
Correct |
371 ms |
50464 KB |
Output is correct |
5 |
Correct |
358 ms |
50500 KB |
Output is correct |
6 |
Correct |
339 ms |
49956 KB |
Output is correct |
7 |
Correct |
350 ms |
50692 KB |
Output is correct |
8 |
Correct |
359 ms |
50612 KB |
Output is correct |
9 |
Correct |
343 ms |
50760 KB |
Output is correct |
10 |
Correct |
339 ms |
49944 KB |
Output is correct |
11 |
Correct |
331 ms |
50924 KB |
Output is correct |
12 |
Correct |
348 ms |
49704 KB |
Output is correct |
13 |
Correct |
336 ms |
50148 KB |
Output is correct |
14 |
Correct |
336 ms |
49972 KB |
Output is correct |
15 |
Correct |
333 ms |
50264 KB |
Output is correct |
16 |
Correct |
385 ms |
50024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9716 KB |
Output is correct |
2 |
Correct |
384 ms |
49608 KB |
Output is correct |
3 |
Correct |
374 ms |
48708 KB |
Output is correct |
4 |
Correct |
366 ms |
49984 KB |
Output is correct |
5 |
Correct |
359 ms |
48816 KB |
Output is correct |
6 |
Correct |
372 ms |
49092 KB |
Output is correct |
7 |
Correct |
355 ms |
49280 KB |
Output is correct |
8 |
Correct |
397 ms |
49476 KB |
Output is correct |
9 |
Correct |
362 ms |
49028 KB |
Output is correct |
10 |
Correct |
358 ms |
48580 KB |
Output is correct |
11 |
Correct |
363 ms |
49840 KB |
Output is correct |
12 |
Correct |
355 ms |
49172 KB |
Output is correct |
13 |
Correct |
364 ms |
49796 KB |
Output is correct |
14 |
Correct |
358 ms |
49020 KB |
Output is correct |
15 |
Correct |
362 ms |
49480 KB |
Output is correct |
16 |
Correct |
359 ms |
48924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
345 ms |
42172 KB |
Output is correct |
2 |
Correct |
330 ms |
41976 KB |
Output is correct |
3 |
Correct |
349 ms |
42808 KB |
Output is correct |
4 |
Correct |
354 ms |
42564 KB |
Output is correct |
5 |
Correct |
358 ms |
42400 KB |
Output is correct |
6 |
Correct |
333 ms |
41868 KB |
Output is correct |
7 |
Correct |
330 ms |
42404 KB |
Output is correct |
8 |
Correct |
421 ms |
42524 KB |
Output is correct |
9 |
Correct |
360 ms |
42376 KB |
Output is correct |
10 |
Correct |
335 ms |
42120 KB |
Output is correct |
11 |
Correct |
344 ms |
42648 KB |
Output is correct |
12 |
Correct |
363 ms |
42580 KB |
Output is correct |
13 |
Correct |
367 ms |
42380 KB |
Output is correct |
14 |
Correct |
347 ms |
42600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9716 KB |
Output is correct |
2 |
Correct |
7 ms |
9680 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9696 KB |
Output is correct |
5 |
Correct |
7 ms |
9712 KB |
Output is correct |
6 |
Correct |
7 ms |
9720 KB |
Output is correct |
7 |
Correct |
7 ms |
9736 KB |
Output is correct |
8 |
Correct |
7 ms |
9736 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9676 KB |
Output is correct |
11 |
Correct |
7 ms |
9700 KB |
Output is correct |
12 |
Correct |
8 ms |
9712 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
8 ms |
9676 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9672 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9712 KB |
Output is correct |
19 |
Correct |
7 ms |
9716 KB |
Output is correct |
20 |
Correct |
7 ms |
9728 KB |
Output is correct |
21 |
Correct |
7 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9720 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9668 KB |
Output is correct |
25 |
Correct |
6 ms |
9676 KB |
Output is correct |
26 |
Correct |
7 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
7 ms |
9676 KB |
Output is correct |
29 |
Correct |
7 ms |
9676 KB |
Output is correct |
30 |
Correct |
7 ms |
9804 KB |
Output is correct |
31 |
Correct |
7 ms |
9716 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
33 |
Correct |
338 ms |
50156 KB |
Output is correct |
34 |
Correct |
321 ms |
49984 KB |
Output is correct |
35 |
Correct |
371 ms |
50464 KB |
Output is correct |
36 |
Correct |
358 ms |
50500 KB |
Output is correct |
37 |
Correct |
339 ms |
49956 KB |
Output is correct |
38 |
Correct |
350 ms |
50692 KB |
Output is correct |
39 |
Correct |
359 ms |
50612 KB |
Output is correct |
40 |
Correct |
343 ms |
50760 KB |
Output is correct |
41 |
Correct |
339 ms |
49944 KB |
Output is correct |
42 |
Correct |
331 ms |
50924 KB |
Output is correct |
43 |
Correct |
348 ms |
49704 KB |
Output is correct |
44 |
Correct |
336 ms |
50148 KB |
Output is correct |
45 |
Correct |
336 ms |
49972 KB |
Output is correct |
46 |
Correct |
333 ms |
50264 KB |
Output is correct |
47 |
Correct |
385 ms |
50024 KB |
Output is correct |
48 |
Correct |
384 ms |
49608 KB |
Output is correct |
49 |
Correct |
374 ms |
48708 KB |
Output is correct |
50 |
Correct |
366 ms |
49984 KB |
Output is correct |
51 |
Correct |
359 ms |
48816 KB |
Output is correct |
52 |
Correct |
372 ms |
49092 KB |
Output is correct |
53 |
Correct |
355 ms |
49280 KB |
Output is correct |
54 |
Correct |
397 ms |
49476 KB |
Output is correct |
55 |
Correct |
362 ms |
49028 KB |
Output is correct |
56 |
Correct |
358 ms |
48580 KB |
Output is correct |
57 |
Correct |
363 ms |
49840 KB |
Output is correct |
58 |
Correct |
355 ms |
49172 KB |
Output is correct |
59 |
Correct |
364 ms |
49796 KB |
Output is correct |
60 |
Correct |
358 ms |
49020 KB |
Output is correct |
61 |
Correct |
362 ms |
49480 KB |
Output is correct |
62 |
Correct |
359 ms |
48924 KB |
Output is correct |
63 |
Correct |
345 ms |
42172 KB |
Output is correct |
64 |
Correct |
330 ms |
41976 KB |
Output is correct |
65 |
Correct |
349 ms |
42808 KB |
Output is correct |
66 |
Correct |
354 ms |
42564 KB |
Output is correct |
67 |
Correct |
358 ms |
42400 KB |
Output is correct |
68 |
Correct |
333 ms |
41868 KB |
Output is correct |
69 |
Correct |
330 ms |
42404 KB |
Output is correct |
70 |
Correct |
421 ms |
42524 KB |
Output is correct |
71 |
Correct |
360 ms |
42376 KB |
Output is correct |
72 |
Correct |
335 ms |
42120 KB |
Output is correct |
73 |
Correct |
344 ms |
42648 KB |
Output is correct |
74 |
Correct |
363 ms |
42580 KB |
Output is correct |
75 |
Correct |
367 ms |
42380 KB |
Output is correct |
76 |
Correct |
347 ms |
42600 KB |
Output is correct |
77 |
Correct |
418 ms |
50140 KB |
Output is correct |
78 |
Correct |
418 ms |
51008 KB |
Output is correct |
79 |
Correct |
443 ms |
50420 KB |
Output is correct |
80 |
Correct |
406 ms |
50216 KB |
Output is correct |
81 |
Correct |
390 ms |
49864 KB |
Output is correct |
82 |
Correct |
414 ms |
50308 KB |
Output is correct |
83 |
Correct |
395 ms |
50048 KB |
Output is correct |
84 |
Correct |
419 ms |
50204 KB |
Output is correct |
85 |
Correct |
421 ms |
50240 KB |
Output is correct |
86 |
Correct |
412 ms |
50332 KB |
Output is correct |
87 |
Correct |
364 ms |
51056 KB |
Output is correct |
88 |
Correct |
347 ms |
50568 KB |
Output is correct |
89 |
Correct |
381 ms |
49876 KB |
Output is correct |
90 |
Correct |
336 ms |
50392 KB |
Output is correct |
91 |
Correct |
349 ms |
49804 KB |
Output is correct |
92 |
Correct |
339 ms |
49656 KB |
Output is correct |
93 |
Correct |
340 ms |
50024 KB |
Output is correct |
94 |
Correct |
356 ms |
51072 KB |
Output is correct |
95 |
Correct |
350 ms |
51016 KB |
Output is correct |
96 |
Correct |
349 ms |
50372 KB |
Output is correct |
97 |
Correct |
356 ms |
50348 KB |
Output is correct |
98 |
Correct |
353 ms |
49476 KB |
Output is correct |
99 |
Correct |
364 ms |
49676 KB |
Output is correct |
100 |
Correct |
400 ms |
50112 KB |
Output is correct |
101 |
Correct |
356 ms |
49604 KB |
Output is correct |
102 |
Correct |
380 ms |
50576 KB |
Output is correct |