#include<bits/stdc++.h>
#define eb emplace_back
using namespace std;
const int N = 2e5+5;
int frek[30]={0};
vector<char>kmpl;
deque<int>indeks[30];
vector<int>ssn;
int segtree[4*N]={0};
void barui(int idx, int low, int high, int value) {
if(low==high && low==value) {
segtree[idx]=1;
return;
}
if(value<low || value>high) return;
int mid = (low+high)/2;
barui(2*idx,low,mid,value);
barui(2*idx+1,mid+1,high,value);
segtree[idx] = segtree[2*idx]+segtree[2*idx+1];
}
int carinya(int idx, int low, int high, int l, int r) {
if(low>=l && high<=r) {
return segtree[idx];
}
if(low>r || high<l) return 0;
int mid = (low+high)/2;
int left = carinya(2*idx,low,mid,l,r);
int right = carinya(2*idx+1,mid+1,high,l,r);
return left+right;
}
int main() {
int n;
cin >> n;
n*=2;
string umum;
cin >> umum;
for(int i=0; i<n; i++) frek[umum[i]-97]++;
for(int i=0; i<n; i++) {
if(frek[umum[i]-97]>0) {
kmpl.eb(umum[i]);
frek[umum[i]-97]-=2;
}
}
int sk=kmpl.size();
for(int i=0; i<sk; i++) kmpl.eb(kmpl[i]);
for(int i=0; i<n; i++) indeks[kmpl[i]-97].eb(i);
for(int i=0; i<n; i++) {
ssn.eb(indeks[umum[i]-97].front());
indeks[umum[i]-97].pop_front();
}
long long jwb=0;
for(int i=0; i<n; i++) {
jwb+=carinya(1,0,n-1,ssn[i]+1,n-1);
barui(1,0,n-1,ssn[i]);
}
cout << jwb << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
16 ms |
2056 KB |
Output is correct |
3 |
Correct |
32 ms |
3876 KB |
Output is correct |
4 |
Correct |
48 ms |
4528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
16 ms |
2056 KB |
Output is correct |
3 |
Correct |
32 ms |
3876 KB |
Output is correct |
4 |
Correct |
48 ms |
4528 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
16 ms |
2056 KB |
Output is correct |
3 |
Correct |
32 ms |
3876 KB |
Output is correct |
4 |
Correct |
48 ms |
4528 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
54 ms |
4588 KB |
Output is correct |
7 |
Correct |
53 ms |
4608 KB |
Output is correct |
8 |
Correct |
52 ms |
4564 KB |
Output is correct |
9 |
Correct |
52 ms |
4536 KB |
Output is correct |
10 |
Correct |
51 ms |
4480 KB |
Output is correct |
11 |
Correct |
51 ms |
4560 KB |
Output is correct |
12 |
Correct |
50 ms |
4464 KB |
Output is correct |
13 |
Correct |
50 ms |
4312 KB |
Output is correct |
14 |
Correct |
49 ms |
4264 KB |
Output is correct |
15 |
Correct |
61 ms |
4304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
16 ms |
2056 KB |
Output is correct |
3 |
Correct |
32 ms |
3876 KB |
Output is correct |
4 |
Correct |
48 ms |
4528 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
54 ms |
4588 KB |
Output is correct |
11 |
Correct |
53 ms |
4608 KB |
Output is correct |
12 |
Correct |
52 ms |
4564 KB |
Output is correct |
13 |
Correct |
52 ms |
4536 KB |
Output is correct |
14 |
Correct |
51 ms |
4480 KB |
Output is correct |
15 |
Correct |
51 ms |
4560 KB |
Output is correct |
16 |
Correct |
50 ms |
4464 KB |
Output is correct |
17 |
Correct |
50 ms |
4312 KB |
Output is correct |
18 |
Correct |
49 ms |
4264 KB |
Output is correct |
19 |
Correct |
61 ms |
4304 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
54 ms |
4604 KB |
Output is correct |
33 |
Correct |
56 ms |
4660 KB |
Output is correct |
34 |
Correct |
59 ms |
4600 KB |
Output is correct |
35 |
Correct |
52 ms |
4588 KB |
Output is correct |
36 |
Correct |
56 ms |
4492 KB |
Output is correct |
37 |
Correct |
51 ms |
4468 KB |
Output is correct |
38 |
Correct |
50 ms |
4456 KB |
Output is correct |
39 |
Correct |
50 ms |
4356 KB |
Output is correct |
40 |
Correct |
49 ms |
4312 KB |
Output is correct |
41 |
Correct |
49 ms |
4316 KB |
Output is correct |