#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define int long long
struct segmentTree {
int n;
vector<int> st;
segmentTree(int n) {
int x = (int)(ceil(log2(n)));
int max_size = 2 * (int)pow(2, x) - 1;
this->n = n;
st.resize(max_size, 0);
}
void build(int startRange, int endRange, int currNode, vector<int> const& v) {
if (startRange == endRange) {
st[currNode] = v[startRange];
return;
}
int mid = (startRange + endRange) / 2;
build(startRange, mid, currNode * 2 + 1, v);
build(mid + 1, endRange, currNode * 2 + 2, v);
st[currNode] = st[currNode * 2 + 1] + st[currNode * 2 + 2];
}
void build(vector<int>const& v) {
build(0, n - 1, 0, v);
}
int query(int startRange, int endRange, int l, int r, int currNode) {
if (endRange < l || startRange > r) return 0;
if (startRange >= l && endRange <= r) return st[currNode];
int mid = (startRange + endRange) / 2;
return (query(startRange, mid, l, r, currNode * 2 + 1) + query(mid + 1, endRange, l, r, currNode * 2 + 2));
}
int query(int l, int r) {
return query(0, n - 1, l, r, 0);
}
void update(int startRange, int endRange, int currNode, int index, int val) {
if (startRange == endRange) {
st[currNode] = val;
return;
}
int mid = (startRange + endRange) / 2;
if (index <= mid) {
update(startRange, mid, currNode * 2 + 1, index, val);
}
else {
update(mid + 1, endRange, currNode * 2 + 2, index, val);
}
st[currNode] = st[currNode * 2 + 1] + st[currNode * 2 + 2];
}
void update(int index, int val) {
update(0, n - 1, 0, index, val);
}
};
int32_t main() {
int n;
cin >> n;
vector<pair<int,int>> v(n);
vector<int> elements(n,0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v[i] = { x,i };
elements.push_back(x);
}
sort(v.begin(), v.end());
segmentTree tr(n);
tr.build(elements);
vector<int> index;
int cnt = 0;
tr.update(v[0].second, 1);
for (int i = 1; i < n; i++) {
index.push_back(v[i].second);
if(v[i].first != v[i - 1].first) {
for(int x : index){
tr.update(x, 1);
}
index.clear();
}
cnt += tr.query(0, v[i].second - 1) * tr.query(v[i].second + 1, n - 1);
}
cout << cnt << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
280 ms |
18428 KB |
Output is correct |
3 |
Correct |
296 ms |
23432 KB |
Output is correct |
4 |
Correct |
279 ms |
23356 KB |
Output is correct |
5 |
Correct |
286 ms |
23432 KB |
Output is correct |
6 |
Correct |
288 ms |
23424 KB |
Output is correct |
7 |
Correct |
287 ms |
23432 KB |
Output is correct |
8 |
Correct |
298 ms |
23368 KB |
Output is correct |
9 |
Correct |
289 ms |
23468 KB |
Output is correct |
10 |
Correct |
300 ms |
23468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
148 ms |
21296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
148 ms |
21296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
148 ms |
21296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
280 ms |
18428 KB |
Output is correct |
3 |
Correct |
296 ms |
23432 KB |
Output is correct |
4 |
Correct |
279 ms |
23356 KB |
Output is correct |
5 |
Correct |
286 ms |
23432 KB |
Output is correct |
6 |
Correct |
288 ms |
23424 KB |
Output is correct |
7 |
Correct |
287 ms |
23432 KB |
Output is correct |
8 |
Correct |
298 ms |
23368 KB |
Output is correct |
9 |
Correct |
289 ms |
23468 KB |
Output is correct |
10 |
Correct |
300 ms |
23468 KB |
Output is correct |
11 |
Incorrect |
148 ms |
21296 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |