답안 #601883

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
601883 2022-07-22T11:44:09 Z idiot123 늑대인간 (IOI18_werewolf) C++14
100 / 100
963 ms 134512 KB
#include "werewolf.h"
#include<bits/stdc++.h>

using namespace std;

int find(int a, vector<int>& rep){
	if(rep[a] != a)rep[a] = find(rep[a], rep);
	return rep[a];
}

void uni(int a, int b, vector<int>& rep, vector<set<int>>& pos){
	a = find(a, rep); b = find(b, rep);
	if(a != b){
		if(pos[a].size() < pos[b].size())swap(a, b);
		rep[b] = a;
		pos[a].insert(pos[b].begin(), pos[b].end());
		pos[b].clear();
	}
}

void computeRange(int v, vector<pair<int, int>>& children, vector<pair<int, int>>& range, int& cnt){
	if(children[v] == make_pair(-1, -1)){
		range[v] = {cnt, cnt}; cnt++;
	}else{
		computeRange(children[v].first, children, range, cnt);
		computeRange(children[v].second, children, range, cnt);
		range[v] = {range[children[v].first].first, range[children[v].second].second};
	}
}

vector<int> check_validity(int N, vector<int> X, vector<int> Y,
                                vector<int> S, vector<int> E,
                                vector<int> L, vector<int> R) {
	int M = X.size();
	int Q = S.size();
	vector<vector<int>> smaller(N);
	vector<vector<int>> bigger(N);
	for(int j = 0; j < M; j++){
		int x, y; x = X[j]; y = Y[j];
		if(x > y)swap(x, y);
		bigger[x].push_back(y);
		smaller[y].push_back(x);
	}
	vector<array<int, 20>> jump(N);
	vector<array<int, 20>> jumpR(N);
	vector<pair<int, int>> children(N, {-1, -1});
	vector<int> repR(N); for(int i = 0; i < N; i++)repR[i] = i;
	int id = N;
	//cout<<"WOW\n";
	for(int r = 1; r < N; r++){
		for(auto l : smaller[r]){
			int x = find(l, repR); int y = find(r, repR);
			if(x != y){
				repR.push_back(id);
				jump.push_back(array<int, 20>());
				jumpR.push_back(array<int, 20>()); 
				jump[x][0] = id; jump[y][0] = id; 
				repR[x] = id; repR[y] = id;
				jumpR[x][0] = r; jumpR[y][0] = r;
				jump[id][0] = id;
				jumpR[id][0] = r;
				id++;
				children.push_back({x, y});
			}
		}
	}
	//cout<<"XD\n";
	vector<pair<int, int>> range(id);
	int cnt = 0;
	computeRange(id-1, children, range, cnt);
	/*for(int i = 0; i < id; i++){
		cout<<children[i].first<<" "<<children[i].second<<"     "<<jump[i][0]<<" "<<jumpR[i][0]<<"      "<<range[i].first<<" "<<range[i].second<<"\n";
	}*/
	vector<int> A(Q);
	for(int k=  1; k < 20; k++){
		for(int i = 0; i < id; i++){
			jump[i][k] = jump[jump[i][k-1]][k-1];
			jumpR[i][k] = jumpR[jump[i][k-1]][k-1];
		}
	}
	vector<array<int, 3>> queries;
	vector<int> rep(N);
	for(int i=  0; i < N; i++)rep[i] = i;
	vector<set<int>> pos(N);
	for(int i = 0; i < N; i++)pos[i].insert(range[i].first);
	for(int i =0; i < Q; i++){
		queries.push_back({L[i], R[i], i});
	}
	sort(queries.begin(), queries.end(), greater<array<int, 3>>());
	int currentL = N-1;
	for(auto query : queries){
		int L = query[0]; int R = query[1]; int ansId = query[2];
		while(L < currentL){
			currentL--;
			for(auto x : bigger[currentL]){
				uni(x, currentL, rep, pos);
			}
		}
		int v = E[ansId];
		for(int k = 19; k >= 0; k--){
			if(jumpR[v][k] <= R){
				v = jump[v][k];
			}
		}
		int x = S[ansId];
		x = find(x, rep);
		auto it = pos[x].lower_bound(range[v].first);
		if(it != pos[x].end() && (*it) <= range[v].second){
			A[ansId] = 1;
		}else{
			A[ansId] = 0;
		}
	}
	return A;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 6 ms 2172 KB Output is correct
11 Correct 6 ms 2172 KB Output is correct
12 Correct 6 ms 2172 KB Output is correct
13 Correct 6 ms 2300 KB Output is correct
14 Correct 8 ms 2300 KB Output is correct
15 Correct 7 ms 2280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 878 ms 123652 KB Output is correct
2 Correct 802 ms 128532 KB Output is correct
3 Correct 778 ms 125512 KB Output is correct
4 Correct 786 ms 123980 KB Output is correct
5 Correct 786 ms 124032 KB Output is correct
6 Correct 869 ms 123848 KB Output is correct
7 Correct 867 ms 124088 KB Output is correct
8 Correct 736 ms 128512 KB Output is correct
9 Correct 612 ms 125336 KB Output is correct
10 Correct 634 ms 124020 KB Output is correct
11 Correct 692 ms 123976 KB Output is correct
12 Correct 755 ms 123784 KB Output is correct
13 Correct 841 ms 134192 KB Output is correct
14 Correct 780 ms 134112 KB Output is correct
15 Correct 842 ms 134112 KB Output is correct
16 Correct 819 ms 134132 KB Output is correct
17 Correct 899 ms 124236 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 6 ms 2172 KB Output is correct
11 Correct 6 ms 2172 KB Output is correct
12 Correct 6 ms 2172 KB Output is correct
13 Correct 6 ms 2300 KB Output is correct
14 Correct 8 ms 2300 KB Output is correct
15 Correct 7 ms 2280 KB Output is correct
16 Correct 878 ms 123652 KB Output is correct
17 Correct 802 ms 128532 KB Output is correct
18 Correct 778 ms 125512 KB Output is correct
19 Correct 786 ms 123980 KB Output is correct
20 Correct 786 ms 124032 KB Output is correct
21 Correct 869 ms 123848 KB Output is correct
22 Correct 867 ms 124088 KB Output is correct
23 Correct 736 ms 128512 KB Output is correct
24 Correct 612 ms 125336 KB Output is correct
25 Correct 634 ms 124020 KB Output is correct
26 Correct 692 ms 123976 KB Output is correct
27 Correct 755 ms 123784 KB Output is correct
28 Correct 841 ms 134192 KB Output is correct
29 Correct 780 ms 134112 KB Output is correct
30 Correct 842 ms 134112 KB Output is correct
31 Correct 819 ms 134132 KB Output is correct
32 Correct 899 ms 124236 KB Output is correct
33 Correct 887 ms 124428 KB Output is correct
34 Correct 270 ms 22784 KB Output is correct
35 Correct 911 ms 127692 KB Output is correct
36 Correct 832 ms 124460 KB Output is correct
37 Correct 906 ms 126768 KB Output is correct
38 Correct 865 ms 125128 KB Output is correct
39 Correct 921 ms 134160 KB Output is correct
40 Correct 947 ms 131492 KB Output is correct
41 Correct 784 ms 126096 KB Output is correct
42 Correct 724 ms 124488 KB Output is correct
43 Correct 963 ms 131240 KB Output is correct
44 Correct 843 ms 126784 KB Output is correct
45 Correct 805 ms 134512 KB Output is correct
46 Correct 883 ms 134272 KB Output is correct
47 Correct 775 ms 134316 KB Output is correct
48 Correct 759 ms 134204 KB Output is correct
49 Correct 776 ms 134232 KB Output is correct
50 Correct 777 ms 134168 KB Output is correct
51 Correct 865 ms 132664 KB Output is correct
52 Correct 864 ms 132516 KB Output is correct