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;
#define N 100010
#define M 50
#define INFLL 2000000000000000020
#define pb push_back
typedef long long ll;
typedef pair<ll,ll> pll;
map<ll,ll>freq;
map<ll,ll>cur;
vector<ll>pos[M];
ll st[N<<2];
void update(ll i,ll l,ll r,ll pos)
{
if(pos<l || pos>r)
return;
if(l==r)
{
st[i]++;
return;
}
ll nxt=(i<<1),mid=((l+r)>>1);
update(nxt,l,mid,pos);
update(nxt+1,mid+1,r,pos);
st[i]=st[nxt]+st[nxt+1];
return;
}
ll query(ll i,ll l,ll r,ll L,ll R)
{
if(l>R || L>r)
return 0LL;
if(L<=l && r<=R)
return st[i];
ll nxt=(i<<1),mid=((l+r)>>1);
return query(nxt,l,mid,L,R)+
query(nxt+1,mid+1,r,L,R);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n,nn,i,mx=0,j=0,ans=0,p,x,aux;
string S,ini,fim;
cin >> n >> S;
nn=2*n;
for(i=0;i<nn;i++)
{
freq[S[i]]++;
}
for(i=0;i<nn;i++)
{
if(!freq[S[i]])
{
fim+=S[i];
continue;
}
ini+=S[i];
ans+=i-j;
freq[S[i]]-=2;
j++;
}
for(i=n-1;i>=0;i--)
{
x=(ll)(fim[i]-'a');
pos[x].pb(i);
}
for(i=0;i<n;i++)
{
x=(ll)(ini[i]-'a');
p=pos[x].back();
pos[x].pop_back();
aux=p;
p+=query(1,1,n,p,n);
ans+=p-i;
update(1,1,n,aux);
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:51:15: warning: unused variable 'mx' [-Wunused-variable]
51 | ll n,nn,i,mx=0,j=0,ans=0,p,x,aux;
| ^~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |