#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 2e5 + 7;
int n, q, a[N], l[N], r[N];
vector <int> d[N];
vector <ii> mem[N], meml[N];
void add(int l, int r, int i, int x) {
mem[l].app(mp(i, x));
mem[r + 1].app(mp(i, -x));
}
void addl(int l, int r, int i, int x) {
meml[l].app(mp(i, x));
meml[r + 1].app(mp(i, -x));
}
int link_l[N], link_r[N];
int ans[N];
struct Fen {
int f[N];
void clear() {
for (int i = 0; i < N; ++i) f[i] = 0;
}
void add(int i, int x) {
for (; i < N; i |= i + 1)
f[i] += x;
}
int get(int i) {
int ans = 0;
for (; i >= 0; i &= i + 1, --i) ans += f[i];
return ans;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
} f, fl;
int pref_mx[N], pref_sum[N];
//bigger left, not less right
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> q;
for (int i = 0; i < n; ++i) {
cin >> a[i];
pref_mx[i + 1] = max(pref_mx[i], a[i]);
pref_sum[i + 1] = pref_sum[i] + pref_mx[i + 1];
}
for (int i = 0; i < q; ++i) {
int t;
cin >> t >> l[i] >> r[i];
--l[i]; --r[i];
d[t + 1].app(i);
}
vector <int> s;
for (int i = 0; i < n; ++i) {
while (s.size() && a[s.back()] <= a[i]) {
link_r[s.back()] = i;
s.pop_back();
}
s.app(i);
}
for (int e : s)
link_r[e] = n;
s.clear();
for (int i = n - 1; i >= 0; --i) {
while (s.size() && a[s.back()] < a[i]) {
link_l[s.back()] = i;
s.pop_back();
}
s.app(i);
}
for (int e : s)
link_l[e] = -1;
s.clear();
for (int i = 0; i < n; ++i) {
if (link_r[i] - i < i - link_l[i]) {
for (int j = i; j < link_r[i]; ++j) {
int len1 = j - i + 1;
int len2 = j - link_l[i];
add(len1, len2, j, a[i]);
}
}
else {
for (int j = link_l[i] + 1; j <= i; ++j) {
int len1 = i - j + 1;
int len2 = link_r[i] - j;
if (link_r[i] == n)
len2 = N - 2;
addl(len1, len2, j, a[i]);
}
}
}
f.clear();
fl.clear();
for (int len = 1; len < N; ++len) {
for (auto e : mem[len])
f.add(e.f, e.s);
for (auto e : meml[len]) {
fl.add(e.f, e.s);
}
for (auto e : d[len]) {
ans[e] += f.get(l[e], r[e]);
if (l[e] + 1 < len) {
int t = min(len - 1, r[e] + 1);
ans[e] += pref_sum[t] - pref_sum[l[e]];
l[e] = t;
}
if (l[e] <= r[e])
ans[e] += fl.get(l[e] - len + 1, r[e] - len + 1);
}
}
for (int i = 0; i < q; ++i)
cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
16 ms |
17656 KB |
Output is correct |
3 |
Correct |
17 ms |
17656 KB |
Output is correct |
4 |
Correct |
16 ms |
17656 KB |
Output is correct |
5 |
Correct |
17 ms |
17656 KB |
Output is correct |
6 |
Correct |
17 ms |
17656 KB |
Output is correct |
7 |
Correct |
17 ms |
17656 KB |
Output is correct |
8 |
Correct |
19 ms |
17656 KB |
Output is correct |
9 |
Correct |
16 ms |
17656 KB |
Output is correct |
10 |
Correct |
16 ms |
17656 KB |
Output is correct |
11 |
Correct |
16 ms |
17656 KB |
Output is correct |
12 |
Correct |
17 ms |
17656 KB |
Output is correct |
13 |
Correct |
16 ms |
17656 KB |
Output is correct |
14 |
Correct |
16 ms |
17660 KB |
Output is correct |
15 |
Correct |
16 ms |
17656 KB |
Output is correct |
16 |
Correct |
16 ms |
17656 KB |
Output is correct |
17 |
Correct |
17 ms |
17784 KB |
Output is correct |
18 |
Correct |
16 ms |
17656 KB |
Output is correct |
19 |
Correct |
16 ms |
17656 KB |
Output is correct |
20 |
Correct |
16 ms |
17656 KB |
Output is correct |
21 |
Correct |
17 ms |
17656 KB |
Output is correct |
22 |
Correct |
17 ms |
17656 KB |
Output is correct |
23 |
Correct |
16 ms |
17656 KB |
Output is correct |
24 |
Correct |
16 ms |
17656 KB |
Output is correct |
25 |
Correct |
16 ms |
17784 KB |
Output is correct |
26 |
Correct |
16 ms |
17656 KB |
Output is correct |
27 |
Correct |
16 ms |
17656 KB |
Output is correct |
28 |
Correct |
17 ms |
17656 KB |
Output is correct |
29 |
Correct |
16 ms |
17656 KB |
Output is correct |
30 |
Correct |
17 ms |
17656 KB |
Output is correct |
31 |
Correct |
16 ms |
17656 KB |
Output is correct |
32 |
Correct |
16 ms |
17656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
302 ms |
85496 KB |
Output is correct |
3 |
Correct |
295 ms |
84944 KB |
Output is correct |
4 |
Correct |
321 ms |
86132 KB |
Output is correct |
5 |
Correct |
302 ms |
84836 KB |
Output is correct |
6 |
Correct |
291 ms |
85220 KB |
Output is correct |
7 |
Correct |
306 ms |
85736 KB |
Output is correct |
8 |
Correct |
305 ms |
89700 KB |
Output is correct |
9 |
Correct |
299 ms |
84068 KB |
Output is correct |
10 |
Correct |
290 ms |
82408 KB |
Output is correct |
11 |
Correct |
304 ms |
85980 KB |
Output is correct |
12 |
Correct |
286 ms |
82780 KB |
Output is correct |
13 |
Correct |
307 ms |
86372 KB |
Output is correct |
14 |
Correct |
294 ms |
84452 KB |
Output is correct |
15 |
Correct |
292 ms |
83940 KB |
Output is correct |
16 |
Correct |
299 ms |
85604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
379 ms |
84688 KB |
Output is correct |
3 |
Correct |
353 ms |
82760 KB |
Output is correct |
4 |
Correct |
401 ms |
87756 KB |
Output is correct |
5 |
Correct |
365 ms |
84036 KB |
Output is correct |
6 |
Correct |
374 ms |
88528 KB |
Output is correct |
7 |
Correct |
389 ms |
89296 KB |
Output is correct |
8 |
Correct |
376 ms |
86092 KB |
Output is correct |
9 |
Correct |
394 ms |
85316 KB |
Output is correct |
10 |
Correct |
376 ms |
90180 KB |
Output is correct |
11 |
Correct |
385 ms |
87244 KB |
Output is correct |
12 |
Correct |
362 ms |
85832 KB |
Output is correct |
13 |
Correct |
381 ms |
86868 KB |
Output is correct |
14 |
Correct |
372 ms |
85224 KB |
Output is correct |
15 |
Correct |
386 ms |
88392 KB |
Output is correct |
16 |
Correct |
374 ms |
86344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
247 ms |
48352 KB |
Output is correct |
2 |
Correct |
253 ms |
48312 KB |
Output is correct |
3 |
Correct |
241 ms |
48988 KB |
Output is correct |
4 |
Correct |
250 ms |
48356 KB |
Output is correct |
5 |
Correct |
250 ms |
48220 KB |
Output is correct |
6 |
Correct |
240 ms |
48476 KB |
Output is correct |
7 |
Correct |
252 ms |
49088 KB |
Output is correct |
8 |
Correct |
241 ms |
48740 KB |
Output is correct |
9 |
Correct |
254 ms |
48376 KB |
Output is correct |
10 |
Correct |
241 ms |
48616 KB |
Output is correct |
11 |
Correct |
255 ms |
48868 KB |
Output is correct |
12 |
Correct |
256 ms |
48484 KB |
Output is correct |
13 |
Correct |
244 ms |
48356 KB |
Output is correct |
14 |
Correct |
251 ms |
48476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
16 ms |
17656 KB |
Output is correct |
3 |
Correct |
17 ms |
17656 KB |
Output is correct |
4 |
Correct |
16 ms |
17656 KB |
Output is correct |
5 |
Correct |
17 ms |
17656 KB |
Output is correct |
6 |
Correct |
17 ms |
17656 KB |
Output is correct |
7 |
Correct |
17 ms |
17656 KB |
Output is correct |
8 |
Correct |
19 ms |
17656 KB |
Output is correct |
9 |
Correct |
16 ms |
17656 KB |
Output is correct |
10 |
Correct |
16 ms |
17656 KB |
Output is correct |
11 |
Correct |
16 ms |
17656 KB |
Output is correct |
12 |
Correct |
17 ms |
17656 KB |
Output is correct |
13 |
Correct |
16 ms |
17656 KB |
Output is correct |
14 |
Correct |
16 ms |
17660 KB |
Output is correct |
15 |
Correct |
16 ms |
17656 KB |
Output is correct |
16 |
Correct |
16 ms |
17656 KB |
Output is correct |
17 |
Correct |
17 ms |
17784 KB |
Output is correct |
18 |
Correct |
16 ms |
17656 KB |
Output is correct |
19 |
Correct |
16 ms |
17656 KB |
Output is correct |
20 |
Correct |
16 ms |
17656 KB |
Output is correct |
21 |
Correct |
17 ms |
17656 KB |
Output is correct |
22 |
Correct |
17 ms |
17656 KB |
Output is correct |
23 |
Correct |
16 ms |
17656 KB |
Output is correct |
24 |
Correct |
16 ms |
17656 KB |
Output is correct |
25 |
Correct |
16 ms |
17784 KB |
Output is correct |
26 |
Correct |
16 ms |
17656 KB |
Output is correct |
27 |
Correct |
16 ms |
17656 KB |
Output is correct |
28 |
Correct |
17 ms |
17656 KB |
Output is correct |
29 |
Correct |
16 ms |
17656 KB |
Output is correct |
30 |
Correct |
17 ms |
17656 KB |
Output is correct |
31 |
Correct |
16 ms |
17656 KB |
Output is correct |
32 |
Correct |
16 ms |
17656 KB |
Output is correct |
33 |
Correct |
382 ms |
88392 KB |
Output is correct |
34 |
Correct |
384 ms |
88124 KB |
Output is correct |
35 |
Correct |
399 ms |
90832 KB |
Output is correct |
36 |
Correct |
408 ms |
86860 KB |
Output is correct |
37 |
Correct |
393 ms |
87116 KB |
Output is correct |
38 |
Correct |
412 ms |
85840 KB |
Output is correct |
39 |
Correct |
385 ms |
86336 KB |
Output is correct |
40 |
Correct |
398 ms |
86736 KB |
Output is correct |
41 |
Correct |
379 ms |
86604 KB |
Output is correct |
42 |
Correct |
385 ms |
85308 KB |
Output is correct |
43 |
Correct |
365 ms |
88016 KB |
Output is correct |
44 |
Correct |
327 ms |
85444 KB |
Output is correct |
45 |
Correct |
347 ms |
84428 KB |
Output is correct |
46 |
Correct |
345 ms |
87108 KB |
Output is correct |
47 |
Correct |
338 ms |
83784 KB |
Output is correct |
48 |
Correct |
321 ms |
82492 KB |
Output is correct |
49 |
Correct |
339 ms |
85832 KB |
Output is correct |
50 |
Correct |
349 ms |
89164 KB |
Output is correct |
51 |
Correct |
338 ms |
86476 KB |
Output is correct |
52 |
Correct |
339 ms |
84424 KB |
Output is correct |
53 |
Correct |
352 ms |
84044 KB |
Output is correct |
54 |
Correct |
357 ms |
84568 KB |
Output is correct |
55 |
Correct |
343 ms |
84540 KB |
Output is correct |
56 |
Correct |
349 ms |
85832 KB |
Output is correct |
57 |
Correct |
345 ms |
85444 KB |
Output is correct |
58 |
Correct |
354 ms |
87884 KB |
Output is correct |
59 |
Correct |
302 ms |
85496 KB |
Output is correct |
60 |
Correct |
295 ms |
84944 KB |
Output is correct |
61 |
Correct |
321 ms |
86132 KB |
Output is correct |
62 |
Correct |
302 ms |
84836 KB |
Output is correct |
63 |
Correct |
291 ms |
85220 KB |
Output is correct |
64 |
Correct |
306 ms |
85736 KB |
Output is correct |
65 |
Correct |
305 ms |
89700 KB |
Output is correct |
66 |
Correct |
299 ms |
84068 KB |
Output is correct |
67 |
Correct |
290 ms |
82408 KB |
Output is correct |
68 |
Correct |
304 ms |
85980 KB |
Output is correct |
69 |
Correct |
286 ms |
82780 KB |
Output is correct |
70 |
Correct |
307 ms |
86372 KB |
Output is correct |
71 |
Correct |
294 ms |
84452 KB |
Output is correct |
72 |
Correct |
292 ms |
83940 KB |
Output is correct |
73 |
Correct |
299 ms |
85604 KB |
Output is correct |
74 |
Correct |
379 ms |
84688 KB |
Output is correct |
75 |
Correct |
353 ms |
82760 KB |
Output is correct |
76 |
Correct |
401 ms |
87756 KB |
Output is correct |
77 |
Correct |
365 ms |
84036 KB |
Output is correct |
78 |
Correct |
374 ms |
88528 KB |
Output is correct |
79 |
Correct |
389 ms |
89296 KB |
Output is correct |
80 |
Correct |
376 ms |
86092 KB |
Output is correct |
81 |
Correct |
394 ms |
85316 KB |
Output is correct |
82 |
Correct |
376 ms |
90180 KB |
Output is correct |
83 |
Correct |
385 ms |
87244 KB |
Output is correct |
84 |
Correct |
362 ms |
85832 KB |
Output is correct |
85 |
Correct |
381 ms |
86868 KB |
Output is correct |
86 |
Correct |
372 ms |
85224 KB |
Output is correct |
87 |
Correct |
386 ms |
88392 KB |
Output is correct |
88 |
Correct |
374 ms |
86344 KB |
Output is correct |
89 |
Correct |
247 ms |
48352 KB |
Output is correct |
90 |
Correct |
253 ms |
48312 KB |
Output is correct |
91 |
Correct |
241 ms |
48988 KB |
Output is correct |
92 |
Correct |
250 ms |
48356 KB |
Output is correct |
93 |
Correct |
250 ms |
48220 KB |
Output is correct |
94 |
Correct |
240 ms |
48476 KB |
Output is correct |
95 |
Correct |
252 ms |
49088 KB |
Output is correct |
96 |
Correct |
241 ms |
48740 KB |
Output is correct |
97 |
Correct |
254 ms |
48376 KB |
Output is correct |
98 |
Correct |
241 ms |
48616 KB |
Output is correct |
99 |
Correct |
255 ms |
48868 KB |
Output is correct |
100 |
Correct |
256 ms |
48484 KB |
Output is correct |
101 |
Correct |
244 ms |
48356 KB |
Output is correct |
102 |
Correct |
251 ms |
48476 KB |
Output is correct |