#include <bits/stdc++.h>
using namespace std;
long long N, o[100000], fw[100001], pA[400000];
pair<long long, long long> mA[400000];
vector<pair<long long, long long> > vA[400000];
stack<long long> s;
long long qf(long long i) {long long t = 0; while (i) t += fw[i], i -= i & -i; return t;}
void uf(long long i, long long v) {while (i < 100001) fw[i] += v, i += i & -i;}
long long qpl(long long i, long long p, long long v, long long l, long long h) {
if (l > p || pA[i] < v) return -1;
if (l >= h) return l;
long long c = qpl(i * 2 + 1, p, v, (l + h) / 2 + 1, h);
if (c > -1) return c;
return qpl(i * 2, p, v, l, (l + h) / 2);
}
long long qpr(long long i, long long p, long long v, long long l, long long h) {
if (h < p || pA[i] < v) return -1;
if (l >= h) return l;
long long c = qpr(i * 2, p, v, l, (l + h) / 2);
if (c > -1) return c;
return qpr(i * 2 + 1, p, v, (l + h) / 2 + 1, h);
}
void up(long long i, long long p, long long v, long long l, long long h) {
if (p < l || h < p) return;
if (p <= l && h <= p) {pA[i] = v; return;}
up(i * 2, p, v, l, (l + h) / 2);
up(i * 2 + 1, p, v, (l + h) / 2 + 1, h);
pA[i] = max(pA[i * 2], pA[i * 2 + 1]);
}
void inm(long long i, long long l, long long h) {
mA[i] = make_pair(0, h - l + 1);
if (l >= h) return;
inm(i * 2, l, (l + h) / 2);
inm(i * 2 + 1, (l + h) / 2 + 1, h);
}
pair<long long, long long> qm(long long i, long long a, long long b, long long l, long long h) {
if (b < l || h < a) return make_pair(LLONG_MAX, 0);
if (a <= l && h <= b) return mA[i];
pair<long long, long long> pa = qm(i * 2, a, b, l, (l + h) / 2), pb = qm(i * 2 + 1, a, b, (l + h) / 2 + 1, h);
if (pa > pb) swap(pa, pb);
if (pa.first == pb.first) pa.second += pb.second;
return pa;
}
void um(long long i, long long a, long long b, long long v, long long l, long long h) {
if (b < l || h < a) return;
if (a <= l && h <= b) {mA[i].first += v; return;}
um(i * 2, a, b, v, l, (l + h) / 2);
um(i * 2 + 1, a, b, v, (l + h) / 2 + 1, h);
mA[i] = min(mA[i * 2], mA[i * 2 + 1]);
if (mA[i * 2].first == mA[i * 2 + 1].first) mA[i].second = mA[i * 2].second + mA[i * 2 + 1].second;
}
void qv(long long i, long long p, long long l, long long h) {
if (p < l || h < p) return;
for (long long j = 0; j < vA[i].size(); ++j) if (vA[i][j].first <= p && p <= vA[i][j].second && min(o[vA[i][j].first], o[vA[i][j].second]) <= qf(vA[i][j].second) - qf(vA[i][j].first + 1)) {
um(1, vA[i][j].first + 1, vA[i][j].second - 1, -1, 0, N - 1);
swap(vA[i][j], vA[i].back());
vA[i].pop_back();
--j;
}
qv(i * 2, p, l, (l + h) / 2 - 1);
qv(i * 2 + 1, p, (l + h) / 2 + 1, h);
}
void uv(long long i, long long a, long long b, long long l, long long h) {
if (a + 1 == b) return;
if (b < l || h < a) return;
if (a <= (l + h) / 2 && (l + h) / 2 <= b) {
for (long long j = 0; j < vA[i].size(); ++j) if (a == vA[i][j].first && b == vA[i][j].second) return;
um(1, a + 1, b - 1, 1, 0, N - 1);
vA[i].emplace_back(a, b);
return;
}
uv(i * 2, a, b, l, (l + h) / 2 - 1);
uv(i * 2 + 1, a, b, (l + h) / 2 + 1, h);
}
int main() {
long long Q, t, a, b, l, h, i, j;
scanf("%lld", &N);
inm(1, 0, N - 1);
for (i = 0; i < N; ++i) scanf("%lld", &o[i]), uf(i + 1, o[i]), up(1, i, o[i], 0, N - 1);
for (i = 0; i < N; ++i) {
while (!s.empty() && o[s.top()] <= o[i]) {if (o[s.top()] > qf(i) - qf(s.top() + 1)) uv(1, s.top(), i, 0, N - 1); s.pop();}
if (!s.empty() && o[i] > qf(i) - qf(s.top() + 1)) uv(1, s.top(), i, 0, N - 1);
s.push(i);
}
scanf("%lld", &Q);
for (i = 0; i < Q; ++i) {
scanf("%lld %lld %lld", &t, &a, &b);
--a;
if (t == 1) {
uf(a + 1, b - o[a]);
up(1, a, b, 0, N - 1);
if (o[a] < b) {
o[a] = b;
qv(1, a, 0, N - 1);
for (l = a - 1; l > -1; l = qpl(1, l - 1, qf(a) - qf(l) + 1, 0, N - 1)) {
if (min(o[l], o[a]) > qf(a) - qf(l + 1)) uv(1, l, a, 0, N - 1);
if (o[l] >= o[a]) break;
}
for (h = a + 1; h > -1; h = qpr(1, h + 1, qf(h + 1) - qf(a + 1) + 1, 0, N - 1)) {
if (min(o[a], o[h]) > qf(h) - qf(a + 1)) uv(1, a, h, 0, N - 1);
if (o[a] <= o[h]) break;
}
} else {
o[a] = b;
qv(1, a, 0, N - 1);
l = qpl(1, a - 1, o[a] + 1, 0, N - 1);
h = qpr(1, a + 1, o[a] + 1, 0, N - 1);
while (l > -1 && h > -1) {
if (min(o[l], o[h]) > qf(h) - qf(l + 1)) uv(1, l, h, 0, N - 1);
if (o[l] < o[h]) l = qpl(1, l - 1, qf(h) - qf(l) + 1, 0, N - 1);
else h = qpr(1, h + 1, qf(h + 1) - qf(l + 1) + 1, 0, N - 1);
}
}
} else {
--b;
l = a;
h = b;
for (j = a; j > -1 && j <= b; j = qpr(1, j + 1, qf(j + 1) - qf(a) + 1, 0, N - 1)) if (o[j] > qf(j) - qf(a)) l = j;
for (j = b; j >= a; j = qpl(1, j - 1, qf(b + 1) - qf(j) + 1, 0, N - 1)) if (o[j] > qf(b + 1) - qf(j + 1)) h = j;
printf("%lld\n", qm(1, l, h, 0, N - 1).second);
}
}
return 0;
}
Compilation message
fish2.cpp: In function 'void qv(long long int, long long int, long long int, long long int)':
fish2.cpp:63:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (long long j = 0; j < vA[i].size(); ++j) if (vA[i][j].first <= p && p <= vA[i][j].second && min(o[vA[i][j].first], o[vA[i][j].second]) <= qf(vA[i][j].second) - qf(vA[i][j].first + 1)) {
| ~~^~~~~~~~~~~~~~
fish2.cpp: In function 'void uv(long long int, long long int, long long int, long long int, long long int)':
fish2.cpp:77:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (long long j = 0; j < vA[i].size(); ++j) if (a == vA[i][j].first && b == vA[i][j].second) return;
| ~~^~~~~~~~~~~~~~
fish2.cpp: In function 'int main()':
fish2.cpp:88:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%lld", &N);
| ~~~~~^~~~~~~~~~~~
fish2.cpp:90:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | for (i = 0; i < N; ++i) scanf("%lld", &o[i]), uf(i + 1, o[i]), up(1, i, o[i], 0, N - 1);
| ~~~~~^~~~~~~~~~~~~~~
fish2.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%lld", &Q);
| ~~~~~^~~~~~~~~~~~
fish2.cpp:98:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | scanf("%lld %lld %lld", &t, &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
8 ms |
9700 KB |
Output is correct |
4 |
Correct |
8 ms |
9664 KB |
Output is correct |
5 |
Incorrect |
8 ms |
9684 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9700 KB |
Output is correct |
2 |
Correct |
78 ms |
19916 KB |
Output is correct |
3 |
Correct |
92 ms |
19520 KB |
Output is correct |
4 |
Correct |
122 ms |
19948 KB |
Output is correct |
5 |
Correct |
90 ms |
19520 KB |
Output is correct |
6 |
Correct |
64 ms |
19436 KB |
Output is correct |
7 |
Correct |
66 ms |
18236 KB |
Output is correct |
8 |
Correct |
62 ms |
19540 KB |
Output is correct |
9 |
Correct |
65 ms |
18272 KB |
Output is correct |
10 |
Correct |
77 ms |
18984 KB |
Output is correct |
11 |
Correct |
82 ms |
18552 KB |
Output is correct |
12 |
Correct |
86 ms |
18624 KB |
Output is correct |
13 |
Correct |
69 ms |
18692 KB |
Output is correct |
14 |
Correct |
81 ms |
19712 KB |
Output is correct |
15 |
Correct |
81 ms |
19652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
8 ms |
9700 KB |
Output is correct |
4 |
Correct |
8 ms |
9664 KB |
Output is correct |
5 |
Incorrect |
8 ms |
9684 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9700 KB |
Output is correct |
2 |
Correct |
78 ms |
19916 KB |
Output is correct |
3 |
Correct |
92 ms |
19520 KB |
Output is correct |
4 |
Correct |
122 ms |
19948 KB |
Output is correct |
5 |
Correct |
90 ms |
19520 KB |
Output is correct |
6 |
Correct |
64 ms |
19436 KB |
Output is correct |
7 |
Correct |
66 ms |
18236 KB |
Output is correct |
8 |
Correct |
62 ms |
19540 KB |
Output is correct |
9 |
Correct |
65 ms |
18272 KB |
Output is correct |
10 |
Correct |
77 ms |
18984 KB |
Output is correct |
11 |
Correct |
82 ms |
18552 KB |
Output is correct |
12 |
Correct |
86 ms |
18624 KB |
Output is correct |
13 |
Correct |
69 ms |
18692 KB |
Output is correct |
14 |
Correct |
81 ms |
19712 KB |
Output is correct |
15 |
Correct |
81 ms |
19652 KB |
Output is correct |
16 |
Correct |
6 ms |
9704 KB |
Output is correct |
17 |
Incorrect |
565 ms |
21144 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9700 KB |
Output is correct |
2 |
Correct |
78 ms |
19916 KB |
Output is correct |
3 |
Correct |
92 ms |
19520 KB |
Output is correct |
4 |
Correct |
122 ms |
19948 KB |
Output is correct |
5 |
Correct |
90 ms |
19520 KB |
Output is correct |
6 |
Correct |
64 ms |
19436 KB |
Output is correct |
7 |
Correct |
66 ms |
18236 KB |
Output is correct |
8 |
Correct |
62 ms |
19540 KB |
Output is correct |
9 |
Correct |
65 ms |
18272 KB |
Output is correct |
10 |
Correct |
77 ms |
18984 KB |
Output is correct |
11 |
Correct |
82 ms |
18552 KB |
Output is correct |
12 |
Correct |
86 ms |
18624 KB |
Output is correct |
13 |
Correct |
69 ms |
18692 KB |
Output is correct |
14 |
Correct |
81 ms |
19712 KB |
Output is correct |
15 |
Correct |
81 ms |
19652 KB |
Output is correct |
16 |
Correct |
6 ms |
9704 KB |
Output is correct |
17 |
Incorrect |
595 ms |
21020 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
8 ms |
9700 KB |
Output is correct |
4 |
Correct |
8 ms |
9664 KB |
Output is correct |
5 |
Incorrect |
8 ms |
9684 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |