Submission #643677

# Submission time Handle Problem Language Result Execution time Memory
643677 2022-09-22T18:34:46 Z _petar_b Arranging Shoes (IOI19_shoes) C++14
0 / 100
1 ms 428 KB
#include "shoes.h"
#include <bits/stdc++.h>
#define MAXN 200010
#define DELTA 100001
#define pb push_back
#define ll long long
#define fi first
#define se second
#define mp make_pair

using namespace std;

int n;
vector<int> t, par, tleaf;
vector<queue<int>> posOf;

void build(int v, int tl, int tr)
{
    if (tl == tr)
        t[v] = 1;
    else
    {
        int tm = (tl + tr) / 2;
        build(v*2, tl, tm);
        build(v*2+1, tm+1, tr);
        t[v] = t[v*2] + t[v*2+1];
    }
}

int sum(int v, int tl, int tr, int l, int r)
{
    if (l > r)
        return 0;
    if (l == tl && r == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    return sum(v*2, tl, tm, l, min(r, tm)) + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
}

void update(int v, int tl, int tr, int pos)
{
    if (tl == tr)
        t[v] = 0;
    else
    {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v*2, tl, tm, pos);
        else
            update(v*2+1, tm+1, tr, pos);
        t[v] = t[v*2] + t[v*2+1];
    }
}

long long count_swaps(std::vector<int> s)
{
    n = s.size();
    t.resize(4*n + 10);
    par.resize(n + 10);
    tleaf.resize(n + 10);
    posOf.resize(n + 10);
    for (int i = 0; i < n; i++)
    {
        tleaf[i] = 1;
        int other = DELTA - s[i];
        if (!posOf[other].empty())
        {
            par[i] = posOf[other].front();
            posOf[other].pop();
        }
        else
        {
            par[i] = -1;
            posOf[s[i]+DELTA].push(i);
        }
    }
    build(1, 0, n-1);
    int res = 0;
    for (int i = n-1; i >= 0; i--)
    {
        if (tleaf[i] == 1)
        {
            res += sum(1, 0, n-1, par[i]+1, i-1);
            tleaf[par[i]] = 0;
            update(1, 0, n-1, par[i]);
            if (s[i] < 0) res++;
        }
    }
    return res;
}
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 428 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -