답안 #588891

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
588891 2022-07-04T07:03:53 Z Arnch Event Hopping (BOI22_events) C++17
100 / 100
384 ms 48028 KB
// oooo
/*
 har chi delet mikhad bebar ~
 gitar o ba khodet nabar! ~
 ;Amoo_Hasan;
*/

#include<bits/stdc++.h>
//#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
//#pragma GCC target("avx2,fma")

using namespace std;

typedef long long ll;
typedef long double ld;

#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x <<endl
//#define endl '\n'

constexpr ll inf = 1e18, N = 1e6 + 10, Log = 30;

int n, q, s[N], e[N];
int val[N], par[N][Log], rpar[N][Log];

void Compress() {
	vector<int> com;
	for(int i = 0; i < n; i++) {
		com.push_back(s[i]), com.push_back(e[i]);
	}
	sort(All(com)), com.erase(unique(All(com)), com.end());
	for(int i = 0; i < n; i++) {
		s[i] = lower_bound(All(com), s[i]) - com.begin();
		e[i] = lower_bound(All(com), e[i]) - com.begin();
	}
}

struct node {
	int cnt;
	node() {
		cnt = -1;
	}
};
struct Seg {
	node x[N << 2];
	int type;
	Seg(int dio) {
		type = dio;
	}
	void build(int l, int r, int v) {
		x[v].cnt = -1;
		if(r - l < 2) return;
		int mid = (l + r) >> 1;
		build(l, mid, 2 * v), build(mid, r, 2 * v + 1);
	}
	node merge(node a, node b) {
		node res = a;
		if(type == 0) {
			if(b.cnt != -1 && (res.cnt == -1 || e[b.cnt] > e[res.cnt])) res = b;
			return res;
		} else {
			if(b.cnt != -1 && (res.cnt == -1 || s[b.cnt] < s[res.cnt])) res = b;
			return res;
		}
	}
	void change(int l, int r, int ind, int val, int v) {
		if(r - l < 2) {
			if(type == 0) {
				if(x[v].cnt == -1 || e[x[v].cnt] < e[val]) 
					x[v].cnt = val;
			} else {
				if(x[v].cnt == -1 || s[x[v].cnt] > s[val]) 
					x[v].cnt = val;
			}
			return;
		}
		int mid = (l + r) >> 1;
		if(ind < mid) change(l, mid, ind, val, 2 * v);
		else change(mid, r, ind, val, 2 * v + 1);
		x[v] = merge(x[2 * v], x[2 * v + 1]);
	}
	node get(int l, int r, int s, int e, int v) {
		node res;
		if(r <= s || l >= e) return res;
		if(l >= s && r <= e) return x[v];
		int mid = (l + r) >> 1;
		return merge(get(l, mid, s, e, 2 * v), get(mid, r, s, e, 2 * v + 1));
	}
} S(0), E(1);

void Pre1() {
	for(int i = 0; i < n; i++) {
		S.change(0, 2 * n, s[i], i, 1);
	}
	for(int i = 0; i < n; i++) {
		int pos = S.get(0, 2 * n, 0, e[i] + 1, 1).cnt;
		if(pos != -1 && e[pos] > e[i]) par[i][0] = pos;
		else par[i][0] = -1;
	}
	
	for(int lg = 1; lg < Log; lg++) {
		for(int i = 0; i < n; i++) {
			if(par[i][lg - 1] == -1 || par[par[i][lg - 1]][lg - 1] == -1) {
				par[i][lg] = -1; continue;
			}
			par[i][lg] = par[par[i][lg - 1]][lg - 1];
		}
	}
}
void Pre2() {
	E.build(0, 2 * n, 1);
	for(int i = 0; i < n; i++) {
		E.change(0, 2 * n, e[i], i, 1);
	}
	for(int i = 0; i < n; i++) {
		int pos = E.get(0, 2 * n, s[i], e[i] + 1, 1).cnt;
		if(pos != -1 && s[pos] < s[i]) rpar[i][0] = pos;
		else rpar[i][0] = -1;
	}

	for(int lg = 1; lg < Log; lg++) {
		for(int i = 0; i < n; i++) {
			if(rpar[i][lg - 1] == -1 || rpar[rpar[i][lg - 1]][lg - 1] == -1) {
				rpar[i][lg] = -1; continue;
			}
			rpar[i][lg] = rpar[rpar[i][lg - 1]][lg - 1];
		}
	}
}

