#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <random>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
const int INF = 1e9;
const int N = 400010;
struct Fenwick {
int n;
vector<ll> f;
void upd(int ind, int d) {
for (int i = ind + 1; i <= n; i += i & -i) {
f[i] += d;
}
}
ll get(int r) {
ll sum = 0;
for (int i = r; i > 0; i -= i & -i) {
sum += f[i];
}
return sum;
}
ll get(int l, int r) {
if (l >= r) {
return 0;
}
return get(r) - get(l);
}
Fenwick(int n_ = 0) : n(n_) {
f.assign(n + 1, 0ll);
}
};
int n, q;
int s[N];
int t[N], L[N], R[N];
vector<int> by_t[N];
ll ans[N];
pair<int, int> mx[2 * N];
vector<pair<int, int>> updl[N], updr[N];
Fenwick fl, fr;
void add(int l, int r, int ind, int d, int type) {
if (type == 0) {
updl[l].push_back({ind, d});
updl[r + 1].push_back({ind, -d});
} else {
updr[l].push_back({ind, d});
updr[r + 1].push_back({ind, -d});
}
}
pair<int, int> get_max(int l, int r) {
l += n, r += n;
pair<int, int> res = {-INF, -INF};
while (l < r) {
if (l & 1) {
res = max(res, mx[l]);
++l;
}
if (r & 1) {
--r;
res = max(res, mx[r]);
}
l >>= 1, r >>= 1;
}
return res;
}
void rec(int l, int r) {
if (l > r) {
return;
}
int m = get_max(l, r + 1).second;
rec(l, m - 1);
rec(m + 1, r);
if (m - l < r - m) {
for (int i = l; i <= m; ++i) {
add(m - i + 1, r - i + 1, i, s[m], 0);
}
} else {
for (int i = m; i <= r; ++i) {
add(i - m + 1, i - l + 1, i, s[m], 1);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
for (int i = 0; i < n; ++i) {
s[i + n] = s[i];
s[i] = -INF;
}
for (int i = 0; i < q; ++i) {
cin >> t[i] >> L[i] >> R[i];
--L[i];
++t[i];
L[i] += n, R[i] += n;
by_t[t[i]].push_back(i);
}
n *= 2;
for (int i = 0; i < n; ++i) {
mx[i + n] = {s[i], i};
}
for (int i = n - 1; i > 0; --i) {
mx[i] = max(mx[i << 1], mx[i << 1 | 1]);
}
rec(0, n - 1);
fl = fr = Fenwick(n);
for (int t = 1; t <= n; ++t) {
for (auto [ind, d] : updl[t]) {
fl.upd(ind, d);
}
for (auto [ind, d] : updr[t]) {
fr.upd(ind, d);
}
for (int i : by_t[t]) {
ans[i] += fr.get(L[i], R[i]);
ans[i] += fl.get(L[i] - t + 1, R[i] - t + 1);
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
28492 KB |
Output is correct |
2 |
Correct |
18 ms |
28492 KB |
Output is correct |
3 |
Correct |
20 ms |
28492 KB |
Output is correct |
4 |
Correct |
20 ms |
28500 KB |
Output is correct |
5 |
Correct |
18 ms |
28612 KB |
Output is correct |
6 |
Correct |
19 ms |
28572 KB |
Output is correct |
7 |
Correct |
20 ms |
28596 KB |
Output is correct |
8 |
Correct |
19 ms |
28620 KB |
Output is correct |
9 |
Correct |
20 ms |
28508 KB |
Output is correct |
10 |
Correct |
19 ms |
28584 KB |
Output is correct |
11 |
Correct |
21 ms |
28500 KB |
Output is correct |
12 |
Correct |
20 ms |
28492 KB |
Output is correct |
13 |
Correct |
20 ms |
28504 KB |
Output is correct |
14 |
Correct |
19 ms |
28492 KB |
Output is correct |
15 |
Correct |
20 ms |
28560 KB |
Output is correct |
16 |
Correct |
18 ms |
28492 KB |
Output is correct |
17 |
Correct |
17 ms |
28492 KB |
Output is correct |
18 |
Correct |
23 ms |
28556 KB |
Output is correct |
19 |
Correct |
20 ms |
28528 KB |
Output is correct |
20 |
Correct |
20 ms |
28556 KB |
Output is correct |
21 |
Correct |
20 ms |
28620 KB |
Output is correct |
22 |
Correct |
20 ms |
28492 KB |
Output is correct |
23 |
Correct |
20 ms |
28576 KB |
Output is correct |
24 |
Correct |
19 ms |
28584 KB |
Output is correct |
25 |
Correct |
18 ms |
28596 KB |
Output is correct |
26 |
Correct |
19 ms |
28512 KB |
Output is correct |
27 |
Correct |
19 ms |
28516 KB |
Output is correct |
28 |
Correct |
21 ms |
28576 KB |
Output is correct |
29 |
Correct |
19 ms |
28552 KB |
Output is correct |
30 |
Correct |
18 ms |
28600 KB |
Output is correct |
31 |
Correct |
18 ms |
28536 KB |
Output is correct |
32 |
Correct |
19 ms |
28520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
28492 KB |
Output is correct |
2 |
Correct |
395 ms |
113920 KB |
Output is correct |
3 |
Correct |
424 ms |
115524 KB |
Output is correct |
4 |
Correct |
409 ms |
114312 KB |
Output is correct |
5 |
Correct |
392 ms |
117184 KB |
Output is correct |
6 |
Correct |
422 ms |
115136 KB |
Output is correct |
7 |
Correct |
373 ms |
115152 KB |
Output is correct |
8 |
Correct |
414 ms |
116572 KB |
Output is correct |
9 |
Correct |
386 ms |
116124 KB |
Output is correct |
10 |
Correct |
360 ms |
113532 KB |
Output is correct |
11 |
Correct |
382 ms |
115832 KB |
Output is correct |
12 |
Correct |
371 ms |
114396 KB |
Output is correct |
13 |
Correct |
414 ms |
114828 KB |
Output is correct |
14 |
Correct |
388 ms |
115748 KB |
Output is correct |
15 |
Correct |
386 ms |
115708 KB |
Output is correct |
16 |
Correct |
400 ms |
116496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
28492 KB |
Output is correct |
2 |
Correct |
486 ms |
118500 KB |
Output is correct |
3 |
Correct |
461 ms |
116824 KB |
Output is correct |
4 |
Correct |
476 ms |
121628 KB |
Output is correct |
5 |
Correct |
448 ms |
115832 KB |
Output is correct |
6 |
Correct |
455 ms |
117260 KB |
Output is correct |
7 |
Correct |
456 ms |
117392 KB |
Output is correct |
8 |
Correct |
469 ms |
116520 KB |
Output is correct |
9 |
Correct |
524 ms |
115968 KB |
Output is correct |
10 |
Correct |
516 ms |
116104 KB |
Output is correct |
11 |
Correct |
469 ms |
120208 KB |
Output is correct |
12 |
Correct |
455 ms |
119048 KB |
Output is correct |
13 |
Correct |
467 ms |
119380 KB |
Output is correct |
14 |
Correct |
447 ms |
114684 KB |
Output is correct |
15 |
Correct |
477 ms |
118872 KB |
Output is correct |
16 |
Correct |
456 ms |
117536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
103244 KB |
Output is correct |
2 |
Correct |
325 ms |
103624 KB |
Output is correct |
3 |
Correct |
327 ms |
105368 KB |
Output is correct |
4 |
Correct |
343 ms |
102880 KB |
Output is correct |
5 |
Correct |
335 ms |
103260 KB |
Output is correct |
6 |
Correct |
318 ms |
103976 KB |
Output is correct |
7 |
Correct |
327 ms |
105168 KB |
Output is correct |
8 |
Correct |
364 ms |
104420 KB |
Output is correct |
9 |
Correct |
329 ms |
103304 KB |
Output is correct |
10 |
Correct |
353 ms |
104520 KB |
Output is correct |
11 |
Correct |
332 ms |
103792 KB |
Output is correct |
12 |
Correct |
321 ms |
104036 KB |
Output is correct |
13 |
Correct |
344 ms |
103756 KB |
Output is correct |
14 |
Correct |
330 ms |
103780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
28492 KB |
Output is correct |
2 |
Correct |
18 ms |
28492 KB |
Output is correct |
3 |
Correct |
20 ms |
28492 KB |
Output is correct |
4 |
Correct |
20 ms |
28500 KB |
Output is correct |
5 |
Correct |
18 ms |
28612 KB |
Output is correct |
6 |
Correct |
19 ms |
28572 KB |
Output is correct |
7 |
Correct |
20 ms |
28596 KB |
Output is correct |
8 |
Correct |
19 ms |
28620 KB |
Output is correct |
9 |
Correct |
20 ms |
28508 KB |
Output is correct |
10 |
Correct |
19 ms |
28584 KB |
Output is correct |
11 |
Correct |
21 ms |
28500 KB |
Output is correct |
12 |
Correct |
20 ms |
28492 KB |
Output is correct |
13 |
Correct |
20 ms |
28504 KB |
Output is correct |
14 |
Correct |
19 ms |
28492 KB |
Output is correct |
15 |
Correct |
20 ms |
28560 KB |
Output is correct |
16 |
Correct |
18 ms |
28492 KB |
Output is correct |
17 |
Correct |
17 ms |
28492 KB |
Output is correct |
18 |
Correct |
23 ms |
28556 KB |
Output is correct |
19 |
Correct |
20 ms |
28528 KB |
Output is correct |
20 |
Correct |
20 ms |
28556 KB |
Output is correct |
21 |
Correct |
20 ms |
28620 KB |
Output is correct |
22 |
Correct |
20 ms |
28492 KB |
Output is correct |
23 |
Correct |
20 ms |
28576 KB |
Output is correct |
24 |
Correct |
19 ms |
28584 KB |
Output is correct |
25 |
Correct |
18 ms |
28596 KB |
Output is correct |
26 |
Correct |
19 ms |
28512 KB |
Output is correct |
27 |
Correct |
19 ms |
28516 KB |
Output is correct |
28 |
Correct |
21 ms |
28576 KB |
Output is correct |
29 |
Correct |
19 ms |
28552 KB |
Output is correct |
30 |
Correct |
18 ms |
28600 KB |
Output is correct |
31 |
Correct |
18 ms |
28536 KB |
Output is correct |
32 |
Correct |
19 ms |
28520 KB |
Output is correct |
33 |
Correct |
395 ms |
113920 KB |
Output is correct |
34 |
Correct |
424 ms |
115524 KB |
Output is correct |
35 |
Correct |
409 ms |
114312 KB |
Output is correct |
36 |
Correct |
392 ms |
117184 KB |
Output is correct |
37 |
Correct |
422 ms |
115136 KB |
Output is correct |
38 |
Correct |
373 ms |
115152 KB |
Output is correct |
39 |
Correct |
414 ms |
116572 KB |
Output is correct |
40 |
Correct |
386 ms |
116124 KB |
Output is correct |
41 |
Correct |
360 ms |
113532 KB |
Output is correct |
42 |
Correct |
382 ms |
115832 KB |
Output is correct |
43 |
Correct |
371 ms |
114396 KB |
Output is correct |
44 |
Correct |
414 ms |
114828 KB |
Output is correct |
45 |
Correct |
388 ms |
115748 KB |
Output is correct |
46 |
Correct |
386 ms |
115708 KB |
Output is correct |
47 |
Correct |
400 ms |
116496 KB |
Output is correct |
48 |
Correct |
486 ms |
118500 KB |
Output is correct |
49 |
Correct |
461 ms |
116824 KB |
Output is correct |
50 |
Correct |
476 ms |
121628 KB |
Output is correct |
51 |
Correct |
448 ms |
115832 KB |
Output is correct |
52 |
Correct |
455 ms |
117260 KB |
Output is correct |
53 |
Correct |
456 ms |
117392 KB |
Output is correct |
54 |
Correct |
469 ms |
116520 KB |
Output is correct |
55 |
Correct |
524 ms |
115968 KB |
Output is correct |
56 |
Correct |
516 ms |
116104 KB |
Output is correct |
57 |
Correct |
469 ms |
120208 KB |
Output is correct |
58 |
Correct |
455 ms |
119048 KB |
Output is correct |
59 |
Correct |
467 ms |
119380 KB |
Output is correct |
60 |
Correct |
447 ms |
114684 KB |
Output is correct |
61 |
Correct |
477 ms |
118872 KB |
Output is correct |
62 |
Correct |
456 ms |
117536 KB |
Output is correct |
63 |
Correct |
328 ms |
103244 KB |
Output is correct |
64 |
Correct |
325 ms |
103624 KB |
Output is correct |
65 |
Correct |
327 ms |
105368 KB |
Output is correct |
66 |
Correct |
343 ms |
102880 KB |
Output is correct |
67 |
Correct |
335 ms |
103260 KB |
Output is correct |
68 |
Correct |
318 ms |
103976 KB |
Output is correct |
69 |
Correct |
327 ms |
105168 KB |
Output is correct |
70 |
Correct |
364 ms |
104420 KB |
Output is correct |
71 |
Correct |
329 ms |
103304 KB |
Output is correct |
72 |
Correct |
353 ms |
104520 KB |
Output is correct |
73 |
Correct |
332 ms |
103792 KB |
Output is correct |
74 |
Correct |
321 ms |
104036 KB |
Output is correct |
75 |
Correct |
344 ms |
103756 KB |
Output is correct |
76 |
Correct |
330 ms |
103780 KB |
Output is correct |
77 |
Correct |
442 ms |
117612 KB |
Output is correct |
78 |
Correct |
470 ms |
120004 KB |
Output is correct |
79 |
Correct |
466 ms |
119872 KB |
Output is correct |
80 |
Correct |
451 ms |
117836 KB |
Output is correct |
81 |
Correct |
461 ms |
117428 KB |
Output is correct |
82 |
Correct |
479 ms |
118796 KB |
Output is correct |
83 |
Correct |
476 ms |
117624 KB |
Output is correct |
84 |
Correct |
442 ms |
115556 KB |
Output is correct |
85 |
Correct |
474 ms |
118520 KB |
Output is correct |
86 |
Correct |
498 ms |
120328 KB |
Output is correct |
87 |
Correct |
423 ms |
117296 KB |
Output is correct |
88 |
Correct |
406 ms |
115584 KB |
Output is correct |
89 |
Correct |
382 ms |
112768 KB |
Output is correct |
90 |
Correct |
411 ms |
116280 KB |
Output is correct |
91 |
Correct |
409 ms |
115524 KB |
Output is correct |
92 |
Correct |
457 ms |
115916 KB |
Output is correct |
93 |
Correct |
423 ms |
116460 KB |
Output is correct |
94 |
Correct |
405 ms |
117228 KB |
Output is correct |
95 |
Correct |
417 ms |
117368 KB |
Output is correct |
96 |
Correct |
418 ms |
117488 KB |
Output is correct |
97 |
Correct |
407 ms |
115832 KB |
Output is correct |
98 |
Correct |
391 ms |
114264 KB |
Output is correct |
99 |
Correct |
410 ms |
116748 KB |
Output is correct |
100 |
Correct |
425 ms |
116816 KB |
Output is correct |
101 |
Correct |
405 ms |
114328 KB |
Output is correct |
102 |
Correct |
411 ms |
115828 KB |
Output is correct |