Submission #866266

# Submission time Handle Problem Language Result Execution time Memory
866266 2023-10-25T16:38:23 Z fanwen Event Hopping (BOI22_events) C++17
100 / 100
128 ms 17860 KB
/**
 *      author : pham van sam 
 *      created : 25 October 2023 (Wednesday)
 **/

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
	#include "debug.h"
#else 
	#define clog if(false) cerr 
#endif

template < class T, T (*f) (T, T), T (*e) ()>
struct segment_tree {
    vector <T> it;
    int n;

    void update(int u, T p) {
        u += n - 1;
        it[u] = f(it[u], p);
        for(; u >>= 1; ) {
            it[u] = f(it[u << 1], it[u << 1 | 1]);
        }
    }

    void set(int u, T p) {
        u--;
        for (it[u += n] = p; u >>= 1; ) {
            it[u] = f(it[u << 1], it[u << 1 | 1]);
        }
    }

    T get(int p) {
        return it[p += n - 1];
    }

    T get(int l, int r) {
        l--;
        T resl = e(), resr = e();
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if(l & 1) resl = f(resl, it[l++]);
            if(r & 1) resr = f(it[--r], resr);
        }
        return f(resl, resr);
    }

    segment_tree(int n = 0) : n(n), it(n << 1 | 1) {
        fill(it.begin(), it.end(), T{});
    }
};

pair <int, int> op(pair <int, int> a, pair <int, int> b) {

	if(a.first == 0) return b;
	if(b.first == 0) return a;
	return a < b ? a : b;
};

pair <int, int> e() {
	return make_pair(0, 0);
};

const int MAXN = 1e5 + 5;
const int LOG = 20;

struct segment {
	int l, r, ind;
	friend ostream & operator << (ostream &out, const segment &a) {
		return out << "(" << a.l << ", " << a.r << ", " << a.ind << ")";
	}
} A[MAXN];

int n, q, anc[MAXN][LOG], pos[MAXN];

