QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625201 | #5113. Bridge | m107239# | WA | 115ms | 17456kb | C++20 | 4.5kb | 2024-10-09 17:54:42 | 2024-10-09 17:54:43 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
using ll = long long;
struct Splay {
struct Node {
int size;
int key;
int value;
Splay *splay;
Node *child[2], *parent;
};
Node *root = nullptr;
static int _side(Node *r) {
return r->parent ? r->parent->child[1] == r : 0;
}
static int _size(Node *r) {
return r ? r->size : 0;
}
static void _rotate(Node *r, int dir) {
Node *x = r->child[!dir];
r->child[!dir] = x->child[dir];
x->child[dir] = r;
if (r->parent) {
r->parent->child[_side(r)] = x;
} else {
r->splay->root = x;
x->splay = r->splay;
}
x->parent = r->parent;
r->parent = x;
if (r->child[!dir]) {
r->child[!dir]->parent = r;
}
}
static void _splay(Node *r) {
while (r->parent && r->parent->parent) {
if (_side(r) == _side(r->parent)) {
_rotate(r->parent->parent, !_side(r->parent));
} else {
_rotate(r->parent, !_side(r));
}
_rotate(r->parent, !_side(r));
}
if (r->parent) {
_rotate(r->parent, !_side(r));
}
}
void _drop(Node *r) {
if (r) {
_drop(r->child[0]);
_drop(r->child[1]);
delete r;
}
}
void clear() {
_drop(root);
root = nullptr;
}
void insert(Node *node) {
if (!root) {
root = node;
node->parent = nullptr;
node->child[0] = node->child[1] = nullptr;
node->splay = this;
return;
}
Node *ptr = root;
while (true) {
bool child = node->key > ptr->key;
if (!ptr->child[child]) {
ptr->child[child] = node;
node->parent = ptr;
node->child[0] = node->child[1] = nullptr;
_splay(node);
return;
}
ptr = ptr->child[child];
}
}
void find(int key) {
Node *max_node = nullptr, *ptr = root;
while (ptr) {
if (ptr->key <= key) {
max_node = ptr;
}
bool child = key > ptr->key;
ptr = ptr->child[child];
}
_splay(max_node);
}
int query() {
Node *ptr = root;
while (ptr->child[1]) {
ptr = ptr->child[1];
}
_splay(ptr);
return ptr->value;
}
void swap_right(Splay *rhs) {
swap(root->child[1], rhs->root->child[1]);
if (root->child[1]) {
root->child[1]->parent = root;
}
if (rhs->root->child[1]) {
rhs->root->child[1]->parent = rhs->root;
}
}
static Splay *find_splay(Node *node) {
_splay(node);
return node->splay;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
vector<Splay> country(n + 1);
vector<set<pair<int, Splay::Node *>>> swaps(n + 1);
vector<Splay::Node> splay_nodes(n + q * 2 + 1);
int node_ptr = 0;
for (int i = 1; i <= n; i++) {
Splay::Node *node = &splay_nodes[node_ptr++];
node->key = 0;
node->value = i;
country[i].insert(node);
swaps[i].insert({0, node});
}
while (q--) {
int op;
cin >> op;
if (op == 1) {
int a, b;
cin >> a >> b;
Splay::Node *node1 = &splay_nodes[node_ptr++];
Splay::Node *node2 = &splay_nodes[node_ptr++];
Splay::Node *xp = prev(swaps[a].lower_bound(pair<int, Splay::Node *>(b, 0)))->second;
Splay::Node *yp = prev(swaps[a + 1].lower_bound(pair<int, Splay::Node *>(b, 0)))->second;
Splay *x = Splay::find_splay(xp);
Splay *y = Splay::find_splay(yp);
swaps[a].insert({b, node2});
swaps[a + 1].insert({b, node1});
node1->key = b;
node2->key = a;
node1->value = a + 1;
node2->value = a;
x->insert(node1);
y->insert(node2);
x->swap_right(y);
} else {
int a;
cin >> a;
cout << country[a].query() << endl;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3860kb
input:
3 4 13 2 2 1 1 3 2 1 2 2 2 3 1 2 4 2 1 2 2 2 3 1 2 1 2 1 2 2 2 3
output:
2 2 1 3 3 1 2 3 2 1
result:
ok 10 numbers
Test #2:
score: -100
Wrong Answer
time: 115ms
memory: 17456kb
input:
3 100000 99997 2 2 2 2 2 3 2 3 2 3 2 3 2 3 1 2 11047 1 1 98732 1 2 90045 1 1 43556 2 1 2 3 1 2 17242 1 1 17027 2 1 1 1 94195 2 1 2 2 2 1 2 3 1 1 34124 1 2 14354 1 2 673 1 2 39812 1 2 35520 1 2 16046 2 3 2 2 1 1 25410 2 3 2 1 2 3 2 2 1 2 55684 2 1 1 2 24811 1 2 92268 1 2 60268 2 2 1 1 89272 1 2 19232...
output:
2 2 3 3 3 3 3 3 1 3 2 1 2 1 2 3 2 2 2 3 3 3 3 3 3 2 3 3 2 2 2 3 1 1 3 2 1 3 2 1 3 2 1 3 2 1 1 1 2 1 2 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 1 2 1 2 1 1 2 1 1 1 2 1 1 1 1 1 1 1 2 2 2 1 1 1 2 2 1 2 2 1 1 2 1 2 1 2 1 1 1 1 2 1 1 2 1 1 2 2 1 1 1 1 1 1 1 1 2 1 1 2 2 2 1 1 1 1 2 1 1 1 1 2 1 1 2 1 2 1 2 1 2 1 2 ...
result:
wrong answer 9th numbers differ - expected: '2', found: '1'