제출 #1163836

#제출 시각아이디문제언어결과실행 시간메모리
1163836hmm789Ekoeko (COCI21_ekoeko)C++20
110 / 110
7 ms6356 KiB

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define INF 1000000000000000000
#define MOD 1000000007

int fw[100005];
void update(int x, int v) {
    for(x++; x < 100005; x += x&-x) fw[x] += v;
}
int query(int x) {
    int ans = 0;
    for(x++; x; x -= x&-x) ans += fw[x];
    return ans;
}
int query(int x, int y) {
    return query(y)-query(x-1);
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, ans = 0;
    string s;
    cin >> n >> s;
    vector<int> occ[26];
    for(int i = 0; i < 2*n; i++) occ[s[i]-'a'].push_back(i);
    int pos[2*n], mp[2*n], cnt = 0;
    memset(pos, 0, sizeof(pos));
    for(int i = 0; i < 26; i++) {
        for(int j = 0; j < occ[i].size()/2; j++) {
            pos[occ[i][j]] = 1;
            mp[occ[i][j]] = occ[i][j+occ[i].size()/2];
        }
    }
    for(int i = 0; i < 2*n; i++) {
        if(pos[i] == 0) cnt++;
        else ans += cnt;
    }
    cnt = 2;
    for(int i = 0; i < 2*n; i++) {
        if(pos[i] == 1) pos[mp[i]] = cnt++;
    }
    for(int i = 0; i < 2*n; i++) if(pos[i] > 1) {
        ans += query(pos[i], n+3);
        update(pos[i], 1);
    }
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...