답안 #574244

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
574244 2022-06-08T08:39:07 Z 8e7 Event Hopping (BOI22_events) C++17
100 / 100
228 ms 25488 KB
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;
bool on[maxn];

struct segment{
	int l, r, id;
	segment(){l = r = id = 0;}
	segment(int a, int b, int c){l = a, r = b, id = c;}
};
pii p[maxn];
segment seg[maxn];
int idx[maxn], ord[maxn];
int lv[maxn], rv[maxn];


struct query{
	int l, id;
	query(){l = id = 0;}
	query(int a, int c){l = a, id = c;}
};
vector<query> que[maxn];
struct BIT{
	int bit[17][maxn];
	void init(int n) {
		for (int i = 0;i < 17;i++) {
			for (int j = 0;j < maxn;j++) bit[i][j] = inf;
		}
	}
	void modify(int ind, int d, int val) {
		ind = maxn - 1 - ind;
		for (;ind < maxn;ind += ind & (-ind)) bit[d][ind] = min(bit[d][ind], val);
	}
	int query(int ind, int d) {
		ind = maxn - 1 - ind;
		int ret = inf;
		for (;ind > 0;ind -= ind &(-ind)) ret = min(ret, bit[d][ind]);
		return ret;
	}
} to;

int ans[maxn];
int main() {
	io
	int n, q;
	cin >> n >> q;
	{
		vector<int> val;
		for (int i = 1;i <= n;i++) {
			cin >> seg[i].l >> seg[i].r;
			val.push_back(seg[i].l);
			val.push_back(seg[i].r);
			seg[i].id = i;
		}
		sort(val.begin(), val.end());
		val.resize(int(unique(val.begin(), val.end()) - val.begin()));
		for (int i = 1;i <= n;i++) {
			seg[i].l = lower_bound(val.begin(), val.end(), seg[i].l) - val.begin();
			seg[i].r = lower_bound(val.begin(), val.end(), seg[i].r) - val.begin();
		}
		sort(seg+1, seg+n+1, [&] (segment x, segment y){return x.r == y.r ? x.l < y.l : x.r < y.r;});	
		for (int i = 1;i <= n;i++) {
			idx[i] = seg[i].id;	
			lv[i] = seg[i].l, rv[i] = seg[i].r;
			ord[seg[i].id] = i;
		}
	}

	//pary(to[0], to[0] + n + 1);
	for (int id = 0;id < q;id++) {
		int s, t;
		cin >> s >> t;	
		s = ord[s], t = ord[t];
		ans[id] = -1;
		if (seg[t].r >= seg[s].r) {
			if (seg[t].l <= seg[s].r) {
				if (s == t) ans[id] = 0;
				else ans[id] = 1;
			} else {
				que[ord[seg[t].id]].push_back(query(ord[seg[s].id], id));
			}
		}
	}
	to.init(n);
	for (int i = 1;i <= n;i++) {
		int ind = lower_bound(rv+1, rv+n+1, lv[i]) - rv;	
		//debug("new", i);
		//debug(ind);
		to.modify(i, 0, ind);	
		for (int x = 1;x < 17;x++) {
			int prv = to.bit[x-1][maxn - 1 - i];
			//debug(prv);
			to.modify(i, x, to.query(prv, x-1));
		}
		
		for (auto [l, id]:que[i]) {
			int cur = i;
			debug("query",l, id, cur);
			ans[id] = 0;
			for (int x = 16;x >= 0;x--) {
				int nxt = to.query(cur, x);
				//debug(nxt);
				if (nxt > l) {
					cur = nxt;
					ans[id] += 1<<x;
				}
			}
			if (to.query(cur, 0) <= l) {
				ans[id]++;
			} else {
				ans[id] = -1;
			}
		}
	}
	for (int i = 0;i < q;i++) {
		if (ans[i] == -1) cout << "impossible\n";
		else cout << ans[i] << "\n";
	}
}

Compilation message

events.cpp: In function 'int main()':
events.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 0
      |                    ^
events.cpp:116:4: note: in expansion of macro 'debug'
  116 |    debug("query",l, id, cur);
      |    ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 20660 KB Output is correct
