Submission #702550

#TimeUsernameProblemLanguageResultExecution timeMemory
702550Chal1shkanEvent Hopping (BOI22_events)C++14
10 / 100
1585 ms128056 KiB
# include <bits/stdc++.h>

# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x  << " = " << x << endl; 

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ll maxn = 1e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};

using namespace std;

int n, q;
pair <pair <int, int>, int> p[5003];
int dist[5003][5003];
vector <int> g[5003];

void dijkstra (int st)
{
	dist[st][st] = 0;
	set <pair <int, int> > q;
	q.insert({0, st});
	while (!q.empty())
	{
		int v = q.begin() -> ss;
		q.erase(q.begin());
		for (int to : g[v])
		{
			if (dist[st][to] > dist[st][v] + 1)
			{
				q.erase({dist[st][to], to});
				dist[st][to] = dist[st][v] + 1;
				q.insert({dist[st][to], to});
			}
		}
	}
}

bool cmp (pair <pair <int, int>, int> a, pair <pair <int, int>, int> b)
{
	if (a.ff.ss != b.ff.ss)
	{
		return a.ff.ss < b.ff.ss;
	}
	else
	{
		return a.ff.ff < b.ff.ff;
	}
}

void ma1n (/* SABR */)
{
	cin >> n >> q;
	for (int i = 1; i <= n; ++i)
	{
		cin >> p[i].ff.ff >> p[i].ff.ss;
		p[i].ss = i;
	}
	sort(p + 1, p + 1 + n, cmp);
	if (n <= 5000)
	{
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j < i; ++j)
			{
				if (p[i].ff.ff <= p[j].ff.ss)
				{
					g[p[j].ss].pb(p[i].ss);
				}
			}
			for (int j = 1; j <= n; ++j)
			{
				dist[i][j] = inf;
			}
		}
		for (int i = 1; i <= n; ++i)
		{
			dijkstra(i);
		}
		while (q--)
		{
			int id1, id2;
			cin >> id1 >> id2;
			if (dist[id1][id2] == inf)
			{
				cout << "impossible" << nl;
			}
			else
			{
				cout << dist[id1][id2] << nl;
			}
		}
	}
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//    freopen("angry.in", "r", stdin);
//    freopen("angry.out", "w", stdout);
    int ttt = 1;
//  cin >> ttt;
    for (int test = 1; test <= ttt; ++test)
    {
//      cout << "Case " << test << ":" << ' ';
        ma1n();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...