QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#410075 | #8338. Quad Kingdoms Chess | bigJ | WA | 34ms | 4648kb | C++20 | 4.3kb | 2024-05-13 11:09:16 | 2024-05-13 11:09:16 |
Judging History
answer
#include <bits/stdc++.h>
constexpr bool isprime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
constexpr int findPrime(int n) {
while (!isprime(n)) {
n++;
}
return n;
}
std::mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
const int P = findPrime(rnd() % 900000000 + 100000000);
const int B = findPrime(P / 2);
using i64 = long long;
constexpr i64 power(i64 base, i64 exp) {
i64 res{ 1 };
for (; exp; exp /= 2, base = base * base % P) {
if (exp % 2) {
res = (res * base % P);
}
}
return res;
}
const int R = power(B, P - 2);
constexpr int N = 100000;
int p[N + 1];
int q[N + 1];
struct Hash {
int val = 0;
int siz = 1;
Hash(int val = 0, int siz = 0) : val(val), siz(siz) {}
constexpr friend Hash operator+(const Hash& a, const Hash& b) {
return Hash((a.val + 1LL * b.val * p[a.siz] % P) % P, a.siz + b.siz);
}
constexpr friend Hash operator-(const Hash& a, const Hash& b) {
return Hash(i64(P - b.val + a.val) * q[b.siz] % P, a.siz - b.siz);
}
constexpr friend bool operator==(const Hash& a, const Hash& b) {
return a.val == b.val;
}
};
template <class Info>
struct SegmentTree {
int n;
std::vector<Info> tr;
std::vector<int> seq;
template<class T>
explicit SegmentTree(std::vector<T> init_) {
init(init_);
}
template<class T>
void init(std::vector<T> init_) {
n = init_.size();
tr.assign(4 << std::__lg(n), Info());
seq.assign(n, 0);
auto build = [&](auto&& self, int p, int l, int r) {
if (r - l == 1) {
tr[p] = init_[l];
seq[l] = init_[l];
return;
}
int m = std::midpoint(l, r);
self(self, 2 * p, l, m);
self(self, 2 * p + 1, m, r);
pull(p, m, r);
};
build(build, 1, 0, n);
}
Hash find(int p, int l, int r, int v) {
if (tr[p].max < v) {
return Hash();
}
if (seq[l] >= v) {
return tr[p].hash;
}
if (r - l == 1) {
return Hash();
}
int m = std::midpoint(l, r);
if (tr[2 * p].max < v) {
return find(2 * p + 1, m, r, v);
}
return find(2 * p, l, m, v) + (tr[p].hash - tr[2 * p].hash);
}
void pull(int p, int bl, int br) {
tr[p].max = std::max(tr[2 * p].max, tr[2 * p + 1].max);
tr[p].hash = tr[2 * p].hash + find(2 * p + 1, bl, br, tr[2 * p].max);
}
void modify(int p, int l, int r, int x, const Info& v) {
if (r - l == 1) {
tr[p] = v;
seq[l] = v.max;
return;
}
int m = std::midpoint(l, r);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p, m, r);
}
void modify(int x, const Info& v) {
return modify(1, 0, n, x, v);
}
};
struct Info {
int max;
Hash hash;
Info(int max, int hash, int siz) :max(max), hash(Hash(hash, siz)) {}
Info(int v = 0) : max(v), hash(Hash(1LL * v * B % P, 1)) {}
};
int main() {
p[0] = 1;
for (int i = 1; i <= N; i++) {
p[i] = (1LL * p[i - 1] * B) % P;
}
q[0] = 1;
for (int i = 1; i <= N; i++) {
q[i] = (1LL * q[i - 1] * R) % P;
}
int n1;
scanf("%d", &n1);
std::vector<int> a(n1);
for (int i = n1 - 1; i >= 0; i--) {
scanf("%d", &a[i]);
}
int n2;
scanf("%d", &n2);
std::vector<int> b(n2);
for (int i = n2 - 1; i >= 0; i--) {
scanf("%d", &b[i]);
}
SegmentTree<Info> sega(a);
SegmentTree<Info> segb(b);
int m;
for (scanf("%d", &m); m; m--) {
int o, x, y;
scanf("%d%d%d", &o, &x, &y);
if (o == 1) {
sega.modify(n1 - x, y);
} else {
segb.modify(n2 - x, y);
}
puts(sega.tr[1].hash == segb.tr[1].hash ? "YES" : "NO");
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 4484kb
input:
5 1 2 3 4 5 5 5 4 3 2 1 8 1 1 5 1 4 2 1 2 4 1 5 1 1 5 5 2 1 4 2 3 5 2 5 5
output:
NO NO NO YES NO NO NO YES
result:
ok 8 tokens
Test #2:
score: 0
Accepted
time: 30ms
memory: 4648kb
input:
1 2 6 2 1 1 1 1 1 200000 2 6 2 1 1 1 1 1 1 1 1 2 2 1 1 1 1 2 1 1 1 2 4 1 2 1 2 1 1 1 1 1 2 2 5 1 1 1 1 1 1 2 1 1 1 2 6 1 1 1 2 1 1 2 1 1 2 2 3 1 1 1 1 2 1 1 2 6 2 1 1 2 2 4 1 1 1 2 2 6 1 1 1 2 1 1 1 2 5 2 2 6 2 1 1 1 2 4 2 2 5 2 2 6 2 1 1 1 2 5 1 2 6 2 1 1 2 1 1 1 1 1 1 2 4 1 1 1 2 1 1 2 1 1 2 2 3 2...
output:
NO NO NO NO YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO N...
result:
ok 200000 tokens
Test #3:
score: 0
Accepted
time: 34ms
memory: 4336kb
input:
6 2 1 1 2 1 2 1 1 200000 2 1 1 1 1 2 1 1 1 2 1 2 2 1 1 2 1 1 2 1 2 2 1 2 1 1 2 1 3 1 1 6 2 1 5 2 1 4 2 1 3 1 2 1 2 1 4 2 1 4 2 2 1 2 2 1 2 1 3 1 1 6 1 1 1 2 2 1 1 1 6 1 1 3 1 1 5 2 1 6 2 1 5 2 2 1 2 1 2 1 1 5 2 2 1 1 2 1 1 1 6 1 2 1 2 2 1 1 1 3 2 2 1 1 1 6 1 1 4 2 1 2 1 1 1 1 2 1 1 1 2 1 1 6 2 1 6 2...
output:
NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO ...
result:
ok 200000 tokens
Test #4:
score: 0
Accepted
time: 32ms
memory: 4384kb
input:
6 1 3 1 2 1 2 6 2 1 3 3 3 1 200000 2 4 2 1 2 1 1 6 2 2 3 2 1 1 1 1 6 2 1 6 2 1 3 2 2 6 1 2 4 3 1 1 2 2 5 2 2 6 2 2 3 1 1 4 3 1 3 1 2 5 2 2 4 2 2 1 3 1 1 1 2 2 2 2 4 2 1 5 3 1 6 3 2 6 3 1 5 3 2 5 3 2 4 1 2 4 2 1 1 2 1 6 1 2 6 1 1 2 3 1 1 3 1 1 1 2 6 3 2 4 1 1 4 2 2 2 1 1 3 1 1 1 3 1 1 3 1 4 3 1 3 3 2...
output:
NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES YES YES NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO N...
result:
ok 200000 tokens
Test #5:
score: 0
Accepted
time: 31ms
memory: 4484kb
input:
4 1 1 2 2 1 1 200000 2 1 2 1 1 3 1 2 1 2 1 2 2 1 2 2 1 3 2 1 1 2 1 1 1 4 2 2 1 3 1 3 1 1 2 1 1 3 2 2 1 1 1 4 2 2 1 1 2 1 2 1 1 1 2 1 2 2 1 1 1 4 1 1 2 2 2 1 1 1 1 1 1 4 3 2 1 3 1 1 3 2 1 2 1 2 1 2 1 1 2 1 1 2 1 1 2 1 3 2 1 2 1 2 3 2 1 3 1 3 3 2 1 1 1 2 3 2 1 2 1 1 2 2 1 2 2 1 3 1 2 1 1 4 3 2 1 2 2 1...
output:
NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO YES NO NO NO NO YES YES NO NO NO NO NO NO NO NO NO N...
result:
ok 200000 tokens
Test #6:
score: -100
Wrong Answer
time: 31ms
memory: 4492kb
input:
2 4 2 4 2 3 3 2 200000 1 1 2 1 2 2 1 2 2 1 2 4 1 1 2 1 2 4 2 2 1 1 1 3 2 3 2 2 3 1 2 2 4 2 3 2 2 2 1 1 1 4 1 2 4 2 2 1 1 1 2 2 1 2 2 4 4 1 2 4 1 2 3 2 4 4 2 3 1 2 1 3 2 3 1 2 2 1 1 1 2 2 2 1 1 2 3 1 2 2 1 2 1 1 2 4 1 2 4 1 1 3 1 1 4 2 1 3 1 1 1 1 2 4 2 1 2 2 3 4 1 2 4 2 2 1 1 2 1 2 3 2 1 2 3 2 3 3 1...
output:
NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES NO NO NO NO NO NO NO NO NO NO YES YES YES YES NO NO YES YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO ...
result:
wrong answer 31st words differ - expected: 'NO', found: 'YES'