QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#608320 | #9356. LRU Algorithm | huabin | WA | 73ms | 3716kb | C++23 | 5.4kb | 2024-10-03 20:40:01 | 2024-10-03 20:40:01 |
Judging History
answer
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <compare>
namespace cave {
template <typename T> int len(const T &x) { return (int)x.size(); }
template <typename T, int S> int len(const T (&)[S]) { return S; }
[[maybe_unused]] static inline constexpr struct __MAX_Kind {
constexpr __MAX_Kind() {}
__MAX_Kind(const __MAX_Kind &&) = delete;
__MAX_Kind(const __MAX_Kind &) = delete;
__MAX_Kind &operator=(const __MAX_Kind &&) = delete;
__MAX_Kind &operator=(const __MAX_Kind &) = delete;
template <typename T> constexpr operator T() const { return std::numeric_limits<T>::max(); }
template <typename T> constexpr bool operator==(T x) const { return x == std::numeric_limits<T>::max(); }
template <typename T> constexpr bool operator!=(T x) const { return x != std::numeric_limits<T>::max(); }
template <typename T> constexpr bool operator<(T x) const { return x < std::numeric_limits<T>::max(); }
template <typename T> constexpr bool operator<=(T x) const { return x <= std::numeric_limits<T>::max(); }
template <typename T> constexpr bool operator>(T x) const { return x > std::numeric_limits<T>::max(); }
template <typename T> constexpr bool operator>=(T x) const { return x >= std::numeric_limits<T>::max(); }
} MAX;
[[maybe_unused]] static inline constexpr struct __MIN_Kind {
constexpr __MIN_Kind() {}
__MIN_Kind(const __MIN_Kind &&) = delete;
__MIN_Kind(const __MIN_Kind &) = delete;
__MIN_Kind &operator=(const __MIN_Kind &&) = delete;
__MIN_Kind &operator=(const __MIN_Kind &) = delete;
template <typename T> constexpr operator T() const { return std::numeric_limits<T>::min(); }
template <typename T> constexpr bool operator==(T x) const { return x == std::numeric_limits<T>::min(); }
template <typename T> constexpr bool operator!=(T x) const { return x != std::numeric_limits<T>::min(); }
template <typename T> constexpr bool operator<(T x) const { return x < std::numeric_limits<T>::min(); }
template <typename T> constexpr bool operator<=(T x) const { return x <= std::numeric_limits<T>::min(); }
template <typename T> constexpr bool operator>(T x) const { return x > std::numeric_limits<T>::min(); }
template <typename T> constexpr bool operator>=(T x) const { return x >= std::numeric_limits<T>::min(); }
} MIN;
} // namespace cave
using ll = long long;
using ull = unsigned long long;
using namespace std;
using Byte = signed char;
using namespace cave;
static void prepare();
static void solve();
int main(void) {
cin.tie(nullptr), cout.tie(nullptr);
ios::sync_with_stdio(false);
prepare();
ll t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
struct Hash {
ull a, b;
Hash() : a(0), b(0) {}
Hash(ull single) : a(single), b(single) {}
Hash(ull a, ull b) : a(a), b(b) {}
explicit Hash(mt19937_64 &rng) : a(rng()), b(rng()) {}
Hash operator+(const Hash &x) const { return {a + x.a, b + x.b}; }
Hash operator-(const Hash &x) const { return {a - x.a, b - x.b}; }
Hash operator*(const Hash &x) const { return {a * x.a, b + x.b}; }
Hash operator^(const Hash &x) const { return {a ^ x.a, b ^ x.b}; }
bool operator==(const Hash &x) const { return a == x.a && b == x.b; }
bool operator<(const Hash &x) const { return tie(a, b) < tie(x.a, x.b); }
};
ull WEIGHT[5001];
static void prepare() {
mt19937_64 rng(random_device{}());
for (auto &x : WEIGHT) {
x = rng();
}
}
struct LinkedNode {
int prev, next, linked;
};
static void solve() {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<Hash> query(q);
vector<LinkedNode> list(n + 1);
auto gh = [&](ull x, ull y) -> Hash {
return Hash(x * (y ^ 347854355345677998), 6 * x + y);
};
for (int i = 0; i < q; i++) {
memset(list.data(), 0, sizeof(LinkedNode) * (n + 1));
int m;
cin >> m;
Hash hash;
int p = 0;
bool ignore = false;
for (int i = 0; i < m; i++) {
int x;
cin >> x;
if (x == 0) {
ignore = true;
continue;
}
if (ignore) {
continue;
}
hash = hash + gh(WEIGHT[p], WEIGHT[x]);
p = x;
}
hash = hash + gh(WEIGHT[p], WEIGHT[0]);
Hash curr_hash = gh(WEIGHT[0], WEIGHT[0]);
int curr = 0;
for (auto x : a) {
if (list[x].linked) {
curr_hash = curr_hash - gh(WEIGHT[list[x].prev], WEIGHT[x]) - gh(WEIGHT[x], WEIGHT[list[x].next]) + gh(WEIGHT[list[x].prev], WEIGHT[list[x].next]);
list[list[x].prev].next = list[x].next;
list[list[x].next].prev = list[x].prev;
list[x].linked = false;
curr--;
}
if (curr == m) {
int d = list[0].prev;
curr_hash = curr_hash - gh(WEIGHT[d], WEIGHT[0]) - gh(WEIGHT[list[d].prev], WEIGHT[d]) + gh(WEIGHT[list[d].prev], WEIGHT[0]);
list[list[d].prev].next = 0;
list[0].prev = list[d].prev;
list[d].linked = false;
curr--;
}
list[x].prev = 0;
list[x].next = list[0].next;
list[list[0].next].prev = x;
list[0].next = x;
list[x].linked = true;
curr_hash = curr_hash + gh(WEIGHT[list[x].prev], WEIGHT[x]) + gh(WEIGHT[x], WEIGHT[list[x].next]) - gh(WEIGHT[list[x].prev], WEIGHT[list[x].next]);
curr++;
if (curr_hash == hash) {
cout << "Yes" << endl;
goto end;
}
}
cout << "No" << endl;
end:;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3636kb
input:
1 7 5 4 3 4 2 3 1 4 1 4 2 2 3 3 3 2 1 4 4 1 3 2 4 3 4 0 0
output:
Yes No No Yes Yes
result:
ok 5 lines
Test #2:
score: -100
Wrong Answer
time: 73ms
memory: 3716kb
input:
105 50 10 23 30 34 20 27 11 21 29 12 7 21 42 45 48 8 15 6 16 19 35 16 14 29 11 31 18 22 4 45 22 14 4 13 40 43 48 27 15 15 15 15 10 15 11 31 37 34 34 50 14 1 25 2 23 6 3 29 21 11 4 12 29 18 39 5 29 21 11 27 20 6 21 10 9 3 34 14 7 49 36 36 43 50 50 35 8 12 29 21 11 27 20 34 30 9 11 27 20 34 30 23 0 0 ...
output:
No No Yes No Yes No No Yes Yes No Yes Yes Yes Yes No Yes No No No Yes Yes Yes No No No No Yes Yes Yes No No No No Yes Yes Yes No No No No No No No No Yes No No No Yes Yes Yes Yes No No Yes No No No Yes Yes Yes Yes No No Yes No No No Yes No Yes No Yes No Yes No No No Yes Yes Yes No No No No No No Yes...
result:
wrong answer 44th lines differ - expected: 'Yes', found: 'No'