Submission #687346

#TimeUsernameProblemLanguageResultExecution timeMemory
687346NeltArranging Shoes (IOI19_shoes)C++17
100 / 100
80 ms18768 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
#include <shoes.h>
/* DEFINES */
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define npos ULLONG_MAX
#define INF LLONG_MAX
#define vv(a) vector<a>
#define pp(a, b) pair<a, b>
#define pq(a) priority_queue<a>
#define qq(a) queue<a>
#define ss(a) set<a>
#define mm(a, b) map<a, b>
#define ump(a, b) unordered_map<a, b>
#define sync                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define elif else if
#define endl "\n"
#define allc(a) begin(a), end(a)
#define all(a) a, a + sizeof(a) / sizeof(a[0])
#define pb push_back
#define logi(a) __lg(a)
#define sqrt(a) sqrtl(a)
#define mpr make_pair
#define ins insert
using namespace std;
using namespace __cxx11;
typedef char chr;
typedef basic_string<chr> str;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct SegTree
{
    ll n;
    vv(ll) st;
    SegTree(ll sz = 0, bool input = false)
    {
        n = sz;
        st.resize((n + 1) << 1, 0);
    }
    void set(ll i, ll val)
    {
        st[i + n - 1] = val;
    }
    void build()
    {
        for (ll i = n - 1; i >= 1; i--)
            st[i] = st[i << 1] + st[i << 1 | 1];
    }
    void modify(ll p, ll val)
    {
        for (st[p += n - 1] = val; p > 1; p >>= 1)
            st[p >> 1] = st[p] + st[p ^ 1];
    }
    ll query(ll l, ll r)
    {
        ll ans = 0;
        for (l += n - 1, r += n; l < r; l >>= 1, r >>= 1)
        {
            if (l & 1)
                ans += st[l++];
            if (r & 1)
                ans += st[--r];
        }
        return ans;
    }
};
ll count_swaps(std::vector<int> a)
{
    ll n = a.size() >> 1;
    SegTree st(n << 1);
    vv(ll) lef[n + 1], rig[n + 1];
    for (ll i = 0; i < (n << 1); i++)
        if (a[i] < 0)
            lef[-a[i]].pb(i + 1);
        else
            rig[a[i]].pb(i + 1);
    ll ans = 0;
    pp(ll, ll) p[n];
    ll ptr = 0;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 0; j < lef[i].size(); j++)
        {
            if (lef[i][j] > rig[i][j])
                swap(lef[i][j], rig[i][j]), ans++;
            p[ptr++] = mpr(lef[i][j], rig[i][j]);
        }
    }
    sort(all(p));
    for (ll i = 0; i < n; i++)
    {
        ans += p[i].S - p[i].F - 1 - st.query(p[i].F, p[i].S);
        st.modify(p[i].S, 1);
    }
    return ans;
}
void solve()
{
    ll n;
    cin >> n;
    vv(int) a(n << 1);
    for (int &i : a)
        cin >> i;
    cout << count_swaps(a) << endl;
}
/*

*/
// int main()
// {
//     // sync
//         // precomp();
//         ll t = 1;
//     cin >> t;
//     for (ll i = 1; i <= t; i++)
//         // cout << "Case #" << i << ": ",
//         solve();
//     cerr << "\nTime elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
// }

Compilation message (stderr)

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:90:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         for (ll j = 0; j < lef[i].size(); j++)
      |                        ~~^~~~~~~~~~~~~~~
#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...