#include <bits/stdc++.h>
using namespace std;
const int MN = 4e5 + 5;
struct Query {
int id,l,r;
};
long long ans[MN];
struct withT {
long long bit[MN], bit2[MN];
void update (int a, int mult) {
for (int i = max(1,mult); i < MN; i += i & -i) {
bit[i] += mult * 1LL * a;
bit2[i] += a;
}
}
array<long long,2> query (int l, int r) {
long long ret = 0, ret2 = 0;
for (int i = r; i>0; i ^= i & -i) {
ret += bit[i];
ret2 += bit2[i];
}
for (int i = l-1; i>0; i ^= i & -i) {
ret -= bit[i];
ret2 -= bit2[i];
}
return {ret,ret2};
}
} rT, lT;
struct noT {
long long bit[MN];
void update (int i, long long v) {
for (;i<MN;i+=i&-i)
bit[i] += v;
}
long long query (int l, int r) {
long long ret = 0;
for (;r;r^=r&-r)
ret += bit[r];
for (--l;l>0;l^=l&-l)
ret -= bit[l];
return ret;
}
} rNoT, lNoT;
int main() {
int n,q;
scanf ("%d %d",&n,&q);
vector<int> a(n+1), nxt(n+1), lst(n+1), stk;
vector<vector<int>> die(n+2), ch1(n+2), ch2(n+2);
for (int i = 1; i <= n; i++) {
scanf ("%d",&a[i]);
while (!stk.empty() && a[stk.back()] < a[i])
stk.pop_back();
lst[i] = stk.empty() ? -1e9 : stk.back();
stk.push_back(i);
}
stk.clear();
for (int i = n; i >= 1; i--) {
while (!stk.empty() && a[stk.back()] <= a[i])
stk.pop_back();
nxt[i] = stk.empty() ? 1e9 : stk.back();
int ded = min(n+1,min(nxt[i],n+1) - lst[i] - 1);
die[ded].push_back(i);
ch1[min(ded,nxt[i] - i - 1)].push_back(i);
ch2[min(ded,i - lst[i] - 1)].push_back(i);
stk.push_back(i);
}
vector<vector<Query>> queries(n+1);
for (int i = 0; i < q; i++) {
int t,l,r;
scanf ("%d %d %d",&t,&l,&r);
queries[t].push_back({i,l,r});
}
multiset<pair<int,int>> slT,slNoT,srT,srNoT;
for (int t = n; t >= 1; t--) {
for (int i : die[t+1]) { //revival
srNoT.insert({nxt[i],a[i]});
rNoT.update(nxt[i],a[i] * 1LL * nxt[i]);
slT.insert({lst[i] + 1,a[i]});
lT.update(a[i],lst[i] + 1);
}
for (int i : ch1[t+1]) { //rNoT --> rT
rNoT.update(nxt[i],-a[i] * 1LL * nxt[i]);
srNoT.erase(srNoT.find({nxt[i],a[i]}));
rT.update(a[i],i+1);
srT.insert({i+1,a[i]});
}
for (int i : ch2[t+1]) { //lT --> lNoT
lT.update(-a[i],lst[i] + 1);
slT.erase(slT.find({lst[i] + 1,a[i]}));
lNoT.update(i,a[i] * 1LL * i);
slNoT.insert({i,a[i]});
}
for (auto &[idx,l,r] : queries[t]) {
auto [r1,r2] = rT.query(l-t+1,r-t);
auto [l1,l2] = lT.query(l-t,r-t);
long long sum = rNoT.query(l+1,r) + r1 + r2 * t - lNoT.query(l,r) - l1 - l2 * t;
{
pair<int,int> mxL = {0,0};
auto it = slNoT.upper_bound(pair(l,(int)1e9));
if (it != slNoT.begin()) mxL = *prev(it);
it = slT.upper_bound(pair(l-t,(int)1e9));
if (it != slT.begin()) mxL = max(mxL,pair(prev(it)->first+t,prev(it)->second));
if (mxL.first != l) sum -= (long long)mxL.second * l;
}
{
pair<int,int> nxtR = {n+5,n+5};
auto it = srNoT.upper_bound(pair(r,(int)1e9));
if (it != srNoT.end()) nxtR = *it;
it = srT.upper_bound(pair(r-t,(int)1e9));
if (it != srT.end()) {
nxtR = min(nxtR,pair(min(it->first+t,n+1),it->second));
}
sum += (long long)nxtR.second * (r+1);
}
ans[idx] = sum;
}
}
for (int i = 0; i < q; i++) printf ("%lld\n",ans[i]);
return 0;
}
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:46:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
46 | scanf ("%d %d",&n,&q);
| ~~~~~~^~~~~~~~~~~~~~~
ho_t5.cpp:50:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
50 | scanf ("%d",&a[i]);
| ~~~~~~^~~~~~~~~~~~
ho_t5.cpp:70:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
70 | scanf ("%d %d %d",&t,&l,&r);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
620 KB |
Output is correct |
24 |
Correct |
1 ms |
620 KB |
Output is correct |
25 |
Correct |
1 ms |
620 KB |
Output is correct |
26 |
Correct |
1 ms |
620 KB |
Output is correct |
27 |
Correct |
1 ms |
620 KB |
Output is correct |
28 |
Correct |
1 ms |
620 KB |
Output is correct |
29 |
Correct |
1 ms |
620 KB |
Output is correct |
30 |
Correct |
1 ms |
620 KB |
Output is correct |
31 |
Correct |
1 ms |
620 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
401 ms |
54220 KB |
Output is correct |
3 |
Correct |
392 ms |
53580 KB |
Output is correct |
4 |
Correct |
413 ms |
54096 KB |
Output is correct |
5 |
Correct |
414 ms |
54988 KB |
Output is correct |
6 |
Correct |
412 ms |
54360 KB |
Output is correct |
7 |
Correct |
410 ms |
54604 KB |
Output is correct |
8 |
Correct |
421 ms |
55244 KB |
Output is correct |
9 |
Correct |
412 ms |
54660 KB |
Output is correct |
10 |
Correct |
408 ms |
53496 KB |
Output is correct |
11 |
Correct |
405 ms |
54988 KB |
Output is correct |
12 |
Correct |
396 ms |
53448 KB |
Output is correct |
13 |
Correct |
425 ms |
54600 KB |
Output is correct |
14 |
Correct |
407 ms |
54860 KB |
Output is correct |
15 |
Correct |
406 ms |
54884 KB |
Output is correct |
16 |
Correct |
417 ms |
54384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
470 ms |
55256 KB |
Output is correct |
3 |
Correct |
456 ms |
54236 KB |
Output is correct |
4 |
Correct |
479 ms |
56664 KB |
Output is correct |
5 |
Correct |
473 ms |
54872 KB |
Output is correct |
6 |
Correct |
455 ms |
55356 KB |
Output is correct |
7 |
Correct |
472 ms |
55768 KB |
Output is correct |
8 |
Correct |
460 ms |
55000 KB |
Output is correct |
9 |
Correct |
454 ms |
56668 KB |
Output is correct |
10 |
Correct |
463 ms |
56536 KB |
Output is correct |
11 |
Correct |
468 ms |
58840 KB |
Output is correct |
12 |
Correct |
465 ms |
58452 KB |
Output is correct |
13 |
Correct |
466 ms |
58456 KB |
Output is correct |
14 |
Correct |
452 ms |
57048 KB |
Output is correct |
15 |
Correct |
458 ms |
58708 KB |
Output is correct |
16 |
Correct |
463 ms |
58328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
595 ms |
62724 KB |
Output is correct |
2 |
Correct |
587 ms |
62944 KB |
Output is correct |
3 |
Correct |
596 ms |
64608 KB |
Output is correct |
4 |
Correct |
607 ms |
62560 KB |
Output is correct |
5 |
Correct |
596 ms |
62688 KB |
Output is correct |
6 |
Correct |
578 ms |
63216 KB |
Output is correct |
7 |
Correct |
591 ms |
64224 KB |
Output is correct |
8 |
Correct |
605 ms |
63712 KB |
Output is correct |
9 |
Correct |
598 ms |
62688 KB |
Output is correct |
10 |
Correct |
600 ms |
63584 KB |
Output is correct |
11 |
Correct |
588 ms |
63072 KB |
Output is correct |
12 |
Correct |
598 ms |
63456 KB |
Output is correct |
13 |
Correct |
579 ms |
62944 KB |
Output is correct |
14 |
Correct |
586 ms |
63360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
620 KB |
Output is correct |
24 |
Correct |
1 ms |
620 KB |
Output is correct |
25 |
Correct |
1 ms |
620 KB |
Output is correct |
26 |
Correct |
1 ms |
620 KB |
Output is correct |
27 |
Correct |
1 ms |
620 KB |
Output is correct |
28 |
Correct |
1 ms |
620 KB |
Output is correct |
29 |
Correct |
1 ms |
620 KB |
Output is correct |
30 |
Correct |
1 ms |
620 KB |
Output is correct |
31 |
Correct |
1 ms |
620 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
474 ms |
58456 KB |
Output is correct |
34 |
Correct |
472 ms |
59096 KB |
Output is correct |
35 |
Correct |
471 ms |
59640 KB |
Output is correct |
36 |
Correct |
458 ms |
58332 KB |
Output is correct |
37 |
Correct |
458 ms |
58204 KB |
Output is correct |
38 |
Correct |
467 ms |
58972 KB |
Output is correct |
39 |
Correct |
476 ms |
58588 KB |
Output is correct |
40 |
Correct |
470 ms |
57900 KB |
Output is correct |
41 |
Correct |
478 ms |
59608 KB |
Output is correct |
42 |
Correct |
470 ms |
58328 KB |
Output is correct |
43 |
Correct |
431 ms |
58964 KB |
Output is correct |
44 |
Correct |
431 ms |
58840 KB |
Output is correct |
45 |
Correct |
419 ms |
56924 KB |
Output is correct |
46 |
Correct |
449 ms |
58516 KB |
Output is correct |
47 |
Correct |
443 ms |
57304 KB |
Output is correct |
48 |
Correct |
420 ms |
56664 KB |
Output is correct |
49 |
Correct |
431 ms |
57560 KB |
Output is correct |
50 |
Correct |
434 ms |
58836 KB |
Output is correct |
51 |
Correct |
436 ms |
58544 KB |
Output is correct |
52 |
Correct |
421 ms |
57436 KB |
Output is correct |
53 |
Correct |
423 ms |
57436 KB |
Output is correct |
54 |
Correct |
470 ms |
56796 KB |
Output is correct |
55 |
Correct |
477 ms |
57308 KB |
Output is correct |
56 |
Correct |
482 ms |
57436 KB |
Output is correct |
57 |
Correct |
479 ms |
56536 KB |
Output is correct |
58 |
Correct |
501 ms |
57664 KB |
Output is correct |
59 |
Correct |
401 ms |
54220 KB |
Output is correct |
60 |
Correct |
392 ms |
53580 KB |
Output is correct |
61 |
Correct |
413 ms |
54096 KB |
Output is correct |
62 |
Correct |
414 ms |
54988 KB |
Output is correct |
63 |
Correct |
412 ms |
54360 KB |
Output is correct |
64 |
Correct |
410 ms |
54604 KB |
Output is correct |
65 |
Correct |
421 ms |
55244 KB |
Output is correct |
66 |
Correct |
412 ms |
54660 KB |
Output is correct |
67 |
Correct |
408 ms |
53496 KB |
Output is correct |
68 |
Correct |
405 ms |
54988 KB |
Output is correct |
69 |
Correct |
396 ms |
53448 KB |
Output is correct |
70 |
Correct |
425 ms |
54600 KB |
Output is correct |
71 |
Correct |
407 ms |
54860 KB |
Output is correct |
72 |
Correct |
406 ms |
54884 KB |
Output is correct |
73 |
Correct |
417 ms |
54384 KB |
Output is correct |
74 |
Correct |
470 ms |
55256 KB |
Output is correct |
75 |
Correct |
456 ms |
54236 KB |
Output is correct |
76 |
Correct |
479 ms |
56664 KB |
Output is correct |
77 |
Correct |
473 ms |
54872 KB |
Output is correct |
78 |
Correct |
455 ms |
55356 KB |
Output is correct |
79 |
Correct |
472 ms |
55768 KB |
Output is correct |
80 |
Correct |
460 ms |
55000 KB |
Output is correct |
81 |
Correct |
454 ms |
56668 KB |
Output is correct |
82 |
Correct |
463 ms |
56536 KB |
Output is correct |
83 |
Correct |
468 ms |
58840 KB |
Output is correct |
84 |
Correct |
465 ms |
58452 KB |
Output is correct |
85 |
Correct |
466 ms |
58456 KB |
Output is correct |
86 |
Correct |
452 ms |
57048 KB |
Output is correct |
87 |
Correct |
458 ms |
58708 KB |
Output is correct |
88 |
Correct |
463 ms |
58328 KB |
Output is correct |
89 |
Correct |
595 ms |
62724 KB |
Output is correct |
90 |
Correct |
587 ms |
62944 KB |
Output is correct |
91 |
Correct |
596 ms |
64608 KB |
Output is correct |
92 |
Correct |
607 ms |
62560 KB |
Output is correct |
93 |
Correct |
596 ms |
62688 KB |
Output is correct |
94 |
Correct |
578 ms |
63216 KB |
Output is correct |
95 |
Correct |
591 ms |
64224 KB |
Output is correct |
96 |
Correct |
605 ms |
63712 KB |
Output is correct |
97 |
Correct |
598 ms |
62688 KB |
Output is correct |
98 |
Correct |
600 ms |
63584 KB |
Output is correct |
99 |
Correct |
588 ms |
63072 KB |
Output is correct |
100 |
Correct |
598 ms |
63456 KB |
Output is correct |
101 |
Correct |
579 ms |
62944 KB |
Output is correct |
102 |
Correct |
586 ms |
63360 KB |
Output is correct |