# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
883234 | borisAngelov | Izbori (COCI22_izbori) | C++17 | 3048 ms | 12720 KiB |
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;
const int maxn = 200005;
int n;
int a[maxn];
vector<int> byPosition[maxn];
void read()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
}
void compress()
{
vector<pair<int, int>> v;
for (int i = 1; i <= n; ++i)
{
v.push_back(make_pair(a[i], i));
}
sort(v.begin(), v.end());
int maxNumber = 0;
for (int i = 0; i < v.size(); ++i)
{
if (i == 0 || v[i].first != v[i - 1].first)
{
++maxNumber;
}
a[v[i].second] = maxNumber;
}
}
int b[maxn];
int pref[maxn];
struct FenwickTree
{
int tree[2 * maxn];
void reset()
{
for (int i = 1; i <= 2 * n; ++i)
{
tree[i] = 0;
}
}
void update(int pos, int val)
{
for (int i = pos; i <= 2 * n; i += (i & (-i)))
{
tree[i] += val;
}
}
int query(int pos)
{
int result = 0;
for (int i = pos; i >= 1; i -= (i & (-i)))
{
result += tree[i];
}
return result;
}
};
FenwickTree tree;
void solve()
{
compress();
for (int i = 1; i <= n; ++i)
{
byPosition[a[i]].push_back(i);
}
long long ans = 0;
for (int i = 1; i <= n; ++i)
{
if (byPosition[i].empty() == true)
{
continue;
}
for (int j = 1; j <= n; ++j)
{
b[j] = -1;
}
for (int j = 0; j < byPosition[i].size(); ++j)
{
b[byPosition[i][j]] = +1;
}
tree.reset();
tree.update(n, +1);
for (int j = 1; j <= n; ++j)
{
pref[j] = pref[j - 1] + b[j];
tree.update(pref[j] + n, +1);
ans += tree.query(pref[j] + n - 1);
}
}
cout << ans << endl;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
read();
solve();
return 0;
}
Compilation message (stderr)
# | 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... |