이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
class FenwickTree {
public:
std::vector<int> f;
int n;
FenwickTree(int n) {
this->n = n;
f.resize(n + 1);
}
void add(int idx, int del) {
++idx;
for (int i = idx; i <= n; i += i & (-i)) {
f[i] += del;
}
}
int query(int idx) {
++idx;
int ans = 0;
for (int i = idx; i >= 1; i -= i & (-i)) {
ans += f[i];
}
return ans;
}
int query(int l, int r) {
if (l > r) {
return 0;
}
int ans = query(r);
if (l - 1 >= 0) {
ans -= query(l - 1);
}
return ans;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<std::pair<long long, int>> h(n);
FenwickTree tree(n);
for (int i = 0; i < n; ++i) {
std::cin >> h[i].first;
h[i].second = i;
tree.add(i, 1);
}
std::sort(h.begin(), h.end());
std::stack<int> stk;
long long ans = 0;
for (int i = 0; i < n; ++i) {
if (i != 0 and h[i].first != h[i - 1].first) {
while (!stk.empty()) {
tree.add(stk.top(), -1);
stk.pop();
}
}
long long left = tree.query(0, h[i].second);
long long right = tree.query(h[i].second, n - 1);
// above is >=, so make it <
ans += (h[i].second - left + 1) * (n - h[i].second - right);
stk.push(h[i].second);
}
std::cout << ans << "\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |