#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});
}
set<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({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({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 |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
748 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 |
620 KB |
Output is correct |
12 |
Correct |
2 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 |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
492 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 |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
620 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 |
2 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 |
640 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
411 ms |
57676 KB |
Output is correct |
3 |
Correct |
401 ms |
57048 KB |
Output is correct |
4 |
Correct |
416 ms |
57296 KB |
Output is correct |
5 |
Correct |
417 ms |
58516 KB |
Output is correct |
6 |
Correct |
416 ms |
57424 KB |
Output is correct |
7 |
Correct |
415 ms |
57804 KB |
Output is correct |
8 |
Correct |
423 ms |
58700 KB |
Output is correct |
9 |
Correct |
420 ms |
57936 KB |
Output is correct |
10 |
Correct |
401 ms |
57148 KB |
Output is correct |
11 |
Correct |
414 ms |
58444 KB |
Output is correct |
12 |
Correct |
393 ms |
56652 KB |
Output is correct |
13 |
Correct |
430 ms |
58096 KB |
Output is correct |
14 |
Correct |
415 ms |
58060 KB |
Output is correct |
15 |
Correct |
408 ms |
57948 KB |
Output is correct |
16 |
Correct |
417 ms |
57552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
474 ms |
58640 KB |
Output is correct |
3 |
Correct |
455 ms |
57792 KB |
Output is correct |
4 |
Correct |
484 ms |
59992 KB |
Output is correct |
5 |
Correct |
469 ms |
58200 KB |
Output is correct |
6 |
Correct |
466 ms |
58584 KB |
Output is correct |
7 |
Correct |
459 ms |
58936 KB |
Output is correct |
8 |
Correct |
467 ms |
58328 KB |
Output is correct |
9 |
Correct |
464 ms |
57692 KB |
Output is correct |
10 |
Correct |
469 ms |
57176 KB |
Output is correct |
11 |
Correct |
479 ms |
59892 KB |
Output is correct |
12 |
Correct |
476 ms |
59348 KB |
Output is correct |
13 |
Correct |
470 ms |
59352 KB |
Output is correct |
14 |
Correct |
459 ms |
57944 KB |
Output is correct |
15 |
Correct |
470 ms |
59860 KB |
Output is correct |
16 |
Correct |
462 ms |
59096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
595 ms |
62276 KB |
Output is correct |
2 |
Correct |
604 ms |
62448 KB |
Output is correct |
3 |
Correct |
613 ms |
64224 KB |
Output is correct |
4 |
Incorrect |
629 ms |
62020 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
748 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 |
620 KB |
Output is correct |
12 |
Correct |
2 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 |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
492 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 |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
620 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 |
2 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 |
640 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
485 ms |
59224 KB |
Output is correct |
34 |
Correct |
480 ms |
59992 KB |
Output is correct |
35 |
Correct |
483 ms |
60308 KB |
Output is correct |
36 |
Correct |
479 ms |
59252 KB |
Output is correct |
37 |
Correct |
467 ms |
59100 KB |
Output is correct |
38 |
Correct |
494 ms |
59752 KB |
Output is correct |
39 |
Correct |
491 ms |
59484 KB |
Output is correct |
40 |
Correct |
469 ms |
59000 KB |
Output is correct |
41 |
Correct |
469 ms |
60380 KB |
Output is correct |
42 |
Correct |
469 ms |
59064 KB |
Output is correct |
43 |
Correct |
448 ms |
59736 KB |
Output is correct |
44 |
Correct |
445 ms |
59864 KB |
Output is correct |
45 |
Correct |
428 ms |
57916 KB |
Output is correct |
46 |
Correct |
440 ms |
59636 KB |
Output is correct |
47 |
Correct |
429 ms |
58364 KB |
Output is correct |
48 |
Correct |
420 ms |
57688 KB |
Output is correct |
49 |
Correct |
434 ms |
58712 KB |
Output is correct |
50 |
Correct |
439 ms |
60140 KB |
Output is correct |
51 |
Correct |
444 ms |
60132 KB |
Output is correct |
52 |
Correct |
434 ms |
58680 KB |
Output is correct |
53 |
Correct |
424 ms |
58716 KB |
Output is correct |
54 |
Correct |
489 ms |
57788 KB |
Output is correct |
55 |
Correct |
488 ms |
58560 KB |
Output is correct |
56 |
Correct |
503 ms |
58688 KB |
Output is correct |
57 |
Correct |
525 ms |
58120 KB |
Output is correct |
58 |
Correct |
501 ms |
59348 KB |
Output is correct |
59 |
Correct |
411 ms |
57676 KB |
Output is correct |
60 |
Correct |
401 ms |
57048 KB |
Output is correct |
61 |
Correct |
416 ms |
57296 KB |
Output is correct |
62 |
Correct |
417 ms |
58516 KB |
Output is correct |
63 |
Correct |
416 ms |
57424 KB |
Output is correct |
64 |
Correct |
415 ms |
57804 KB |
Output is correct |
65 |
Correct |
423 ms |
58700 KB |
Output is correct |
66 |
Correct |
420 ms |
57936 KB |
Output is correct |
67 |
Correct |
401 ms |
57148 KB |
Output is correct |
68 |
Correct |
414 ms |
58444 KB |
Output is correct |
69 |
Correct |
393 ms |
56652 KB |
Output is correct |
70 |
Correct |
430 ms |
58096 KB |
Output is correct |
71 |
Correct |
415 ms |
58060 KB |
Output is correct |
72 |
Correct |
408 ms |
57948 KB |
Output is correct |
73 |
Correct |
417 ms |
57552 KB |
Output is correct |
74 |
Correct |
474 ms |
58640 KB |
Output is correct |
75 |
Correct |
455 ms |
57792 KB |
Output is correct |
76 |
Correct |
484 ms |
59992 KB |
Output is correct |
77 |
Correct |
469 ms |
58200 KB |
Output is correct |
78 |
Correct |
466 ms |
58584 KB |
Output is correct |
79 |
Correct |
459 ms |
58936 KB |
Output is correct |
80 |
Correct |
467 ms |
58328 KB |
Output is correct |
81 |
Correct |
464 ms |
57692 KB |
Output is correct |
82 |
Correct |
469 ms |
57176 KB |
Output is correct |
83 |
Correct |
479 ms |
59892 KB |
Output is correct |
84 |
Correct |
476 ms |
59348 KB |
Output is correct |
85 |
Correct |
470 ms |
59352 KB |
Output is correct |
86 |
Correct |
459 ms |
57944 KB |
Output is correct |
87 |
Correct |
470 ms |
59860 KB |
Output is correct |
88 |
Correct |
462 ms |
59096 KB |
Output is correct |
89 |
Correct |
595 ms |
62276 KB |
Output is correct |
90 |
Correct |
604 ms |
62448 KB |
Output is correct |
91 |
Correct |
613 ms |
64224 KB |
Output is correct |
92 |
Incorrect |
629 ms |
62020 KB |
Output isn't correct |
93 |
Halted |
0 ms |
0 KB |
- |