比赛 NOIP2025模拟赛4 评测结果 AAAAAAAATTTTTTTTTTTT
题目名称 Swap Swap Sort 最终得分 40
用户昵称 淮淮清子 运行时间 25.396 s
代码语言 C++ 内存使用 7.35 MiB
提交时间 2025-11-27 12:26:25
显示代码纯文本
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define ll long long
const int MAXK = 1e5 + 5;

int n, k, q;
int a[MAXK], sum[MAXK];
vector<int> pos[MAXK];
ll cnt[MAXK];
int p[MAXK];

inline int lowbit(int x){
    return x & -x;
}

inline void add(int x, int t){
    while(x <= k){
        sum[x] += t;
        x += lowbit(x);
    }
}

inline int query(int x){
    int ans = 0;
    while(x){
        ans += sum[x];
        x -= lowbit(x);
    }
    return ans;
}

//x about y's cnt
inline ll clc(int x, int y){
    ll res = 0;
    for(int &py : pos[y]){
        res += lower_bound(pos[x].begin(), pos[x].end(), py) - pos[x].begin();
    }
    return res;
}

int main(){
    freopen("Sort.in", "r", stdin);
    freopen("Sort.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k >> q;
    for(int i = 1;i <= n;++ i){
        cin >> a[i];
        pos[a[i]].push_back(i);
        cnt[a[i]] ++;
    }
    ll tot = 0;
    for (int i = 1; i <= n; ++i) {
        int x = a[i];
        tot += (query(k) - query(x));
        add(x, 1);
    }
    for(int i = 1;i <= k;++ i){
        p[i - 1] = i;
    }
    /*
    res - (cnt[x] * cnt[y] - res)
    */
    while(q --){
        int b; cin >> b; b--;
        int &x = p[b], &y = p[b + 1];
        ll res = clc(x, y);
        tot += 2 * res - cnt[x] * cnt[y];
        swap(x, y);
        cout << tot << '\n';
    }
    // cerr << "Time : " << clock() / CLOCKS_PER_SEC << "s \n";
    return 0;
}