#include<bits/stdc++.h>
using namespace std;
int n, m;
set<int> bl;
set< array<int, 2> > sx, sy;
array<int, 2> getR(int x, int y) {
auto it = sx.lower_bound({x, -1});
if (it != sx.end() && (*it)[1] >= y) {
return *it;
}
it = sy.lower_bound({y, -1});
if (it != sy.end() && (*it)[1] >= x) {
return {(*it)[1], (*it)[0]};
}
return {-1, -1};
}
array<int, 2> getL(int x, int y) {
auto it = sx.lower_bound({x + 1, -1});
if (it != sx.begin()) {
it--;
if ((*it)[1] <= y) {
return *it;
}
}
it = sy.lower_bound({y + 1, -1});
if (it != sy.begin()) {
it--;
if ((*it)[1] <= x) {
return {(*it)[1], (*it)[0]};
}
}
return {-1, -1};
}
bool chk(int x, int y) {
if (!(~x) || !(~y)) {
return false;
}
if (x == y) {
return true;
}
if (x < n && y < n) {
if (x < y) {
auto it = bl.lower_bound(x);
return it == bl.end() || *it >= y;
}
else {
auto m = getR(x, -1);
return (chk(x, m[0]) && chk(m[1], y));
}
}
if (x >= n && y >= n) {
if (x > y) {
auto it = bl.lower_bound(y);
return it == bl.end() || *it >= x;
}
else {
auto m = getL(n, x);
return chk(x, m[1]) && chk(m[0], y);
}
}
if (x < n) {
auto m = getR(x, y);
return chk(x, m[0]) && chk(m[1], y);
}
else {
auto m = getL(y, x);
return chk(x, m[1]) && chk(m[0], y);
}
return false;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
char type; int x, y; cin >> type >> x >> y;
x--; y--;
if (type == 'Q') {
cout << (chk(x, y) ? "DA": "NE") << "\n";
}
else {
if (x > y) {
swap(x, y);
}
if (type == 'A') {
sx.insert({x, y}); sy.insert({y, x});
}
else {
bl.insert(x);
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
131 ms |
12040 KB |
Output is correct |
8 |
Correct |
141 ms |
12076 KB |
Output is correct |
9 |
Correct |
132 ms |
12068 KB |
Output is correct |
10 |
Correct |
186 ms |
14332 KB |
Output is correct |