이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 25;
int arr[MAXN], n;
vector <int> occ[MAXN];
vector <int> d;
struct bit {
int tree[2 * MAXN + 25] = {};
void add (int pos, int val) {
for (; pos < 2 * MAXN + 25; pos += pos & (-pos)) tree[pos] += val;
}
ll get (int pos) {
ll sum = 0; for (; pos; pos -= pos & (-pos)) sum += tree[pos];
return sum;
}
};
ll solve1 (int x) {
int a[n + 1] = {}; bit cur;
for (int i = 1; i <= n; i++) {
a[i] = -1;
}
for (auto i : occ[x]) a[i] = 1;
cur.add(MAXN, 1);
ll ret = 0;
for (int i = 1; i <= n; i++) {
a[i] += a[i - 1];
ret += cur.get(a[i] - 1 + MAXN);
cur.add(a[i] + MAXN, 1);
}
return ret;
}
ll solve2 (int x) {
return solve1(x);
}
int main () {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
d.push_back(arr[i]);
}
sort(d.begin(), d.end()); d.resize(unique(d.begin(), d.end()) - d.begin());
for (int i = 1; i <= n; i++) arr[i] = lower_bound(d.begin(), d.end(), arr[i]) - d.begin();
for (int i = 1; i <= n; i++) {
occ[arr[i]].push_back(i);
}
ll sum = 0;
int x = sqrt(n); while (x * x < n) x++; while (x * x > n) x--;
for (int i = 0; i < n; i++) {
if (occ[i].empty()) continue;
if (occ[i].size() > x) sum += solve1(i);
else sum += solve2(i);
}
cout << sum << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:51:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | if (occ[i].size() > x) sum += solve1(i);
| ~~~~~~~~~~~~~~^~~
# | 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... |