Submission #817865

# Submission time Handle Problem Language Result Execution time Memory
817865 2023-08-09T18:18:35 Z MohamedAhmed04 Inside information (BOI21_servers) C++14
87.5 / 100
3500 ms 49092 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 3e5 + 10 ;
const int B = 480 ;

int n , q ;

vector< vector< pair<int , int> > >adj(MAX) ;
vector< vector<int> >adj2(MAX) ;

int val[MAX] , up_inc[MAX] , up_dec[MAX] ;
int dep[MAX] , P[MAX][18] ;

int in[MAX] , out[MAX] ;
int tim = 0 ;

vector<int>order ;

void dfs(int node , int par)
{
	in[node] = ++tim , order.push_back(node) ;
	P[node][0] = par ;
	for(int i = 1 ; i < 18 ; ++i)
		P[node][i] = P[P[node][i-1]][i-1] ;
	up_inc[node] = up_dec[node] = par ;
	if(val[node] < val[par])
		up_inc[node] = up_inc[par] ;
	else
		up_dec[node] = up_dec[par] ;
	for(auto &childd : adj[node])
	{
		int child = childd.first , y = childd.second ;
		if(child == par)
			continue ;
		dep[child] = dep[node] + 1 , val[child] = y ;
		dfs(child , node) ;
	}
	out[node] = tim ;
}

bool Ancestor(int x , int y)
{
	return (in[y] >= in[x] && in[y] <= out[x]) ;
}

bool check(int x , int y , int tim) // x has server y
{
	if(x == y)
		return true ;
	if((!Ancestor(up_inc[y] , x)))
		return false ;
	if((!Ancestor(up_dec[x] , y)))
		return false ;
	if(Ancestor(y , x))
		return (val[x] <= tim) ;
	if((!Ancestor(x , y)) && val[x] > tim)
		return false ;
	bool flag = false ;
	if(dep[x] < dep[y])
		swap(x , y) , flag = true ;
	for(int k = 17 ; k >= 0 ; --k)
	{
		if(dep[x] - (1 << k) > dep[y])
			x = P[x][k] ;
	}
	if(P[x][0] == y)
		return (val[x] <= tim) ;
	if(dep[x] != dep[y])
		x = P[x][0] ;
	if(flag)
		swap(x , y) ;
	for(int k = 17 ; k >= 0 ; --k)
	{
		if(P[x][k] != P[y][k])
			x = P[x][k] , y = P[y][k] ;
	}
	return (val[x] > val[y]) ;
}

int dp[MAX] , dpup[MAX] , dpdown[MAX] ;
int vis[MAX] ;

void dfs2(int node , int par)
{
	vis[node] = 1 ;
	dpdown[node] = dp[node] = 1 ;
	for(auto &child : adj2[node])
	{
		if(child == par)
			continue ;
		dfs2(child , node) ;
		if(val[child] > val[node])
			dpdown[node] += dpdown[child] ;
		dp[node] += dpdown[child] ;
	}
}

void dfs3(int node , int par , int sum)
{
	dpup[node] = sum + (par != node) ;
	if(val[par] > val[node])
		dpup[node] += dpup[par] ;
	dp[node] += dpup[node] ;
	int sz = adj2[node].size() , sum2 = 0 ;
	for(int i = sz-1 ; i >= 0 ; --i)
	{
		int child = adj2[node][i] ;
		if(child == par)
			continue ;
		dfs3(child , node , sum2) ;
		sum2 += dpdown[child] ;
	}
}

void calc()
{
	for(int i = 1 ; i <= n ; ++i)
		vis[i] = 0 ;
	for(auto &node : order)
	{
		if(vis[node])
			continue ;
		dfs2(node , node) ;
		dfs3(node , node , 0) ;
	}
}

char ch[MAX] ;
int X[MAX] , Y[MAX] ;

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>q ;
	for(int i = 1 ; i <= n+q-1 ; ++i)
	{
		cin>>ch[i] ;
		if(ch[i] == 'S')
		{
			cin>>X[i]>>Y[i] ;
			adj[X[i]].emplace_back(Y[i] , i) ;
			adj[Y[i]].emplace_back(X[i] , i) ;
		}
		else if(ch[i] == 'Q')
			cin>>X[i]>>Y[i] ;
		else if(ch[i] == 'C')
			cin>>X[i] ;
	}
	dfs(1 , 1) ;
	for(int i = 1 ; i <= n ; ++i)
		dp[i] = 1 ;
	int cnt = 0 ;
	vector<int>v ;
	for(int i = 1 ; i <= n+q-1 ; ++i)
	{
		if(cnt == B)
			calc() , cnt = 0 , v.clear() ;
		if(ch[i] == 'S')
		{
			v.push_back(i) ;
			adj2[X[i]].push_back(Y[i]) ;
			adj2[Y[i]].push_back(X[i]) ;
			cnt++ ;
		}
		else if(ch[i] == 'Q')
		{
			if(check(X[i] , Y[i] , i))
				cout<<"yes\n" ;
			else
				cout<<"no\n" ;
		}
		else if(ch[i] == 'C')
		{
			int ans = dp[X[i]] ;
			for(auto &j : v)
			{
				int a = X[j] , b = Y[j] ;
				if(val[a] != j)
					swap(a , b) ;
				if(check(a , X[i] , i) && (!Ancestor(a , X[i])))
					ans++ ;
				else if(check(b , X[i] , i) && Ancestor(a , X[i]))
					ans++ ;
			}
			cout<<ans<<"\n" ;
		}
	}
	return 0 ;
}
# Verdict Execution time Memory Grader output
1 Correct 35 ms 15892 KB Output is correct
2 Correct 28 ms 16576 KB Output is correct
3 Correct 49 ms 16788 KB Output is correct
4 Correct 28 ms 16748 KB Output is correct
5 Correct 27 ms 16872 KB Output is correct
6 Correct 32 ms 16648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 15892 KB Output is correct
2 Correct 28 ms 16576 KB Output is correct
3 Correct 49 ms 16788 KB Output is correct
4 Correct 28 ms 16748 KB Output is correct
5 Correct 27 ms 16872 KB Output is correct
6 Correct 32 ms 16648 KB Output is correct
7 Correct 31 ms 15856 KB Output is correct
8 Correct 152 ms 16544 KB Output is correct
9 Correct 389 ms 16748 KB Output is correct
10 Correct 196 ms 16656 KB Output is correct
11 Correct 127 ms 16756 KB Output is correct
12 Correct 628 ms 16800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 15904 KB Output is correct
2 Correct 678 ms 39460 KB Output is correct
3 Correct 668 ms 39552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 15904 KB Output is correct
2 Correct 678 ms 39460 KB Output is correct
3 Correct 668 ms 39552 KB Output is correct
4 Correct 42 ms 15856 KB Output is correct
5 Correct 1030 ms 39504 KB Output is correct
6 Correct 822 ms 39816 KB Output is correct
7 Correct 1951 ms 39804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 15828 KB Output is correct
2 Correct 835 ms 45596 KB Output is correct
3 Correct 795 ms 45644 KB Output is correct
4 Correct 649 ms 47268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 15828 KB Output is correct
2 Correct 835 ms 45596 KB Output is correct
3 Correct 795 ms 45644 KB Output is correct
4 Correct 649 ms 47268 KB Output is correct
5 Correct 25 ms 15836 KB Output is correct
6 Correct 965 ms 45636 KB Output is correct
7 Correct 1296 ms 46180 KB Output is correct
8 Correct 1173 ms 45456 KB Output is correct
9 Correct 1072 ms 45472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 15820 KB Output is correct
2 Correct 443 ms 39196 KB Output is correct
3 Correct 1022 ms 39128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 15820 KB Output is correct
2 Correct 443 ms 39196 KB Output is correct
3 Correct 1022 ms 39128 KB Output is correct
4 Correct 28 ms 15860 KB Output is correct
5 Correct 951 ms 39092 KB Output is correct
6 Correct 1012 ms 39036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15876 KB Output is correct
2 Correct 852 ms 45644 KB Output is correct
3 Correct 788 ms 45652 KB Output is correct
4 Correct 659 ms 47312 KB Output is correct
5 Correct 22 ms 15840 KB Output is correct
6 Correct 516 ms 39000 KB Output is correct
7 Correct 994 ms 39268 KB Output is correct
8 Correct 1327 ms 40332 KB Output is correct
9 Correct 1199 ms 40508 KB Output is correct
10 Correct 1885 ms 41844 KB Output is correct
11 Correct 1842 ms 41652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15876 KB Output is correct
2 Correct 852 ms 45644 KB Output is correct
3 Correct 788 ms 45652 KB Output is correct
4 Correct 659 ms 47312 KB Output is correct
5 Correct 22 ms 15840 KB Output is correct
6 Correct 516 ms 39000 KB Output is correct
7 Correct 994 ms 39268 KB Output is correct
8 Correct 1327 ms 40332 KB Output is correct
9 Correct 1199 ms 40508 KB Output is correct
10 Correct 1885 ms 41844 KB Output is correct
11 Correct 1842 ms 41652 KB Output is correct
12 Correct 24 ms 15920 KB Output is correct
13 Correct 938 ms 45556 KB Output is correct
14 Correct 1316 ms 46108 KB Output is correct
15 Correct 1061 ms 45404 KB Output is correct
16 Correct 1145 ms 45440 KB Output is correct
17 Correct 37 ms 15800 KB Output is correct
18 Correct 950 ms 39052 KB Output is correct
19 Correct 1074 ms 39016 KB Output is correct
20 Correct 1327 ms 40220 KB Output is correct
21 Correct 1260 ms 43196 KB Output is correct
22 Execution timed out 3567 ms 43732 KB Time limit exceeded
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 23 ms 15836 KB Output is correct
2 Correct 27 ms 16596 KB Output is correct
3 Correct 31 ms 16752 KB Output is correct
4 Correct 28 ms 16640 KB Output is correct
5 Correct 27 ms 16852 KB Output is correct
6 Correct 35 ms 16716 KB Output is correct
7 Correct 23 ms 15820 KB Output is correct
8 Correct 638 ms 39492 KB Output is correct
9 Correct 638 ms 39480 KB Output is correct
10 Correct 21 ms 15828 KB Output is correct
11 Correct 791 ms 45668 KB Output is correct
12 Correct 795 ms 45616 KB Output is correct
13 Correct 639 ms 47288 KB Output is correct
14 Correct 22 ms 15820 KB Output is correct
15 Correct 452 ms 38956 KB Output is correct
16 Correct 891 ms 39136 KB Output is correct
17 Correct 1071 ms 40316 KB Output is correct
18 Correct 1094 ms 40392 KB Output is correct
19 Correct 1823 ms 41884 KB Output is correct
20 Correct 1874 ms 41612 KB Output is correct
21 Correct 689 ms 39568 KB Output is correct
22 Correct 713 ms 39564 KB Output is correct
23 Correct 862 ms 40232 KB Output is correct
24 Correct 847 ms 40308 KB Output is correct
25 Correct 1049 ms 40896 KB Output is correct
26 Correct 1248 ms 39056 KB Output is correct
27 Correct 1089 ms 39264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 15836 KB Output is correct
2 Correct 27 ms 16596 KB Output is correct
3 Correct 31 ms 16752 KB Output is correct
4 Correct 28 ms 16640 KB Output is correct
5 Correct 27 ms 16852 KB Output is correct
6 Correct 35 ms 16716 KB Output is correct
7 Correct 23 ms 15820 KB Output is correct
8 Correct 638 ms 39492 KB Output is correct
9 Correct 638 ms 39480 KB Output is correct
10 Correct 21 ms 15828 KB Output is correct
11 Correct 791 ms 45668 KB Output is correct
12 Correct 795 ms 45616 KB Output is correct
13 Correct 639 ms 47288 KB Output is correct
14 Correct 22 ms 15820 KB Output is correct
15 Correct 452 ms 38956 KB Output is correct
16 Correct 891 ms 39136 KB Output is correct
17 Correct 1071 ms 40316 KB Output is correct
18 Correct 1094 ms 40392 KB Output is correct
19 Correct 1823 ms 41884 KB Output is correct
20 Correct 1874 ms 41612 KB Output is correct
21 Correct 689 ms 39568 KB Output is correct
22 Correct 713 ms 39564 KB Output is correct
23 Correct 862 ms 40232 KB Output is correct
24 Correct 847 ms 40308 KB Output is correct
25 Correct 1049 ms 40896 KB Output is correct
26 Correct 1248 ms 39056 KB Output is correct
27 Correct 1089 ms 39264 KB Output is correct
28 Correct 35 ms 15816 KB Output is correct
29 Correct 152 ms 16520 KB Output is correct
30 Correct 384 ms 16712 KB Output is correct
31 Correct 161 ms 17796 KB Output is correct
32 Correct 103 ms 17848 KB Output is correct
33 Correct 578 ms 17808 KB Output is correct
34 Correct 40 ms 16724 KB Output is correct
35 Correct 998 ms 42396 KB Output is correct
36 Correct 883 ms 41496 KB Output is correct
37 Correct 1729 ms 41780 KB Output is correct
38 Correct 24 ms 16732 KB Output is correct
39 Correct 957 ms 48492 KB Output is correct
40 Correct 1304 ms 49092 KB Output is correct
41 Correct 1071 ms 48124 KB Output is correct
42 Correct 1066 ms 48112 KB Output is correct
43 Correct 28 ms 16716 KB Output is correct
44 Correct 986 ms 41924 KB Output is correct
45 Correct 1067 ms 41896 KB Output is correct
46 Correct 1513 ms 43176 KB Output is correct
47 Correct 1335 ms 43320 KB Output is correct
48 Correct 3413 ms 43800 KB Output is correct
49 Correct 2591 ms 46068 KB Output is correct
50 Correct 1887 ms 46080 KB Output is correct
51 Correct 1270 ms 42624 KB Output is correct
52 Correct 1840 ms 42476 KB Output is correct
53 Correct 1866 ms 41968 KB Output is correct
54 Correct 891 ms 42656 KB Output is correct
55 Correct 2037 ms 42400 KB Output is correct
56 Correct 920 ms 43236 KB Output is correct
57 Correct 1325 ms 43452 KB Output is correct
58 Correct 1453 ms 41692 KB Output is correct
59 Correct 1123 ms 42384 KB Output is correct