This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
struct fenwick {
int size;
vector<int> tree;
fenwick(int n) : size(n + 1), tree(n + 1) {}
void modify(int i, int value) {
for (i++; i < size; i += i & -i) {
tree[i] += value;
}
}
void update(int l, int r, int value) {
modify(l, value);
modify(r, -value);
}
int get(int r) {
int answer = 0;
for (++r; r > 0; r -= r & -r) {
answer += tree[r];
}
return answer;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> h(N);
for (int &value : h) {
cin >> value;
}
vector<int> type(M), pos(M), val(M), H(M);
for (int i = 0; i < M; i++) {
cin >> type[i];
if (type[i] == 1) {
cin >> pos[i] >> val[i];
} else {
cin >> H[i];
}
}
vector<int> diff;
for (int value : h) {
diff.push_back(value);
}
for (int value : H) {
diff.push_back(value);
}
for (int value : val) {
diff.push_back(value);
}
sort(diff.begin(), diff.end());
diff.resize(unique(diff.begin(), diff.end()) - diff.begin());
for (int &value : h) {
value = lower_bound(diff.begin(), diff.end(), value) - diff.begin();
}
for (int &value : H) {
value = lower_bound(diff.begin(), diff.end(), value) - diff.begin();
}
for (int &value : val) {
value = lower_bound(diff.begin(), diff.end(), value) - diff.begin();
}
fenwick bit(diff.size());
auto Add = [&](int i, int sign) {
if (i >= 0 && i < N - 1) {
if (h[i] < h[i + 1]) {
bit.update(h[i], h[i + 1], sign);
} else {
bit.update(h[i + 1], h[i], sign);
}
}
};
for (int i = 0; i < N - 1; i++) {
Add(i, 1);
}
for (int i = 0; i < M; i++) {
if (type[i] == 1) {
pos[i]--;
Add(pos[i] - 1, -1);
Add(pos[i], -1);
h[pos[i]] = val[i];
Add(pos[i] - 1, 1);
Add(pos[i], 1);
} else {
cout << bit.get(H[i]) << "\n";
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |