Created
January 24, 2019 16:08
-
-
Save rendon/a86801740a29ee786d1510fd72c5df0f to your computer and use it in GitHub Desktop.
Solution to https://open.kattis.com/problems/moviecollection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
template<typename T> | |
T read() { | |
T var; | |
std::cin >> var; | |
return var; | |
} | |
const int kMax = 100005; | |
int T[2*kMax]; | |
int P[kMax]; | |
inline int lowestOneBit(int i) { | |
return i & -i; | |
} | |
void update(int i, int delta) { | |
while (i < 2 * kMax) { | |
T[i] += delta; | |
i += lowestOneBit(i); | |
} | |
} | |
int read(int i) { | |
int sum = 0; | |
while (i > 0) { | |
sum += T[i]; | |
i -= lowestOneBit(i); | |
} | |
return sum; | |
} | |
int main() { | |
std::ios::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
const int tests = read<int>(); | |
for (int test = 1; test <= tests; test++) { | |
const int m = read<int>(); | |
const int r = read<int>(); | |
memset(T, 0, sizeof T); | |
int nextIndex = 2 * kMax - 1; | |
for (int v = m; v >= 1; v--) { | |
update(nextIndex, 1); | |
P[v] = nextIndex; | |
nextIndex--; | |
} | |
for (int i = 0; i < r; i++) { | |
const int a = read<int>(); | |
std::cout << read(P[a] - 1) << " "; | |
update(P[a], -1); | |
P[a] = nextIndex--; | |
update(P[a], +1); | |
} | |
std::cout << "\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment