# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1012928 |
2024-07-02T20:15:32 Z |
EJIC_B_KEDAX |
Fire (JOI20_ho_t5) |
C++17 |
|
237 ms |
65608 KB |
#include <bits/stdc++.h>
#include <immintrin.h>
using namespace std;
using ll = long long;
using ld = long double;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
mt19937 mt(123);
void solve();
void init();
int32_t main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cout << fixed << setprecision(30);
init();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
struct segment_tree {
vector<ll> st;
int sz;
segment_tree() = default;
segment_tree(vector<int> a) {
sz = 1;
while (sz < a.size()) {
sz <<= 1;
}
st.resize(2 * sz, 0);
for (int i = 0; i < a.size(); i++) {
st[sz + i] = a[i];
}
for (int i = sz - 1; i > 0; i--) {
st[i] = st[2 * i] + st[2 * i + 1];
}
}
void add(int i, ll x) {
i += sz;
while (i) {
st[i] += x;
i >>= 1;
}
}
ll get(int l, int r) {
l += sz;
r += sz;
ll res = 0;
while (l <= r) {
if (l & 1) {
res += st[l++];
}
if (~r & 1) {
res += st[r--];
}
l >>= 1;
r >>= 1;
}
return res;
}
};
struct mseg_tree {
segment_tree def, step;
int now_step;
mseg_tree() = default;
mseg_tree(int n) {
now_step = 0;
def = segment_tree(vector<int>(n, 0));
step = segment_tree(vector<int>(n, 0));
}
void add_def(int i, int x) {
def.add(i, x);
}
void add_step(int i, int x) {
step.add(i, x);
def.add(i, -1ll * x * now_step);
}
void next_step() {
now_step++;
}
void set_step(int new_step) {
now_step = new_step;
}
ll get(int l, int r) {
return def.get(l, r) + now_step * step.get(l, r);
}
};
struct sparse_max {
using Type = pair<int, int>;
vector<vector<Type>> sp;
vector<int> p;
sparse_max(const vector<Type>& a) {
int n = a.size(), lg = 1;
while ((1 << lg) < n) {
lg++;
}
sp.resize(lg, vector<Type>(n));
for (int i = 0; i < n; i++) {
sp[0][i] = a[i];
}
for (int l = 1; l < lg; l++) {
for (int i = 0; i <= n - (1 << l); i++) {
sp[l][i] = max(sp[l - 1][i], sp[l - 1][i + (1 << (l - 1))]);
}
}
int nw = 0;
p.resize(n + 1, 0);
for (int i = 1; i <= n; i++) {
if ((1 << nw) * 2 < i) {
nw++;
}
p[i] = nw;
}
}
Type get(int l, int r) {
int lev = p[r - l + 1];
return max(sp[lev][l], sp[lev][r - (1 << lev) + 1]);
}
};
struct sparse_min {
using Type = pair<int, int>;
vector<vector<Type>> sp;
vector<int> p;
sparse_min(const vector<Type>& a) {
int n = a.size(), lg = 1;
while ((1 << lg) < n) {
lg++;
}
sp.resize(lg, vector<Type>(n));
for (int i = 0; i < n; i++) {
sp[0][i] = a[i];
}
for (int l = 1; l < lg; l++) {
for (int i = 0; i <= n - (1 << l); i++) {
sp[l][i] = min(sp[l - 1][i], sp[l - 1][i + (1 << (l - 1))]);
}
}
int nw = 0;
p.resize(n + 1, 0);
for (int i = 1; i <= n; i++) {
if ((1 << nw) * 2 < i) {
nw++;
}
p[i] = nw;
}
}
Type get(int l, int r) {
int lev = p[r - l + 1];
return min(sp[lev][l], sp[lev][r - (1 << lev) + 1]);
}
};
void init() {}
void solve() {
int n, q;
cin >> n >> q;
vector<int> a(n), nxt(n), prv(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// assert(n == 1);
mseg_tree st(n);
for (int i = 0; i < n; i++) {
st.add_step(i, a[i]);
}
vector<pair<int, int>> stack(1, {INT32_MAX, n});
for (int i = n - 1; i >= 0; i--) {
while (a[i] >= stack.back().x) {
stack.pop_back();
}
nxt[i] = stack.back().y;
stack.emplace_back(a[i], i);
}
stack.clear(); stack.emplace_back(INT32_MAX, -1);
for (int i = 0; i < n; i++) {
while (a[i] > stack.back().x) {
stack.pop_back();
}
prv[i] = stack.back().y;
stack.emplace_back(a[i], i);
}
vector<pair<int, int>> fsp(n);
for (int i = 0; i < n; i++) {
fsp[i] = {a[i], -i};
}
sparse_max sp(fsp);
vector<pair<int, pair<int, int>>> ev;
vector<pair<int, int>> qu;
vector<pair<int, int>> seg(q);
vector<ll> ans(q);
for (int i = 0; i < q; i++) {
int t, l, r;
cin >> t >> l >> r; l--; r--;
qu.emplace_back(t, i);
seg[i] = {l, r};
}
for (int i = 0; i < n; i++) {
ev.emplace_back(nxt[i] - i, make_pair(i, -a[i]));
if (prv[i] != -1) {
ev.emplace_back(i - prv[i], make_pair(i, -a[i]));
ev.emplace_back(nxt[i] - prv[i], make_pair(i, a[i]));
}
}
sort(all(ev));
sort(all(qu));
int ptrev = 0, ptrqu = 0;
for (int t = 0; t <= n; t++) {
while (ptrev < ev.size() && ev[ptrev].x == t) {
st.add_step(ev[ptrev].y.x, ev[ptrev].y.y);
ptrev++;
}
st.next_step();
while (ptrqu < qu.size() && qu[ptrqu].x == t) {
int i = qu[ptrqu++].y;
auto [l, r] = seg[i];
r++;
ll lans = 0, rans = 0;
{
auto [mx, mxi] = sp.get(max(0, l - t), l);
mxi *= -1;
lans = st.get(mxi + 1, n - 1);
int lst = min({mxi + t, nxt[mxi] - 1});
lans += 1ll * (lst - l + 1) * mx;
}
if (r < n) {
auto [mx, mxi] = sp.get(max(0, r - t), r);
mxi *= -1;
rans = st.get(mxi + 1, n - 1);
int lst = min(mxi + t, nxt[mxi] - 1);
rans += 1ll * (lst - r + 1) * mx;
}
ans[i] = lans - rans;
}
}
for (ll i : ans) {
cout << i << '\n';
}
}
Compilation message
ho_t5.cpp: In constructor 'segment_tree::segment_tree(std::vector<int>)':
ho_t5.cpp:37:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | while (sz < a.size()) {
| ~~~^~~~~~~~~~
ho_t5.cpp:41:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
ho_t5.cpp: In function 'void solve()':
ho_t5.cpp:222:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
222 | while (ptrev < ev.size() && ev[ptrev].x == t) {
| ~~~~~~^~~~~~~~~~~
ho_t5.cpp:227:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
227 | while (ptrqu < qu.size() && qu[ptrqu].x == t) {
| ~~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
456 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
192 ms |
65368 KB |
Output is correct |
3 |
Correct |
204 ms |
64880 KB |
Output is correct |
4 |
Correct |
190 ms |
65088 KB |
Output is correct |
5 |
Correct |
191 ms |
63928 KB |
Output is correct |
6 |
Correct |
179 ms |
65088 KB |
Output is correct |
7 |
Correct |
202 ms |
65556 KB |
Output is correct |
8 |
Correct |
178 ms |
64020 KB |
Output is correct |
9 |
Correct |
190 ms |
65516 KB |
Output is correct |
10 |
Correct |
186 ms |
64892 KB |
Output is correct |
11 |
Correct |
178 ms |
63828 KB |
Output is correct |
12 |
Correct |
175 ms |
64692 KB |
Output is correct |
13 |
Correct |
178 ms |
63484 KB |
Output is correct |
14 |
Correct |
174 ms |
63528 KB |
Output is correct |
15 |
Correct |
175 ms |
63676 KB |
Output is correct |
16 |
Correct |
175 ms |
65148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
207 ms |
65128 KB |
Output is correct |
3 |
Correct |
186 ms |
64536 KB |
Output is correct |
4 |
Correct |
201 ms |
64176 KB |
Output is correct |
5 |
Correct |
206 ms |
64868 KB |
Output is correct |
6 |
Correct |
206 ms |
65208 KB |
Output is correct |
7 |
Correct |
206 ms |
63432 KB |
Output is correct |
8 |
Correct |
197 ms |
65012 KB |
Output is correct |
9 |
Correct |
200 ms |
64592 KB |
Output is correct |
10 |
Correct |
188 ms |
64144 KB |
Output is correct |
11 |
Correct |
200 ms |
63920 KB |
Output is correct |
12 |
Correct |
206 ms |
63676 KB |
Output is correct |
13 |
Correct |
201 ms |
63684 KB |
Output is correct |
14 |
Correct |
204 ms |
64636 KB |
Output is correct |
15 |
Correct |
201 ms |
63884 KB |
Output is correct |
16 |
Correct |
199 ms |
63676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
206 ms |
59768 KB |
Output is correct |
2 |
Correct |
206 ms |
64100 KB |
Output is correct |
3 |
Correct |
217 ms |
63412 KB |
Output is correct |
4 |
Correct |
208 ms |
63892 KB |
Output is correct |
5 |
Correct |
198 ms |
64144 KB |
Output is correct |
6 |
Correct |
217 ms |
64348 KB |
Output is correct |
7 |
Correct |
236 ms |
62980 KB |
Output is correct |
8 |
Correct |
217 ms |
64596 KB |
Output is correct |
9 |
Correct |
213 ms |
64224 KB |
Output is correct |
10 |
Correct |
207 ms |
64588 KB |
Output is correct |
11 |
Correct |
223 ms |
64396 KB |
Output is correct |
12 |
Correct |
218 ms |
64356 KB |
Output is correct |
13 |
Correct |
213 ms |
64368 KB |
Output is correct |
14 |
Correct |
223 ms |
64320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
456 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
192 ms |
65368 KB |
Output is correct |
34 |
Correct |
204 ms |
64880 KB |
Output is correct |
35 |
Correct |
190 ms |
65088 KB |
Output is correct |
36 |
Correct |
191 ms |
63928 KB |
Output is correct |
37 |
Correct |
179 ms |
65088 KB |
Output is correct |
38 |
Correct |
202 ms |
65556 KB |
Output is correct |
39 |
Correct |
178 ms |
64020 KB |
Output is correct |
40 |
Correct |
190 ms |
65516 KB |
Output is correct |
41 |
Correct |
186 ms |
64892 KB |
Output is correct |
42 |
Correct |
178 ms |
63828 KB |
Output is correct |
43 |
Correct |
175 ms |
64692 KB |
Output is correct |
44 |
Correct |
178 ms |
63484 KB |
Output is correct |
45 |
Correct |
174 ms |
63528 KB |
Output is correct |
46 |
Correct |
175 ms |
63676 KB |
Output is correct |
47 |
Correct |
175 ms |
65148 KB |
Output is correct |
48 |
Correct |
207 ms |
65128 KB |
Output is correct |
49 |
Correct |
186 ms |
64536 KB |
Output is correct |
50 |
Correct |
201 ms |
64176 KB |
Output is correct |
51 |
Correct |
206 ms |
64868 KB |
Output is correct |
52 |
Correct |
206 ms |
65208 KB |
Output is correct |
53 |
Correct |
206 ms |
63432 KB |
Output is correct |
54 |
Correct |
197 ms |
65012 KB |
Output is correct |
55 |
Correct |
200 ms |
64592 KB |
Output is correct |
56 |
Correct |
188 ms |
64144 KB |
Output is correct |
57 |
Correct |
200 ms |
63920 KB |
Output is correct |
58 |
Correct |
206 ms |
63676 KB |
Output is correct |
59 |
Correct |
201 ms |
63684 KB |
Output is correct |
60 |
Correct |
204 ms |
64636 KB |
Output is correct |
61 |
Correct |
201 ms |
63884 KB |
Output is correct |
62 |
Correct |
199 ms |
63676 KB |
Output is correct |
63 |
Correct |
206 ms |
59768 KB |
Output is correct |
64 |
Correct |
206 ms |
64100 KB |
Output is correct |
65 |
Correct |
217 ms |
63412 KB |
Output is correct |
66 |
Correct |
208 ms |
63892 KB |
Output is correct |
67 |
Correct |
198 ms |
64144 KB |
Output is correct |
68 |
Correct |
217 ms |
64348 KB |
Output is correct |
69 |
Correct |
236 ms |
62980 KB |
Output is correct |
70 |
Correct |
217 ms |
64596 KB |
Output is correct |
71 |
Correct |
213 ms |
64224 KB |
Output is correct |
72 |
Correct |
207 ms |
64588 KB |
Output is correct |
73 |
Correct |
223 ms |
64396 KB |
Output is correct |
74 |
Correct |
218 ms |
64356 KB |
Output is correct |
75 |
Correct |
213 ms |
64368 KB |
Output is correct |
76 |
Correct |
223 ms |
64320 KB |
Output is correct |
77 |
Correct |
200 ms |
65376 KB |
Output is correct |
78 |
Correct |
223 ms |
65608 KB |
Output is correct |
79 |
Correct |
208 ms |
63928 KB |
Output is correct |
80 |
Correct |
208 ms |
64880 KB |
Output is correct |
81 |
Correct |
204 ms |
64696 KB |
Output is correct |
82 |
Correct |
202 ms |
65344 KB |
Output is correct |
83 |
Correct |
221 ms |
65108 KB |
Output is correct |
84 |
Correct |
196 ms |
64684 KB |
Output is correct |
85 |
Correct |
215 ms |
63668 KB |
Output is correct |
86 |
Correct |
196 ms |
64940 KB |
Output is correct |
87 |
Correct |
229 ms |
64040 KB |
Output is correct |
88 |
Correct |
193 ms |
63916 KB |
Output is correct |
89 |
Correct |
188 ms |
64652 KB |
Output is correct |
90 |
Correct |
208 ms |
63940 KB |
Output is correct |
91 |
Correct |
182 ms |
65120 KB |
Output is correct |
92 |
Correct |
203 ms |
64540 KB |
Output is correct |
93 |
Correct |
216 ms |
65580 KB |
Output is correct |
94 |
Correct |
203 ms |
64244 KB |
Output is correct |
95 |
Correct |
209 ms |
64196 KB |
Output is correct |
96 |
Correct |
202 ms |
65368 KB |
Output is correct |
97 |
Correct |
205 ms |
65376 KB |
Output is correct |
98 |
Correct |
212 ms |
64628 KB |
Output is correct |
99 |
Correct |
237 ms |
65092 KB |
Output is correct |
100 |
Correct |
229 ms |
65244 KB |
Output is correct |
101 |
Correct |
216 ms |
64864 KB |
Output is correct |
102 |
Correct |
216 ms |
63680 KB |
Output is correct |