QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#352047 | #8226. 堆操作练习题2 | JCY_ | 0 | 4ms | 47056kb | C++14 | 4.2kb | 2024-03-12 19:59:38 | 2024-03-12 19:59: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>
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, greater<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
3 2 1
4
1 2 2
1 3 2
1 1 1
3 1 3
*/
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 46692kb
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 1 1 2 0 1 0 0 0 0
result:
wrong answer 7th numbers differ - expected: '0', found: '1'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #5:
score: 0
Wrong Answer
time: 4ms
memory: 47056kb
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 0 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 ...
result:
wrong answer 17th numbers differ - expected: '1', found: '0'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%