답안 #377231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
377231 2021-03-13T13:46:01 Z CaroLinda 늑대인간 (IOI18_werewolf) C++14
100 / 100
2308 ms 156044 KB
#include "werewolf.h"
#include <bits/stdc++.h>

#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define ll long long

const int MAXN = 2e5+10 ;
const int LOG = 20 ;

using namespace std ;

struct Tree
{
	vector<int> tree[MAXN] ;
	int currTime ;
	int tin[MAXN] , tout[MAXN] , rep[MAXN] ;   
	int dp[LOG][MAXN] ;
	int dsu[MAXN] , label[MAXN] ;

	Tree()
	{
		for(int i = 0 ; i < MAXN ; i++ ) dsu[i] =rep[i] = i ;
		memset(dp, -1, sizeof dp );
	}

	int find(int x)
	{
		if( x == dsu[x] ) return x ;
		return dsu[x] = find(dsu[x] ) ;
	}

	void join(int x, int y )
	{
		x = find(x) ;
		y = find(y) ;

		if( x == y ) return ;

		if(rand() % 2 ) swap(x,y) ;

		dsu[x] = y ;
		
		if(label[ rep[x] ] > label[ rep[y] ] ) 
		{	
			tree[ rep[x] ].push_back(rep[y] ) ;
			rep[y] = rep[x] ;
		}
		else tree[ rep[y] ].push_back(rep[x] ) ;

	}


	void dfs(int x)
	{
		
		for(int i = 1 ; i < LOG && dp[i-1][x] != -1 ; i++ )
			dp[i][x] = dp[i-1][ dp[i-1][x] ] ;

	  	tin[x] = tout[x] = currTime++ ;

		for(auto child : tree[x] )
		{
			dp[0][child] = x ;
			dfs(child) ;
			tout[x] = tout[child] ;
		}

	}

} leftToRight, rightToLeft ;

struct SegmentTree
{

	vector< int > tree[MAXN*4] ;

	int m(int l, int r ) { return (l+r)>>1 ; }

	void upd(int pos, int l, int r, int idx , int k )
	{
		tree[pos].push_back(k) ;

		if( l == r ) return ;

		if(idx <= m(l,r) ) upd(pos<<1 , l, m(l,r) , idx , k ) ;
		else upd(pos<<1|1 , m(l,r)+1 , r , idx , k ) ;

	}

	void qry(int pos, int l, int r, int beg, int en , pair<int,int> interval , bool &ok )
	{
		if( l > en || r < beg ) return ;
		if( !(l >= beg && r <= en ) )
		{	
			qry(pos<<1 , l , m(l,r) , beg, en, interval, ok ) ;
			qry(pos<<1|1 , m(l,r)+1, r, beg, en, interval, ok ) ;
			return ;
		}

		int low = 0 , high = sz(tree[pos])-1 , best = interval.second+1 , mid ;

		while(low <= high )
		{
			mid = (low+high)>>1 ;

			if(tree[pos][mid] >= interval.first )
			{
				best = tree[pos][mid] ;
				high = mid-1 ;
			}
			else low = mid+1 ;

		}

		ok |= ( best <= interval.second ) ;

	}

} seg ;

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 = sz(X) ;

	vector< vector<int> > biggerThanMe(N, vector<int>() ) ;
	vector< vector<int> > smallerThanMe(N, vector<int>() ) ;

	for(int i = 0 ; i < M ; i++ )
	{
		if(X[i] > Y[i] ) swap(X[i], Y[i]) ;

		biggerThanMe[X[i]].push_back(Y[i]) ;
		smallerThanMe[Y[i]].push_back(X[i]) ;	

	}
	
	for(int i = 0 ; i < N ; i++ )
	{
		leftToRight.label[i] = i ;
		rightToLeft.label[i] = -i ;
	}
	
	for(int i = 0 ; i < N ; i++ ) 
		for(auto neighbor : smallerThanMe[i] ) 
			leftToRight.join(neighbor, i) ;

	leftToRight.dfs(N-1) ;

	for(int i = N-1 ; i >= 0 ; i-- )
		for(auto neighbor : biggerThanMe[i] )
			rightToLeft.join(neighbor, i) ;

  	rightToLeft.dfs(0) ; 

	vector<int> inRightToLeft(N) ;

	for(int i = 0 ; i < N ; i++ )
		inRightToLeft[ leftToRight.tin[i] ] = rightToLeft.tin[i] ; 

	for(int i= 0 ; i < N ; i++ ) seg.upd(1,0,N-1, inRightToLeft[i], i ) ;

	vector<int> ans ;

	for(int i = 0 ; i < sz(S) ; i++ )
	{
		
		if( !( S[i] >= L[i] && E[i] <= R[i]) )
		{
			ans.push_back(0) ;
			continue ;
		}

		int bestAncestor = S[i] ;

		for(int j = LOG-1 ; j >= 0 ; j-- )
			if( rightToLeft.dp[j][bestAncestor] >= L[i] )
				bestAncestor = rightToLeft.dp[j][bestAncestor] ;

		pair<int,int> intervalHuman = make_pair( rightToLeft.tin[bestAncestor], rightToLeft.tout[bestAncestor] ) ;

		bestAncestor = E[i] ;

		for(int j = LOG-1 ; j >= 0 ; j-- )
		{
			int anc = leftToRight.dp[j][bestAncestor] ;
			if(anc != -1 && anc <= R[i] ) bestAncestor=anc; 
		}

		pair<int,int> intervalWolf = make_pair( leftToRight.tin[bestAncestor], leftToRight.tout[bestAncestor] ) ;

		bool ok = false ;

		seg.qry(1, 0, N-1, intervalHuman.first, intervalHuman.second, intervalWolf, ok ) ;

		ans.push_back( ok ? 1 : 0 ) ;

	}

	return ans ;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 62956 KB Output is correct
