답안 #702615

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
702615 2023-02-24T15:06:16 Z Chal1shkan Event Hopping (BOI22_events) C++14
0 / 100
178 ms 13380 KB
# 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, pos[maxn];
pair <pair <int, int>, int> p[maxn];
int t[maxn * 8], up[maxn][20];

int combine (int x, int y)
{
	if (x == 0) return y;
	if (y == 0) return x;
	return (p[x].ff.ff < p[y].ff.ff ? x : y);
}

void update (int pos, int x, int v = 1, int tl = 1, int tr = n + n)
{
	if (tl == tr)
	{
		t[v] = combine(t[v], x);	
		return;
	}
	else
	{
		int tm = (tl + tr) >> 1;
		if (pos <= tm)
		{
			update(pos, x, v * 2, tl, tm);
		}
		else
		{
			update(pos, x, v * 2 + 1, tm + 1, tr);
		}
		t[v] = combine(t[v * 2], t[v * 2 + 1]);
	}
}

int get (int l, int r, int v = 1, int tl = 1, int tr = n + n)
{
	if (tr < l || r < tl) return 0;
	if (l <= tl && tr <= r) return t[v];
	int tm = (tl + tr) >> 1;
	return combine(get(l, r, v * 2, tl, tm), get(l, r, v * 2 + 1, tm + 1, tr));
}

void ma1n (/* SABR */)
{
	cin >> n >> q;
	vector <int> v;
	for (int i = 1; i <= n; ++i)
	{
		cin >> p[i].ff.ff >> p[i].ff.ss;
		p[i].ss = i;
		v.pb(p[i].ff.ff);
		v.pb(p[i].ff.ss);
	}
	sort(v.begin(), v.end());
	auto ittttt = unique(v.begin(), v.end());
  	v.resize(distance(v.begin(), ittttt));
	for (int i = 1; i <= n; ++i)
	{
		p[i].ff.ff = lower_bound(v.begin(), v.end(), p[i].ff.ff) - v.begin() + 1;
		p[i].ff.ss = lower_bound(v.begin(), v.end(), p[i].ff.ss) - v.begin() + 1;
	}
	for (int i = 1; i <= n; ++i)
	{
		update(p[i].ff.ss, i);
	}
	for (int i = 1; i <= n; ++i)
	{
		up[i][0] = get(p[i].ff.ff, p[i].ff.ss);
	}
	for (int j = 1; j <= 19; ++j)
	{
		for (int i = 1; i <= n; ++i)
		{
			up[i][j] = up[up[i][j - 1]][j - 1];
		}
	}
	while (q--)
	{
		int id1, id2;
		cin >> id1 >> id2;
		if (id1 == id2)
		{
			cout << 0 << nl;
		}
		else if (p[id1].ff.ss > p[id2].ff.ss)
		{
			cout << "impossible" << nl;
		}
		else
		{
			int x = id2, ans = 0;
			for (int j = 19; j >= 0; --j)
			{
				int nxt = up[x][j];
				if (p[id1].ff.ss < p[nxt].ff.ff)
				{
					ans += (1LL << j);
					x = nxt;
				}
			}
			if (p[x].ff.ff <= p[id1].ff.ss && p[id1].ff.ss <= p[x].ff.ss)
			{
				ans++;
				x = up[x][0];
			}
			else
			{
				ans += 2;
				x = up[x][1];
			}
			if (p[x].ff.ff <= p[id1].ff.ss && p[id1].ff.ss <= p[x].ff.ss)
			{
				cout << ans << nl;
			}
			else
			{
				cout << "impossible" << 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 134 ms 11848 KB Output is correct
3 Correct 129 ms 11912 KB Output is correct
4 Correct 163 ms 11840 KB Output is correct
5 Incorrect 145 ms 12112 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 136 ms 11796 KB Output is correct
2 Correct 158 ms 11820 KB Output is correct
3 Correct 178 ms 11768 KB Output is correct
4 Correct 125 ms 13380 KB Output is correct
5 Correct 141 ms 12192 KB Output is correct
6 Incorrect 162 ms 13360 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 134 ms 11848 KB Output is correct
3 Correct 129 ms 11912 KB Output is correct
4 Correct 163 ms 11840 KB Output is correct
5 Incorrect 145 ms 12112 KB Output isn't correct
6 Halted 0 ms 0 KB -