#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
int t[N],seg[(SS<<1)+10],wsk[N],il[N],pom[N];
vi g[N];
void upd(int a,int b){
for(a+=SS;a>0;a>>=1) seg[a]+=b;
}
int Query(int a,int b){
int res=0;
for(a+=SS-1,b+=SS+1;(a>>1)!=(b>>1);a>>=1,b>>=1){
if(!(a&1)) res+=seg[a+1];
if(b&1) res+=seg[b-1];
}
return res;
}
ll cnt(string a,string b){
a="#"+a,b="#"+b;
for(int i=1;i<a.size();i++){
int curr=int(a[i])-96;
g[curr].push_back(i);
}
ll res=0;
for(int i=1;i<b.size();i++){
int curr=int(b[i])-96;
int currl=g[curr][wsk[curr]++];
res+=(ll)Query(currl,a.size());
upd(currl,1);
}
return res;
}
void solve(){
int n;
string s;
cin>>n>>s;
for(int i=0;i<s.size();i++) il[int(s[i])-96]++;
string res;
for(int i=0;i<s.size();i++){
pom[int(s[i])-96]++;
if(pom[int(s[i])-96]<=(il[int(s[i])-96]>>1)) res+=s[i];
}
res=res+res;
cout<<cnt(s,res)<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
Compilation message
Main.cpp: In function 'll cnt(std::string, std::string)':
Main.cpp:35:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i=1;i<a.size();i++){
| ~^~~~~~~~~
Main.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=1;i<b.size();i++){
| ~^~~~~~~~~
Main.cpp: In function 'void solve()':
Main.cpp:52:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=0;i<s.size();i++) il[int(s[i])-96]++;
| ~^~~~~~~~~
Main.cpp:54:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i=0;i<s.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
20 ms |
25216 KB |
Output is correct |
3 |
Correct |
31 ms |
26076 KB |
Output is correct |
4 |
Correct |
33 ms |
27104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
20 ms |
25216 KB |
Output is correct |
3 |
Correct |
31 ms |
26076 KB |
Output is correct |
4 |
Correct |
33 ms |
27104 KB |
Output is correct |
5 |
Correct |
13 ms |
23812 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
20 ms |
25216 KB |
Output is correct |
3 |
Correct |
31 ms |
26076 KB |
Output is correct |
4 |
Correct |
33 ms |
27104 KB |
Output is correct |
5 |
Correct |
12 ms |
23844 KB |
Output is correct |
6 |
Correct |
37 ms |
27336 KB |
Output is correct |
7 |
Correct |
43 ms |
27324 KB |
Output is correct |
8 |
Correct |
42 ms |
27344 KB |
Output is correct |
9 |
Correct |
45 ms |
27240 KB |
Output is correct |
10 |
Correct |
43 ms |
27276 KB |
Output is correct |
11 |
Correct |
35 ms |
27168 KB |
Output is correct |
12 |
Correct |
35 ms |
27160 KB |
Output is correct |
13 |
Correct |
35 ms |
27164 KB |
Output is correct |
14 |
Correct |
34 ms |
27036 KB |
Output is correct |
15 |
Correct |
36 ms |
27012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23800 KB |
Output is correct |
2 |
Correct |
18 ms |
23856 KB |
Output is correct |
3 |
Correct |
19 ms |
23924 KB |
Output is correct |
4 |
Correct |
16 ms |
23860 KB |
Output is correct |
5 |
Correct |
12 ms |
23892 KB |
Output is correct |
6 |
Correct |
19 ms |
23836 KB |
Output is correct |
7 |
Correct |
21 ms |
23936 KB |
Output is correct |
8 |
Correct |
16 ms |
23884 KB |
Output is correct |
9 |
Correct |
15 ms |
23812 KB |
Output is correct |
10 |
Correct |
16 ms |
23896 KB |
Output is correct |
11 |
Correct |
14 ms |
23904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
20 ms |
25216 KB |
Output is correct |
3 |
Correct |
31 ms |
26076 KB |
Output is correct |
4 |
Correct |
33 ms |
27104 KB |
Output is correct |
5 |
Correct |
13 ms |
23812 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23844 KB |
Output is correct |
10 |
Correct |
37 ms |
27336 KB |
Output is correct |
11 |
Correct |
43 ms |
27324 KB |
Output is correct |
12 |
Correct |
42 ms |
27344 KB |
Output is correct |
13 |
Correct |
45 ms |
27240 KB |
Output is correct |
14 |
Correct |
43 ms |
27276 KB |
Output is correct |
15 |
Correct |
35 ms |
27168 KB |
Output is correct |
16 |
Correct |
35 ms |
27160 KB |
Output is correct |
17 |
Correct |
35 ms |
27164 KB |
Output is correct |
18 |
Correct |
34 ms |
27036 KB |
Output is correct |
19 |
Correct |
36 ms |
27012 KB |
Output is correct |
20 |
Correct |
19 ms |
23800 KB |
Output is correct |
21 |
Correct |
18 ms |
23856 KB |
Output is correct |
22 |
Correct |
19 ms |
23924 KB |
Output is correct |
23 |
Correct |
16 ms |
23860 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
19 ms |
23836 KB |
Output is correct |
26 |
Correct |
21 ms |
23936 KB |
Output is correct |
27 |
Correct |
16 ms |
23884 KB |
Output is correct |
28 |
Correct |
15 ms |
23812 KB |
Output is correct |
29 |
Correct |
16 ms |
23896 KB |
Output is correct |
30 |
Correct |
14 ms |
23904 KB |
Output is correct |
31 |
Correct |
17 ms |
23808 KB |
Output is correct |
32 |
Correct |
41 ms |
27316 KB |
Output is correct |
33 |
Correct |
46 ms |
27256 KB |
Output is correct |
34 |
Correct |
40 ms |
27360 KB |
Output is correct |
35 |
Correct |
39 ms |
27196 KB |
Output is correct |
36 |
Correct |
35 ms |
27224 KB |
Output is correct |
37 |
Correct |
37 ms |
27232 KB |
Output is correct |
38 |
Correct |
44 ms |
27156 KB |
Output is correct |
39 |
Correct |
36 ms |
27144 KB |
Output is correct |
40 |
Correct |
35 ms |
27052 KB |
Output is correct |
41 |
Correct |
35 ms |
27132 KB |
Output is correct |