2 Correct 38 ms 63212 KB Output is correct
3 Correct 37 ms 62956 KB Output is correct
4 Correct 37 ms 62956 KB Output is correct
5 Correct 43 ms 62956 KB Output is correct
6 Correct 39 ms 62956 KB Output is correct
7 Correct 38 ms 62956 KB Output is correct
8 Correct 37 ms 62956 KB Output is correct
9 Correct 38 ms 62956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 62956 KB Output is correct
2 Correct 38 ms 63212 KB Output is correct
3 Correct 37 ms 62956 KB Output is correct
4 Correct 37 ms 62956 KB Output is correct
5 Correct 43 ms 62956 KB Output is correct
6 Correct 39 ms 62956 KB Output is correct
7 Correct 38 ms 62956 KB Output is correct
8 Correct 37 ms 62956 KB Output is correct
9 Correct 38 ms 62956 KB Output is correct
10 Correct 47 ms 64108 KB Output is correct
11 Correct 48 ms 64108 KB Output is correct
12 Correct 45 ms 64108 KB Output is correct
13 Correct 47 ms 64364 KB Output is correct
14 Correct 47 ms 64364 KB Output is correct
15 Correct 51 ms 64364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1503 ms 140792 KB Output is correct
2 Correct 1867 ms 145620 KB Output is correct
3 Correct 1775 ms 142188 KB Output is correct
4 Correct 1617 ms 141140 KB Output is correct
5 Correct 1655 ms 141024 KB Output is correct
6 Correct 1567 ms 140884 KB Output is correct
7 Correct 1351 ms 140700 KB Output is correct
8 Correct 1704 ms 145672 KB Output is correct
9 Correct 973 ms 142316 KB Output is correct
10 Correct 852 ms 141016 KB Output is correct
11 Correct 919 ms 141012 KB Output is correct
12 Correct 779 ms 140844 KB Output is correct
13 Correct 1688 ms 155588 KB Output is correct
14 Correct 1628 ms 155480 KB Output is correct
15 Correct 1682 ms 155608 KB Output is correct
16 Correct 1681 ms 155608 KB Output is correct
17 Correct 1335 ms 140764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 62956 KB Output is correct
2 Correct 38 ms 63212 KB Output is correct
3 Correct 37 ms 62956 KB Output is correct
4 Correct 37 ms 62956 KB Output is correct
5 Correct 43 ms 62956 KB Output is correct
6 Correct 39 ms 62956 KB Output is correct
7 Correct 38 ms 62956 KB Output is correct
8 Correct 37 ms 62956 KB Output is correct
9 Correct 38 ms 62956 KB Output is correct
10 Correct 47 ms 64108 KB Output is correct
11 Correct 48 ms 64108 KB Output is correct
12 Correct 45 ms 64108 KB Output is correct
13 Correct 47 ms 64364 KB Output is correct
14 Correct 47 ms 64364 KB Output is correct
15 Correct 51 ms 64364 KB Output is correct
16 Correct 1503 ms 140792 KB Output is correct
17 Correct 1867 ms 145620 KB Output is correct
18 Correct 1775 ms 142188 KB Output is correct
19 Correct 1617 ms 141140 KB Output is correct
20 Correct 1655 ms 141024 KB Output is correct
21 Correct 1567 ms 140884 KB Output is correct
22 Correct 1351 ms 140700 KB Output is correct
23 Correct 1704 ms 145672 KB Output is correct
24 Correct 973 ms 142316 KB Output is correct
25 Correct 852 ms 141016 KB Output is correct
26 Correct 919 ms 141012 KB Output is correct
27 Correct 779 ms 140844 KB Output is correct
28 Correct 1688 ms 155588 KB Output is correct
29 Correct 1628 ms 155480 KB Output is correct
30 Correct 1682 ms 155608 KB Output is correct
31 Correct 1681 ms 155608 KB Output is correct
32 Correct 1335 ms 140764 KB Output is correct
33 Correct 1972 ms 140868 KB Output is correct
34 Correct 446 ms 93676 KB Output is correct
35 Correct 2172 ms 144416 KB Output is correct
36 Correct 1897 ms 141728 KB Output is correct
37 Correct 2140 ms 143448 KB Output is correct
38 Correct 1982 ms 142528 KB Output is correct
39 Correct 1740 ms 155864 KB Output is correct
40 Correct 1564 ms 153224 KB Output is correct
41 Correct 1631 ms 142520 KB Output is correct
42 Correct 1051 ms 141656 KB Output is correct
43 Correct 2308 ms 151272 KB Output is correct
44 Correct 2005 ms 143296 KB Output is correct
45 Correct 1464 ms 156044 KB Output is correct
46 Correct 1729 ms 155864 KB Output is correct
47 Correct 1651 ms 155568 KB Output is correct
48 Correct 1621 ms 155312 KB Output is correct
49 Correct 1685 ms 155736 KB Output is correct
50 Correct 1647 ms 155476 KB Output is correct
51 Correct 1384 ms 152564 KB Output is correct
52 Correct 1356 ms 152536 KB Output is correct