답안 #729620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
729620 2023-04-24T09:42:31 Z Sam_a17 Event Hopping (BOI22_events) C++17
100 / 100
215 ms 31480 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount

#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long

void print(long long t) { cerr << t; }
void print(int t) { cerr << t; }
void print(string t) { cerr << t; }
void print(char t) { cerr << t; }
void print(double t) { cerr << t; }
void print(long double t) { cerr << t; }
void print(unsigned long long t) { cerr << t; }

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[], V n) { cerr << "["; for (int i = 0; i < n; i++) { print(v[i]); cerr << " "; } cerr << "]"; }
template <class T, class V> void print(pair <T, V> p) { cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}"; }
template <class T> void print(vector <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(set <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(multiset <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T, class V> void print(map <T, V> v) { cerr << "[ "; for (auto i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T, class V> void print(unordered_map <T, V> v) { cerr << "[ "; for (auto i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(deque <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);

// for grid problems
int dx[8] = { -1,0,1,0,1,-1,1,-1 };
int dy[8] = { 0,1,0,-1,1,1,-1,-1 };

// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
	fastIO();

	if (str == "input") {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
	else {
		// freopen("skis.in", "r", stdin);
		// freopen("skis.out", "w", stdout);
	}
}
// Indexed Set  
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10, M = 5e3 + 10, inf = 1e8, LOG = 18;
int n, q, ord[N];
long long down[N][LOG];

struct node {
	long long l, r;
	long long id;

	bool operator<(const node &y) const { return l < y.l; }
};

vector<node> intervals;

struct segTreeMin {
  vector<pair<long long, int>> mTree;
  int size;
 
  void init(long long n) {
    size = 1;
    while(size < n)  {
      size *= 2;
    }
    mTree.assign(2 * size - 1, {INT64_MAX, -1});
  }
 
  void upd(int u, long long v, int ind, int x, int lx, int rx) { // set value at pos u
    if(rx - lx == 1) {
      mTree[x] = min(mTree[x], make_pair(v, ind));
      return;
    }
 
    int m = (lx + rx) / 2;
    if(u < m) {
      upd(u, v, ind, 2 * x + 1, lx, m);
    }else {
      upd(u, v, ind, 2 * x + 2, m, rx);
    }
    mTree[x] = min(mTree[2 * x + 1], mTree[2 * x + 2]);
  }
 
  void upd(int u, long long v, int ind) {
    upd(u, v, ind, 0, 0, size);
  }
 
  pair<long long, int> qry (int l, int r, int x, int lx, int rx) { // range queries
    if(l >= rx || lx >= r) {
      return {INT64_MAX, -1};
    }
    if(lx >= l && r >= rx) {
      return mTree[x];
    }
 
    int m = (rx + lx) / 2;
    auto s1 = qry(l, r, 2 * x + 1, lx, m);
    auto s2 = qry(l, r, 2 * x + 2, m, rx);
    return min(s1, s2);
  }
 
  auto qry(int l, int r) {
    return qry(l, r, 0, 0, size);
  }
};

segTreeMin seg_min;

void solve_() {
	cin >> n >> q;

	vector<int> compress;
	for(int i = 1; i <= n; i++) {
		int li, ri; cin >> li >> ri;
		intervals.push_back({li, ri, i});
		
		//
		compress.push_back(li);
		compress.push_back(ri);
	}

	sort(all(compress));
	uniq(compress);
	
	sort(all(intervals));

	seg_min.init(sz(compress) + 5);
	for(int i = 0; i < n; i++) {
		intervals[i].l = lower_bound(all(compress), intervals[i].l) - compress.begin() + 1;
		intervals[i].r = lower_bound(all(compress), intervals[i].r) - compress.begin() + 1;
		seg_min.upd(intervals[i].r, intervals[i].l, i);
		ord[intervals[i].id] = i;
	}

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < LOG; j++) {
			down[i][j] = -1;
		}
	}

	for(int i = 1; i < n; i++) {
		auto ans = seg_min.qry(intervals[i].l, intervals[i].r + 1);
		if(ans.first < intervals[i].l) {
			down[i][0] = ans.second;
		}
	}

	for(int i = 1; i < LOG; i++) {
		for(int j = 0; j < n; j++) {
			if(down[j][i - 1] == -1) {
				down[j][i] = -1;
				continue;
			}
			down[j][i] = down[down[j][i - 1]][i - 1];
		}
	}

	while(q--) {
		int x, y; cin >> x >> y;
		x = ord[x], y = ord[y];
		if(x == y) {
			cout << 0 << '\n';
			continue;
		}

		if(intervals[x].r == intervals[y].r) {
			cout << 1 << '\n';
			continue;
		}

		if(intervals[x].r > intervals[y].r) {
			cout << "impossible" << '\n';
			continue;
		}
		int curr = y, aa = 0;
		for(int i = LOG - 1; i >= 0; i--) {
			if(down[curr][i] == -1) continue;
			if(intervals[down[curr][i]].l > intervals[x].r) {
				curr = down[curr][i];
				aa += 1 << i;
			}
		}

		if(down[curr][0] == -1) {
			if(intervals[curr].l > intervals[x].r) {
				cout << "impossible" << '\n';
			} else {
				cout << aa + 1 << '\n';
			}
		} else {
			aa += 1;
			if(intervals[curr].l > intervals[x].r) {
				cout << aa + 1 << '\n';
			} else {
				cout << aa << '\n';
			}
		}
	}

}

int main() {
	setIO();

	auto solve = [&](int test_case)-> void {
		for (int i = 1; i <= test_case; i++) {
			solve_();
		}
	};

	int test_cases = 1;
	// cin >> test_cases;
	solve(test_cases);

	return 0;
}

Compilation message

events.cpp: In function 'void setIO(std::string)':
events.cpp:69:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |   freopen("input.txt", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:70:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |   freopen("output.txt", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 178 ms 23868 KB Output is correct
3 Correct 152 ms 23836 KB Output is correct
4 Correct 148 ms 23884 KB Output is correct
5 Correct 134 ms 23924 KB Output is correct
6 Correct 130 ms 24008 KB Output is correct
7 Correct 130 ms 23976 KB Output is correct
8 Correct 129 ms 28452 KB Output is correct
9 Correct 122 ms 28480 KB Output is correct
10 Correct 185 ms 24224 KB Output is correct
11 Correct 155 ms 28452 KB Output is correct
12 Correct 108 ms 24432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 1 ms 476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 1 ms 476 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 36 ms 2312 KB Output is correct
20 Correct 37 ms 2380 KB Output is correct
21 Correct 37 ms 2584 KB Output is correct
22 Correct 36 ms 2704 KB Output is correct
23 Correct 42 ms 2688 KB Output is correct
24 Correct 37 ms 2712 KB Output is correct
25 Correct 28 ms 2432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 1 ms 476 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 118 ms 23076 KB Output is correct
20 Correct 88 ms 23056 KB Output is correct
21 Correct 116 ms 23196 KB Output is correct
22 Correct 103 ms 27188 KB Output is correct
23 Correct 121 ms 27120 KB Output is correct
24 Correct 126 ms 27272 KB Output is correct
25 Correct 49 ms 18932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 23604 KB Output is correct
2 Correct 120 ms 23668 KB Output is correct
3 Correct 135 ms 23576 KB Output is correct
4 Correct 115 ms 28204 KB Output is correct
5 Correct 154 ms 24008 KB Output is correct
6 Correct 202 ms 27832 KB Output is correct
7 Correct 189 ms 27848 KB Output is correct
8 Correct 153 ms 28048 KB Output is correct
9 Correct 112 ms 27176 KB Output is correct
10 Correct 135 ms 30608 KB Output is correct
11 Correct 177 ms 30380 KB Output is correct
12 Correct 154 ms 30664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 178 ms 23868 KB Output is correct
3 Correct 152 ms 23836 KB Output is correct
4 Correct 148 ms 23884 KB Output is correct
5 Correct 134 ms 23924 KB Output is correct
6 Correct 130 ms 24008 KB Output is correct
7 Correct 130 ms 23976 KB Output is correct
8 Correct 129 ms 28452 KB Output is correct
9 Correct 122 ms 28480 KB Output is correct
10 Correct 185 ms 24224 KB Output is correct
11 Correct 155 ms 28452 KB Output is correct
12 Correct 108 ms 24432 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 2 ms 504 KB Output is correct
20 Correct 2 ms 600 KB Output is correct
21 Correct 1 ms 476 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 2 ms 600 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 36 ms 2312 KB Output is correct
32 Correct 37 ms 2380 KB Output is correct
33 Correct 37 ms 2584 KB Output is correct
34 Correct 36 ms 2704 KB Output is correct
35 Correct 42 ms 2688 KB Output is correct
36 Correct 37 ms 2712 KB Output is correct
37 Correct 28 ms 2432 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 2 ms 468 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 2 ms 596 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 118 ms 23076 KB Output is correct
48 Correct 88 ms 23056 KB Output is correct
49 Correct 116 ms 23196 KB Output is correct
50 Correct 103 ms 27188 KB Output is correct
51 Correct 121 ms 27120 KB Output is correct
52 Correct 126 ms 27272 KB Output is correct
53 Correct 49 ms 18932 KB Output is correct
54 Correct 118 ms 23604 KB Output is correct
55 Correct 120 ms 23668 KB Output is correct
56 Correct 135 ms 23576 KB Output is correct
57 Correct 115 ms 28204 KB Output is correct
58 Correct 154 ms 24008 KB Output is correct
59 Correct 202 ms 27832 KB Output is correct
60 Correct 189 ms 27848 KB Output is correct
61 Correct 153 ms 28048 KB Output is correct
62 Correct 112 ms 27176 KB Output is correct
63 Correct 135 ms 30608 KB Output is correct
64 Correct 177 ms 30380 KB Output is correct
65 Correct 154 ms 30664 KB Output is correct
66 Correct 0 ms 340 KB Output is correct
67 Correct 126 ms 26716 KB Output is correct
68 Correct 121 ms 26744 KB Output is correct
69 Correct 140 ms 26700 KB Output is correct
70 Correct 134 ms 26768 KB Output is correct
71 Correct 147 ms 26896 KB Output is correct
72 Correct 129 ms 26836 KB Output is correct
73 Correct 124 ms 31480 KB Output is correct
74 Correct 133 ms 31348 KB Output is correct
75 Correct 171 ms 27136 KB Output is correct
76 Correct 155 ms 31184 KB Output is correct
77 Correct 131 ms 26416 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 468 KB Output is correct
80 Correct 1 ms 468 KB Output is correct
81 Correct 1 ms 468 KB Output is correct
82 Correct 1 ms 472 KB Output is correct
83 Correct 2 ms 596 KB Output is correct
84 Correct 2 ms 596 KB Output is correct
85 Correct 1 ms 468 KB Output is correct
86 Correct 38 ms 3072 KB Output is correct
87 Correct 43 ms 3064 KB Output is correct
88 Correct 37 ms 3276 KB Output is correct
89 Correct 40 ms 3404 KB Output is correct
90 Correct 32 ms 3532 KB Output is correct
91 Correct 29 ms 3492 KB Output is correct
92 Correct 30 ms 3148 KB Output is correct
93 Correct 106 ms 25036 KB Output is correct
94 Correct 96 ms 24220 KB Output is correct
95 Correct 122 ms 24812 KB Output is correct
96 Correct 107 ms 29236 KB Output is correct
97 Correct 118 ms 29232 KB Output is correct
98 Correct 120 ms 29124 KB Output is correct
99 Correct 66 ms 20148 KB Output is correct
100 Correct 177 ms 30940 KB Output is correct
101 Correct 172 ms 30968 KB Output is correct
102 Correct 178 ms 31268 KB Output is correct
103 Correct 141 ms 30616 KB Output is correct
104 Correct 154 ms 30400 KB Output is correct
105 Correct 146 ms 30736 KB Output is correct
106 Correct 189 ms 26124 KB Output is correct
107 Correct 215 ms 26192 KB Output is correct
108 Correct 188 ms 26912 KB Output is correct
109 Correct 175 ms 26892 KB Output is correct
110 Correct 181 ms 30916 KB Output is correct
111 Correct 206 ms 30880 KB Output is correct