Submission #1340594

#TimeUsernameProblemLanguageResultExecution timeMemory
1340594NikoBaoticMostovi (COI14_mostovi)C++20
50 / 100
330 ms12468 KiB
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

#define all(x) x.begin(), x.end()
#define sz(x) ((int) ((x).size()))
#define pb push_back
#define F first
#define S second
#define FIO ios_base::sync_with_stdio(false); cin.tie(0)

ll n, m;
set<pii> b;
set<ll> bl1;
set<ll> bl2;

bool check(ll x, ll y, bool c1, bool c2);

ll findFirst(ll x) {
	if (x <= n) {
		auto iter = b.lower_bound(make_pair(x, 0));

		if (iter == b.end()) return x;

		if (check(x, iter->F, 0, 0)) return iter->S;
	} else {
		auto iter = b.lower_bound(make_pair(x + 1, 0));

		if (iter == b.begin()) return x;

		if (check(x, prev(iter)->F, 0, 0)) return prev(iter)->S;
	}

	return x;
}

ll findFirstBl(ll x) {
	if (x <= n) {
		auto iter = bl1.lower_bound(x);
		
		if (iter == bl1.end()) return n;
		return *iter;
	} else {
		auto iter = bl2.lower_bound(x + 1);

		if (iter == bl2.begin()) return n + 1;
		return *prev(iter);
	}
}

ll findLast(ll x) {
	ll bl = findFirstBl(x);

	if (x <= n) {
		auto iter = b.lower_bound(make_pair(bl + 1, 0));

		if (iter == b.begin()) return x;

		if (check(x, prev(iter)->F, 0, 0)) return prev(iter)->S;
	} else {
		auto iter = b.lower_bound(make_pair(bl + 1, 0));

		if (iter == b.end()) return x;

		if (check(x, iter->F, 0, 0)) return iter->S;
	}

	 return x;
}

ll findFirstBef(ll x) {
	if (x <= n) {
		auto iter = b.lower_bound(make_pair(x + 1, 0));

		if (iter == b.begin()) return x;

		if (check(prev(iter)->F, x, 0, 0)) return prev(iter)->S;
	} else {
		auto iter = b.lower_bound(make_pair(x + 1, 0));

		if (iter == b.begin()) return x;

		if (check(prev(iter)->F, x, 0, 0)) return prev(iter)->S;
	}

	return x;
}

ll findFirstBlBef(ll x) {
	if (x <= n) {
		auto iter = bl1.lower_bound(x + 1);

		if (iter == bl1.begin()) return 0;
		return *prev(iter);
	} else {
		auto iter = bl2.lower_bound(x + 1);

		if (iter == bl2.end()) return n * 2 + 1;
		return *iter;
	}
}

ll findLastBef(ll x) {
	ll br = findFirstBlBef(x);

	if (x <= n) {
		auto iter = b.lower_bound(make_pair(br + 1, 0));

		if (iter == b.end()) return x;

		if (check(iter->F, x, 0, 0)) return iter->S;
	} else {
		auto iter = b.lower_bound(make_pair(br, 0));

		if (iter == b.begin()) return x;
		
		if (check(prev(iter)->F, x, 0, 0)) return prev(iter)->S;
	}

	return x;
}

bool check(ll x, ll y, bool c1 = 1, bool c2 = 1) {
	if (max(x, y) <= n and x <= y) {
		auto iter = bl1.lower_bound(x);
	   
		if (iter == bl1.end() or *iter >= y) return 1;
	}

	if (min(x, y) > n and x >= y) {
		auto iter = bl2.lower_bound(y + 1); 

		if (iter == bl2.end() or *iter > x) return 1;
	}

	if (c1) {
		if (check(findFirst(x), y, 0, c2)) return 1;
		if (check(findLast(x), y, 0, c2)) return 1;
	}

	if (c2) {
		if (check(x, findFirstBef(y), c1, 0)) return 1;
		if (check(x, findLastBef(y), c1, 0)) return 1;
	}

	return 0;
}

int main() {
	FIO;

	cin >> n >> m;

	for (int i = 0; i < m; i++) {
		ll x, y;
		char c;
		cin >> c >> x >> y;

		if (c == 'A') {
			b.insert({x, y});
			b.insert({y, x});
		} else if (c == 'B') {
			if (x > y) swap(x, y);

			if (y <= n) {
				bl1.insert(x);	
			} else {
				bl2.insert(y);
			}
		} else {
			cout << (check(x, y) ? "DA" : "NE") << endl;
		}
	}

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...