1293C - NEKO's Maze Game - CodeForces Solution


constructive algorithms implementation *1400

Please click on ads to support us..

C++ Code:

#define mod 1000000007
#define ll long long
#define ull unsigned long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define pb push_back
#define ff first
#define ss second
#define N 100005
// #include <boost/multiprecision/cpp_int.hpp> 
#include <bits/stdc++.h> 
//#include <unordered_map>
#define endl "\n"
using namespace std;
ll gcd(ll a, ll b) { if (a > b)swap(a, b);  if (a == 0)  return b; return gcd(b % a, a); }
int pow2(ll n) { ll c = 0; while (n % 2 == 0 && n != 0) { c++; n /= 2; } return c; }
ll m_mod(ll x, ll y) { return ((x % mod) * (y % mod)) % mod; }


ll power(ll x, ll y, ll M)
{
    if (y == 0)
        return 1;
 
    int p = power(x, y / 2, M) % M;
    p = (p * p) % M;
 
    return (y % 2 == 0) ? p : (x * p) % M;
}
ll modInverse(ll A, ll M)
{
    return power(A, M - 2, M);
    
}



void solve()
{
  ll n,q;
  cin >> n >> q;
  set<pair<ll,ll> > s;
  vector<pair<ll,ll> > v;
  for(ll i = 0;i<q;i++){
    ll l,r;
    cin >> l >> r;
    v.pb(make_pair(l,r));
  }
  ll c_block = 0;
  for(ll i = 0;i<q;i++){
    if(s.find(v[i]) == s.end()){
        s.insert(v[i]);
        if(v[i].ff == 1){
            if(s.find(make_pair(v[i].ff+1,v[i].ss)) != s.end()){
                c_block++;
            }
            if(s.find(make_pair(v[i].ff+1,v[i].ss+1)) != s.end() && v[i].ss < n){
                c_block++;
            }
            if(s.find(make_pair(v[i].ff+1,v[i].ss-1)) != s.end() && v[i].ss > 1){
                c_block++;
            }
        }
        else{
            if(s.find(make_pair(v[i].ff-1,v[i].ss)) != s.end()){
                c_block++;
            }
            if(s.find(make_pair(v[i].ff-1,v[i].ss+1)) != s.end() && v[i].ss < n){
                c_block++;
            }
            if(s.find(make_pair(v[i].ff-1,v[i].ss-1)) != s.end() && v[i].ss > 1){
                c_block++;
            }
        }
    }
    else{
        if(v[i].ff == 1){
            if(s.find(make_pair(v[i].ff+1,v[i].ss)) != s.end()){
                c_block--;
            }
            if(s.find(make_pair(v[i].ff+1,v[i].ss+1)) != s.end() && v[i].ss < n){
                c_block--;
            }
            if(s.find(make_pair(v[i].ff+1,v[i].ss-1)) != s.end() && v[i].ss > 1){
                c_block--;
            }
        }
        else{
            if(s.find(make_pair(v[i].ff-1,v[i].ss)) != s.end()){
                c_block--;
            }
            if(s.find(make_pair(v[i].ff-1,v[i].ss+1)) != s.end() && v[i].ss < n){
                c_block--;
            }
            if(s.find(make_pair(v[i].ff-1,v[i].ss-1)) != s.end() && v[i].ss > 1){
                c_block--;
            }
        }
        s.erase(v[i]);
    }
    if(c_block > 0){
        cn;
    }
    else{
        cy;
    }
  }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    //cin >> t;
    for (int i = 0; i < t; i++)
    {
        //cout << "Case #" << i+1 << ": ";
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution