#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#include <x86intrin.h>
#include <bits/stdc++.h>
#include <chrono>
#include <random>
// @author: Vlapos
namespace operators
{
template <typename T1, typename T2>
std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
{
in >> x.first >> x.second;
return in;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
{
out << x.first << " " << x.second;
return out;
}
template <typename T1>
std::istream &operator>>(std::istream &in, std::vector<T1> &x)
{
for (auto &i : x)
in >> i;
return in;
}
template <typename T1>
std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
{
for (auto &i : x)
out << i << " ";
return out;
}
template <typename T1>
std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
{
for (auto &i : x)
out << i << " ";
return out;
}
}
// name spaces
using namespace std;
using namespace operators;
// end of name spaces
// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines
// usefull stuff
void boost()
{
ios_base ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
inline int getbit(int &x, int &bt) { return (x >> bt) & 1; }
const int dx4[4] = {-1, 0, 0, 1};
const int dy4[4] = {0, -1, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[8] = {-1, -0, 1, -1, 1, -1, 0, 1};
const ll INF = (1e15) + 500;
const int BIG = (2e9) + 500;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;
// #define int ll
struct segTreeAdd
{
vector<pll> tree;
int sz;
void init(int n)
{
sz = 1;
while (sz < n)
sz *= 2;
tree.resize(2 * sz, {0, 0});
}
void upd(int v, int lv, int rv, ll val)
{
tree[v].s += val;
tree[v].f += (ll)(rv - lv) * (ll)val;
}
void push(int v, int lv, int rv)
{
if (rv - lv == 1 or tree[v].s == 0)
return;
int m = (lv + rv) >> 1;
upd(v * 2 + 1, lv, m, tree[v].s);
upd(v * 2 + 2, m, rv, tree[v].s);
tree[v].s = 0;
}
void update(int l, int r, ll val, int v, int lv, int rv)
{
push(v, lv, rv);
if (l <= lv and rv <= r)
{
upd(v, lv, rv, val);
return;
}
if (rv <= l or r <= lv)
return;
int m = (lv + rv) >> 1;
update(l, r, val, v * 2 + 1, lv, m);
update(l, r, val, v * 2 + 2, m, rv);
tree[v].f = tree[v * 2 + 1].f + tree[v * 2 + 2].f;
}
void update(int l, int r, ll val)
{
update(l, r, val, 0, 0, sz);
}
ll get(int l, int r, int v, int lv, int rv)
{
push(v, lv, rv);
if (l <= lv and rv <= r)
return tree[v].f;
if (rv <= l or r <= lv)
return 0;
int m = (lv + rv) >> 1;
return get(l, r, v * 2 + 1, lv, m) + get(l, r, v * 2 + 2, m, rv);
}
ll get(int l, int r)
{
return get(l, r, 0, 0, sz);
}
};
struct test
{
struct op
{
int t, l, r, val;
};
vector<vector<op>> reqs;
int n, q;
void addTriangle(int l, int r, int val)
{
if (l >= r)
return;
reqs[0].push_back({0, 0, n - l + 1, +val});
reqs[0].push_back({1, r, n, -val});
reqs[r - l].push_back({1, r, n, +val});
reqs[r - l].push_back({0, 0, n - l + 1, -val});
}
void solve(int testcase)
{
boost();
cin >> n >> q;
vector<int> a(n), L(n);
cin >> a;
reqs.resize(3 * n + 10);
{
stack<int> bf;
for (int i = 0; i < n; ++i)
{
while (bf.size() and a[bf.top()] <= a[i])
bf.pop();
if (bf.size())
L[i] = bf.top();
else
L[i] = i - n - 3;
bf.push(i);
}
}
{
stack<int> bf;
for (int i = n - 1; i >= 0; --i)
{
while (bf.size() and a[bf.top()] < a[i])
bf.pop();
int curL = L[i] + 1;
int curR = bf.size() ? bf.top() : n;
addTriangle(curL, curR, a[i]); // [L, R)
addTriangle(curL, i, -a[i]); // [L, R)
addTriangle(i + 1, curR, -a[i]);
bf.push(i);
}
}
vector<vector<pair<int, pii>>> qrs(n + 10 + 10);
vector<ll> ans(q);
for (int i = 0; i < q; ++i)
{
int t, l, r;
cin >> t >> l >> r;
--l;
qrs[t].pb({i, {l, r}});
}
segTreeAdd diag, square;
diag.init(n * 3 + 10);
square.init(n + 100);
for (int t = 0; t <= n; ++t)
{
for (op req : reqs[t])
{
if (req.t == 0)
// triangle
diag.update(req.l, req.r, req.val);
else
// square
square.update(req.l, req.r, req.val);
}
// for (int i = 0; i < n; ++i)
// {
// cout << diag.get(n - i + t, n - i + t + 1) + square.get(i, i + 1) << ' ';
// }
// cout << "\n";
for (auto &[id, seg] : qrs[t])
{
auto [l, r] = seg;
ans[id] = diag.get(n - (r - 1) + t, n - l + t + 1) + square.get(l, r);
}
}
for (int i = 0; i < q; ++i)
cout << ans[i] << "\n";
}
};
main()
{
boost();
int q = 1;
// cin >> q;
for (int i = 0; i < q; i++)
{
test t;
t.solve(i);
}
return 0;
}
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
// //
// Coded by Der_Vlἀpos //
// //
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
Compilation message
ho_t5.cpp:270:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
270 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
607 ms |
99944 KB |
Output is correct |
3 |
Correct |
568 ms |
97292 KB |
Output is correct |
4 |
Correct |
600 ms |
98812 KB |
Output is correct |
5 |
Correct |
609 ms |
104200 KB |
Output is correct |
6 |
Correct |
619 ms |
98364 KB |
Output is correct |
7 |
Correct |
587 ms |
98988 KB |
Output is correct |
8 |
Correct |
621 ms |
100212 KB |
Output is correct |
9 |
Correct |
626 ms |
102292 KB |
Output is correct |
10 |
Correct |
621 ms |
97000 KB |
Output is correct |
11 |
Correct |
618 ms |
99244 KB |
Output is correct |
12 |
Correct |
599 ms |
97464 KB |
Output is correct |
13 |
Correct |
620 ms |
104624 KB |
Output is correct |
14 |
Correct |
605 ms |
102180 KB |
Output is correct |
15 |
Correct |
630 ms |
104856 KB |
Output is correct |
16 |
Correct |
628 ms |
98728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
593 ms |
96684 KB |
Output is correct |
3 |
Correct |
587 ms |
95544 KB |
Output is correct |
4 |
Correct |
576 ms |
101372 KB |
Output is correct |
5 |
Correct |
578 ms |
96076 KB |
Output is correct |
6 |
Correct |
596 ms |
97356 KB |
Output is correct |
7 |
Correct |
595 ms |
98476 KB |
Output is correct |
8 |
Correct |
587 ms |
98480 KB |
Output is correct |
9 |
Correct |
567 ms |
95912 KB |
Output is correct |
10 |
Correct |
584 ms |
94312 KB |
Output is correct |
11 |
Correct |
609 ms |
101396 KB |
Output is correct |
12 |
Correct |
584 ms |
98292 KB |
Output is correct |
13 |
Correct |
674 ms |
98184 KB |
Output is correct |
14 |
Correct |
590 ms |
96540 KB |
Output is correct |
15 |
Correct |
623 ms |
97548 KB |
Output is correct |
16 |
Correct |
581 ms |
100628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
520 ms |
93776 KB |
Output is correct |
2 |
Correct |
538 ms |
94892 KB |
Output is correct |
3 |
Correct |
538 ms |
95936 KB |
Output is correct |
4 |
Correct |
538 ms |
94616 KB |
Output is correct |
5 |
Correct |
525 ms |
93484 KB |
Output is correct |
6 |
Correct |
534 ms |
96828 KB |
Output is correct |
7 |
Correct |
556 ms |
95400 KB |
Output is correct |
8 |
Correct |
540 ms |
96168 KB |
Output is correct |
9 |
Correct |
519 ms |
95640 KB |
Output is correct |
10 |
Correct |
537 ms |
97104 KB |
Output is correct |
11 |
Correct |
548 ms |
96684 KB |
Output is correct |
12 |
Correct |
540 ms |
95280 KB |
Output is correct |
13 |
Correct |
533 ms |
95404 KB |
Output is correct |
14 |
Correct |
543 ms |
95744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
607 ms |
99944 KB |
Output is correct |
34 |
Correct |
568 ms |
97292 KB |
Output is correct |
35 |
Correct |
600 ms |
98812 KB |
Output is correct |
36 |
Correct |
609 ms |
104200 KB |
Output is correct |
37 |
Correct |
619 ms |
98364 KB |
Output is correct |
38 |
Correct |
587 ms |
98988 KB |
Output is correct |
39 |
Correct |
621 ms |
100212 KB |
Output is correct |
40 |
Correct |
626 ms |
102292 KB |
Output is correct |
41 |
Correct |
621 ms |
97000 KB |
Output is correct |
42 |
Correct |
618 ms |
99244 KB |
Output is correct |
43 |
Correct |
599 ms |
97464 KB |
Output is correct |
44 |
Correct |
620 ms |
104624 KB |
Output is correct |
45 |
Correct |
605 ms |
102180 KB |
Output is correct |
46 |
Correct |
630 ms |
104856 KB |
Output is correct |
47 |
Correct |
628 ms |
98728 KB |
Output is correct |
48 |
Correct |
593 ms |
96684 KB |
Output is correct |
49 |
Correct |
587 ms |
95544 KB |
Output is correct |
50 |
Correct |
576 ms |
101372 KB |
Output is correct |
51 |
Correct |
578 ms |
96076 KB |
Output is correct |
52 |
Correct |
596 ms |
97356 KB |
Output is correct |
53 |
Correct |
595 ms |
98476 KB |
Output is correct |
54 |
Correct |
587 ms |
98480 KB |
Output is correct |
55 |
Correct |
567 ms |
95912 KB |
Output is correct |
56 |
Correct |
584 ms |
94312 KB |
Output is correct |
57 |
Correct |
609 ms |
101396 KB |
Output is correct |
58 |
Correct |
584 ms |
98292 KB |
Output is correct |
59 |
Correct |
674 ms |
98184 KB |
Output is correct |
60 |
Correct |
590 ms |
96540 KB |
Output is correct |
61 |
Correct |
623 ms |
97548 KB |
Output is correct |
62 |
Correct |
581 ms |
100628 KB |
Output is correct |
63 |
Correct |
520 ms |
93776 KB |
Output is correct |
64 |
Correct |
538 ms |
94892 KB |
Output is correct |
65 |
Correct |
538 ms |
95936 KB |
Output is correct |
66 |
Correct |
538 ms |
94616 KB |
Output is correct |
67 |
Correct |
525 ms |
93484 KB |
Output is correct |
68 |
Correct |
534 ms |
96828 KB |
Output is correct |
69 |
Correct |
556 ms |
95400 KB |
Output is correct |
70 |
Correct |
540 ms |
96168 KB |
Output is correct |
71 |
Correct |
519 ms |
95640 KB |
Output is correct |
72 |
Correct |
537 ms |
97104 KB |
Output is correct |
73 |
Correct |
548 ms |
96684 KB |
Output is correct |
74 |
Correct |
540 ms |
95280 KB |
Output is correct |
75 |
Correct |
533 ms |
95404 KB |
Output is correct |
76 |
Correct |
543 ms |
95744 KB |
Output is correct |
77 |
Correct |
666 ms |
97820 KB |
Output is correct |
78 |
Correct |
644 ms |
97904 KB |
Output is correct |
79 |
Correct |
654 ms |
100940 KB |
Output is correct |
80 |
Correct |
629 ms |
97192 KB |
Output is correct |
81 |
Correct |
620 ms |
97024 KB |
Output is correct |
82 |
Correct |
685 ms |
97812 KB |
Output is correct |
83 |
Correct |
658 ms |
98472 KB |
Output is correct |
84 |
Correct |
651 ms |
97200 KB |
Output is correct |
85 |
Correct |
654 ms |
101548 KB |
Output is correct |
86 |
Correct |
636 ms |
97036 KB |
Output is correct |
87 |
Correct |
629 ms |
101320 KB |
Output is correct |
88 |
Correct |
606 ms |
99996 KB |
Output is correct |
89 |
Correct |
609 ms |
96704 KB |
Output is correct |
90 |
Correct |
647 ms |
100708 KB |
Output is correct |
91 |
Correct |
626 ms |
96180 KB |
Output is correct |
92 |
Correct |
602 ms |
97292 KB |
Output is correct |
93 |
Correct |
615 ms |
97464 KB |
Output is correct |
94 |
Correct |
644 ms |
100776 KB |
Output is correct |
95 |
Correct |
635 ms |
99224 KB |
Output is correct |
96 |
Correct |
631 ms |
96928 KB |
Output is correct |
97 |
Correct |
637 ms |
95812 KB |
Output is correct |
98 |
Correct |
644 ms |
97060 KB |
Output is correct |
99 |
Correct |
638 ms |
98196 KB |
Output is correct |
100 |
Correct |
685 ms |
97456 KB |
Output is correct |
101 |
Correct |
638 ms |
96052 KB |
Output is correct |
102 |
Correct |
672 ms |
105260 KB |
Output is correct |