int main() {
    ios :: sync_with_stdio(0), cin.tie(0);

	cin >>n >>q;
	for(int i = 0; i < n; i++) {
		cin >>s[i] >>e[i];
	}
	Compress();

//	Pre1();
	Pre2();

	for(int i = 0; i < q; i++) {
		int x, y; cin >>x >>y;
		x--, y--;

		if(x != y && e[x] > e[y]) {
			cout<<"impossible" <<endl;
			continue;
		}
		if(x == y) {
			cout<<0 <<endl;
			continue;
		}
		if(e[x] >= s[y] && e[x] <= e[y]) {
			cout<<1 <<endl;
			continue;
		}
		
		int t = 0;
/*		for(int lg = Log - 1; lg >= 0; lg--) {
			if(par[x][lg] == -1) continue;
			if(e[par[x][lg]] < s[y]) {
				x = par[x][lg];
				t += (1 << lg);
			}
		}
		if(par[x][0] != -1 && e[par[x][0]] <= e[y]) {
			cout<<t + 2 <<endl;
			continue;
		}*/
		for(int lg = Log - 1; lg >= 0; lg--) {
			if(rpar[y][lg] == -1) continue;
			if(s[rpar[y][lg]] > e[x]) {
				y = rpar[y][lg];
				t += (1 << lg);
			}
		}
		if(rpar[y][0] != -1 && s[rpar[y][0]] <= e[x]) {
			cout<<t + 2 <<endl;
			continue;
		}
		cout<<"impossible" <<endl;
	}

    return 0;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 31572 KB Output is correct
2 Correct 292 ms 45124 KB Output is correct
3 Correct 274 ms 44936 KB Output is correct
4 Correct 355 ms 44964 KB Output is correct
5 Correct 291 ms 44952 KB Output is correct
6 Correct 285 ms 45048 KB Output is correct
7 Correct 311 ms 45124 KB Output is correct
8 Correct 268 ms 45464 KB Output is correct
9 Correct 262 ms 45560 KB Output is correct
10 Correct 312 ms 45268 KB Output is correct
11 Correct 284 ms 45352 KB Output is correct
12 Correct 235 ms 45512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 31572 KB Output is correct
2 Correct 16 ms 31548 KB Output is correct
3 Correct 13 ms 31784 KB Output is correct
4 Correct 13 ms 31700 KB Output is correct
5 Correct 14 ms 31736 KB Output is correct
6 Correct 14 ms 31800 KB Output is correct
7 Correct 13 ms 31700 KB Output is correct
8 Correct 14 ms 31700 KB Output is correct
9 Correct 15 ms 31780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 31572 KB Output is correct
2 Correct 16 ms 31548 KB Output is correct
3 Correct 13 ms 31784 KB Output is correct
4 Correct 13 ms 31700 KB Output is correct
5 Correct 14 ms 31736 KB Output is correct
6 Correct 14 ms 31800 KB Output is correct
7 Correct 13 ms 31700 KB Output is correct
8 Correct 14 ms 31700 KB Output is correct
9 Correct 15 ms 31780 KB Output is correct
10 Correct 13 ms 31572 KB Output is correct
11 Correct 14 ms 31560 KB Output is correct
12 Correct 13 ms 31736 KB Output is correct
13 Correct 13 ms 31768 KB Output is correct
14 Correct 12 ms 31808 KB Output is correct
15 Correct 18 ms 31692 KB Output is correct
16 Correct 14 ms 31692 KB Output is correct
17 Correct 15 ms 31700 KB Output is correct
18 Correct 13 ms 31804 KB Output is correct
19 Correct 184 ms 33256 KB Output is correct
20 Correct 165 ms 33668 KB Output is correct
21 Correct 171 ms 33796 KB Output is correct
22 Correct 160 ms 34128 KB Output is correct
23 Correct 171 ms 33676 KB Output is correct
24 Correct 147 ms 33588 KB Output is correct
25 Correct 152 ms 33228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 31572 KB Output is correct
2 Correct 16 ms 31548 KB Output is correct
3 Correct 13 ms 31784 KB Output is correct
4 Correct 13 ms 31700 KB Output is correct
5 Correct 14 ms 31736 KB Output is correct
6 Correct 14 ms 31800 KB Output is correct
7 Correct 13 ms 31700 KB Output is correct
8 Correct 14 ms 31700 KB Output is correct
9 Correct 15 ms 31780 KB Output is correct
10 Correct 14 ms 31580 KB Output is correct
11 Correct 12 ms 31608 KB Output is correct
12 Correct 17 ms 31700 KB Output is correct
13 Correct 17 ms 31700 KB Output is correct
14 Correct 17 ms 31732 KB Output is correct
15 Correct 15 ms 31796 KB Output is correct
16 Correct 14 ms 31700 KB Output is correct
17 Correct 13 ms 31684 KB Output is correct
18 Correct 13 ms 31700 KB Output is correct
19 Correct 139 ms 45900 KB Output is correct
20 Correct 133 ms 44540 KB Output is correct
21 Correct 140 ms 44684 KB Output is correct
22 Correct 134 ms 44532 KB Output is correct
23 Correct 180 ms 44932 KB Output is correct
24 Correct 180 ms 45056 KB Output is correct
25 Correct 98 ms 44408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 287 ms 45012 KB Output is correct
2 Correct 309 ms 44916 KB Output is correct
3 Correct 335 ms 45004 KB Output is correct
4 Correct 324 ms 45436 KB Output is correct
5 Correct 337 ms 45412 KB Output is correct
6 Correct 384 ms 45080 KB Output is correct
7 Correct 376 ms 45092 KB Output is correct
8 Correct 324 ms 45228 KB Output is correct
9 Correct 171 ms 44352 KB Output is correct
10 Correct 346 ms 44816 KB Output is correct
11 Correct 308 ms 44548 KB Output is correct
12 Correct 350 ms 44784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 31572 KB Output is correct
2 Correct 292 ms 45124 KB Output is correct
3 Correct 274 ms 44936 KB Output is correct
4 Correct 355 ms 44964 KB Output is correct
5 Correct 291 ms 44952 KB Output is correct
6 Correct 285 ms 45048 KB Output is correct
7 Correct 311 ms 45124 KB Output is correct
8 Correct 268 ms 45464 KB Output is correct
9 Correct 262 ms 45560 KB Output is correct
10 Correct 312 ms 45268 KB Output is correct
11 Correct 284 ms 45352 KB Output is correct
12 Correct 235 ms 45512 KB Output is correct
13 Correct 13 ms 31572 KB Output is correct
14 Correct 16 ms 31548 KB Output is correct
15 Correct 13 ms 31784 KB Output is correct
16 Correct 13 ms 31700 KB Output is correct
17 Correct 14 ms 31736 KB Output is correct
18 Correct 14 ms 31800 KB Output is correct
19 Correct 13 ms 31700 KB Output is correct
20 Correct 14 ms 31700 KB Output is correct
21 Correct 15 ms 31780 KB Output is correct
22 Correct 13 ms 31572 KB Output is correct
23 Correct 14 ms 31560 KB Output is correct
24 Correct 13 ms 31736 KB Output is correct
25 Correct 13 ms 31768 KB Output is correct
26 Correct 12 ms 31808 KB Output is correct
27 Correct 18 ms 31692 KB Output is correct
28 Correct 14 ms 31692 KB Output is correct
29 Correct 15 ms 31700 KB Output is correct
30 Correct 13 ms 31804 KB Output is correct
31 Correct 184 ms 33256 KB Output is correct
32 Correct 165 ms 33668 KB Output is correct
33 Correct 171 ms 33796 KB Output is correct
34 Correct 160 ms 34128 KB Output is correct
35 Correct 171 ms 33676 KB Output is correct
36 Correct 147 ms 33588 KB Output is correct
37 Correct 152 ms 33228 KB Output is correct
38 Correct 14 ms 31580 KB Output is correct
39 Correct 12 ms 31608 KB Output is correct
40 Correct 17 ms 31700 KB Output is correct
41 Correct 17 ms 31700 KB Output is correct
42 Correct 17 ms 31732 KB Output is correct
43 Correct 15 ms 31796 KB Output is correct
44 Correct 14 ms 31700 KB Output is correct
45 Correct 13 ms 31684 KB Output is correct
46 Correct 13 ms 31700 KB Output is correct
47 Correct 139 ms 45900 KB Output is correct
48 Correct 133 ms 44540 KB Output is correct
49 Correct 140 ms 44684 KB Output is correct
50 Correct 134 ms 44532 KB Output is correct
51 Correct 180 ms 44932 KB Output is correct
52 Correct 180 ms 45056 KB Output is correct
53 Correct 98 ms 44408 KB Output is correct
54 Correct 287 ms 45012 KB Output is correct
55 Correct 309 ms 44916 KB Output is correct
56 Correct 335 ms 45004 KB Output is correct
57 Correct 324 ms 45436 KB Output is correct
58 Correct 337 ms 45412 KB Output is correct
59 Correct 384 ms 45080 KB Output is correct
60 Correct 376 ms 45092 KB Output is correct
61 Correct 324 ms 45228 KB Output is correct
62 Correct 171 ms 44352 KB Output is correct
63 Correct 346 ms 44816 KB Output is correct
64 Correct 308 ms 44548 KB Output is correct
65 Correct 350 ms 44784 KB Output is correct
66 Correct 16 ms 31628 KB Output is correct
67 Correct 287 ms 45304 KB Output is correct
68 Correct 329 ms 45892 KB Output is correct
69 Correct 324 ms 45116 KB Output is correct
70 Correct 333 ms 45552 KB Output is correct
71 Correct 320 ms 45228 KB Output is correct
72 Correct 323 ms 45820 KB Output is correct
73 Correct 294 ms 45584 KB Output is correct
74 Correct 337 ms 48028 KB Output is correct
75 Correct 355 ms 45736 KB Output is correct
76 Correct 324 ms 45416 KB Output is correct
77 Correct 240 ms 45584 KB Output is correct
78 Correct 16 ms 31572 KB Output is correct
79 Correct 17 ms 31700 KB Output is correct
80 Correct 13 ms 31700 KB Output is correct
81 Correct 13 ms 31700 KB Output is correct
82 Correct 12 ms 31700 KB Output is correct
83 Correct 14 ms 31700 KB Output is correct
84 Correct 14 ms 31804 KB Output is correct
85 Correct 13 ms 31772 KB Output is correct
86 Correct 177 ms 33576 KB Output is correct
87 Correct 174 ms 33740 KB Output is correct
88 Correct 192 ms 33556 KB Output is correct
89 Correct 175 ms 34116 KB Output is correct
90 Correct 177 ms 33996 KB Output is correct
91 Correct 170 ms 34004 KB Output is correct
92 Correct 171 ms 33444 KB Output is correct
93 Correct 174 ms 45832 KB Output is correct
94 Correct 142 ms 45268 KB Output is correct
95 Correct 152 ms 45416 KB Output is correct
96 Correct 138 ms 44456 KB Output is correct
97 Correct 164 ms 44808 KB Output is correct
98 Correct 172 ms 44728 KB Output is correct
99 Correct 88 ms 44484 KB Output is correct
100 Correct 323 ms 45576 KB Output is correct
101 Correct 308 ms 45672 KB Output is correct
102 Correct 306 ms 46684 KB Output is correct
103 Correct 294 ms 46508 KB Output is correct
104 Correct 296 ms 45748 KB Output is correct
105 Correct 312 ms 47032 KB Output is correct
106 Correct 296 ms 47100 KB Output is correct
107 Correct 313 ms 47336 KB Output is correct
108 Correct 275 ms 47236 KB Output is correct
109 Correct 287 ms 46392 KB Output is correct
110 Correct 292 ms 45640 KB Output is correct
111 Correct 301 ms 45264 KB Output is correct