// 花啊啊啊啊啊啊啊啊啊啊啊啊
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 1e5 + 7;
const ll oo = 1e18;
ll a[N];
int n;
namespace cover {
int t[N * 4];
char tag[N * 4];
int sum(int v, int len) {
return tag[v] ? len : t[v];
}
void _add(int l, int r, int x, int v = 1, int tl = 1, int tr = n + 1) {
if(l <= tl && tr <= r) {
tag[v] += x;
return;
}
int tm = (tl + tr) / 2;
if(l < tm) _add(l, r, x, v * 2, tl, tm);
if(r > tm) _add(l, r, x, v * 2 + 1, tm, tr);
t[v] = sum(v * 2, tm - tl) + sum(v * 2 + 1, tr - tm);
}
void add(int l, int r, int x) {
if(l == 1 && r == n + 1) return;
_add(l, r, x);
}
int qry(int l, int r, int v = 1, int tl = 1, int tr = n + 1) {
if(l <= tl && tr <= r)
return sum(v, tr - tl);
int tm = (tl + tr) / 2, res = 0;
if(l < tm) res += qry(l, r, v * 2, tl, tm);
if(r > tm) res += qry(l, r, v * 2 + 1, tm, tr);
return res;
}
}
namespace seg {
template<class A, class B> struct DS {
pair<A, B> a[31];
int sz;
DS() { sz = 0; }
void clear() { sz = 0; }
void EB(A x, B y) { a[sz++] = {x, y}; }
void PB(pair<A, B> p) { a[sz++] = p; }
pair<A, B>& operator[](int i) { return a[i]; }
void resize(int s) { sz = s; }
};
struct node {
DS<int, ll> bad_pref, bad_suff;
DS<int, int> bad_seg;
ll sum;
} t[N * 2];
int id(int tl, int tr) {
return (tl + tr - 1) | (tl + 1 != tr);
}
void pull(int v, int lc, int rc, int posl = INF, int posr = 0) {
for(int i = 0; i < t[lc].bad_pref.sz; i++)
t[v].bad_pref.PB(t[lc].bad_pref[i]);
for(int i = 0; i < t[rc].bad_suff.sz; i++)
t[v].bad_suff.PB(t[rc].bad_suff[i]);
for(int i = 0; i < t[rc].bad_pref.sz; i++) {
// auto[pos1, sum1] = t[rc].bad_pref[i];
#define pos1 t[rc].bad_pref[i].F
#define sum1 t[rc].bad_pref[i].S
if(sum1 + t[lc].sum < a[pos1 + 1])
t[v].bad_pref.EB(pos1, sum1 + t[lc].sum);
#undef pos1
#undef sum1
}
for(int i = 0; i < t[lc].bad_suff.sz; i++) {
// auto[pos1, sum1] = t[lc].bad_suff[i];
#define pos1 t[lc].bad_suff[i].F
#define sum1 t[lc].bad_suff[i].S
if(sum1 + t[rc].sum < a[pos1 - 1])
t[v].bad_suff.EB(pos1, sum1 + t[rc].sum);
#undef pos1
#undef sum1
}
int szi = t[lc].bad_suff.sz;
int szj = t[rc].bad_pref.sz;
int i = 0, j = 0;
while(i < szi && t[lc].bad_suff[i].F > posl)
i++;
while(j < szj && t[rc].bad_pref[j].F < posr)
j++;
while(i < szi && j < szj) {
auto& he = t[lc].bad_suff[i], be = t[rc].bad_pref[j];
if(he.S + be.S < min(a[he.F - 1], a[be.F + 1])) {
t[v].bad_seg.EB(he.F, be.F);
cover::add(he.F, be.F + 1, 1);
}
if(a[he.F - 1] < a[be.F + 1])
i++;
else
j++;
}
t[v].sum = t[lc].sum + t[rc].sum;
}
void build( int tl = 1, int tr = n + 1) {
int v = id(tl, tr);
if(tr - tl == 1) {
t[v].sum = a[tl];
if(a[tl] < a[tl + 1])
t[v].bad_pref.EB(tl, a[tl]);
if(a[tl] < a[tl - 1])
t[v].bad_suff.EB(tl, a[tl]);
if(a[tl] < a[tl + 1] && a[tl] < a[tl - 1]) {
t[v].bad_seg.EB(tl, tl);
cover::add(tl, tl + 1, 1);
}
} else {
int tm = (tl + tr + 1) / 2;
build(tl, tm);
build(tm, tr);
pull(v, id(tl, tm), id(tm, tr));
}
}
void upd(int pos, int tl = 1, int tr = n + 1) {
int v = id(tl, tr);
int j = 0;
for(int i = 0; i < t[v].bad_seg.sz; i++) {
auto [l, r] = t[v].bad_seg[i];
if(r < pos - 1 || l > pos + 1) {
t[v].bad_seg[j++] = t[v].bad_seg[i];
} else {
cover::add(l, r + 1, -1);
}
}
t[v].bad_seg.resize(j);
t[v].bad_pref.clear(), t[v].bad_suff.clear();
if(tr - tl == 1) {
t[v].sum = a[tl];
if(a[tl] < a[tl + 1])
t[v].bad_pref.EB(tl, a[tl]);
if(a[tl] < a[tl - 1])
t[v].bad_suff.EB(tl, a[tl]);
if(a[tl] < a[tl + 1] && a[tl] < a[tl - 1]) {
t[v].bad_seg.EB(tl, tl);
cover::add(tl, tl + 1, 1);
}
} else {
int tm = (tl + tr + 1) / 2;
if(pos - 1 < tm)
upd(pos, tl, tm);
if(pos + 1 >= tm)
upd(pos, tm, tr);
pull(v, id(tl, tm), id(tm, tr), pos + 1, pos - 1);
}
}
}
void upd(int pos, ll val) {
a[pos] = val;
seg::upd(pos);
}
#define gc getchar_unlocked
// #define gc getchar
inline int readInt () {
int c = gc();
while(!isdigit(c)) c = gc();
int res = c - '0';
while(true) {
c = gc();
if(isdigit(c)) res = res * 10 + c - '0';
else break;
}
return res;
}
signed main()
{
IO_OP;
// cin >> n;
n = readInt();
a[0] = a[n + 1] = oo;
for(int i = 1; i <= n; i++)
// cin >> a[i];
a[i] = readInt();
seg::build();
int q;
// cin >> q;
q = readInt();
while(q--) {
int op;
// cin >> op;
op = readInt();
if(op == 1) {
int x, y;
// cin >> x >> y;
x = readInt(), y = readInt();
upd(x, y);
} else {
int l, r;
// cin >> l >> r;
l = readInt(), r = readInt();
ll tl = a[l - 1], tr = a[r + 1];
if(l - 1 >= 1) upd(l - 1, 1e15);
if(r + 1 <= n) upd(r + 1, 1e15);
if(l != 1 || r != n)
cover::add(l, r + 1, -1);
cout << r - l + 1 - cover::qry(l, r + 1) << '\n';
if(l != 1 || r != n)
cover::add(l, r + 1, 1);
if(l - 1 >= 1) upd(l - 1, tl);
if(r + 1 <= n) upd(r + 1, tr);
}
}
}
Compilation message
fish2.cpp: In function 'void seg::upd(int, int, int)':
fish2.cpp:146:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
146 | auto [l, r] = t[v].bad_seg[i];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
249172 KB |
Output is correct |
2 |
Correct |
114 ms |
249252 KB |
Output is correct |
3 |
Correct |
110 ms |
249216 KB |
Output is correct |
4 |
Correct |
107 ms |
249236 KB |
Output is correct |
5 |
Correct |
109 ms |
249212 KB |
Output is correct |
6 |
Correct |
110 ms |
249252 KB |
Output is correct |
7 |
Correct |
110 ms |
249252 KB |
Output is correct |
8 |
Correct |
110 ms |
249252 KB |
Output is correct |
9 |
Correct |
115 ms |
249324 KB |
Output is correct |
10 |
Correct |
114 ms |
249236 KB |
Output is correct |
11 |
Correct |
107 ms |
249276 KB |
Output is correct |
12 |
Correct |
107 ms |
249252 KB |
Output is correct |
13 |
Correct |
108 ms |
249164 KB |
Output is correct |
14 |
Correct |
106 ms |
249276 KB |
Output is correct |
15 |
Correct |
107 ms |
249376 KB |
Output is correct |
16 |
Correct |
108 ms |
249228 KB |
Output is correct |
17 |
Correct |
116 ms |
249204 KB |
Output is correct |
18 |
Correct |
119 ms |
249172 KB |
Output is correct |
19 |
Correct |
117 ms |
249372 KB |
Output is correct |
20 |
Correct |
128 ms |
249240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
249244 KB |
Output is correct |
2 |
Correct |
164 ms |
251356 KB |
Output is correct |
3 |
Correct |
143 ms |
251136 KB |
Output is correct |
4 |
Correct |
144 ms |
251304 KB |
Output is correct |
5 |
Correct |
149 ms |
251316 KB |
Output is correct |
6 |
Correct |
134 ms |
251644 KB |
Output is correct |
7 |
Correct |
138 ms |
251052 KB |
Output is correct |
8 |
Correct |
135 ms |
251700 KB |
Output is correct |
9 |
Correct |
137 ms |
250912 KB |
Output is correct |
10 |
Correct |
138 ms |
251312 KB |
Output is correct |
11 |
Correct |
142 ms |
251092 KB |
Output is correct |
12 |
Correct |
142 ms |
250948 KB |
Output is correct |
13 |
Correct |
145 ms |
250968 KB |
Output is correct |
14 |
Correct |
140 ms |
251348 KB |
Output is correct |
15 |
Correct |
157 ms |
251312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
249172 KB |
Output is correct |
2 |
Correct |
114 ms |
249252 KB |
Output is correct |
3 |
Correct |
110 ms |
249216 KB |
Output is correct |
4 |
Correct |
107 ms |
249236 KB |
Output is correct |
5 |
Correct |
109 ms |
249212 KB |
Output is correct |
6 |
Correct |
110 ms |
249252 KB |
Output is correct |
7 |
Correct |
110 ms |
249252 KB |
Output is correct |
8 |
Correct |
110 ms |
249252 KB |
Output is correct |
9 |
Correct |
115 ms |
249324 KB |
Output is correct |
10 |
Correct |
114 ms |
249236 KB |
Output is correct |
11 |
Correct |
107 ms |
249276 KB |
Output is correct |
12 |
Correct |
107 ms |
249252 KB |
Output is correct |
13 |
Correct |
108 ms |
249164 KB |
Output is correct |
14 |
Correct |
106 ms |
249276 KB |
Output is correct |
15 |
Correct |
107 ms |
249376 KB |
Output is correct |
16 |
Correct |
108 ms |
249228 KB |
Output is correct |
17 |
Correct |
116 ms |
249204 KB |
Output is correct |
18 |
Correct |
119 ms |
249172 KB |
Output is correct |
19 |
Correct |
117 ms |
249372 KB |
Output is correct |
20 |
Correct |
128 ms |
249240 KB |
Output is correct |
21 |
Correct |
109 ms |
249244 KB |
Output is correct |
22 |
Correct |
164 ms |
251356 KB |
Output is correct |
23 |
Correct |
143 ms |
251136 KB |
Output is correct |
24 |
Correct |
144 ms |
251304 KB |
Output is correct |
25 |
Correct |
149 ms |
251316 KB |
Output is correct |
26 |
Correct |
134 ms |
251644 KB |
Output is correct |
27 |
Correct |
138 ms |
251052 KB |
Output is correct |
28 |
Correct |
135 ms |
251700 KB |
Output is correct |
29 |
Correct |
137 ms |
250912 KB |
Output is correct |
30 |
Correct |
138 ms |
251312 KB |
Output is correct |
31 |
Correct |
142 ms |
251092 KB |
Output is correct |
32 |
Correct |
142 ms |
250948 KB |
Output is correct |
33 |
Correct |
145 ms |
250968 KB |
Output is correct |
34 |
Correct |
140 ms |
251348 KB |
Output is correct |
35 |
Correct |
157 ms |
251312 KB |
Output is correct |
36 |
Correct |
174 ms |
251352 KB |
Output is correct |
37 |
Correct |
174 ms |
251160 KB |
Output is correct |
38 |
Correct |
182 ms |
251132 KB |
Output is correct |
39 |
Correct |
166 ms |
251344 KB |
Output is correct |
40 |
Correct |
197 ms |
251128 KB |
Output is correct |
41 |
Correct |
158 ms |
251836 KB |
Output is correct |
42 |
Correct |
154 ms |
251724 KB |
Output is correct |
43 |
Correct |
170 ms |
250932 KB |
Output is correct |
44 |
Correct |
189 ms |
251024 KB |
Output is correct |
45 |
Correct |
192 ms |
251380 KB |
Output is correct |
46 |
Correct |
184 ms |
251320 KB |
Output is correct |
47 |
Correct |
159 ms |
251040 KB |
Output is correct |
48 |
Correct |
142 ms |
250996 KB |
Output is correct |
49 |
Correct |
156 ms |
251024 KB |
Output is correct |
50 |
Correct |
152 ms |
251336 KB |
Output is correct |
51 |
Correct |
162 ms |
251416 KB |
Output is correct |
52 |
Correct |
189 ms |
251400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
249244 KB |
Output is correct |
2 |
Correct |
164 ms |
251356 KB |
Output is correct |
3 |
Correct |
143 ms |
251136 KB |
Output is correct |
4 |
Correct |
144 ms |
251304 KB |
Output is correct |
5 |
Correct |
149 ms |
251316 KB |
Output is correct |
6 |
Correct |
134 ms |
251644 KB |
Output is correct |
7 |
Correct |
138 ms |
251052 KB |
Output is correct |
8 |
Correct |
135 ms |
251700 KB |
Output is correct |
9 |
Correct |
137 ms |
250912 KB |
Output is correct |
10 |
Correct |
138 ms |
251312 KB |
Output is correct |
11 |
Correct |
142 ms |
251092 KB |
Output is correct |
12 |
Correct |
142 ms |
250948 KB |
Output is correct |
13 |
Correct |
145 ms |
250968 KB |
Output is correct |
14 |
Correct |
140 ms |
251348 KB |
Output is correct |
15 |
Correct |
157 ms |
251312 KB |
Output is correct |
16 |
Correct |
114 ms |
249236 KB |
Output is correct |
17 |
Execution timed out |
4057 ms |
252744 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
249244 KB |
Output is correct |
2 |
Correct |
164 ms |
251356 KB |
Output is correct |
3 |
Correct |
143 ms |
251136 KB |
Output is correct |
4 |
Correct |
144 ms |
251304 KB |
Output is correct |
5 |
Correct |
149 ms |
251316 KB |
Output is correct |
6 |
Correct |
134 ms |
251644 KB |
Output is correct |
7 |
Correct |
138 ms |
251052 KB |
Output is correct |
8 |
Correct |
135 ms |
251700 KB |
Output is correct |
9 |
Correct |
137 ms |
250912 KB |
Output is correct |
10 |
Correct |
138 ms |
251312 KB |
Output is correct |
11 |
Correct |
142 ms |
251092 KB |
Output is correct |
12 |
Correct |
142 ms |
250948 KB |
Output is correct |
13 |
Correct |
145 ms |
250968 KB |
Output is correct |
14 |
Correct |
140 ms |
251348 KB |
Output is correct |
15 |
Correct |
157 ms |
251312 KB |
Output is correct |
16 |
Correct |
121 ms |
249212 KB |
Output is correct |
17 |
Correct |
918 ms |
252448 KB |
Output is correct |
18 |
Correct |
714 ms |
252840 KB |
Output is correct |
19 |
Correct |
707 ms |
252248 KB |
Output is correct |
20 |
Correct |
554 ms |
252852 KB |
Output is correct |
21 |
Correct |
828 ms |
252376 KB |
Output is correct |
22 |
Correct |
685 ms |
252720 KB |
Output is correct |
23 |
Correct |
692 ms |
252236 KB |
Output is correct |
24 |
Correct |
775 ms |
252780 KB |
Output is correct |
25 |
Correct |
710 ms |
252244 KB |
Output is correct |
26 |
Correct |
367 ms |
253424 KB |
Output is correct |
27 |
Correct |
429 ms |
253360 KB |
Output is correct |
28 |
Correct |
446 ms |
252624 KB |
Output is correct |
29 |
Correct |
382 ms |
253388 KB |
Output is correct |
30 |
Correct |
394 ms |
253108 KB |
Output is correct |
31 |
Correct |
562 ms |
252720 KB |
Output is correct |
32 |
Correct |
717 ms |
252720 KB |
Output is correct |
33 |
Correct |
420 ms |
252396 KB |
Output is correct |
34 |
Correct |
600 ms |
252936 KB |
Output is correct |
35 |
Correct |
366 ms |
252732 KB |
Output is correct |
36 |
Correct |
542 ms |
252864 KB |
Output is correct |
37 |
Correct |
541 ms |
252388 KB |
Output is correct |
38 |
Correct |
350 ms |
252652 KB |
Output is correct |
39 |
Correct |
443 ms |
253064 KB |
Output is correct |
40 |
Correct |
251 ms |
253088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
249172 KB |
Output is correct |
2 |
Correct |
114 ms |
249252 KB |
Output is correct |
3 |
Correct |
110 ms |
249216 KB |
Output is correct |
4 |
Correct |
107 ms |
249236 KB |
Output is correct |
5 |
Correct |
109 ms |
249212 KB |
Output is correct |
6 |
Correct |
110 ms |
249252 KB |
Output is correct |
7 |
Correct |
110 ms |
249252 KB |
Output is correct |
8 |
Correct |
110 ms |
249252 KB |
Output is correct |
9 |
Correct |
115 ms |
249324 KB |
Output is correct |
10 |
Correct |
114 ms |
249236 KB |
Output is correct |
11 |
Correct |
107 ms |
249276 KB |
Output is correct |
12 |
Correct |
107 ms |
249252 KB |
Output is correct |
13 |
Correct |
108 ms |
249164 KB |
Output is correct |
14 |
Correct |
106 ms |
249276 KB |
Output is correct |
15 |
Correct |
107 ms |
249376 KB |
Output is correct |
16 |
Correct |
108 ms |
249228 KB |
Output is correct |
17 |
Correct |
116 ms |
249204 KB |
Output is correct |
18 |
Correct |
119 ms |
249172 KB |
Output is correct |
19 |
Correct |
117 ms |
249372 KB |
Output is correct |
20 |
Correct |
128 ms |
249240 KB |
Output is correct |
21 |
Correct |
109 ms |
249244 KB |
Output is correct |
22 |
Correct |
164 ms |
251356 KB |
Output is correct |
23 |
Correct |
143 ms |
251136 KB |
Output is correct |
24 |
Correct |
144 ms |
251304 KB |
Output is correct |
25 |
Correct |
149 ms |
251316 KB |
Output is correct |
26 |
Correct |
134 ms |
251644 KB |
Output is correct |
27 |
Correct |
138 ms |
251052 KB |
Output is correct |
28 |
Correct |
135 ms |
251700 KB |
Output is correct |
29 |
Correct |
137 ms |
250912 KB |
Output is correct |
30 |
Correct |
138 ms |
251312 KB |
Output is correct |
31 |
Correct |
142 ms |
251092 KB |
Output is correct |
32 |
Correct |
142 ms |
250948 KB |
Output is correct |
33 |
Correct |
145 ms |
250968 KB |
Output is correct |
34 |
Correct |
140 ms |
251348 KB |
Output is correct |
35 |
Correct |
157 ms |
251312 KB |
Output is correct |
36 |
Correct |
174 ms |
251352 KB |
Output is correct |
37 |
Correct |
174 ms |
251160 KB |
Output is correct |
38 |
Correct |
182 ms |
251132 KB |
Output is correct |
39 |
Correct |
166 ms |
251344 KB |
Output is correct |
40 |
Correct |
197 ms |
251128 KB |
Output is correct |
41 |
Correct |
158 ms |
251836 KB |
Output is correct |
42 |
Correct |
154 ms |
251724 KB |
Output is correct |
43 |
Correct |
170 ms |
250932 KB |
Output is correct |
44 |
Correct |
189 ms |
251024 KB |
Output is correct |
45 |
Correct |
192 ms |
251380 KB |
Output is correct |
46 |
Correct |
184 ms |
251320 KB |
Output is correct |
47 |
Correct |
159 ms |
251040 KB |
Output is correct |
48 |
Correct |
142 ms |
250996 KB |
Output is correct |
49 |
Correct |
156 ms |
251024 KB |
Output is correct |
50 |
Correct |
152 ms |
251336 KB |
Output is correct |
51 |
Correct |
162 ms |
251416 KB |
Output is correct |
52 |
Correct |
189 ms |
251400 KB |
Output is correct |
53 |
Correct |
114 ms |
249236 KB |
Output is correct |
54 |
Execution timed out |
4057 ms |
252744 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |