//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
struct pytanie {
int l, r, id;
};
struct SegTree {
struct node {
long long A, B;
};
vector<node> st;
int com;
SegTree(int n) {
com = 1;
while (com < n) {
com <<= 1;
}
st.resize(com << 1);
--com;
}
void wstaw(int x, long long A, long long B) {
x += com;
st[x] = {A, B};
x >>= 1;
while (x) {
st[x] = {st[x * 2].A + st[x * 2 + 1].A, st[x * 2].B + st[x * 2 + 1].B};
x >>= 1;
}
}
long long pytaj(int a, int b, int tm) {
a += com;
b += com + 1;
long long ret = 0;
while (a < b) {
if (a & 1) {
ret += st[a].A * tm + st[a].B;
++a;
}
if (b & 1) {
ret += st[b - 1].A * tm + st[b - 1].B;
}
a >>= 1;
b >>= 1;
}
return ret;
}
};
struct nod {
int lf, rg;
};
//~ const int N = 100;
const int N = 201000;
int n, q, com;
int t[N];
int lf[N]; // następny na lewo większy
int rg[N]; // następny na prawo większy równy
int slp[N]; // aktualny slope
long long odp[N];
vector<pytanie> pyt[N]; // zapytania po czasie
vector<int> zgon[N]; // kto właśnie znika
vector<int> slope[N]; // komu maleje slope w tym momencie
vector<nod> ts;
int kiedy_zgon(int x) { // pierwszy moment nieistnienia x
if (lf[x] == 0) {
return 1e9;
}
return rg[x] - lf[x] - 1;
}
pair<int, int> aktualny(int x, int tm) { // aktualny przedział pożaru x w momencie tm
int l = x;
if (lf[x]) {
l = max(l, lf[x] + tm + 1);
}
int r = min(rg[x] - 1, x + tm);
return {l, r};
}
void update(int x) {
int l = x << 1;
int r = l ^ 1;
ts[x] = {min(ts[l].lf, ts[r].lf), max(ts[l].rg, ts[r].rg)};
}
// pożar, który posiada w sobie p w momencie tm
int fin(int x, int l, int r, int tm, int p) {
if (l == r) {
return l;
}
if (ts[x * 2].lf > ts[x * 2].rg) {
return fin(x * 2 + 1, (l + r) / 2 + 1, r, tm, p);
}
auto lf = aktualny(ts[x * 2].rg, tm);
if (lf.second < p) {
return fin(x * 2 + 1, (l + r) / 2 + 1, r, tm, p);
}
return fin(x * 2, l, (l + r) / 2, tm, p);
}
void zgas(int x) {
x += com;
ts[x] = {(int)1e9, (int)-1e9};
x >>= 1;
while (x) {
update(x);
x >>= 1;
}
}
int main() {
scanf("%d%d", &n, &q);
com = 1;
while (com < n) {
com <<= 1;
}
ts.resize(com << 1);
--com;
SegTree st(n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &t[i]);
st.wstaw(i, t[i], t[i]);
ts[i + com] = {i, i};
slp[i] = 1;
}
for (int i = n + 1; i <= com + 1; ++i) {
ts[i + com] = {(int)1e9, (int)-1e9};
}
for (int i = com; i; --i) {
update(i);
}
t[0] = 1e9 + 7;
t[n + 1] = 1e9 + 7;
for (int i = 1; i <= n; ++i) {
lf[i] = i - 1;
while (t[i] >= t[lf[i]]) {
lf[i] = lf[lf[i]];
}
}
for (int i = n; i; --i) {
rg[i] = i + 1;
while (t[i] > t[rg[i]]) {
rg[i] = rg[rg[i]];
}
}
for (int i = 1; i <= n; ++i) {
int x = kiedy_zgon(i);
//~ debug(i, x);
if (x <= n) {
zgon[x].push_back(i);
}
if (lf[i]) {
slope[i - lf[i] - 1].push_back(i);
}
slope[rg[i] - i - 1].push_back(i);
}
//~ for (int i = 0; i <= n; ++i) {
//~ debug(i, slope[i]);
//~ }
//~ for (int i = 0; i <= n; ++i) {
//~ for (int j = 1; j <= n; ++j) {
//~ debug(i, j, aktualny(j, i));
//~ }
//~ }
for (int i = 1; i <= q; ++i) {
int a, b, c;
scanf("%d%d%d", &c, &a, &b);
pyt[c].push_back({a, b, i});
}
for (int tm = 0; tm <= n; ++tm) {
//~ debug(tm);
for (int i : zgon[tm]) {
//~ debug("ZGON", i);
zgas(i);
st.wstaw(i, 0, 0);
}
for (int i : slope[tm]) {
if (slp[i] == 1) { // zamiana na stały
auto x = aktualny(i, tm);
//~ debug(i, x);
st.wstaw(i, 0, (long long)(x.second - x.first + 1) * t[i]);
}
else { // zamiana na malejący
auto x = aktualny(i, tm);
//~ debug(i, x);
st.wstaw(i, -t[i], (long long)(tm + x.second - x.first + 1) * t[i]);
}
--slp[i];
//~ debug(i, slp[i]);
}
//~ for (int i = 1; i <= n; ++i) {
//~ printf("%lld ", st.pytaj(i, i, tm));
//~ }
//~ printf("\n");
for (auto i : pyt[tm]) {
int poc = fin(1, 1, com + 1, tm, i.l);
int kon = fin(1, 1, com + 1, tm, i.r);
auto p = aktualny(poc, tm);
auto k = aktualny(kon, tm);
//~ debug(i.l, i.r, i.id, poc, kon, p, k);
odp[i.id] = st.pytaj(poc, kon, tm) - (long long)(i.l - p.first) * t[poc] - (long long)(k.second - i.r) * t[kon];
}
}
for (int i = 1; i <= q; ++i) {
printf("%lld\n", odp[i]);
}
return 0;
}
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:126:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:135:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d", &t[i]);
| ~~~~~^~~~~~~~~~~~~
ho_t5.cpp:190:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
190 | scanf("%d%d%d", &c, &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
11 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14388 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14400 KB |
Output is correct |
15 |
Correct |
8 ms |
14388 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14444 KB |
Output is correct |
18 |
Correct |
8 ms |
14440 KB |
Output is correct |
19 |
Correct |
9 ms |
14512 KB |
Output is correct |
20 |
Correct |
8 ms |
14432 KB |
Output is correct |
21 |
Correct |
9 ms |
14548 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
9 ms |
14420 KB |
Output is correct |
24 |
Correct |
8 ms |
14420 KB |
Output is correct |
25 |
Correct |
8 ms |
14420 KB |
Output is correct |
26 |
Correct |
10 ms |
14496 KB |
Output is correct |
27 |
Correct |
8 ms |
14428 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
29 |
Correct |
8 ms |
14420 KB |
Output is correct |
30 |
Correct |
8 ms |
14420 KB |
Output is correct |
31 |
Correct |
8 ms |
14420 KB |
Output is correct |
32 |
Correct |
9 ms |
14484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
217 ms |
39840 KB |
Output is correct |
3 |
Correct |
206 ms |
45496 KB |
Output is correct |
4 |
Correct |
224 ms |
45612 KB |
Output is correct |
5 |
Correct |
214 ms |
46008 KB |
Output is correct |
6 |
Correct |
223 ms |
45312 KB |
Output is correct |
7 |
Correct |
240 ms |
45768 KB |
Output is correct |
8 |
Correct |
222 ms |
46088 KB |
Output is correct |
9 |
Correct |
219 ms |
45960 KB |
Output is correct |
10 |
Correct |
210 ms |
45544 KB |
Output is correct |
11 |
Correct |
241 ms |
45940 KB |
Output is correct |
12 |
Correct |
248 ms |
45396 KB |
Output is correct |
13 |
Correct |
236 ms |
45832 KB |
Output is correct |
14 |
Correct |
266 ms |
45800 KB |
Output is correct |
15 |
Correct |
232 ms |
45804 KB |
Output is correct |
16 |
Correct |
275 ms |
45348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
245 ms |
40440 KB |
Output is correct |
3 |
Correct |
276 ms |
45676 KB |
Output is correct |
4 |
Correct |
280 ms |
46372 KB |
Output is correct |
5 |
Correct |
248 ms |
45680 KB |
Output is correct |
6 |
Correct |
288 ms |
45784 KB |
Output is correct |
7 |
Correct |
251 ms |
45892 KB |
Output is correct |
8 |
Correct |
280 ms |
46024 KB |
Output is correct |
9 |
Correct |
251 ms |
45804 KB |
Output is correct |
10 |
Correct |
273 ms |
45432 KB |
Output is correct |
11 |
Correct |
256 ms |
46384 KB |
Output is correct |
12 |
Correct |
283 ms |
45988 KB |
Output is correct |
13 |
Correct |
282 ms |
46164 KB |
Output is correct |
14 |
Correct |
298 ms |
45936 KB |
Output is correct |
15 |
Correct |
278 ms |
46304 KB |
Output is correct |
16 |
Correct |
238 ms |
46008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
349 ms |
38404 KB |
Output is correct |
2 |
Correct |
356 ms |
38388 KB |
Output is correct |
3 |
Correct |
334 ms |
38592 KB |
Output is correct |
4 |
Correct |
336 ms |
38640 KB |
Output is correct |
5 |
Correct |
366 ms |
38592 KB |
Output is correct |
6 |
Correct |
406 ms |
38384 KB |
Output is correct |
7 |
Correct |
328 ms |
38616 KB |
Output is correct |
8 |
Correct |
324 ms |
38540 KB |
Output is correct |
9 |
Correct |
326 ms |
38520 KB |
Output is correct |
10 |
Correct |
324 ms |
38720 KB |
Output is correct |
11 |
Correct |
353 ms |
38848 KB |
Output is correct |
12 |
Correct |
323 ms |
38688 KB |
Output is correct |
13 |
Correct |
340 ms |
38640 KB |
Output is correct |
14 |
Correct |
376 ms |
38572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
11 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14388 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14400 KB |
Output is correct |
15 |
Correct |
8 ms |
14388 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14444 KB |
Output is correct |
18 |
Correct |
8 ms |
14440 KB |
Output is correct |
19 |
Correct |
9 ms |
14512 KB |
Output is correct |
20 |
Correct |
8 ms |
14432 KB |
Output is correct |
21 |
Correct |
9 ms |
14548 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
9 ms |
14420 KB |
Output is correct |
24 |
Correct |
8 ms |
14420 KB |
Output is correct |
25 |
Correct |
8 ms |
14420 KB |
Output is correct |
26 |
Correct |
10 ms |
14496 KB |
Output is correct |
27 |
Correct |
8 ms |
14428 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
29 |
Correct |
8 ms |
14420 KB |
Output is correct |
30 |
Correct |
8 ms |
14420 KB |
Output is correct |
31 |
Correct |
8 ms |
14420 KB |
Output is correct |
32 |
Correct |
9 ms |
14484 KB |
Output is correct |
33 |
Correct |
217 ms |
39840 KB |
Output is correct |
34 |
Correct |
206 ms |
45496 KB |
Output is correct |
35 |
Correct |
224 ms |
45612 KB |
Output is correct |
36 |
Correct |
214 ms |
46008 KB |
Output is correct |
37 |
Correct |
223 ms |
45312 KB |
Output is correct |
38 |
Correct |
240 ms |
45768 KB |
Output is correct |
39 |
Correct |
222 ms |
46088 KB |
Output is correct |
40 |
Correct |
219 ms |
45960 KB |
Output is correct |
41 |
Correct |
210 ms |
45544 KB |
Output is correct |
42 |
Correct |
241 ms |
45940 KB |
Output is correct |
43 |
Correct |
248 ms |
45396 KB |
Output is correct |
44 |
Correct |
236 ms |
45832 KB |
Output is correct |
45 |
Correct |
266 ms |
45800 KB |
Output is correct |
46 |
Correct |
232 ms |
45804 KB |
Output is correct |
47 |
Correct |
275 ms |
45348 KB |
Output is correct |
48 |
Correct |
245 ms |
40440 KB |
Output is correct |
49 |
Correct |
276 ms |
45676 KB |
Output is correct |
50 |
Correct |
280 ms |
46372 KB |
Output is correct |
51 |
Correct |
248 ms |
45680 KB |
Output is correct |
52 |
Correct |
288 ms |
45784 KB |
Output is correct |
53 |
Correct |
251 ms |
45892 KB |
Output is correct |
54 |
Correct |
280 ms |
46024 KB |
Output is correct |
55 |
Correct |
251 ms |
45804 KB |
Output is correct |
56 |
Correct |
273 ms |
45432 KB |
Output is correct |
57 |
Correct |
256 ms |
46384 KB |
Output is correct |
58 |
Correct |
283 ms |
45988 KB |
Output is correct |
59 |
Correct |
282 ms |
46164 KB |
Output is correct |
60 |
Correct |
298 ms |
45936 KB |
Output is correct |
61 |
Correct |
278 ms |
46304 KB |
Output is correct |
62 |
Correct |
238 ms |
46008 KB |
Output is correct |
63 |
Correct |
349 ms |
38404 KB |
Output is correct |
64 |
Correct |
356 ms |
38388 KB |
Output is correct |
65 |
Correct |
334 ms |
38592 KB |
Output is correct |
66 |
Correct |
336 ms |
38640 KB |
Output is correct |
67 |
Correct |
366 ms |
38592 KB |
Output is correct |
68 |
Correct |
406 ms |
38384 KB |
Output is correct |
69 |
Correct |
328 ms |
38616 KB |
Output is correct |
70 |
Correct |
324 ms |
38540 KB |
Output is correct |
71 |
Correct |
326 ms |
38520 KB |
Output is correct |
72 |
Correct |
324 ms |
38720 KB |
Output is correct |
73 |
Correct |
353 ms |
38848 KB |
Output is correct |
74 |
Correct |
323 ms |
38688 KB |
Output is correct |
75 |
Correct |
340 ms |
38640 KB |
Output is correct |
76 |
Correct |
376 ms |
38572 KB |
Output is correct |
77 |
Correct |
272 ms |
46868 KB |
Output is correct |
78 |
Correct |
262 ms |
47204 KB |
Output is correct |
79 |
Correct |
258 ms |
46892 KB |
Output is correct |
80 |
Correct |
253 ms |
46968 KB |
Output is correct |
81 |
Correct |
260 ms |
46868 KB |
Output is correct |
82 |
Correct |
246 ms |
46952 KB |
Output is correct |
83 |
Correct |
269 ms |
46748 KB |
Output is correct |
84 |
Correct |
253 ms |
46880 KB |
Output is correct |
85 |
Correct |
245 ms |
47060 KB |
Output is correct |
86 |
Correct |
248 ms |
46940 KB |
Output is correct |
87 |
Correct |
247 ms |
46880 KB |
Output is correct |
88 |
Correct |
230 ms |
46784 KB |
Output is correct |
89 |
Correct |
229 ms |
46436 KB |
Output is correct |
90 |
Correct |
229 ms |
46756 KB |
Output is correct |
91 |
Correct |
210 ms |
46192 KB |
Output is correct |
92 |
Correct |
223 ms |
46224 KB |
Output is correct |
93 |
Correct |
226 ms |
46400 KB |
Output is correct |
94 |
Correct |
224 ms |
47016 KB |
Output is correct |
95 |
Correct |
229 ms |
47216 KB |
Output is correct |
96 |
Correct |
224 ms |
46496 KB |
Output is correct |
97 |
Correct |
244 ms |
46632 KB |
Output is correct |
98 |
Correct |
253 ms |
45992 KB |
Output is correct |
99 |
Correct |
256 ms |
46008 KB |
Output is correct |
100 |
Correct |
260 ms |
46256 KB |
Output is correct |
101 |
Correct |
257 ms |
46052 KB |
Output is correct |
102 |
Correct |
258 ms |
46656 KB |
Output is correct |