답안 #588698

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
588698 2022-07-03T21:41:59 Z peuch Event Hopping (BOI22_events) C++17
20 / 100
136 ms 14052 KB
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 10;
const int INF = 1e9;

int n, q;
int s[MAXN], e[MAXN];
int ans[MAXN];

struct event{
	int s, e, id;
	event(int _s, int _e, int _id) {
		s = _s;
		e = _e;
		id = _id;
	}
	bool operator < (event x){
		if(e == x.e) return s < x.s;
		return e < x.e;
	}
};

struct query{
	int l, r, id;
	query(int _l, int _r, int _id){
		l = _l;
		r = _r;
		id = _id;
	}
};

vector<query> ar[MAXN];

vector<event> line;
int dist[MAXN];
int tam[MAXN];

vector<vector<int> > auxPoint;

int main(){
	scanf("%d %d", &n, &q);
	for(int i = 1; i <= n; i++){
		scanf("%d %d", &s[i], &e[i]);
		line.push_back(event(s[i], e[i], i));
	}
	for(int i = 1; i <= q; i++){
		int ini, fim;
		scanf("%d %d", &ini, &fim);
		ans[i] = INF;
		if(ini == fim) ans[i] = 0;
		else ar[fim].push_back(query(e[ini], e[fim], i));
	}
	
	sort(line.begin(), line.end());
	
	vector<int> pilha;
	for(int i = 0; i < line.size(); i++){
//		printf("Adding event: %d %d\n", line[i].s, line[i].e);
		while(!pilha.empty() && line[pilha.back()].s >= line[i].s){
			pilha.pop_back();
		}
		pilha.push_back(i);
		
		int ini = 0, fim = pilha.size() - 1;
		while(ini != fim){
			int mid = (ini + fim) >> 1;
			if(line[pilha[mid]].e >= line[i].s) fim = mid;
			else ini = mid + 1;
		}
//		printf("\t -> %d (%d)\n", pilha[ini], ini);
		ini = pilha[ini];
		if(i == ini){
			dist[i] = auxPoint.size();
			auxPoint.push_back(vector<int> (0));
		}
		else dist[i] = dist[ini];
		while(!auxPoint[dist[i]].empty() && auxPoint[dist[i]].back() >= line[ini].s)
			auxPoint[dist[i]].pop_back();
		auxPoint[dist[i]].push_back(line[ini].s);
		if(ini != i) auxPoint[dist[i]].push_back(line[i].s);
		
//		printf("\t(%d) ", dist[i]);
//		for(int j = 0; j < auxPoint[dist[i]].size(); j++)
//			printf("%d ", auxPoint[dist[i]][j]);
//		printf("\n");
		for(int j = 0; j < ar[line[i].id].size(); j++){
//			printf("Query event: %d %d %d\n", ar[line[i].id][j].l, ar[line[i].id][j].r, ar[line[i].id][j].id);
			if(ar[line[i].id][j].l > ar[line[i].id][j].r) continue;
			int k = upper_bound(auxPoint[dist[i]].begin(), auxPoint[dist[i]].end(), ar[line[i].id][j].l) - auxPoint[dist[i]].begin() - 1;
//			printf("\t%d\n", k);
			if(k < 0) continue;
			ans[ar[line[i].id][j].id] = auxPoint[dist[i]].size() - k;
		}
//		for(int j = 0; j <= i; j++){
//			printf("dist[%d] = ", j);
//			for(int k = 0; k < auxPoint[dist[j]].size(); k++)
//				printf("%d ", auxPoint[dist[j]][k]);
//			printf("\n");
//		}
	}
	
	for(int i = 1; i <= q; i++){
		if(ans[i] == INF) printf("impossible\n");
		else printf("%d\n", ans[i]);
	}
}

Compilation message

events.cpp: In function 'int main()':
events.cpp:58:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |  for(int i = 0; i < line.size(); i++){
      |                 ~~^~~~~~~~~~~~~
events.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   for(int j = 0; j < ar[line[i].id].size(); j++){
      |                  ~~^~~~~~~~~~~~~~~~~~~~~~~
events.cpp:42:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |  scanf("%d %d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
events.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |   scanf("%d %d", &s[i], &e[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
events.cpp:49:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |   scanf("%d %d", &ini, &fim);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 71 ms 8292 KB Output is correct
3 Correct 69 ms 8796 KB Output is correct
4 Correct 76 ms 8764 KB Output is correct
5 Correct 70 ms 9460 KB Output is correct
6 Correct 74 ms 10180 KB Output is correct
7 Correct 69 ms 9996 KB Output is correct
8 Correct 71 ms 14012 KB Output is correct
9 Correct 69 ms 14052 KB Output is correct
10 Correct 78 ms 9640 KB Output is correct
11 Correct 82 ms 11912 KB Output is correct
12 Correct 53 ms 13244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 31 ms 5300 KB Output is correct
20 Correct 32 ms 5368 KB Output is correct
21 Correct 34 ms 5984 KB Output is correct
22 Incorrect 30 ms 6228 KB Output isn't correct
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 44 ms 6244 KB Output is correct
20 Correct 36 ms 6168 KB Output is correct
21 Correct 39 ms 7964 KB Output is correct
22 Correct 42 ms 8440 KB Output is correct
23 Correct 40 ms 6396 KB Output is correct
24 Incorrect 40 ms 7260 KB Output isn't correct
25 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 8348 KB Output is correct
2 Correct 71 ms 8812 KB Output is correct
3 Correct 88 ms 8764 KB Output is correct
4 Correct 73 ms 13968 KB Output is correct
5 Correct 87 ms 9680 KB Output is correct
6 Incorrect 136 ms 9580 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 71 ms 8292 KB Output is correct
3 Correct 69 ms 8796 KB Output is correct
4 Correct 76 ms 8764 KB Output is correct
5 Correct 70 ms 9460 KB Output is correct
6 Correct 74 ms 10180 KB Output is correct
7 Correct 69 ms 9996 KB Output is correct
8 Correct 71 ms 14012 KB Output is correct
9 Correct 69 ms 14052 KB Output is correct
10 Correct 78 ms 9640 KB Output is correct
11 Correct 82 ms 11912 KB Output is correct
12 Correct 53 ms 13244 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 31 ms 5300 KB Output is correct
32 Correct 32 ms 5368 KB Output is correct
33 Correct 34 ms 5984 KB Output is correct
34 Incorrect 30 ms 6228 KB Output isn't correct
35 Halted 0 ms 0 KB -