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>
#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() {
ios_base::sync_with_stdio(false);
cin.tie(0);
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;
}
# | 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... |