#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];
void add(int l, int r, int i, int x) {
mem[l].app(mp(i, x));
mem[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;
//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];
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) {
for (int j = i; j < link_r[i]; ++j) {
int len1 = j - i + 1;
int len2 = j - link_l[i];
if (link_l[i] == -1)
len2 = N - 2;
add(len1, len2, j, a[i]);
}
}
f.clear();
for (int len = 1; len < N; ++len) {
for (auto e : mem[len])
f.add(e.f, e.s);
for (auto e : d[len])
ans[e] = f.get(l[e], r[e]);
}
for (int i = 0; i < q; ++i)
cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
11256 KB |
Output is correct |
2 |
Correct |
13 ms |
11384 KB |
Output is correct |
3 |
Correct |
14 ms |
11512 KB |
Output is correct |
4 |
Correct |
12 ms |
11384 KB |
Output is correct |
5 |
Correct |
13 ms |
11384 KB |
Output is correct |
6 |
Correct |
12 ms |
11384 KB |
Output is correct |
7 |
Correct |
12 ms |
11384 KB |
Output is correct |
8 |
Correct |
12 ms |
11384 KB |
Output is correct |
9 |
Correct |
12 ms |
11256 KB |
Output is correct |
10 |
Correct |
12 ms |
11384 KB |
Output is correct |
11 |
Correct |
12 ms |
11416 KB |
Output is correct |
12 |
Correct |
12 ms |
11384 KB |
Output is correct |
13 |
Correct |
13 ms |
11384 KB |
Output is correct |
14 |
Correct |
13 ms |
11384 KB |
Output is correct |
15 |
Correct |
13 ms |
11384 KB |
Output is correct |
16 |
Correct |
12 ms |
11384 KB |
Output is correct |
17 |
Correct |
12 ms |
11384 KB |
Output is correct |
18 |
Correct |
13 ms |
11384 KB |
Output is correct |
19 |
Correct |
12 ms |
11384 KB |
Output is correct |
20 |
Correct |
12 ms |
11384 KB |
Output is correct |
21 |
Correct |
12 ms |
11512 KB |
Output is correct |
22 |
Correct |
12 ms |
11384 KB |
Output is correct |
23 |
Correct |
12 ms |
11384 KB |
Output is correct |
24 |
Correct |
12 ms |
11384 KB |
Output is correct |
25 |
Correct |
12 ms |
11384 KB |
Output is correct |
26 |
Correct |
12 ms |
11384 KB |
Output is correct |
27 |
Correct |
13 ms |
11384 KB |
Output is correct |
28 |
Correct |
13 ms |
11384 KB |
Output is correct |
29 |
Correct |
14 ms |
11384 KB |
Output is correct |
30 |
Correct |
13 ms |
11468 KB |
Output is correct |
31 |
Correct |
12 ms |
11388 KB |
Output is correct |
32 |
Correct |
13 ms |
11388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
11256 KB |
Output is correct |
2 |
Correct |
363 ms |
117596 KB |
Output is correct |
3 |
Correct |
366 ms |
114024 KB |
Output is correct |
4 |
Correct |
351 ms |
112600 KB |
Output is correct |
5 |
Correct |
377 ms |
119796 KB |
Output is correct |
6 |
Correct |
368 ms |
119124 KB |
Output is correct |
7 |
Correct |
361 ms |
112852 KB |
Output is correct |
8 |
Correct |
362 ms |
115964 KB |
Output is correct |
9 |
Correct |
389 ms |
130520 KB |
Output is correct |
10 |
Correct |
364 ms |
117972 KB |
Output is correct |
11 |
Correct |
376 ms |
120920 KB |
Output is correct |
12 |
Correct |
378 ms |
119768 KB |
Output is correct |
13 |
Correct |
345 ms |
109656 KB |
Output is correct |
14 |
Correct |
401 ms |
128216 KB |
Output is correct |
15 |
Correct |
370 ms |
121060 KB |
Output is correct |
16 |
Correct |
393 ms |
124908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
11256 KB |
Output is correct |
2 |
Correct |
470 ms |
128956 KB |
Output is correct |
3 |
Correct |
441 ms |
119336 KB |
Output is correct |
4 |
Correct |
488 ms |
135884 KB |
Output is correct |
5 |
Correct |
426 ms |
115772 KB |
Output is correct |
6 |
Correct |
451 ms |
120388 KB |
Output is correct |
7 |
Correct |
425 ms |
113212 KB |
Output is correct |
8 |
Correct |
425 ms |
112936 KB |
Output is correct |
9 |
Correct |
417 ms |
109496 KB |
Output is correct |
10 |
Correct |
425 ms |
112704 KB |
Output is correct |
11 |
Correct |
443 ms |
120896 KB |
Output is correct |
12 |
Correct |
451 ms |
125252 KB |
Output is correct |
13 |
Correct |
447 ms |
121300 KB |
Output is correct |
14 |
Correct |
411 ms |
110396 KB |
Output is correct |
15 |
Correct |
447 ms |
120428 KB |
Output is correct |
16 |
Correct |
414 ms |
116568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
213 ms |
37084 KB |
Output is correct |
2 |
Correct |
205 ms |
37536 KB |
Output is correct |
3 |
Correct |
221 ms |
38480 KB |
Output is correct |
4 |
Correct |
220 ms |
36900 KB |
Output is correct |
5 |
Correct |
227 ms |
37904 KB |
Output is correct |
6 |
Correct |
205 ms |
36948 KB |
Output is correct |
7 |
Correct |
209 ms |
37080 KB |
Output is correct |
8 |
Correct |
221 ms |
37980 KB |
Output is correct |
9 |
Correct |
207 ms |
36664 KB |
Output is correct |
10 |
Correct |
213 ms |
36544 KB |
Output is correct |
11 |
Correct |
217 ms |
36644 KB |
Output is correct |
12 |
Correct |
214 ms |
38100 KB |
Output is correct |
13 |
Correct |
223 ms |
37192 KB |
Output is correct |
14 |
Correct |
216 ms |
38744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
11256 KB |
Output is correct |
2 |
Correct |
13 ms |
11384 KB |
Output is correct |
3 |
Correct |
14 ms |
11512 KB |
Output is correct |
4 |
Correct |
12 ms |
11384 KB |
Output is correct |
5 |
Correct |
13 ms |
11384 KB |
Output is correct |
6 |
Correct |
12 ms |
11384 KB |
Output is correct |
7 |
Correct |
12 ms |
11384 KB |
Output is correct |
8 |
Correct |
12 ms |
11384 KB |
Output is correct |
9 |
Correct |
12 ms |
11256 KB |
Output is correct |
10 |
Correct |
12 ms |
11384 KB |
Output is correct |
11 |
Correct |
12 ms |
11416 KB |
Output is correct |
12 |
Correct |
12 ms |
11384 KB |
Output is correct |
13 |
Correct |
13 ms |
11384 KB |
Output is correct |
14 |
Correct |
13 ms |
11384 KB |
Output is correct |
15 |
Correct |
13 ms |
11384 KB |
Output is correct |
16 |
Correct |
12 ms |
11384 KB |
Output is correct |
17 |
Correct |
12 ms |
11384 KB |
Output is correct |
18 |
Correct |
13 ms |
11384 KB |
Output is correct |
19 |
Correct |
12 ms |
11384 KB |
Output is correct |
20 |
Correct |
12 ms |
11384 KB |
Output is correct |
21 |
Correct |
12 ms |
11512 KB |
Output is correct |
22 |
Correct |
12 ms |
11384 KB |
Output is correct |
23 |
Correct |
12 ms |
11384 KB |
Output is correct |
24 |
Correct |
12 ms |
11384 KB |
Output is correct |
25 |
Correct |
12 ms |
11384 KB |
Output is correct |
26 |
Correct |
12 ms |
11384 KB |
Output is correct |
27 |
Correct |
13 ms |
11384 KB |
Output is correct |
28 |
Correct |
13 ms |
11384 KB |
Output is correct |
29 |
Correct |
14 ms |
11384 KB |
Output is correct |
30 |
Correct |
13 ms |
11468 KB |
Output is correct |
31 |
Correct |
12 ms |
11388 KB |
Output is correct |
32 |
Correct |
13 ms |
11388 KB |
Output is correct |
33 |
Correct |
427 ms |
113076 KB |
Output is correct |
34 |
Correct |
443 ms |
121648 KB |
Output is correct |
35 |
Correct |
436 ms |
117560 KB |
Output is correct |
36 |
Correct |
444 ms |
113416 KB |
Output is correct |
37 |
Correct |
464 ms |
129976 KB |
Output is correct |
38 |
Correct |
473 ms |
123000 KB |
Output is correct |
39 |
Correct |
456 ms |
120496 KB |
Output is correct |
40 |
Correct |
419 ms |
113088 KB |
Output is correct |
41 |
Correct |
449 ms |
122300 KB |
Output is correct |
42 |
Correct |
453 ms |
118488 KB |
Output is correct |
43 |
Correct |
399 ms |
115384 KB |
Output is correct |
44 |
Correct |
371 ms |
111792 KB |
Output is correct |
45 |
Correct |
380 ms |
107956 KB |
Output is correct |
46 |
Correct |
419 ms |
125492 KB |
Output is correct |
47 |
Correct |
399 ms |
116292 KB |
Output is correct |
48 |
Correct |
415 ms |
118480 KB |
Output is correct |
49 |
Correct |
403 ms |
121424 KB |
Output is correct |
50 |
Correct |
406 ms |
119776 KB |
Output is correct |
51 |
Correct |
388 ms |
118896 KB |
Output is correct |
52 |
Correct |
394 ms |
117700 KB |
Output is correct |
53 |
Correct |
388 ms |
114556 KB |
Output is correct |
54 |
Correct |
393 ms |
115808 KB |
Output is correct |
55 |
Correct |
420 ms |
119772 KB |
Output is correct |
56 |
Correct |
421 ms |
124240 KB |
Output is correct |
57 |
Correct |
381 ms |
110532 KB |
Output is correct |
58 |
Correct |
402 ms |
116000 KB |
Output is correct |
59 |
Correct |
363 ms |
117596 KB |
Output is correct |
60 |
Correct |
366 ms |
114024 KB |
Output is correct |
61 |
Correct |
351 ms |
112600 KB |
Output is correct |
62 |
Correct |
377 ms |
119796 KB |
Output is correct |
63 |
Correct |
368 ms |
119124 KB |
Output is correct |
64 |
Correct |
361 ms |
112852 KB |
Output is correct |
65 |
Correct |
362 ms |
115964 KB |
Output is correct |
66 |
Correct |
389 ms |
130520 KB |
Output is correct |
67 |
Correct |
364 ms |
117972 KB |
Output is correct |
68 |
Correct |
376 ms |
120920 KB |
Output is correct |
69 |
Correct |
378 ms |
119768 KB |
Output is correct |
70 |
Correct |
345 ms |
109656 KB |
Output is correct |
71 |
Correct |
401 ms |
128216 KB |
Output is correct |
72 |
Correct |
370 ms |
121060 KB |
Output is correct |
73 |
Correct |
393 ms |
124908 KB |
Output is correct |
74 |
Correct |
470 ms |
128956 KB |
Output is correct |
75 |
Correct |
441 ms |
119336 KB |
Output is correct |
76 |
Correct |
488 ms |
135884 KB |
Output is correct |
77 |
Correct |
426 ms |
115772 KB |
Output is correct |
78 |
Correct |
451 ms |
120388 KB |
Output is correct |
79 |
Correct |
425 ms |
113212 KB |
Output is correct |
80 |
Correct |
425 ms |
112936 KB |
Output is correct |
81 |
Correct |
417 ms |
109496 KB |
Output is correct |
82 |
Correct |
425 ms |
112704 KB |
Output is correct |
83 |
Correct |
443 ms |
120896 KB |
Output is correct |
84 |
Correct |
451 ms |
125252 KB |
Output is correct |
85 |
Correct |
447 ms |
121300 KB |
Output is correct |
86 |
Correct |
411 ms |
110396 KB |
Output is correct |
87 |
Correct |
447 ms |
120428 KB |
Output is correct |
88 |
Correct |
414 ms |
116568 KB |
Output is correct |
89 |
Correct |
213 ms |
37084 KB |
Output is correct |
90 |
Correct |
205 ms |
37536 KB |
Output is correct |
91 |
Correct |
221 ms |
38480 KB |
Output is correct |
92 |
Correct |
220 ms |
36900 KB |
Output is correct |
93 |
Correct |
227 ms |
37904 KB |
Output is correct |
94 |
Correct |
205 ms |
36948 KB |
Output is correct |
95 |
Correct |
209 ms |
37080 KB |
Output is correct |
96 |
Correct |
221 ms |
37980 KB |
Output is correct |
97 |
Correct |
207 ms |
36664 KB |
Output is correct |
98 |
Correct |
213 ms |
36544 KB |
Output is correct |
99 |
Correct |
217 ms |
36644 KB |
Output is correct |
100 |
Correct |
214 ms |
38100 KB |
Output is correct |
101 |
Correct |
223 ms |
37192 KB |
Output is correct |
102 |
Correct |
216 ms |
38744 KB |
Output is correct |