// g++ -std=c++14
/*
Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.
- Tooru Oikawa.
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double lld;
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define all(c) c.begin(),c.end()
#define pii pair<int, int>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r)
{
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}
template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}
template <typename T>
ostream &operator<<(ostream &out, set<T> const &v) {
for (auto i = v.begin(); i != v.end(); i++)
out << (*i) << ' ';
return out;
}
template <typename T>
ostream &operator<<(ostream &out, multiset<T> const &v) {
for (auto i = v.begin(); i != v.end(); i++)
out << (*i) << ' ';
return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, map<T, V> const &v) {
for (auto i = v.begin(); i != v.end(); i++)
out << "\n" << (i->first) << ":" << (i->second);
return out;
}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
int n, m;
void no() {
cout << "NE" << endl;
}
void yes() {
cout << "DA" << endl;
}
const int inf = 1e9 + 1;
map<int, int> bridge;
int get_after(int a, set<int> &s) {
auto itr = s.lower_bound(a);
if (itr == s.end()) return -1;
else {
return (*itr);
}
}
int get_before(int a, set<int> &s) {
auto itr = s.lower_bound(a);
if (itr == s.begin()) {
if (*itr == a) return a;
else return -1;
} else if (itr == s.end()) {
itr--;
if (*itr <= a) return *itr;
else return -1;
} else {
if (*itr == a) return a;
else {
itr--;
if (*itr <= a) return *itr;
else return -1;
}
}
}
set<int> bb;
set<int> nb, sb;
bool reach(int a, int b) {
// tr(a, b);
if (a == b) return 1;
if (a <= n && b <= n) {
if (b > a) {
int x = get_after(a, bb);
if (x == -1 || x >= b) return 1;
else return 0;
} else {
int x = get_after(a, nb);
int y = get_after(a, bb);
if (y == -1) y = inf;
if (x == -1 || y < x) return 0;
else {
int opp = bridge[x];
if (reach(opp, b)) return 1;
else return 0;
}
}
} else if (a > n && b > n) {
if (b < a) {
int x = get_before(a, bb);
if (x == -1 || x <= b) return 1;
else return 0;
} else {
int x = get_before(a, sb);
int y = get_before(a, bb);
if (x == -1 || y > x) return 0;
else {
int opp = bridge[x];
if (reach(opp, b)) return 1;
else return 0;
}
}
} else if (a <= n && b > n) {
int x = get_after(b, sb);
int y = get_after(b, bb);
int z = get_after(a, nb);
if (y == -1) y = inf;
if (x == -1 || y < x) return 0;
else {
int opp = bridge[x];
opp = max(opp, z);
if (reach(a, opp)) return 1;
else return 0;
}
} else {
int x = get_before(b, nb);
int y = get_before(b, bb) + 1;
if (x == -1 || (y > x && y < b)) return 0;
else {
int opp = bridge[x];
if (reach(a, opp)) return 1;
else return 0;
}
}
}
int solve() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
char c;
int g1, g2;
cin >> c;
cin >> g1 >> g2;
if (c == 'Q') {
if (reach(g1, g2)) yes();
else no();
} else if (c == 'A') {
if (g1 > g2) {
swap(g1, g2);
}
sb.insert(g2);
nb.insert(g1);
bridge[g1] = g2;
bridge[g2] = g1;
} else {
if (g1 > g2) {
swap(g1, g2);
}
if (g1 > n) {
bb.insert(g2);
} else {
bb.insert(g1);
}
}
}
return 0;
}
int32_t main(){ _
int t;
// cin >> t;
t = 1;
while (t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
392 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
5 |
Incorrect |
6 ms |
384 KB |
Output isn't correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Incorrect |
283 ms |
16856 KB |
Output isn't correct |
8 |
Incorrect |
284 ms |
16936 KB |
Output isn't correct |
9 |
Incorrect |
230 ms |
15276 KB |
Output isn't correct |
10 |
Incorrect |
364 ms |
22356 KB |
Output isn't correct |