//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 100;
int n, q;
ll a[N];
int p[N], r[N];
ll st[6 * N], si[6 * N], sd[6 * N], que[6 * N];
ll ans[6 * N];
bool comp1(int i, int j) {
if(st[i] == st[j])
return (i < j);
return (st[i] < st[j]);
}
bool comp2(int i, int j) {
if(si[i] == si[j])
return (i < j);
return (si[i] < si[j]);
}
int shift;
struct fen {
ll t[4 * N] = {0}, t2[4 * N] = {0};
ll sum1 = 0, sum2 = 0;
void clear() {
sum1 = 0, sum2 = 0;
for(int i = 0; i <= 2 * n + 10; i++)
t[i] = t2[i] = 0;
}
void add(ll p, ll z, ll z2) {
sum1 += z, sum2 += z2;
for(int i = p; i <= 2 * n + 10; i = i|(i + 1)) {
t[i] += z;
t2[i] += z2;
}
}
pair<ll, ll> get(ll r) {
ll s1 = 0, s2 = 0;
for(int i = r; i >= 0; i = (i&(i+1)) - 1) {
s1 += t[i];
s2 += t2[i];
}
return mp(s1, s2);
}
} ret;
int main() {
speed;
cin >> n >> q;
shift = n + 1;
deque<int> s;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int j = 1; j <= n; j++) {
while(s.size() && a[s.back()] < a[j])s.pop_back();
if(s.size())p[j] = s.back();
s.push_back(j);
}
while(s.size())s.pop_back();
for(int j = n; j >= 1; j--) {
r[j] = n + 1;
while(s.size() && a[s.back()] <= a[j])s.pop_back();
if(s.size())r[j] = s.back();
s.push_back(j);
}
int cnt = 1;
for(int i = 1, t; i <= n; i++) {
// 1
st[cnt] = 1;
sd[cnt] = a[i];
si[cnt++] = i;
// 2
if(r[i] != n + 1) {
st[cnt] = 1 + (r[i] - i);
sd[cnt] = -a[i];
si[cnt++] = r[i];
}
// 3
if(p[i] != 0) {
st[cnt] = 1 + (i - p[i]);
sd[cnt] = -a[i];
si[cnt++] = i;
//4
if(r[i] != n + 1) {
st[cnt] = 1 + (r[i] - p[i]);
sd[cnt] = a[i];
si[cnt++] = r[i];
}
}
}
vector<int> sbt;
for(int i = 1; i <= n; i++)
a[i] += a[i - 1];
for(int i = 1; i <= q; i++) {
ll t, l, r;
cin >> t >> l >> r;
t++;
que[cnt] = i;
st[cnt] = t;
si[cnt++] = r;
que[cnt] = -i;
st[cnt] = t;
si[cnt++] = l - 1;
}
//kek
cnt--;
for(int i = 1; i <= cnt; i++) {
sbt.pb(i);
}
vector<int> queries;
sort(sbt.begin(), sbt.end(), comp1);
for(int i = 0; i < cnt; i++) {
int s = sbt[i];
if(que[s]) {
ll qt = st[s], qi = si[s], znak = (que[s] < 0 ? -1 : 1);
pair<ll, ll> ggg = ret.get(qt - qi + shift);
ggg.f = ret.sum1 - ggg.f;
ggg.s = ret.sum2 - ggg.s;
ans[que[s] * znak] += znak * (qt + 1) * ggg.f;
ans[que[s] * znak] -= znak * ggg.s;
} else {
ret.add(st[s] - si[s] + shift, sd[s], st[s] * sd[s]);
}
}
ret.clear();
sort(sbt.begin(), sbt.end(), comp2);
for(int i = 0; i < cnt; i++) {
int s = sbt[i];
if(que[s]) {
ll qt = st[s], qi = si[s], znak = (que[s] < 0 ? -1 : 1);
pair<ll, ll> ggg = ret.get(qt - qi + shift);
ans[que[s] * znak] += znak * (qi + 1) * ggg.f;
ans[que[s] * znak] -= znak * ggg.s;
} else {
ret.add(st[s] - si[s] + shift, sd[s], si[s] * sd[s]);
}
}
for(int i = 1; i <= q; i++)
cout << ans[i] << "\n";
}
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:85:17: warning: unused variable 't' [-Wunused-variable]
85 | for(int i = 1, t; i <= n; i++) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
452 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
484 ms |
46300 KB |
Output is correct |
3 |
Correct |
496 ms |
49184 KB |
Output is correct |
4 |
Correct |
573 ms |
49792 KB |
Output is correct |
5 |
Correct |
497 ms |
52312 KB |
Output is correct |
6 |
Correct |
553 ms |
51444 KB |
Output is correct |
7 |
Correct |
495 ms |
52388 KB |
Output is correct |
8 |
Correct |
574 ms |
52356 KB |
Output is correct |
9 |
Correct |
521 ms |
52272 KB |
Output is correct |
10 |
Correct |
486 ms |
51244 KB |
Output is correct |
11 |
Correct |
516 ms |
52652 KB |
Output is correct |
12 |
Correct |
475 ms |
51272 KB |
Output is correct |
13 |
Correct |
548 ms |
51980 KB |
Output is correct |
14 |
Correct |
555 ms |
51700 KB |
Output is correct |
15 |
Correct |
559 ms |
52028 KB |
Output is correct |
16 |
Correct |
569 ms |
51608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
540 ms |
45608 KB |
Output is correct |
3 |
Correct |
532 ms |
48048 KB |
Output is correct |
4 |
Correct |
533 ms |
49628 KB |
Output is correct |
5 |
Correct |
519 ms |
48296 KB |
Output is correct |
6 |
Correct |
531 ms |
48784 KB |
Output is correct |
7 |
Correct |
531 ms |
48940 KB |
Output is correct |
8 |
Correct |
565 ms |
48708 KB |
Output is correct |
9 |
Correct |
528 ms |
48268 KB |
Output is correct |
10 |
Correct |
516 ms |
47744 KB |
Output is correct |
11 |
Correct |
545 ms |
49600 KB |
Output is correct |
12 |
Correct |
533 ms |
49240 KB |
Output is correct |
13 |
Correct |
554 ms |
49624 KB |
Output is correct |
14 |
Correct |
535 ms |
48576 KB |
Output is correct |
15 |
Correct |
551 ms |
49456 KB |
Output is correct |
16 |
Correct |
526 ms |
48864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
480 ms |
39228 KB |
Output is correct |
2 |
Correct |
454 ms |
39220 KB |
Output is correct |
3 |
Correct |
472 ms |
40184 KB |
Output is correct |
4 |
Correct |
469 ms |
39308 KB |
Output is correct |
5 |
Correct |
489 ms |
39368 KB |
Output is correct |
6 |
Correct |
457 ms |
39320 KB |
Output is correct |
7 |
Correct |
473 ms |
40032 KB |
Output is correct |
8 |
Correct |
486 ms |
39724 KB |
Output is correct |
9 |
Correct |
478 ms |
39348 KB |
Output is correct |
10 |
Correct |
471 ms |
39600 KB |
Output is correct |
11 |
Correct |
488 ms |
39660 KB |
Output is correct |
12 |
Correct |
517 ms |
39696 KB |
Output is correct |
13 |
Correct |
512 ms |
39464 KB |
Output is correct |
14 |
Correct |
516 ms |
40032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
452 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
484 ms |
46300 KB |
Output is correct |
34 |
Correct |
496 ms |
49184 KB |
Output is correct |
35 |
Correct |
573 ms |
49792 KB |
Output is correct |
36 |
Correct |
497 ms |
52312 KB |
Output is correct |
37 |
Correct |
553 ms |
51444 KB |
Output is correct |
38 |
Correct |
495 ms |
52388 KB |
Output is correct |
39 |
Correct |
574 ms |
52356 KB |
Output is correct |
40 |
Correct |
521 ms |
52272 KB |
Output is correct |
41 |
Correct |
486 ms |
51244 KB |
Output is correct |
42 |
Correct |
516 ms |
52652 KB |
Output is correct |
43 |
Correct |
475 ms |
51272 KB |
Output is correct |
44 |
Correct |
548 ms |
51980 KB |
Output is correct |
45 |
Correct |
555 ms |
51700 KB |
Output is correct |
46 |
Correct |
559 ms |
52028 KB |
Output is correct |
47 |
Correct |
569 ms |
51608 KB |
Output is correct |
48 |
Correct |
540 ms |
45608 KB |
Output is correct |
49 |
Correct |
532 ms |
48048 KB |
Output is correct |
50 |
Correct |
533 ms |
49628 KB |
Output is correct |
51 |
Correct |
519 ms |
48296 KB |
Output is correct |
52 |
Correct |
531 ms |
48784 KB |
Output is correct |
53 |
Correct |
531 ms |
48940 KB |
Output is correct |
54 |
Correct |
565 ms |
48708 KB |
Output is correct |
55 |
Correct |
528 ms |
48268 KB |
Output is correct |
56 |
Correct |
516 ms |
47744 KB |
Output is correct |
57 |
Correct |
545 ms |
49600 KB |
Output is correct |
58 |
Correct |
533 ms |
49240 KB |
Output is correct |
59 |
Correct |
554 ms |
49624 KB |
Output is correct |
60 |
Correct |
535 ms |
48576 KB |
Output is correct |
61 |
Correct |
551 ms |
49456 KB |
Output is correct |
62 |
Correct |
526 ms |
48864 KB |
Output is correct |
63 |
Correct |
480 ms |
39228 KB |
Output is correct |
64 |
Correct |
454 ms |
39220 KB |
Output is correct |
65 |
Correct |
472 ms |
40184 KB |
Output is correct |
66 |
Correct |
469 ms |
39308 KB |
Output is correct |
67 |
Correct |
489 ms |
39368 KB |
Output is correct |
68 |
Correct |
457 ms |
39320 KB |
Output is correct |
69 |
Correct |
473 ms |
40032 KB |
Output is correct |
70 |
Correct |
486 ms |
39724 KB |
Output is correct |
71 |
Correct |
478 ms |
39348 KB |
Output is correct |
72 |
Correct |
471 ms |
39600 KB |
Output is correct |
73 |
Correct |
488 ms |
39660 KB |
Output is correct |
74 |
Correct |
517 ms |
39696 KB |
Output is correct |
75 |
Correct |
512 ms |
39464 KB |
Output is correct |
76 |
Correct |
516 ms |
40032 KB |
Output is correct |
77 |
Correct |
655 ms |
51520 KB |
Output is correct |
78 |
Correct |
657 ms |
52436 KB |
Output is correct |
79 |
Correct |
697 ms |
52092 KB |
Output is correct |
80 |
Correct |
616 ms |
51496 KB |
Output is correct |
81 |
Correct |
598 ms |
51256 KB |
Output is correct |
82 |
Correct |
613 ms |
51884 KB |
Output is correct |
83 |
Correct |
613 ms |
51668 KB |
Output is correct |
84 |
Correct |
572 ms |
51332 KB |
Output is correct |
85 |
Correct |
608 ms |
52008 KB |
Output is correct |
86 |
Correct |
612 ms |
51608 KB |
Output is correct |
87 |
Correct |
568 ms |
52620 KB |
Output is correct |
88 |
Correct |
584 ms |
52372 KB |
Output is correct |
89 |
Correct |
554 ms |
51248 KB |
Output is correct |
90 |
Correct |
571 ms |
52184 KB |
Output is correct |
91 |
Correct |
557 ms |
51372 KB |
Output is correct |
92 |
Correct |
561 ms |
50956 KB |
Output is correct |
93 |
Correct |
565 ms |
51580 KB |
Output is correct |
94 |
Correct |
575 ms |
52808 KB |
Output is correct |
95 |
Correct |
568 ms |
52632 KB |
Output is correct |
96 |
Correct |
560 ms |
51760 KB |
Output is correct |
97 |
Correct |
573 ms |
52000 KB |
Output is correct |
98 |
Correct |
566 ms |
51004 KB |
Output is correct |
99 |
Correct |
567 ms |
51336 KB |
Output is correct |
100 |
Correct |
565 ms |
51720 KB |
Output is correct |
101 |
Correct |
547 ms |
51152 KB |
Output is correct |
102 |
Correct |
576 ms |
52152 KB |
Output is correct |