QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#352086 | #8226. 堆操作练习题2 | JCY_ | 10 | 8ms | 47048kb | C++14 | 4.2kb | 2024-03-12 20:36:37 | 2024-03-12 20:36:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128;
using u128 = unsigned __int128;
template <typename T>
void chkmax(T &x, const T &y) {
if (x < y) x = y;
}
template <typename T>
void chkmin(T &x, const T &y) {
if (y < x) x = y;
}
template <typename T, typename cmp = less<T>>
struct deletable_heap {
priority_queue<T, vector<T>, cmp> pq1, pq2;
void push(const T &x) { pq1.emplace(x); }
void erase(const T &x) {
pq2.emplace(x);
while (!pq2.empty() && pq1.top() == pq2.top()) {
pq1.pop();
pq2.pop();
}
}
T top() const { return pq1.top(); }
bool empty() const { return pq1.empty(); }
};
template <typename T>
struct fenwick_tree {
T sum;
vector<T> bit, val;
void resize(int siz) {
bit.resize(siz);
val.resize(siz);
}
void update(int p, T x) {
val[p] += x;
sum += x;
for (++p; p <= (int)bit.size(); p += p & -p) bit[p - 1] += x;
}
T query(int p) const {
T ret = 0;
for (++p; p >= 1; p -= p & -p) ret += bit[p - 1];
return ret;
}
};
constexpr int MAXH = 18, MOD = 1e9 + 7;
int add(int x, int y) {
x += y;
return x >= MOD ? x - MOD : x;
}
int h, a[1 << MAXH], rk1[1 << MAXH][MAXH + 1], rk2[1 << MAXH][MAXH + 1];
int pw2[1 << MAXH];
pair<int, int> ord[1 << MAXH];
vector<int> ord1[1 << MAXH], ord2[1 << MAXH];
deletable_heap<int> pq[1 << MAXH];
fenwick_tree<int> tr[1 << MAXH];
void dfs(int u, int d) {
if (d == h - 1) {
ord1[u] = ord2[u] = {u};
} else {
int ls = u << 1, rs = u << 1 | 1, pl = 0, pr = 0;
dfs(ls, d + 1);
dfs(rs, d + 1);
ord1[u].reserve(ord1[ls].size() + ord1[rs].size() + 1);
ord2[u].reserve(ord1[ls].size() + ord1[rs].size() + 1);
ord2[u].emplace_back(u);
while (pl < (int)ord1[ls].size() || pr < (int)ord1[rs].size()) {
if (pl < (int)ord1[ls].size() && (pr == (int)ord1[rs].size() ||
a[ord1[ls][pl]] < a[ord2[rs][pr]])) {
ord1[u].emplace_back(ord1[ls][pl]);
ord2[u].emplace_back(ord2[ls][pl++]);
} else {
ord1[u].emplace_back(ord1[rs][pr]);
ord2[u].emplace_back(ord2[rs][pr++]);
}
}
ord1[u].emplace_back(u);
for (int i = 0; i < (int)ord1[u].size(); ++i) {
rk1[ord1[u][i]][d] = i;
rk2[ord2[u][i]][d] = i;
}
}
tr[u].resize(ord1[u].size());
}
bool in_tree(int u, int v) {
int cu = __builtin_clz(u), cv = __builtin_clz(v);
return cu <= cv && u >> (cv - cu) == v;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h;
for (int i = 1; i < 1 << h; ++i) {
cin >> a[i];
ord[i] = {a[i], i};
}
sort(ord + 1, ord + (1 << h));
dfs(1, 0);
pw2[0] = 1;
for (int i = 1; i < 1 << h; ++i) pw2[i] = add(pw2[i - 1], pw2[i - 1]);
int q;
cin >> q;
int s2 = 0;
while (q--) {
int typ, x, y;
cin >> typ >> x >> y;
if (typ == 1) {
if (y == 1) {
for (int i = x, d = __lg(x); i; i >>= 1, --d)
pq[i].push(a[ord1[i][rk2[x][d]]]);
} else {
++s2;
for (int i = x, d = __lg(x); i; i >>= 1, --d)
tr[i].update(rk2[x][d], 1);
}
} else if (typ == 2) {
if (y == 1) {
for (int i = x, d = __lg(x); i; i >>= 1, --d)
pq[i].erase(a[ord1[i][rk2[x][d]]]);
} else {
--s2;
for (int i = x, d = __lg(x); i; i >>= 1, --d)
tr[i].update(rk2[x][d], -1);
}
} else {
auto it = lower_bound(ord + 1, ord + (1 << h), make_pair(y, 0));
if (it == ord + (1 << h) || it->first != y || !in_tree(it->second, x) ||
(!pq[x].empty() && pq[x].top() > y)) {
cout << "0\n";
continue;
}
int u = it->second, d = __lg(x);
if (!pq[x].empty() && pq[x].top() == y) {
cout << pw2[s2 - tr[x].sum + tr[x].query(rk1[u][d])] << "\n";
} else {
cout << (tr[x].val[rk1[u][d]] ? pw2[s2 - tr[x].sum + tr[x].query(rk1[u][d]) - 1] : 0) << "\n";
}
}
}
return 0;
}
/*
g++ C.cpp -o C -std=c++14 -O2 -Wall -Wextra -Wshadow -g -fsanitize=address,undefined
*/
/*
2
10 9 3
4
1 3 1
1 2 2
1 1 1
3 1 9
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 3ms
memory: 46724kb
input:
2 3 2 1 50 3 3 1 1 1 2 1 2 1 2 2 1 1 2 2 2 1 2 1 1 1 1 3 2 2 1 1 2 2 2 3 1 2 3 1 3 2 3 2 1 3 2 1 2 2 2 2 2 1 2 1 1 1 2 3 1 1 2 1 2 1 1 1 2 1 1 3 1 2 3 1 3 2 3 2 1 3 2 2 2 1 1 2 1 1 1 1 3 1 2 2 1 1 1 1 1 3 3 1 2 1 1 2 3 2 1 3 1 2 3 1 1 1 2 3 1 3 2 1 2 3 3 1 3 1 3 3 1 1 3 1 1 1 3 2 1 1 1 2 1 1 3 1 1 2...
output:
0 1 0 0 0 2 0 1 2 0 1 0 0 0 0
result:
ok 15 numbers
Test #2:
score: 0
Accepted
time: 4ms
memory: 46688kb
input:
2 3 1 2 50 1 2 2 3 3 2 2 2 2 1 3 1 1 1 1 3 3 2 2 1 1 3 1 3 2 3 1 1 3 1 1 1 1 1 2 2 2 1 1 3 1 3 2 3 1 2 2 2 1 1 2 1 2 1 1 3 1 2 1 2 3 3 2 1 1 1 2 3 1 1 3 2 2 3 2 1 3 1 2 2 1 3 3 2 3 1 1 2 3 1 2 1 1 1 3 1 2 3 1 3 1 3 3 1 3 3 1 3 1 1 1 2 1 1 3 1 2 1 2 1 3 1 1 3 3 2 3 1 3 2 2 1 1 1 1 2 1 1 1 2 1 1 1 2 2...
output:
0 1 1 2 1 1 0 0 0 0 0 0 0 0 0
result:
ok 15 numbers
Subtask #2:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Test #3:
score: 0
Wrong Answer
time: 3ms
memory: 46680kb
input:
4 15 14 13 9 10 11 12 2 7 4 5 1 6 8 3 500 3 1 15 3 1 13 1 9 1 1 6 2 1 15 2 1 14 1 1 10 2 1 5 2 3 12 1 1 1 1 1 4 1 3 6 6 3 10 4 1 3 1 3 13 6 1 11 2 2 4 1 2 14 1 3 6 6 3 1 11 3 1 14 3 2 4 2 6 2 2 15 2 3 1 9 3 7 12 1 13 2 2 9 1 2 5 2 3 14 8 1 15 2 1 12 1 3 11 5 1 5 2 3 1 15 3 5 10 3 1 11 1 14 1 2 13 2 ...
output:
0 0 0 0 8 0 0 4 0 4 0 0 0 8 16 16 0 0 0 16 0 0 32 8 0 0 16 0 0 0 0 4 0 0 0 0 8 0 0 8 0 32 64 64 0 128 0 0 0 128 64 256 0 0 64 0 0 0 8 4 2 0 2 0 0 0 0 8 4 16 0 0 2 0 2 8 0 0 16 0 8 0 0 16 32 0 32 0 4 4 0 0 0 0 0 0 4 0 0 0 2 0 0 0 0 0 0 2 0 0 0 16 8 0 0 0 0 8 4 4 0 4 4 4 16 0 8 8 16 16 0 0 8 0 0 0 8 0...
result:
wrong answer 8th numbers differ - expected: '8', found: '4'
Subtask #3:
score: 0
Wrong Answer
Test #5:
score: 0
Wrong Answer
time: 8ms
memory: 47048kb
input:
9 511 509 510 504 507 505 508 501 503 506 502 494 500 499 493 473 483 495 475 491 497 461 487 490 489 498 496 478 485 480 488 378 469 482 477 462 448 422 470 424 467 421 492 439 454 484 451 376 385 458 464 463 486 411 472 449 474 459 468 479 413 457 455 371 315 432 437 466 453 476 418 433 363 434 38...
output:
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1 0 1 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 0 0 1 0 0 0 0 1 0 1 1 0 1 0 0 1 0 0 1 1 1 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0 0 1 1 ...
result:
wrong answer 17th numbers differ - expected: '1', found: '0'
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%