Submission #164587

#TimeUsernameProblemLanguageResultExecution timeMemory
164587qwerty234Arranging Shoes (IOI19_shoes)C++14
100 / 100
216 ms21752 KiB
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define f first
#define se second
#define pb push_back
 
 
using namespace std;
 
 
const int N = 4e5 + 219;
const ll M = 3e5 + 19;
const ll inf = 1e15 + 10;
const ll mod = 1e9 + 7;
 
 
ll n, a[N], Last[N], Lastt[N], curL[N], curR[N], curL1[N], curR1[N], nx[N], prv[N], t[4 * N];
bool vis[N];
 
 
void upd(int v, int tl, int tr, int pos) {
    if (tl == tr) {
        t[v] = 1;
        return;
    }
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        upd(v * 2, tl, tm, pos);
    else
        upd(v * 2 + 1, tm + 1, tr, pos);
    t[v] = t[v * 2] + t[v * 2 + 1];
}
 
 
ll get(int v, int tl, int tr, int l, int r) {
    if (r < tl || tr < l)
        return 0;
    int tm = (tl + tr) / 2;
    if (l <= tl && tr <= r)
        return t[v];
    return get(v * 2, tl, tm, l, r) + get(v * 2 + 1, tm + 1, tr, l, r);
}
 
 
ll count_swaps(vector <int> b) {
    n = b.size();
    for (int i = 0; i < b.size(); i++)
        a[i + 1] = b[i];
    for (int i = 1; i <= n; i++) {
        if (a[i] < 0) {
            prv[i] = Lastt[-a[i]];
            curR1[-a[i]] = i;
            Lastt[-a[i]] = i;
        } else {
            prv[i] = Last[a[i]];
            curR[a[i]] = i;
            Last[a[i]] = i;
        }
    }
    memset(Last, 0, sizeof(Last));
    memset(Lastt, 0, sizeof(Lastt));
    for (int i = n; i >= 1; i--) {
        if (a[i] < 0) {
            nx[i] = Lastt[-a[i]];
            curL1[-a[i]] = i;
            Lastt[-a[i]] = i;
        } else {
            nx[i] = Last[a[i]];
            curL[a[i]] = i;
            Last[a[i]] = i;
        }
    }
    ll ans = 0, t1, t2;
    ll L = 1, R = n;
    while (L <= R) {
        int crl, crr;
        if (a[L] < 0)
            t1 = curL[-a[L]] - L - get(1, 1, n, L, curL[-a[L]]) - 1;
        else
            t1 = curL1[a[L]] - L - get(1, 1, n, L, curL1[a[L]]) - 1;
        if (a[L] > 0)
            t1++;
        if (a[R] < 0)
            t2 = R - curR[-a[R]] - get(1, 1, n, curR[-a[R]], R) - 1;
        else
            t2 = R - curR1[a[R]] - get(1, 1, n, curR1[a[R]], R) - 1;
        if (a[R] < 0)
            t2++;
        if (t1 < t2) {
            ans += t1;
            if (a[L] < 0) {
                vis[L] = vis[curL[-a[L]]] = 1;
                upd(1, 1, n, L);
                upd(1, 1, n, curL[-a[L]]);
                curL1[-a[L]] = nx[curL1[-a[L]]];
                curL[-a[L]] = nx[curL[-a[L]]];
            } else {
                vis[L] = vis[curL1[a[L]]] = 1;
                upd(1, 1, n, L);
                upd(1, 1, n, curL1[a[L]]);
                curL[a[L]] = nx[curL[a[L]]];
                curL1[a[L]] = nx[curL1[a[L]]];
            }
        } else {
            ans += t2;
            if (a[R] < 0) {
                vis[R] = vis[curR[-a[R]]] = 1;
                upd(1, 1, n, R);
                upd(1, 1, n, curR[-a[R]]);
                curR1[-a[R]] = prv[curR1[-a[R]]];
                curR[-a[R]] = prv[curR[-a[R]]];
            } else {
                vis[R] = vis[curR1[a[R]]] = 1;
                upd(1, 1, n, R);
                upd(1, 1, n, curR1[a[R]]);
                curR[a[R]] = prv[curR[a[R]]];
                curR1[a[R]] = prv[curR1[a[R]]];
            }
        }
        while (vis[L])
            L++;
        while (vis[R])
            R--;
        if (R < L || L > n || R < 0)
            break;
    }
    return ans;
}

Compilation message (stderr)

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:49:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < b.size(); i++)
                     ~~^~~~~~~~~~
shoes.cpp:78:13: warning: unused variable 'crl' [-Wunused-variable]
         int crl, crr;
             ^~~
shoes.cpp:78:18: warning: unused variable 'crr' [-Wunused-variable]
         int crl, crr;
                  ^~~
#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...