/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 100000;
const int BITS = 17;
int N, Q;
struct Event {
int l, r;
};
bool operator < (const Event &e1, const Event &e2) {
return make_pair(e1.r, e1.l) < make_pair(e2.r, e2.l);
}
Event events[N_MAX + 2];
int order[N_MAX + 2];
int id[N_MAX + 2];
struct SegTree {
int left, right;
int sum;
};
SegTree trees[20000000];
int curr;
void build (int node, int l, int r) {
if (l == r) {
return;
}
int mid = (l + r) / 2;
trees[node].left = ++curr;
trees[node].right = ++curr;
build(trees[node].left, l, mid);
build(trees[node].right, mid + 1, r);
}
void build (int node) {
build(node, 1, N);
}
int update (int node, int l, int r, int upos, int uval) {
trees[++curr] = trees[node]; node = curr;
if (l == r) {
trees[node].sum += uval;
return node;
}
int mid = (l + r) / 2;
if (upos <= mid) {
trees[node].left = update(trees[node].left, l, mid, upos, uval);
} else {
trees[node].right = update(trees[node].right, mid + 1, r, upos, uval);
}
trees[node].sum = trees[trees[node].left].sum + trees[trees[node].right].sum;
return node;
}
int update (int node, int upos, int uval) {
if (!(1 <= upos && upos <= N)) {
return node;
}
return update(node, 1, N, upos, uval);
}
int query (int node, int l, int r, int qpos) {
if (qpos <= l) {
return trees[node].sum;
}
int mid = (l + r) / 2;
if (qpos <= mid) {
return query(trees[node].left, l, mid, qpos) + trees[trees[node].right].sum;
} else {
return query(trees[node].right, mid + 1, r, qpos);
}
}
int query (int node, int qpos) {
return query(node, 1, N, qpos);
}
int root[N_MAX + 2];
int rm[N_MAX + 2][BITS];
int lg2[N_MAX + 2];
void precalc () {
for (int i = 1; i <= N; i++) {
rm[i][0] = i;
lg2[i] = lg2[i - 1];
if ((1 << (lg2[i] + 1)) <= i) {
lg2[i]++;
}
}
for (int bit = 1; bit < BITS; bit++) {
for (int i = 1; i + (1 << bit) - 1 <= N; i++) {
int j = rm[i][bit - 1], k = rm[i + (1 << (bit - 1))][bit - 1];
rm[i][bit] = (events[j].l <= events[k].l ? j : k);
}
}
}
int rmin (int l, int r) {
int len = r - l + 1;
int bit = lg2[len];
int j = rm[l][bit];
int k = rm[r - (1 << bit) + 1][bit];
return (events[j].l <= events[k].l ? j : k);
}
int minl[N_MAX + 2];
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
for (int i = 1; i <= N; i++) {
cin >> events[i].l >> events[i].r;
}
iota(order + 1, order + N + 1, 1);
sort(order + 1, order + N + 1, [&] (const int &i, const int &j) {
return events[i] < events[j];
});
for (int i = 1; i <= N; i++) {
id[order[i]] = i;
}
sort(events + 1, events + N + 1);
precalc();
root[0] = ++curr;
build(root[0]);
for (int i = 1; i <= N; i++) {
int l = 1, r = i;
while (l < r) {
int mid = (l + r) / 2;
if (events[i].l <= events[mid].r) {
r = mid;
} else {
l = mid + 1;
}
}
int j;
if (l == i) {
j = 0;
minl[i] = i;
} else {
j = rmin(l, i - 1);
minl[i] = minl[j];
}
root[i] = root[j];
root[i] = update(root[i], i - 1, +1);
root[i] = update(root[i], j - 1, -1);
root[i] = update(root[i], l - 1, +1);
}
while (Q--) {
int i, j;
cin >> i >> j;
i = id[i]; j = id[j];
if (i == j) {
cout << 0 << "\n";
} else if (events[i].r == events[j].r) {
cout << 1 << "\n";
} else if (minl[j] <= i && i < j) {
cout << query(root[j], i) << "\n";
} else {
cout << "impossible\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
139 ms |
75836 KB |
Output is correct |
3 |
Correct |
151 ms |
78000 KB |
Output is correct |
4 |
Correct |
160 ms |
77892 KB |
Output is correct |
5 |
Correct |
152 ms |
75980 KB |
Output is correct |
6 |
Correct |
139 ms |
74068 KB |
Output is correct |
7 |
Correct |
147 ms |
73516 KB |
Output is correct |
8 |
Correct |
108 ms |
57676 KB |
Output is correct |
9 |
Correct |
120 ms |
57620 KB |
Output is correct |
10 |
Correct |
150 ms |
78396 KB |
Output is correct |
11 |
Correct |
166 ms |
70172 KB |
Output is correct |
12 |
Correct |
114 ms |
67308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
800 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
800 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
35 ms |
3764 KB |
Output is correct |
20 |
Correct |
32 ms |
3680 KB |
Output is correct |
21 |
Correct |
36 ms |
4052 KB |
Output is correct |
22 |
Correct |
39 ms |
3772 KB |
Output is correct |
23 |
Correct |
31 ms |
3904 KB |
Output is correct |
24 |
Correct |
29 ms |
2268 KB |
Output is correct |
25 |
Correct |
46 ms |
3484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
800 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
131 ms |
74260 KB |
Output is correct |
20 |
Correct |
132 ms |
74348 KB |
Output is correct |
21 |
Correct |
139 ms |
67312 KB |
Output is correct |
22 |
Correct |
120 ms |
66648 KB |
Output is correct |
23 |
Correct |
70 ms |
32764 KB |
Output is correct |
24 |
Correct |
130 ms |
74108 KB |
Output is correct |
25 |
Correct |
65 ms |
32760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
75908 KB |
Output is correct |
2 |
Correct |
156 ms |
77968 KB |
Output is correct |
3 |
Correct |
157 ms |
77816 KB |
Output is correct |
4 |
Correct |
119 ms |
57568 KB |
Output is correct |
5 |
Correct |
174 ms |
78268 KB |
Output is correct |
6 |
Correct |
171 ms |
78052 KB |
Output is correct |
7 |
Correct |
176 ms |
77964 KB |
Output is correct |
8 |
Correct |
165 ms |
78284 KB |
Output is correct |
9 |
Correct |
72 ms |
34664 KB |
Output is correct |
10 |
Correct |
152 ms |
77660 KB |
Output is correct |
11 |
Correct |
141 ms |
67172 KB |
Output is correct |
12 |
Correct |
158 ms |
77616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
139 ms |
75836 KB |
Output is correct |
3 |
Correct |
151 ms |
78000 KB |
Output is correct |
4 |
Correct |
160 ms |
77892 KB |
Output is correct |
5 |
Correct |
152 ms |
75980 KB |
Output is correct |
6 |
Correct |
139 ms |
74068 KB |
Output is correct |
7 |
Correct |
147 ms |
73516 KB |
Output is correct |
8 |
Correct |
108 ms |
57676 KB |
Output is correct |
9 |
Correct |
120 ms |
57620 KB |
Output is correct |
10 |
Correct |
150 ms |
78396 KB |
Output is correct |
11 |
Correct |
166 ms |
70172 KB |
Output is correct |
12 |
Correct |
114 ms |
67308 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
800 KB |
Output is correct |
19 |
Correct |
1 ms |
852 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
35 ms |
3764 KB |
Output is correct |
32 |
Correct |
32 ms |
3680 KB |
Output is correct |
33 |
Correct |
36 ms |
4052 KB |
Output is correct |
34 |
Correct |
39 ms |
3772 KB |
Output is correct |
35 |
Correct |
31 ms |
3904 KB |
Output is correct |
36 |
Correct |
29 ms |
2268 KB |
Output is correct |
37 |
Correct |
46 ms |
3484 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
852 KB |
Output is correct |
41 |
Correct |
1 ms |
852 KB |
Output is correct |
42 |
Correct |
1 ms |
852 KB |
Output is correct |
43 |
Correct |
1 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
852 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
600 KB |
Output is correct |
47 |
Correct |
131 ms |
74260 KB |
Output is correct |
48 |
Correct |
132 ms |
74348 KB |
Output is correct |
49 |
Correct |
139 ms |
67312 KB |
Output is correct |
50 |
Correct |
120 ms |
66648 KB |
Output is correct |
51 |
Correct |
70 ms |
32764 KB |
Output is correct |
52 |
Correct |
130 ms |
74108 KB |
Output is correct |
53 |
Correct |
65 ms |
32760 KB |
Output is correct |
54 |
Correct |
159 ms |
75908 KB |
Output is correct |
55 |
Correct |
156 ms |
77968 KB |
Output is correct |
56 |
Correct |
157 ms |
77816 KB |
Output is correct |
57 |
Correct |
119 ms |
57568 KB |
Output is correct |
58 |
Correct |
174 ms |
78268 KB |
Output is correct |
59 |
Correct |
171 ms |
78052 KB |
Output is correct |
60 |
Correct |
176 ms |
77964 KB |
Output is correct |
61 |
Correct |
165 ms |
78284 KB |
Output is correct |
62 |
Correct |
72 ms |
34664 KB |
Output is correct |
63 |
Correct |
152 ms |
77660 KB |
Output is correct |
64 |
Correct |
141 ms |
67172 KB |
Output is correct |
65 |
Correct |
158 ms |
77616 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
136 ms |
77932 KB |
Output is correct |
68 |
Correct |
153 ms |
77940 KB |
Output is correct |
69 |
Correct |
182 ms |
77960 KB |
Output is correct |
70 |
Correct |
184 ms |
76080 KB |
Output is correct |
71 |
Correct |
150 ms |
74076 KB |
Output is correct |
72 |
Correct |
136 ms |
73460 KB |
Output is correct |
73 |
Correct |
113 ms |
57668 KB |
Output is correct |
74 |
Correct |
113 ms |
57676 KB |
Output is correct |
75 |
Correct |
152 ms |
78400 KB |
Output is correct |
76 |
Correct |
148 ms |
70180 KB |
Output is correct |
77 |
Correct |
109 ms |
67380 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
852 KB |
Output is correct |
80 |
Correct |
1 ms |
860 KB |
Output is correct |
81 |
Correct |
1 ms |
852 KB |
Output is correct |
82 |
Correct |
1 ms |
724 KB |
Output is correct |
83 |
Correct |
1 ms |
872 KB |
Output is correct |
84 |
Correct |
1 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
596 KB |
Output is correct |
86 |
Correct |
30 ms |
4644 KB |
Output is correct |
87 |
Correct |
32 ms |
4820 KB |
Output is correct |
88 |
Correct |
42 ms |
4968 KB |
Output is correct |
89 |
Correct |
28 ms |
4784 KB |
Output is correct |
90 |
Correct |
31 ms |
4932 KB |
Output is correct |
91 |
Correct |
26 ms |
3284 KB |
Output is correct |
92 |
Correct |
31 ms |
4500 KB |
Output is correct |
93 |
Correct |
133 ms |
76188 KB |
Output is correct |
94 |
Correct |
110 ms |
75428 KB |
Output is correct |
95 |
Correct |
126 ms |
69196 KB |
Output is correct |
96 |
Correct |
114 ms |
68604 KB |
Output is correct |
97 |
Correct |
75 ms |
34664 KB |
Output is correct |
98 |
Correct |
119 ms |
76104 KB |
Output is correct |
99 |
Correct |
57 ms |
34080 KB |
Output is correct |
100 |
Correct |
182 ms |
77992 KB |
Output is correct |
101 |
Correct |
184 ms |
78020 KB |
Output is correct |
102 |
Correct |
150 ms |
78168 KB |
Output is correct |
103 |
Correct |
160 ms |
77584 KB |
Output is correct |
104 |
Correct |
143 ms |
67004 KB |
Output is correct |
105 |
Correct |
153 ms |
77796 KB |
Output is correct |
106 |
Correct |
162 ms |
77192 KB |
Output is correct |
107 |
Correct |
165 ms |
77396 KB |
Output is correct |
108 |
Correct |
154 ms |
71148 KB |
Output is correct |
109 |
Correct |
164 ms |
71132 KB |
Output is correct |
110 |
Correct |
190 ms |
78132 KB |
Output is correct |
111 |
Correct |
201 ms |
77844 KB |
Output is correct |