2 Correct 182 ms 25040 KB Output is correct
3 Correct 190 ms 24352 KB Output is correct
4 Correct 211 ms 24932 KB Output is correct
5 Correct 184 ms 24416 KB Output is correct
6 Correct 195 ms 24508 KB Output is correct
7 Correct 200 ms 24412 KB Output is correct
8 Correct 185 ms 24804 KB Output is correct
9 Correct 176 ms 24788 KB Output is correct
10 Correct 218 ms 24748 KB Output is correct
11 Correct 187 ms 24672 KB Output is correct
12 Correct 154 ms 25488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 20700 KB Output is correct
2 Correct 13 ms 20724 KB Output is correct
3 Correct 12 ms 20696 KB Output is correct
4 Correct 13 ms 20692 KB Output is correct
5 Correct 12 ms 20692 KB Output is correct
6 Correct 12 ms 20692 KB Output is correct
7 Correct 11 ms 20708 KB Output is correct
8 Correct 12 ms 20692 KB Output is correct
9 Correct 11 ms 20708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 20700 KB Output is correct
2 Correct 13 ms 20724 KB Output is correct
3 Correct 12 ms 20696 KB Output is correct
4 Correct 13 ms 20692 KB Output is correct
5 Correct 12 ms 20692 KB Output is correct
6 Correct 12 ms 20692 KB Output is correct
7 Correct 11 ms 20708 KB Output is correct
8 Correct 12 ms 20692 KB Output is correct
9 Correct 11 ms 20708 KB Output is correct
10 Correct 11 ms 20708 KB Output is correct
11 Correct 11 ms 20692 KB Output is correct
12 Correct 15 ms 20636 KB Output is correct
13 Correct 13 ms 20744 KB Output is correct
14 Correct 17 ms 20692 KB Output is correct
15 Correct 12 ms 20692 KB Output is correct
16 Correct 15 ms 20716 KB Output is correct
17 Correct 14 ms 20636 KB Output is correct
18 Correct 11 ms 20696 KB Output is correct
19 Correct 63 ms 22732 KB Output is correct
20 Correct 76 ms 22724 KB Output is correct
21 Correct 58 ms 22604 KB Output is correct
22 Correct 45 ms 22420 KB Output is correct
23 Correct 43 ms 22032 KB Output is correct
24 Correct 37 ms 21912 KB Output is correct
25 Correct 61 ms 22496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 20700 KB Output is correct
2 Correct 13 ms 20724 KB Output is correct
3 Correct 12 ms 20696 KB Output is correct
4 Correct 13 ms 20692 KB Output is correct
5 Correct 12 ms 20692 KB Output is correct
6 Correct 12 ms 20692 KB Output is correct
7 Correct 11 ms 20708 KB Output is correct
8 Correct 12 ms 20692 KB Output is correct
9 Correct 11 ms 20708 KB Output is correct
10 Correct 14 ms 20684 KB Output is correct
11 Correct 14 ms 20692 KB Output is correct
12 Correct 12 ms 20692 KB Output is correct
13 Correct 13 ms 20692 KB Output is correct
14 Correct 13 ms 20692 KB Output is correct
15 Correct 12 ms 20740 KB Output is correct
16 Correct 14 ms 20716 KB Output is correct
17 Correct 12 ms 20692 KB Output is correct
18 Correct 13 ms 20692 KB Output is correct
19 Correct 137 ms 22368 KB Output is correct
20 Correct 143 ms 22456 KB Output is correct
21 Correct 138 ms 22372 KB Output is correct
22 Correct 133 ms 22360 KB Output is correct
23 Correct 123 ms 22388 KB Output is correct
24 Correct 152 ms 22368 KB Output is correct
25 Correct 93 ms 22296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 179 ms 25000 KB Output is correct
2 Correct 195 ms 24276 KB Output is correct
3 Correct 205 ms 24744 KB Output is correct
4 Correct 168 ms 24800 KB Output is correct
5 Correct 187 ms 24668 KB Output is correct
6 Correct 201 ms 24528 KB Output is correct
7 Correct 190 ms 24416 KB Output is correct
8 Correct 189 ms 24580 KB Output is correct
9 Correct 130 ms 22328 KB Output is correct
10 Correct 183 ms 24048 KB Output is correct
11 Correct 220 ms 24104 KB Output is correct
12 Correct 189 ms 24128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 20660 KB Output is correct
2 Correct 182 ms 25040 KB Output is correct
3 Correct 190 ms 24352 KB Output is correct
4 Correct 211 ms 24932 KB Output is correct
5 Correct 184 ms 24416 KB Output is correct
6 Correct 195 ms 24508 KB Output is correct
7 Correct 200 ms 24412 KB Output is correct
8 Correct 185 ms 24804 KB Output is correct
9 Correct 176 ms 24788 KB Output is correct
10 Correct 218 ms 24748 KB Output is correct
11 Correct 187 ms 24672 KB Output is correct
12 Correct 154 ms 25488 KB Output is correct
13 Correct 11 ms 20700 KB Output is correct
14 Correct 13 ms 20724 KB Output is correct
15 Correct 12 ms 20696 KB Output is correct
16 Correct 13 ms 20692 KB Output is correct
17 Correct 12 ms 20692 KB Output is correct
18 Correct 12 ms 20692 KB Output is correct
19 Correct 11 ms 20708 KB Output is correct
20 Correct 12 ms 20692 KB Output is correct
21 Correct 11 ms 20708 KB Output is correct
22 Correct 11 ms 20708 KB Output is correct
23 Correct 11 ms 20692 KB Output is correct
24 Correct 15 ms 20636 KB Output is correct
25 Correct 13 ms 20744 KB Output is correct
26 Correct 17 ms 20692 KB Output is correct
27 Correct 12 ms 20692 KB Output is correct
28 Correct 15 ms 20716 KB Output is correct
29 Correct 14 ms 20636 KB Output is correct
30 Correct 11 ms 20696 KB Output is correct
31 Correct 63 ms 22732 KB Output is correct
32 Correct 76 ms 22724 KB Output is correct
33 Correct 58 ms 22604 KB Output is correct
34 Correct 45 ms 22420 KB Output is correct
35 Correct 43 ms 22032 KB Output is correct
36 Correct 37 ms 21912 KB Output is correct
37 Correct 61 ms 22496 KB Output is correct
38 Correct 14 ms 20684 KB Output is correct
39 Correct 14 ms 20692 KB Output is correct
40 Correct 12 ms 20692 KB Output is correct
41 Correct 13 ms 20692 KB Output is correct
42 Correct 13 ms 20692 KB Output is correct
43 Correct 12 ms 20740 KB Output is correct
44 Correct 14 ms 20716 KB Output is correct
45 Correct 12 ms 20692 KB Output is correct
46 Correct 13 ms 20692 KB Output is correct
47 Correct 137 ms 22368 KB Output is correct
48 Correct 143 ms 22456 KB Output is correct
49 Correct 138 ms 22372 KB Output is correct
50 Correct 133 ms 22360 KB Output is correct
51 Correct 123 ms 22388 KB Output is correct
52 Correct 152 ms 22368 KB Output is correct
53 Correct 93 ms 22296 KB Output is correct
54 Correct 179 ms 25000 KB Output is correct
55 Correct 195 ms 24276 KB Output is correct
56 Correct 205 ms 24744 KB Output is correct
57 Correct 168 ms 24800 KB Output is correct
58 Correct 187 ms 24668 KB Output is correct
59 Correct 201 ms 24528 KB Output is correct
60 Correct 190 ms 24416 KB Output is correct
61 Correct 189 ms 24580 KB Output is correct
62 Correct 130 ms 22328 KB Output is correct
63 Correct 183 ms 24048 KB Output is correct
64 Correct 220 ms 24104 KB Output is correct
65 Correct 189 ms 24128 KB Output is correct
66 Correct 11 ms 20692 KB Output is correct
67 Correct 181 ms 24976 KB Output is correct
68 Correct 214 ms 24276 KB Output is correct
69 Correct 210 ms 24728 KB Output is correct
70 Correct 197 ms 24396 KB Output is correct
71 Correct 214 ms 24436 KB Output is correct
72 Correct 184 ms 24456 KB Output is correct
73 Correct 173 ms 24780 KB Output is correct
74 Correct 191 ms 24816 KB Output is correct
75 Correct 184 ms 24672 KB Output is correct
76 Correct 194 ms 24780 KB Output is correct
77 Correct 155 ms 25484 KB Output is correct
78 Correct 13 ms 20612 KB Output is correct
79 Correct 13 ms 20628 KB Output is correct
80 Correct 12 ms 20692 KB Output is correct
81 Correct 12 ms 20720 KB Output is correct
82 Correct 12 ms 20636 KB Output is correct
83 Correct 14 ms 20624 KB Output is correct
84 Correct 12 ms 20704 KB Output is correct
85 Correct 13 ms 20732 KB Output is correct
86 Correct 68 ms 22668 KB Output is correct
87 Correct 67 ms 22672 KB Output is correct
88 Correct 65 ms 22540 KB Output is correct
89 Correct 60 ms 22404 KB Output is correct
90 Correct 41 ms 21972 KB Output is correct
91 Correct 36 ms 21840 KB Output is correct
92 Correct 64 ms 22544 KB Output is correct
93 Correct 136 ms 22536 KB Output is correct
94 Correct 146 ms 22368 KB Output is correct
95 Correct 138 ms 22392 KB Output is correct
96 Correct 151 ms 22388 KB Output is correct
97 Correct 133 ms 22384 KB Output is correct
98 Correct 155 ms 22364 KB Output is correct
99 Correct 91 ms 22392 KB Output is correct
100 Correct 187 ms 24492 KB Output is correct
101 Correct 190 ms 24440 KB Output is correct
102 Correct 196 ms 24620 KB Output is correct
103 Correct 195 ms 24140 KB Output is correct
104 Correct 185 ms 23888 KB Output is correct
105 Correct 228 ms 24140 KB Output is correct
106 Correct 165 ms 24052 KB Output is correct
107 Correct 226 ms 24556 KB Output is correct
108 Correct 164 ms 24232 KB Output is correct
109 Correct 168 ms 24260 KB Output is correct
110 Correct 161 ms 23728 KB Output is correct
111 Correct 186 ms 23720 KB Output is correct