Submission #158229

# Submission time Handle Problem Language Result Execution time Memory
158229 2019-10-15T15:03:05 Z nickmet2004 Arranging Shoes (IOI19_shoes) C++14
Compilation error
0 ms 0 KB
#include<bits/stdc++.h>
//#include<shoes.h>

using namespace std;

const int NAX = 1e5 + 5;

struct Hash{
    size_t operator()(const pair<int , int> &x) const{
        return ((long long)x.first) ^ (((long long)x.second) << 32);
    }
};
// fenwick tree to count range sums (+0 , +1 , +2)

int f[2 * NAX + 1];

void update(int idx , int u){
    ++idx;
    while(idx <= N){
        f[idx] += u;
        idx += idx & (-idx);
    }
}
long long sum(int idx){
    long long sum = 0;
    ++idx;
    while(idx){
        sum += f[idx];
        idx -= idx & (-idx);
    }
    return sum;
}

unordered_map<pair<long , long> , bool , Hash> paired , px , nx;
queue<int> q[NAX];

long long count_swaps(vector<int> S){
    int N = S.size();
    for(int i = 1; i <= N; ++i){
        px[{i , 1}] = false;
        nx[{i , -1}] = false;
    }
    // update BIT
    for(int i = 0; i < N; ++i){
        update(i , 1);
    }
    for(int i = 0; i < N; ++i){
        int x = abs(S[i]);
        int k = S[i];
        // when queue is empty
        if(q[x].empty()){
            if(k > 0){
                q[x].push(i);
                //positive x's are in the queue
                px[{x , 1}] = true;
                nx[{x , -1}] = false;
            }
            else {
                q[x].push(i);
                // negative x's are int the queue
                nx[{x , -1}] = true;
                px[{x , 1}] = false;
            }
        } else if(px[{x , 1}]){
            if(k < 0){
                ans += (sum(i) - sum(q[x].front() - 1) - 1);
                update(i , -1);
                if(q[x].front()){
                    update(q[x].front() - 1 , 1);
                }
                q[x].pop();
            } else {
                q[x].push(i);
            }
        } else if(nx[{x , -1}]){
                if(k > 0){
                    ans += (sum(i) - sum(q[x].front() - 1) - 2);
                    update(q[x].front() , -1);
                    update(i - 1 , 1);
                    q[x].pop();
            } else {
                q[x].push(i);
            }
        }
        if(q[x].empty()){
            px[{x , 1}] = false;
            nx[{x , -1}] = false;
        }
    }
    return ans;
}

Compilation message

shoes.cpp: In function 'void update(int, int)':
shoes.cpp:19:18: error: 'N' was not declared in this scope
     while(idx <= N){
                  ^
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:66:17: error: 'ans' was not declared in this scope
                 ans += (sum(i) - sum(q[x].front() - 1) - 1);
                 ^~~
shoes.cpp:66:17: note: suggested alternative: 'abs'
                 ans += (sum(i) - sum(q[x].front() - 1) - 1);
                 ^~~
                 abs
shoes.cpp:77:21: error: 'ans' was not declared in this scope
                     ans += (sum(i) - sum(q[x].front() - 1) - 2);
                     ^~~
shoes.cpp:77:21: note: suggested alternative: 'abs'
                     ans += (sum(i) - sum(q[x].front() - 1) - 2);
                     ^~~
                     abs
shoes.cpp:90:12: error: 'ans' was not declared in this scope
     return ans;
            ^~~
shoes.cpp:90:12: note: suggested alternative: 'abs'
     return ans;
            ^~~
            abs