제출 #524573

#제출 시각아이디문제언어결과실행 시간메모리
524573AA_SurelyArranging Shoes (IOI19_shoes)C++17
100 / 100
167 ms16964 KiB
#include "shoes.h"
#include <bits/stdc++.h>

#define FOR(i,x,n) 	for(int i=x; i<n; i++)
#define F0R(i,n) 	FOR(i,0,n)
#define ROF(i,x,n) 	for(int i=n-1; i>=x; i--)
#define R0F(i,n) 	ROF(i,0,n)

#define WTF 		cout << "WTF" << endl

#define IOS 		ios::sync_with_stdio(false); cin.tie(0)
#define F 			first
#define S	 		second
#define pb 			push_back

#define ALL(x) 		x.begin(), x.end()
#define RALL(x) 	x.rbegin(), x.rend()

using namespace std;
typedef long long 		LL;

typedef pair<int, int> 	PII;
typedef pair<LL, LL> 	PLL;

typedef vector<int> 	VI;
typedef vector<LL> 		VLL;
typedef vector<PII> 	VPII;
typedef vector<PLL> 	VPLL;

const int MAXN = 2e5 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;

int n;
int ns[MAXN], tree[MAXN << 2];
int ptr[MAXN];
VI group[MAXN];

void change(int id, int val, int now = 1, int ls = 0, int rs = n - 1) {
    if (ls == rs) {
        tree[now] = val;
        return;
    }

    int mid = (ls + rs) >> 1;
    if (id <= mid) change(id, val, now << 1, ls, mid);
    else change(id, val, now << 1 | 1, mid + 1, rs);

    tree[now] = tree[now << 1] + tree[now << 1 | 1];
    return;
}

int get(int lq, int rq, int now = 1, int ls = 0, int rs = n - 1) {
    if (rq < lq || rq < ls || rs < lq) return 0;
    if (lq <= ls && rs <= rq) return tree[now];
    int mid = (ls + rs) >> 1;
    return get(lq, rq, now << 1, ls, mid) + get(lq, rq, now << 1 | 1, mid + 1, rs);
}

long long count_swaps(VI inp) {
    n = inp.size();
    F0R(i, n) ns[i] = inp[i];

    F0R(i, n) {
        if (ns[i] < 0) ns[i] = (-ns[i] << 1);
        else ns[i] = (ns[i] << 1) | 1;
        group[ ns[i] ].pb(i);
    }
    
    F0R(i, n) change(i, 1);
    LL ans = 0;

    F0R(i, n) {
        if (!get(i, i)) continue;

        if (ns[i] & 1) {
            while(!get(group[ ns[i] - 1 ][ ptr[ ns[i] - 1 ] ], group[ ns[i] - 1 ][ ptr[ ns[i] - 1 ] ])) ptr[ ns[i] - 1 ]++;
            ans += get(i, group[ ns[i] - 1 ][ ptr[ ns[i] - 1 ] ] - 1);
            change(group[ ns[i] - 1 ][ ptr[ ns[i] - 1 ] ], 0);
        }

        else {
            while(!get(group[ ns[i] + 1 ][ ptr[ ns[i] + 1 ] ], group[ ns[i] + 1 ][ ptr[ ns[i] + 1 ] ])) ptr[ ns[i] + 1 ]++;
            ans += get(i + 1, group[ ns[i] + 1 ][ ptr[ ns[i] + 1 ] ] - 1);
            change(group[ ns[i] + 1 ][ ptr[ ns[i] + 1 ] ], 0);
        }

        change(i, 0);
    }

    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...