Submission #50856104
Source Code Expand
#include <bits/stdc++.h>
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using int64 = long long;
using uint = unsigned int;
using uint64 = unsigned long long;
bool ckmin(auto& a, auto b) { return b < a ? a = b, 1 : 0; }
bool ckmax(auto& a, auto b) { return b > a ? a = b, 1 : 0; }
using namespace std;
template <typename Info>
struct SegmentTree {
struct Node : public Info {
int l, r, ts;
Node *lch, *rch;
Node(int l_ = 0, int r_ = 0, int ts_ = 0)
: l(l_), r(r_), ts(ts_), lch(nullptr), rch(nullptr) {}
Node(Node* x, int ts_ = 0) {
*this = *x;
this->ts = ts_;
}
};
int l_range, r_range;
Node* root;
bool persist;
int ts;
SegmentTree(int l, int r, bool persist_ = false)
: l_range(l), r_range(r), root(nullptr), persist(persist_), ts(0) {
assert(l_range <= r_range);
}
void PushDown(Node* x) {
if (x->NeedPushDown()) {
if (persist) {
if (x->lch && x->lch->ts != ts) x->lch = new Node(x->lch, ts);
if (x->rch && x->rch->ts != ts) x->rch = new Node(x->rch, ts);
}
x->PushDown();
}
}
template <typename F>
Node* Modify(int from, int to, F f) {
assert(l_range <= from && from <= to && to <= r_range);
std::function<Node*(Node*, int, int)> Modify = [&](Node* x, int l, int r) {
if (!x) {
x = new Node(l, r, ts);
} else if (persist && x->ts != ts) {
x = new Node(x, ts);
}
if (from <= x->l && x->r <= to) {
f(x);
return x;
}
PushDown(x);
int m = l + (r - l) / 2;
if (from <= m) x->lch = Modify(x->lch, l, m);
if (to > m) x->rch = Modify(x->rch, m + 1, r);
x->Update();
return x;
};
root = Modify(root, l_range, r_range);
return root;
}
template <typename F>
Node* ModifyPath(int p, F f) {
assert(l_range <= p && p <= r_range);
std::function<Node*(Node*, int, int)> Dfs = [&](Node* x, int l, int r) {
if (!x) {
x = new Node(l, r, ts);
} else if (persist && x->ts != ts) {
x = new Node(x, ts);
}
f(x);
if (x->l == x->r) return x;
PushDown(x);
int m = l + (r - l) / 2;
if (p <= m) x->lch = Dfs(x->lch, l, m);
else x->rch = Dfs(x->rch, m + 1, r);
x->Update();
return x;
};
root = Dfs(root, l_range, r_range);
return root;
}
std::vector<Node*> GetAllNodes(int from, int to) {
assert(l_range <= from && from <= to && to <= r_range);
std::vector<Node*> all;
std::function<void(Node*, int, int)> Dfs = [&](Node* x, int l, int r) {
if (!x) return;
if (from <= l && r <= to) {
all.push_back(x);
return;
}
PushDown(x);
int m = l + (r - l) / 2;
if (from <= m) Dfs(x->lch, l, m);
if (to > m) Dfs(x->rch, m + 1, r);
};
Dfs(root, l_range, r_range);
return all;
}
std::vector<Node*> GetPathNodes(int i) {
assert(l_range <= i && i <= r_range);
std::vector<Node*> all;
Node* x = root;
while (x) {
all.push_back(x);
if (x->l == x->r) break;
int mid = (x->l + x->r) / 2;
if (i <= mid) x = x->lch;
else x = x->rch;
}
return all;
}
template <typename F>
int GetFirstAfter(int p, F f) {
assert(l_range <= p && p <= r_range);
auto nodes = GetAllNodes(p, r_range);
for (auto x : nodes) {
if (f(x)) {
while (x->r > x->l) {
assert(f(x));
PushDown(x);
if (x->lch && f(x->lch)) {
x = x->lch;
} else {
x = x->rch;
}
}
return x->l;
}
}
return -1;
}
template <typename F>
int GetLastBefore(int p, F f) {
assert(l_range <= p && p <= r_range);
auto nodes = GetAllNodes(l_range, p);
reverse(nodes.begin(), nodes.end());
for (auto x : nodes) {
if (f(x)) {
while (x->r > x->l) {
assert(f(x));
PushDown(x);
if (x->rch && f(x->rch)) {
x = x->rch;
} else {
x = x->lch;
}
}
return x->l;
}
}
return -1;
}
template <typename F>
void TraverseLeaf(Node* x, F f) {
if (!x) return;
if (x->l == x->r) {
f(x);
return;
}
PushDown(x);
TraverseLeaf(x->lch, f);
TraverseLeaf(x->rch, f);
}
template <typename F>
void TraverseAllNode(Node* x, F f) {
if (!x) return;
f(x);
if (x->l < x->r) PushDown(x);
TraverseAllNode(x->lch, f);
TraverseAllNode(x->rch, f);
}
template <typename F>
static Node* BuildTree(int l, int r, F f) {
Node* x = new Node(l, r);
if (l == r) {
f(l, x);
return x;
}
int m = l + (r - l) / 2;
x->lch = BuildTree(l, m, f);
x->rch = BuildTree(m + 1, r, f);
x->Update();
return x;
}
void GC(Node* x) {
if (!x) return;
GC(x->lch);
GC(x->rch);
delete x;
}
};
struct Info {
multiset<int> data;
auto Node() { return reinterpret_cast<SegmentTree<Info>::Node*>(this); }
Info() {}
bool NeedPushDown() { return false; }
void PushDown() {}
void Update() {}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
SegmentTree<Info> seg(0, n - 1);
seg.root = seg.BuildTree(0, n - 1,
[&](int i, Info* info) { info->data.insert(a[i]); });
int q;
cin >> q;
vector<array<int, 3>> query(q);
for (int i = 0; i < q; i++) {
int op, l, r, x;
cin >> op;
if (op == 1) {
cin >> l >> r >> x;
l--, r--;
query[i] = {l, r, x};
seg.Modify(l, r, [&](Info* info) { info->data.insert(x); });
} else if (op == 2) {
cin >> x;
x--;
seg.Modify(query[x][0], query[x][1], [&](Info* info) {
info->data.erase(info->data.find(query[x][2]));
});
} else {
cin >> x;
x--;
int ans = 0;
for (auto node : seg.GetPathNodes(x)) {
if (!node->data.empty()) {
ckmax(ans, *prev(node->data.end()));
}
}
cout << ans << '\n';
}
}
return 0;
}
Submission Info
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
625 / 625 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00_sample_00.txt, 00_sample_01.txt |
All |
00_sample_00.txt, 00_sample_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 02_handmade_35.txt, 02_handmade_36.txt, 02_handmade_37.txt, 02_handmade_38.txt, 02_handmade_39.txt, 02_handmade_40.txt, 02_handmade_41.txt, 02_handmade_42.txt, 02_handmade_43.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_00.txt |
AC |
1 ms |
3368 KiB |
00_sample_01.txt |
AC |
1 ms |
3504 KiB |
01_random_02.txt |
AC |
309 ms |
53776 KiB |
01_random_03.txt |
AC |
294 ms |
53536 KiB |
01_random_04.txt |
AC |
39 ms |
20920 KiB |
01_random_05.txt |
AC |
166 ms |
53216 KiB |
01_random_06.txt |
AC |
182 ms |
53180 KiB |
01_random_07.txt |
AC |
90 ms |
40044 KiB |
01_random_08.txt |
AC |
227 ms |
53236 KiB |
01_random_09.txt |
AC |
240 ms |
53260 KiB |
01_random_10.txt |
AC |
47 ms |
7016 KiB |
01_random_11.txt |
AC |
1455 ms |
195776 KiB |
01_random_12.txt |
AC |
1483 ms |
195868 KiB |
01_random_13.txt |
AC |
380 ms |
84168 KiB |
01_random_14.txt |
AC |
362 ms |
53748 KiB |
01_random_15.txt |
AC |
322 ms |
53204 KiB |
01_random_16.txt |
AC |
154 ms |
22624 KiB |
01_random_17.txt |
AC |
746 ms |
125352 KiB |
01_random_18.txt |
AC |
719 ms |
125780 KiB |
01_random_19.txt |
AC |
591 ms |
100520 KiB |
01_random_20.txt |
AC |
158 ms |
53268 KiB |
01_random_21.txt |
AC |
164 ms |
53232 KiB |
01_random_22.txt |
AC |
11 ms |
6796 KiB |
01_random_23.txt |
AC |
321 ms |
53464 KiB |
01_random_24.txt |
AC |
357 ms |
53484 KiB |
01_random_25.txt |
AC |
117 ms |
23588 KiB |
01_random_26.txt |
AC |
698 ms |
126352 KiB |
01_random_27.txt |
AC |
746 ms |
126632 KiB |
01_random_28.txt |
AC |
651 ms |
101236 KiB |
01_random_29.txt |
AC |
1713 ms |
200236 KiB |
01_random_30.txt |
AC |
1636 ms |
200228 KiB |
01_random_31.txt |
AC |
513 ms |
113532 KiB |
01_random_32.txt |
AC |
160 ms |
53404 KiB |
01_random_33.txt |
AC |
167 ms |
53276 KiB |
01_random_34.txt |
AC |
65 ms |
32088 KiB |
02_handmade_35.txt |
AC |
537 ms |
131384 KiB |
02_handmade_36.txt |
AC |
131 ms |
57736 KiB |
02_handmade_37.txt |
AC |
62 ms |
55268 KiB |
02_handmade_38.txt |
AC |
62 ms |
55364 KiB |
02_handmade_39.txt |
AC |
78 ms |
57692 KiB |
02_handmade_40.txt |
AC |
78 ms |
57724 KiB |
02_handmade_41.txt |
AC |
101 ms |
62428 KiB |
02_handmade_42.txt |
AC |
600 ms |
131384 KiB |
02_handmade_43.txt |
AC |
1 ms |
3496 KiB |