void you_make_it(void) {
    cin >> n >> q;
    vector <int> compress;
    for (int i = 1; i <= n; ++i) {
    	cin >> A[i].l >> A[i].r;
    	A[i].ind = i;
    	compress.push_back(A[i].l);
    	compress.push_back(A[i].r);
    }
    sort(compress.begin(), compress.end());
    compress.erase(unique(compress.begin(), compress.end()), compress.end());

    for (int i = 1; i <= n; ++i) {
    	A[i].l = lower_bound(compress.begin(), compress.end(), A[i].l) - compress.begin() + 1;
    	A[i].r = lower_bound(compress.begin(), compress.end(), A[i].r) - compress.begin() + 1;
    }

    sort(A + 1, A + n + 1, [&] (const segment &a, const segment &b) -> bool {
    	return make_pair(a.l, a.r) < make_pair(b.l, b.r);
    });

    // for (int i = 1; i <= n; ++i) clog << debug(A[i]) << '\n';

    segment_tree <pair <int, int>, op, e> it((int) compress.size());

    for (int i = 1; i <= n; ++i) {
    	pos[A[i].ind] = i;
    	it.update(A[i].r, make_pair(A[i].l, i));
    	auto [_, id] = it.get(A[i].l, A[i].r);
    	anc[i][0] = id;

    	// clog << anc[i][0] << " \n"[i == 1];
    }

    for (int i = 1; i < LOG; ++i) {
    	for (int u = 1; u <= n; ++u) {
    		anc[u][i] = anc[anc[u][i - 1]][i - 1];
    	}
    }

    while(q--) {
    	int u, v; cin >> u >> v;
    	u = pos[u], v = pos[v];
    	if(u == v) {
    		cout << "0\n"; 
    		continue;
    	}
    	if(A[u].r > A[v].r) {
    		cout << "impossible\n";
    		continue;
    	}
    	if(A[u].r == A[v].r) {
    		cout << "1\n";
    		continue;
    	}

    	int ans = 0;
    	for (int i = LOG - 1; i >= 0; --i) {
    		if(A[u].r < A[anc[v][i]].l) {
    			ans += 1 << i;
    			v = anc[v][i];
    		}
    	}
    	if(A[v].l <= A[u].r && A[u].r <= A[v].r) ans++;
    	else {
    		v = anc[v][0];
    		ans += 2;
    	}
    	if(A[v].l <= A[u].r && A[u].r <= A[v].r) cout << ans;
    	else cout << "impossible";
    	cout << '\n';
    	// clog << u << " " << v << '\n';
    }
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message

events.cpp: In instantiation of 'segment_tree<T, f, e>::segment_tree(int) [with T = std::pair<int, int>; T (* f)(T, T) = op; T (* e)() = e]':
events.cpp:101:67:   required from here
events.cpp:19:9: warning: 'segment_tree<std::pair<int, int>, op, e>::n' will be initialized after [-Wreorder]
   19 |     int n;
      |         ^
events.cpp:18:16: warning:   'std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > segment_tree<std::pair<int, int>, op, e>::it' [-Wreorder]
   18 |     vector <T> it;
      |                ^~
events.cpp:50:5: warning:   when initialized here [-Wreorder]
   50 |     segment_tree(int n = 0) : n(n), it(n << 1 | 1) {
      |     ^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 94 ms 12872 KB Output is correct
3 Correct 89 ms 12748 KB Output is correct
4 Correct 92 ms 12892 KB Output is correct
5 Correct 91 ms 13004 KB Output is correct
6 Correct 90 ms 13144 KB Output is correct
7 Correct 91 ms 13436 KB Output is correct
8 Correct 94 ms 14920 KB Output is correct
9 Correct 92 ms 17860 KB Output is correct
10 Correct 102 ms 16336 KB Output is correct
11 Correct 106 ms 16880 KB Output is correct
12 Correct 62 ms 15560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4560 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4620 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4440 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 31 ms 6220 KB Output is correct
20 Correct 27 ms 6240 KB Output is correct
21 Correct 27 ms 6492 KB Output is correct
22 Correct 23 ms 6480 KB Output is correct
23 Correct 22 ms 6488 KB Output is correct
24 Correct 22 ms 6476 KB Output is correct
25 Correct 32 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 2 ms 4440 KB Output is correct
17 Correct 1 ms 4604 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 67 ms 14284 KB Output is correct
20 Correct 63 ms 13256 KB Output is correct
21 Correct 83 ms 13964 KB Output is correct
22 Correct 68 ms 14864 KB Output is correct
23 Correct 79 ms 15612 KB Output is correct
24 Correct 78 ms 15580 KB Output is correct
25 Correct 22 ms 11856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 12832 KB Output is correct
2 Correct 102 ms 12732 KB Output is correct
3 Correct 94 ms 12876 KB Output is correct
4 Correct 92 ms 14796 KB Output is correct
5 Correct 93 ms 13264 KB Output is correct
6 Correct 128 ms 14416 KB Output is correct
7 Correct 103 ms 14404 KB Output is correct
8 Correct 101 ms 14744 KB Output is correct
9 Correct 78 ms 13816 KB Output is correct
10 Correct 99 ms 14176 KB Output is correct
11 Correct 115 ms 14032 KB Output is correct
12 Correct 96 ms 14284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 94 ms 12872 KB Output is correct
3 Correct 89 ms 12748 KB Output is correct
4 Correct 92 ms 12892 KB Output is correct
5 Correct 91 ms 13004 KB Output is correct
6 Correct 90 ms 13144 KB Output is correct
7 Correct 91 ms 13436 KB Output is correct
8 Correct 94 ms 14920 KB Output is correct
9 Correct 92 ms 17860 KB Output is correct
10 Correct 102 ms 16336 KB Output is correct
11 Correct 106 ms 16880 KB Output is correct
12 Correct 62 ms 15560 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4560 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4620 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4440 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
31 Correct 31 ms 6220 KB Output is correct
32 Correct 27 ms 6240 KB Output is correct
33 Correct 27 ms 6492 KB Output is correct
34 Correct 23 ms 6480 KB Output is correct
35 Correct 22 ms 6488 KB Output is correct
36 Correct 22 ms 6476 KB Output is correct
37 Correct 32 ms 5980 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4444 KB Output is correct
40 Correct 1 ms 4444 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4444 KB Output is correct
43 Correct 1 ms 4444 KB Output is correct
44 Correct 2 ms 4440 KB Output is correct
45 Correct 1 ms 4604 KB Output is correct
46 Correct 1 ms 4444 KB Output is correct
47 Correct 67 ms 14284 KB Output is correct
48 Correct 63 ms 13256 KB Output is correct
49 Correct 83 ms 13964 KB Output is correct
50 Correct 68 ms 14864 KB Output is correct
51 Correct 79 ms 15612 KB Output is correct
52 Correct 78 ms 15580 KB Output is correct
53 Correct 22 ms 11856 KB Output is correct
54 Correct 87 ms 12832 KB Output is correct
55 Correct 102 ms 12732 KB Output is correct
56 Correct 94 ms 12876 KB Output is correct
57 Correct 92 ms 14796 KB Output is correct
58 Correct 93 ms 13264 KB Output is correct
59 Correct 128 ms 14416 KB Output is correct
60 Correct 103 ms 14404 KB Output is correct
61 Correct 101 ms 14744 KB Output is correct
62 Correct 78 ms 13816 KB Output is correct
63 Correct 99 ms 14176 KB Output is correct
64 Correct 115 ms 14032 KB Output is correct
65 Correct 96 ms 14284 KB Output is correct
66 Correct 1 ms 4444 KB Output is correct
67 Correct 90 ms 16056 KB Output is correct
68 Correct 89 ms 16028 KB Output is correct
69 Correct 93 ms 15808 KB Output is correct
70 Correct 92 ms 16060 KB Output is correct
71 Correct 97 ms 16580 KB Output is correct
72 Correct 92 ms 16336 KB Output is correct
73 Correct 93 ms 17844 KB Output is correct
74 Correct 91 ms 17852 KB Output is correct
75 Correct 97 ms 16332 KB Output is correct
76 Correct 100 ms 16840 KB Output is correct
77 Correct 61 ms 15604 KB Output is correct
78 Correct 1 ms 4440 KB Output is correct
79 Correct 1 ms 4444 KB Output is correct
80 Correct 1 ms 4444 KB Output is correct
81 Correct 1 ms 4444 KB Output is correct
82 Correct 1 ms 4444 KB Output is correct
83 Correct 2 ms 4444 KB Output is correct
84 Correct 1 ms 4444 KB Output is correct
85 Correct 1 ms 4444 KB Output is correct
86 Correct 29 ms 6244 KB Output is correct
87 Correct 26 ms 6236 KB Output is correct
88 Correct 28 ms 6488 KB Output is correct
89 Correct 28 ms 6484 KB Output is correct
90 Correct 23 ms 6492 KB Output is correct
91 Correct 22 ms 6540 KB Output is correct
92 Correct 27 ms 6220 KB Output is correct
93 Correct 66 ms 14032 KB Output is correct
94 Correct 62 ms 13260 KB Output is correct
95 Correct 73 ms 13992 KB Output is correct
96 Correct 69 ms 14840 KB Output is correct
97 Correct 78 ms 16076 KB Output is correct
98 Correct 75 ms 15596 KB Output is correct
99 Correct 23 ms 11980 KB Output is correct
100 Correct 102 ms 17600 KB Output is correct
101 Correct 110 ms 17652 KB Output is correct
102 Correct 109 ms 17856 KB Output is correct
103 Correct 104 ms 17356 KB Output is correct
104 Correct 102 ms 17144 KB Output is correct
105 Correct 98 ms 17336 KB Output is correct
106 Correct 99 ms 14872 KB Output is correct
107 Correct 102 ms 15252 KB Output is correct
108 Correct 93 ms 15924 KB Output is correct
109 Correct 94 ms 15936 KB Output is correct
110 Correct 116 ms 17604 KB Output is correct
111 Correct 112 ms 17608 KB Output is correct