Submission #408801

#TimeUsernameProblemLanguageResultExecution timeMemory
408801dxz05Arranging Shoes (IOI19_shoes)C++14
30 / 100
1113 ms275916 KiB
#include "shoes.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 4e5 + 6e3;

int f[MAXN];

void add(int x, int y){
    x++;
    while (x < MAXN){
        f[x] += y;
        x += (-x & x);
    }
}

void add(int l, int r, int y){
    if (l > r) return;
    add(l, y);
    add(r + 1, -y);
}

int get(int x){
    x++;
    int sum = 0;
    while (x > 0){
        sum += f[x];
        x -= (-x & x);
    }
    return sum;
}

int get(int l, int r){
    return get(r) - get(l - 1);
}

int code(int val){
    return (val < 0 ? -val : val + 200000);
}

set<pair<int, pair<int, int>>> added_segments;

int original_position(int x, bool flag){
    int i = x;
    set<pair<int, pair<int, int>>> er;
    for (auto now : added_segments){
        int l = now.second.first, r = now.second.second;
        if (l < x && x < r) x++;
        if (flag && r < i) er.insert(now);
    }

    for (auto now : er) added_segments.erase(now);

    return x;
}

queue<int> q[MAXN];

long long count_swaps(vector<int> a) {
	int n = a.size();

	for (int i = 0; i < n; i++){
        q[code(a[i])].push(i);
	}

    vector<bool> used(n, false);

    ll ans = 0;

    for (int i = 0; i < n; i++){
        if (used[i]) continue;

        int x = code(a[i]), y = code(-a[i]);

        int pos1 = i, pos2 = q[y].front();
        used[pos2] = true;

        pos1 = original_position(pos1, true), pos2 = original_position(pos2, false);

        added_segments.insert(make_pair(added_segments.size(), make_pair(pos1, pos2)));

        //cout << pos1 << ' ' << pos2 << endl;

        ans += pos2 - pos1 - 1 + (a[i] > 0);

        q[x].pop();
        q[y].pop();
    }

	return ans;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...