-
Notifications
You must be signed in to change notification settings - Fork 1
/
q5.txt
125 lines (98 loc) · 2.08 KB
/
q5.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int , int>
#define int long long int
#define ALL(v) v.begin(),v.end()
#define ALLR(v) v.rbegin(),v.rend()
#define pi 3.14159265358979323
#define inf intONG_MAX
#define ones(x) __builtin_popcount(x)
#define fiint(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
int mod_pow(int a,int b,int m)
{
int res = 1;
while(b)
{
if(b&1)
{
res = (res*a) % m;
}
a = (a*a) % m;
b >>= 1;
}
return res;
}
int mod_inverse(int a , int m)
{
return mod_pow(a , m - 2 , m);
}
struct ft
{
vector<int> bit;
int n;
ft(int n1)
{
n = n1;
bit.assign(n,0);
}
int sum(int r)
{
int ret = 0;
for(; r >= 0; r = (r&(r+1))-1)
ret += bit[r];
return ret;
}
void add(int idx, int d)
{
for(; idx < n; idx = idx | (idx+1))
bit[idx] += d;
}
int sum(int l, int r)
{
return sum(r) - sum(l-1);
}
};
void solve() {
int n;
cin >> n;
string s;
cin >> s;
string t = s;
ft ft1(n);
reverse(ALL(t));
vector<int> v[26];
for(int i=0;i<s.size();i++) {
v[s[i]-'a'].pb(i);
}
int iddi[26] = {0};
int ans = 0;
for(int i=0;i<n;i++)
{
int nolol = s[i] - 'a';
int noobda = t[i] - 'a';
for(int j = 0; j <= 5; ++j) {
}
int id1 = v[noobda][iddi[noobda]];
iddi[noobda]++;
int sum = ft1.sum(0,id1);
ans += id1;
ans -= sum;
ft1.add(id1,1);
}
cout<<ans<<"\n";
}
signed main() {
fast;
int t = 1;
//cin >> t;
while(t--) {
solve();
}
return 0;
}