답안 #639079

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
639079 2022-09-08T14:00:18 Z LeonaRaging Swap Swap Sort (CCO21_day1problem1) C++14
3 / 25
204 ms 33452 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "

const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e9;
const int block = 5000;

struct seg_tree {
    vector<int> t;
    seg_tree() {
        t.resize(4 * maxn);
    }
    void update(int pos, int v = 1, int l = 1, int r = maxn - 1) {
        if (l == r) {
            t[v]++;
            return;
        }
        int m = (l + r) / 2;
        if (pos <= m) update(pos, 2 * v, l, m);
        else update(pos, 2 * v + 1, m + 1, r);
        t[v] = t[2 * v] + t[2 * v + 1];
    }
    int get(int l, int r, int v = 1, int tl = 1, int tr = maxn - 1) {
        if (tl > r || tr < l) return 0;
        if (tl >= l && tr <= r) return t[v];
        int m = (tl + tr) / 2;
        int L = get(l, r, 2 * v, tl, m);
        int R = get(l, r, 2 * v + 1, m + 1, tr);
        return L + R;
    }
} IT;

int n, k, q, a[maxn], p[maxn], sum[maxn], delta[maxn];
vector<int> pos[maxn];
vector<pair<int,int>> adj[maxn];

int main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0); cout.tie(0);
    //freopen(".INP", "r", stdin);
    //freopen(".OUT", "w", stdout);
    cin >> n >> k >> q;
    int res = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        res += IT.get(a[i] + 1, n);
        pos[a[i]].pb(i);
        IT.update(a[i]);
    }
    for (int i = 1; i <= n; i++)
        p[i] = i;
    for (int i = 1; i <= q; i++) {
        int j; cin >> j;
        // clog << db(j);
        int x = p[j], y = p[j + 1];
        if (pos[x].size() <= block && pos[y].size() <= block) {
            int l = 0, inv = 0;
            for (int r = 0; r < int(pos[x].size()); r++) {
                while (l + 1 < int(pos[y].size()) && pos[y][l + 1] < pos[x][r])
                    l++;
                if (l < int(pos[y].size()) && pos[y][l] < pos[x][r])
                    inv += l + 1;
            }
            delta[i] = -2 * inv + pos[x].size() * pos[y].size();
            // if (i == 2) clog << inv << '\n';
        }
        else {
            if (pos[x].size() > block) adj[x].pb({y, i});
            else adj[x].pb({y, -i});
        }
        swap(p[j], p[j + 1]);
    }
    for (int x = 1; x <= n; x++) {
        if (pos[x].size() <= block) continue;
        memset(sum, 0, sizeof sum);
        for (auto i : pos[x])
            sum[i] = 1;
        for (int i = 1; i <= n; i++)
            sum[i] += sum[i - 1];
        sort(adj[x].begin(), adj[x].end());
        int i = 0;
        while (i < int(adj[x].size())) {
            int y = adj[x][i].fi, inv = pos[x].size() * pos[y].size();
            for (int j : pos[y])
                inv -= 2 * sum[j];
            while (adj[x][i].fi == y && i < int(adj[x].size())) {
                if (adj[x][i].se > 0) delta[adj[x][i].se] = inv;
                else delta[adj[x][i].se] = -inv;
                i++;
            }
        }
    }
    for (int i = 1; i <= q; i++)
        res += delta[i], cout << res << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 112 ms 6680 KB Output is correct
3 Correct 82 ms 6684 KB Output is correct
4 Correct 16 ms 6712 KB Output is correct
5 Correct 9 ms 6724 KB Output is correct
6 Correct 8 ms 6740 KB Output is correct
7 Correct 8 ms 6740 KB Output is correct
8 Correct 8 ms 6740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 39 ms 8248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 204 ms 33452 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 112 ms 6680 KB Output is correct
3 Correct 82 ms 6684 KB Output is correct
4 Correct 16 ms 6712 KB Output is correct
5 Correct 9 ms 6724 KB Output is correct
6 Correct 8 ms 6740 KB Output is correct
7 Correct 8 ms 6740 KB Output is correct
8 Correct 8 ms 6740 KB Output is correct
9 Incorrect 39 ms 8248 KB Output isn't correct
10 Halted 0 ms 0 KB -