//Challenge: Accepted
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define mod 998244353
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
struct query{
int l, r, id;
query(){l = r = id = 0;}
query(int a, int b, int c){l = a, r = b, id = c;}
};
struct line{
ll m, k;
line(){m = k = 0;}
line(ll x, ll y){m = x, k = y;}
ll val(ll x){return m * x + k;}
line operator +(line l){
return line(m + l.m, k + l.k);
}
};
struct segtree{
line seg[4 * maxn];
void init(int cur, int l, int r, ll a[]) {
if (r <= l) return;
if (r - l == 1) {
seg[cur] = line{a[l], a[l]};
return;
}
int m = (l + r) / 2;
init(cur*2, l, m, a), init(cur*2+1, m, r, a);
seg[cur] = seg[cur*2] + seg[cur*2+1];
}
void modify(int cur, int l, int r, int ind, line li) {
if (r <= l) return;
if (r - l == 1) {
seg[cur] = li;
return;
}
int m = (l + r) / 2;
if (ind < m) modify(cur*2, l, m, ind, li);
else modify(cur*2+1, m, r, ind, li);
seg[cur] = seg[cur*2] + seg[cur*2+1];
}
ll query(int cur, int l, int r, int ql, int qr, int x) {
if (r <= l || ql >= r || qr <= l) return 0;
if (ql <= l && qr >= r) return seg[cur].val(x);
int m = (l + r) / 2;
return query(cur*2, l, m, ql, qr, x) + query(cur*2+1, m, r, ql, qr, x);
}
} tr;
struct sparse_table{
pii sp[18][maxn];
void build(int n, ll a[]) {
for (int i = 1;i <= n;i++) sp[0][i] = {a[i], i};
for (int i = 1;i < 18;i++) {
for (int j = 1;j <= n - (1<<i) + 1;j++) {
sp[i][j] = max(sp[i-1][j], sp[i-1][j + (1<<(i-1))]);
}
}
}
int query(int l, int r) { //[l, r]
l = max(l, 1);
int h = __lg(r - l + 1);
return max(sp[h][l], sp[h][r - (1<<h) + 1]).ss;
}
} sp;
vector<query> que[maxn];
vector<int> ev[maxn];
ll a[maxn], ans[maxn];
int lef[maxn], rig[maxn], type[maxn];
int main() {
io
int n, q;
cin >> n >> q;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 0;i < q;i++) {
int t, l, r;
cin >> t >> l >> r;
que[t].push_back(query(l, r, i));
}
stack<int> stk;
for (int i = 1;i <= n;i++) {
while (stk.size() && a[i] >= a[stk.top()]) {
rig[stk.top()] = i;
stk.pop();
}
if (stk.size()) lef[i] = stk.top();
else lef[i] = -n;
stk.push(i);
}
while (stk.size()) {
rig[stk.top()] = n + 1;
stk.pop();
}
for (int i = 1;i <= n;i++) {
if (i - lef[i] - 1 < maxn) ev[i - lef[i] - 1].push_back(i);
if (rig[i] - i - 1 < maxn) ev[rig[i] - i - 1].push_back(i);
if (rig[i] - lef[i] < maxn) ev[rig[i] - lef[i]].push_back(i);
}
tr.init(1, 1, n+1, a);
sp.build(n, a);
for (int i = 0;i <= n;i++) {
for (int j:ev[i]) {
//debug("ch", j);
if (type[j] < 2) {
ll cur = tr.query(1, 1, n+1, j, j+1, i);
tr.modify(1, 1, n+1, j, line(-type[j] * a[j], cur + type[j] * a[j] * i));
} else {
tr.modify(1, 1, n+1, j, line());
}
type[j]++;
}
for (auto [l, r, id]:que[i]) {
ans[id] = tr.query(1, 1, n+1, l - i, r+1, i);
debug(i, l, r);
int mr = sp.query(r-i, r), ml = sp.query(l - i, l);
debug(ans[id], ml, mr);
ans[id] -= (min(rig[mr] - 1, mr + i) - r) * a[mr];
ans[id] -= (l - max(lef[ml] + i + 1, ml)) * a[ml];
ans[id] -= tr.query(1, 1, n+1, l - i, ml, i);
ans[id] -= tr.query(1, 1, n+1, mr+1, r+1, i);
}
}
for (int i = 0;i < q;i++) cout << ans[i] << "\n";
}
/*
10 1
3 1 4 1 5 9 2 6 5 3
3 2 8
*/
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 0
| ^
ho_t5.cpp:131:4: note: in expansion of macro 'debug'
131 | debug(i, l, r);
| ^~~~~
ho_t5.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 0
| ^
ho_t5.cpp:133:4: note: in expansion of macro 'debug'
133 | debug(ans[id], ml, mr);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
13 ms |
22356 KB |
Output is correct |
3 |
Correct |
11 ms |
22276 KB |
Output is correct |
4 |
Correct |
13 ms |
22356 KB |
Output is correct |
5 |
Correct |
13 ms |
22328 KB |
Output is correct |
6 |
Correct |
13 ms |
22356 KB |
Output is correct |
7 |
Correct |
12 ms |
22356 KB |
Output is correct |
8 |
Correct |
12 ms |
22368 KB |
Output is correct |
9 |
Correct |
12 ms |
22356 KB |
Output is correct |
10 |
Correct |
13 ms |
22356 KB |
Output is correct |
11 |
Correct |
11 ms |
22356 KB |
Output is correct |
12 |
Correct |
12 ms |
22356 KB |
Output is correct |
13 |
Correct |
12 ms |
22300 KB |
Output is correct |
14 |
Correct |
12 ms |
22352 KB |
Output is correct |
15 |
Correct |
12 ms |
22364 KB |
Output is correct |
16 |
Correct |
12 ms |
22292 KB |
Output is correct |
17 |
Correct |
11 ms |
22356 KB |
Output is correct |
18 |
Correct |
13 ms |
22260 KB |
Output is correct |
19 |
Correct |
12 ms |
22356 KB |
Output is correct |
20 |
Correct |
14 ms |
22340 KB |
Output is correct |
21 |
Correct |
15 ms |
22356 KB |
Output is correct |
22 |
Correct |
13 ms |
22356 KB |
Output is correct |
23 |
Correct |
13 ms |
22356 KB |
Output is correct |
24 |
Correct |
16 ms |
22356 KB |
Output is correct |
25 |
Correct |
12 ms |
22356 KB |
Output is correct |
26 |
Correct |
12 ms |
22256 KB |
Output is correct |
27 |
Correct |
11 ms |
22356 KB |
Output is correct |
28 |
Correct |
12 ms |
22260 KB |
Output is correct |
29 |
Correct |
11 ms |
22356 KB |
Output is correct |
30 |
Correct |
12 ms |
22356 KB |
Output is correct |
31 |
Correct |
12 ms |
22256 KB |
Output is correct |
32 |
Correct |
12 ms |
22356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
456 ms |
61596 KB |
Output is correct |
3 |
Correct |
384 ms |
66804 KB |
Output is correct |
4 |
Correct |
420 ms |
66932 KB |
Output is correct |
5 |
Correct |
488 ms |
68060 KB |
Output is correct |
6 |
Correct |
466 ms |
67140 KB |
Output is correct |
7 |
Correct |
431 ms |
67284 KB |
Output is correct |
8 |
Correct |
536 ms |
67968 KB |
Output is correct |
9 |
Correct |
462 ms |
67252 KB |
Output is correct |
10 |
Correct |
440 ms |
66264 KB |
Output is correct |
11 |
Correct |
462 ms |
67628 KB |
Output is correct |
12 |
Correct |
433 ms |
66684 KB |
Output is correct |
13 |
Correct |
492 ms |
67792 KB |
Output is correct |
14 |
Correct |
444 ms |
67452 KB |
Output is correct |
15 |
Correct |
470 ms |
67844 KB |
Output is correct |
16 |
Correct |
514 ms |
67280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
478 ms |
61824 KB |
Output is correct |
3 |
Correct |
463 ms |
66744 KB |
Output is correct |
4 |
Correct |
515 ms |
68904 KB |
Output is correct |
5 |
Correct |
553 ms |
67092 KB |
Output is correct |
6 |
Correct |
564 ms |
67780 KB |
Output is correct |
7 |
Correct |
493 ms |
68304 KB |
Output is correct |
8 |
Correct |
469 ms |
67472 KB |
Output is correct |
9 |
Correct |
476 ms |
66972 KB |
Output is correct |
10 |
Correct |
499 ms |
66656 KB |
Output is correct |
11 |
Correct |
523 ms |
68924 KB |
Output is correct |
12 |
Correct |
525 ms |
68376 KB |
Output is correct |
13 |
Correct |
533 ms |
68492 KB |
Output is correct |
14 |
Correct |
534 ms |
67300 KB |
Output is correct |
15 |
Correct |
551 ms |
68624 KB |
Output is correct |
16 |
Correct |
478 ms |
68292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
464 ms |
60480 KB |
Output is correct |
2 |
Correct |
470 ms |
65024 KB |
Output is correct |
3 |
Correct |
502 ms |
66028 KB |
Output is correct |
4 |
Correct |
464 ms |
64724 KB |
Output is correct |
5 |
Correct |
465 ms |
64700 KB |
Output is correct |
6 |
Correct |
491 ms |
64696 KB |
Output is correct |
7 |
Correct |
492 ms |
65672 KB |
Output is correct |
8 |
Correct |
470 ms |
65364 KB |
Output is correct |
9 |
Correct |
467 ms |
64708 KB |
Output is correct |
10 |
Correct |
479 ms |
65076 KB |
Output is correct |
11 |
Correct |
506 ms |
64984 KB |
Output is correct |
12 |
Correct |
439 ms |
64964 KB |
Output is correct |
13 |
Correct |
514 ms |
64936 KB |
Output is correct |
14 |
Correct |
576 ms |
65180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
13 ms |
22356 KB |
Output is correct |
3 |
Correct |
11 ms |
22276 KB |
Output is correct |
4 |
Correct |
13 ms |
22356 KB |
Output is correct |
5 |
Correct |
13 ms |
22328 KB |
Output is correct |
6 |
Correct |
13 ms |
22356 KB |
Output is correct |
7 |
Correct |
12 ms |
22356 KB |
Output is correct |
8 |
Correct |
12 ms |
22368 KB |
Output is correct |
9 |
Correct |
12 ms |
22356 KB |
Output is correct |
10 |
Correct |
13 ms |
22356 KB |
Output is correct |
11 |
Correct |
11 ms |
22356 KB |
Output is correct |
12 |
Correct |
12 ms |
22356 KB |
Output is correct |
13 |
Correct |
12 ms |
22300 KB |
Output is correct |
14 |
Correct |
12 ms |
22352 KB |
Output is correct |
15 |
Correct |
12 ms |
22364 KB |
Output is correct |
16 |
Correct |
12 ms |
22292 KB |
Output is correct |
17 |
Correct |
11 ms |
22356 KB |
Output is correct |
18 |
Correct |
13 ms |
22260 KB |
Output is correct |
19 |
Correct |
12 ms |
22356 KB |
Output is correct |
20 |
Correct |
14 ms |
22340 KB |
Output is correct |
21 |
Correct |
15 ms |
22356 KB |
Output is correct |
22 |
Correct |
13 ms |
22356 KB |
Output is correct |
23 |
Correct |
13 ms |
22356 KB |
Output is correct |
24 |
Correct |
16 ms |
22356 KB |
Output is correct |
25 |
Correct |
12 ms |
22356 KB |
Output is correct |
26 |
Correct |
12 ms |
22256 KB |
Output is correct |
27 |
Correct |
11 ms |
22356 KB |
Output is correct |
28 |
Correct |
12 ms |
22260 KB |
Output is correct |
29 |
Correct |
11 ms |
22356 KB |
Output is correct |
30 |
Correct |
12 ms |
22356 KB |
Output is correct |
31 |
Correct |
12 ms |
22256 KB |
Output is correct |
32 |
Correct |
12 ms |
22356 KB |
Output is correct |
33 |
Correct |
456 ms |
61596 KB |
Output is correct |
34 |
Correct |
384 ms |
66804 KB |
Output is correct |
35 |
Correct |
420 ms |
66932 KB |
Output is correct |
36 |
Correct |
488 ms |
68060 KB |
Output is correct |
37 |
Correct |
466 ms |
67140 KB |
Output is correct |
38 |
Correct |
431 ms |
67284 KB |
Output is correct |
39 |
Correct |
536 ms |
67968 KB |
Output is correct |
40 |
Correct |
462 ms |
67252 KB |
Output is correct |
41 |
Correct |
440 ms |
66264 KB |
Output is correct |
42 |
Correct |
462 ms |
67628 KB |
Output is correct |
43 |
Correct |
433 ms |
66684 KB |
Output is correct |
44 |
Correct |
492 ms |
67792 KB |
Output is correct |
45 |
Correct |
444 ms |
67452 KB |
Output is correct |
46 |
Correct |
470 ms |
67844 KB |
Output is correct |
47 |
Correct |
514 ms |
67280 KB |
Output is correct |
48 |
Correct |
478 ms |
61824 KB |
Output is correct |
49 |
Correct |
463 ms |
66744 KB |
Output is correct |
50 |
Correct |
515 ms |
68904 KB |
Output is correct |
51 |
Correct |
553 ms |
67092 KB |
Output is correct |
52 |
Correct |
564 ms |
67780 KB |
Output is correct |
53 |
Correct |
493 ms |
68304 KB |
Output is correct |
54 |
Correct |
469 ms |
67472 KB |
Output is correct |
55 |
Correct |
476 ms |
66972 KB |
Output is correct |
56 |
Correct |
499 ms |
66656 KB |
Output is correct |
57 |
Correct |
523 ms |
68924 KB |
Output is correct |
58 |
Correct |
525 ms |
68376 KB |
Output is correct |
59 |
Correct |
533 ms |
68492 KB |
Output is correct |
60 |
Correct |
534 ms |
67300 KB |
Output is correct |
61 |
Correct |
551 ms |
68624 KB |
Output is correct |
62 |
Correct |
478 ms |
68292 KB |
Output is correct |
63 |
Correct |
464 ms |
60480 KB |
Output is correct |
64 |
Correct |
470 ms |
65024 KB |
Output is correct |
65 |
Correct |
502 ms |
66028 KB |
Output is correct |
66 |
Correct |
464 ms |
64724 KB |
Output is correct |
67 |
Correct |
465 ms |
64700 KB |
Output is correct |
68 |
Correct |
491 ms |
64696 KB |
Output is correct |
69 |
Correct |
492 ms |
65672 KB |
Output is correct |
70 |
Correct |
470 ms |
65364 KB |
Output is correct |
71 |
Correct |
467 ms |
64708 KB |
Output is correct |
72 |
Correct |
479 ms |
65076 KB |
Output is correct |
73 |
Correct |
506 ms |
64984 KB |
Output is correct |
74 |
Correct |
439 ms |
64964 KB |
Output is correct |
75 |
Correct |
514 ms |
64936 KB |
Output is correct |
76 |
Correct |
576 ms |
65180 KB |
Output is correct |
77 |
Correct |
548 ms |
68364 KB |
Output is correct |
78 |
Correct |
520 ms |
69088 KB |
Output is correct |
79 |
Correct |
540 ms |
69596 KB |
Output is correct |
80 |
Correct |
578 ms |
68348 KB |
Output is correct |
81 |
Correct |
498 ms |
68012 KB |
Output is correct |
82 |
Correct |
492 ms |
68676 KB |
Output is correct |
83 |
Correct |
503 ms |
68488 KB |
Output is correct |
84 |
Correct |
478 ms |
68304 KB |
Output is correct |
85 |
Correct |
472 ms |
69328 KB |
Output is correct |
86 |
Correct |
559 ms |
68328 KB |
Output is correct |
87 |
Correct |
484 ms |
69060 KB |
Output is correct |
88 |
Correct |
468 ms |
69188 KB |
Output is correct |
89 |
Correct |
446 ms |
67416 KB |
Output is correct |
90 |
Correct |
432 ms |
68680 KB |
Output is correct |
91 |
Correct |
416 ms |
67772 KB |
Output is correct |
92 |
Correct |
454 ms |
67172 KB |
Output is correct |
93 |
Correct |
426 ms |
68092 KB |
Output is correct |
94 |
Correct |
445 ms |
69584 KB |
Output is correct |
95 |
Correct |
490 ms |
69276 KB |
Output is correct |
96 |
Correct |
406 ms |
68208 KB |
Output is correct |
97 |
Correct |
453 ms |
68068 KB |
Output is correct |
98 |
Correct |
486 ms |
67368 KB |
Output is correct |
99 |
Correct |
531 ms |
68100 KB |
Output is correct |
100 |
Correct |
512 ms |
68020 KB |
Output is correct |
101 |
Correct |
508 ms |
67796 KB |
Output is correct |
102 |
Correct |
497 ms |
68928 KB |
Output is correct |