#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
#define debug(...) void(23)
#endif
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, Q;
std::cin >> N >> Q;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
std::cin >> A[i];
}
std::vector<int> vals(A.begin(), A.end());
std::vector<std::array<int, 3>> qq(Q);
for (int i = 0; i < Q; ++i) {
std::cin >> qq[i][0];
if (qq[i][0] == 1) {
std::cin >> qq[i][1] >> qq[i][2];
--qq[i][1];
vals.emplace_back(qq[i][2]);
} else {
std::cin >> qq[i][1];
vals.emplace_back(qq[i][1]);
}
}
vals.emplace_back(-1);
int nvals;
std::sort(vals.begin(), vals.end());
vals.resize(nvals = int(std::unique(vals.begin(), vals.end()) - vals.begin()));
for (int i = 0; i < N; ++i) {
A[i] = std::lower_bound(vals.begin(), vals.end(), A[i]) - vals.begin();
}
for (int i = 0; i < Q; ++i) {
if (qq[i][0] == 1) {
qq[i][2] = std::lower_bound(vals.begin(), vals.end(), qq[i][2]) - vals.begin();
} else {
qq[i][1] = std::lower_bound(vals.begin(), vals.end(), qq[i][1]) - vals.begin();
}
}
debug("wtf");
constexpr int B = 1000;
std::vector<int> suf(B + 1), save_ans(B + 1);
auto construct = [&](int l, int r) -> void {
vals.clear();
for (int i = l; i < r; ++i) {
if (qq[i][0] == 2) {
vals.emplace_back(qq[i][1]);
}
}
std::sort(vals.begin(), vals.end());
vals.resize(std::unique(vals.begin(), vals.end()) - vals.begin());
std::vector<std::pair<int, int>> piivec;
for (int i = 0; i + 1 < N; ++i) {
piivec.emplace_back(std::min(A[i], A[i + 1]), std::max(A[i], A[i + 1]));
}
std::sort(piivec.begin(), piivec.end());
int pivot = 0;
for (int i = 0; i < vals.size(); ++i) {
while (pivot < piivec.size() && piivec[pivot].first <= vals[i]) {
int idxvals = std::upper_bound(vals.begin(), vals.end(), piivec[pivot].second) - vals.begin() - 1;
for (int j = idxvals; j >= 0; --j) {
suf[j]++;
}
pivot++;
}
save_ans[i] = suf[i];
}
};
std::vector<std::tuple<int, int, int>> changed;
for (int l = 0; l < Q; l += B) {
int r = std::min(Q, l + B);
changed.clear();
changed.reserve(4 * B);
debug(l, r);
construct(l, r);
debug("???");
for (int i = l; i < r; ++i) {
if (qq[i][0] == 1) {
for (int idx : {qq[i][1] - 1, qq[i][1]}) {
if (0 <= idx && idx + 1 < N) {
changed.emplace_back(std::min(A[idx], A[idx + 1]), std::max(A[idx], A[idx + 1]), -1);
}
}
A[qq[i][1]] = qq[i][2];
for (int idx : {qq[i][1] - 1, qq[i][1]}) {
if (0 <= idx && idx + 1 < N) {
changed.emplace_back(std::min(A[idx], A[idx + 1]), std::max(A[idx], A[idx + 1]), +1);
}
}
} else {
int idxvals = std::lower_bound(vals.begin(), vals.end(), qq[i][1]) - vals.begin();
int ans = save_ans[idxvals];
for (auto[x, y, d] : changed) {
if (x <= qq[i][1] && qq[i][1] <= y) {
ans += d;
}
}
std::cout << ans << '\n';
}
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |