Submission #381761

# Submission time Handle Problem Language Result Execution time Memory
381761 2021-03-25T20:51:18 Z MohamedAhmed04 Traffickers (RMI18_traffickers) C++14
100 / 100
3091 ms 55796 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 3e4 + 10 ;

int arr[MAX] ;
int n , m , q ;

vector< vector<int> >adj(MAX) ;

int tim = 0 ;
int in[MAX] , out[MAX] , P[MAX][16] ;
int dep[MAX] ;
int bit[22][22][2*MAX] ;

void Add(int a , int b , int x , int val)
{
	for(int i = x ; i <= 2*n ; i += (i & (-i)))
		bit[a][b][i] += val ;
}

int Query(int a , int b , int x)
{
	int cnt = 0 ;
	for(int i = x ; i ; i -= (i & (-i)))
		cnt += bit[a][b][i] ;
	return cnt ;
}

void dfs(int node , int par)
{
	in[node] = ++tim ;
	P[node][0] = par ;
	for(int j = 1 ; j < 16 ; ++j)
		P[node][j] = P[P[node][j-1]][j-1] ;
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		dep[child] = dep[node] + 1 ;
		dfs(child , node) ;
	}
	out[node] = ++tim ;
}

int LCA(int x , int y)
{
	if(dep[x] < dep[y])
		swap(x , y) ;
	for(int j = 15 ; j >= 0 ; --j)
	{
		if(dep[x] - (1 << j) >= dep[y])
			x = P[x][j] ;
	}
	if(x == y)
		return x ;
	for(int j = 15 ; j >= 0 ; --j)
	{
		if(P[x][j] != P[y][j])
			x = P[x][j] , y = P[y][j] ;
	}
	return P[x][0] ;
}

vector<int>v , v1 , v2 ;

vector<int> getPath(int x , int y)
{
	v1.clear() , v2.clear() ;
	int lca = LCA(x , y) ;
	while(x != lca)
		v1.push_back(x) , x = P[x][0] ;
	v1.push_back(lca) ;
	while(y != lca)
		v2.push_back(y) , y = P[y][0] ;
	reverse(v2.begin() , v2.end()) ;
	for(auto &i : v2)
		v1.push_back(i) ;
	return v1 ;
}

void modify(int x , int y , int val)
{
	v = getPath(x , y) ;
	for(int i = 0 ; i < v.size() ; ++i)
	{
		Add(v.size() , i , in[v[i]] , val) ;
		Add(v.size() , i , out[v[i]] , -val) ;
	}
}

long long solve(int x , int y , int t)
{
	if(t < 0)
		return 0ll ;
	int lca = LCA(x , y) ;
	long long cnt = 0 ;
	for(int i = 1 ; i <= 20 ; ++i)
	{
		for(int j = 0 ; j < min(i , t+1) ; ++j)
		{
			long long a = 1ll + (t - j) / i ;
			long long b = Query(i , j , in[x]) - Query(i , j , in[P[lca][0]]) ;
			b += Query(i , j , in[y]) - Query(i , j , in[lca]) ;
			cnt += a * b ;
		}
	}
	return cnt ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n ;
	for(int i = 0 ; i < n-1 ; ++i)
	{
		int x , y ;
		cin>>x>>y ;
		adj[x].push_back(y) ;
		adj[y].push_back(x) ;
	}
	dfs(1 , -1) ;
	cin>>m ;
	for(int i = 0 ; i < m ; ++i)
	{
		int x , y ;
		cin>>x>>y ;
		modify(x , y , 1) ;
	}
	cin>>q ;
	while(q--)
	{
		int ty ;
		cin>>ty ;
		if(ty == 1)
		{
			int x , y ;
			cin>>x>>y ;
			modify(x , y , 1) ;
		}
		else if(ty == 2)
		{
			int x , y ;
			cin>>x>>y ;
			modify(x , y , -1) ;
		}
		else if(ty == 3)
		{
			int x , y , l , r ;
			cin>>x>>y>>l>>r ;
			cout<<solve(x , y , r) - solve(x , y , l-1)<<"\n" ;
		}
	}
	return 0 ;
}		

Compilation message

traffickers.cpp: In function 'void modify(int, int, int)':
traffickers.cpp:86:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |  for(int i = 0 ; i < v.size() ; ++i)
      |                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2028 KB Output is correct
2 Correct 6 ms 3820 KB Output is correct
3 Correct 6 ms 3820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 209 ms 19820 KB Output is correct
2 Correct 209 ms 17900 KB Output is correct
3 Correct 200 ms 19180 KB Output is correct
4 Correct 229 ms 19820 KB Output is correct
5 Correct 212 ms 19692 KB Output is correct
6 Correct 213 ms 19960 KB Output is correct
7 Correct 216 ms 19464 KB Output is correct
8 Correct 200 ms 19948 KB Output is correct
9 Correct 168 ms 19948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2581 ms 55168 KB Output is correct
2 Correct 2625 ms 55796 KB Output is correct
3 Correct 2430 ms 55220 KB Output is correct
4 Correct 2792 ms 54560 KB Output is correct
5 Correct 3091 ms 54252 KB Output is correct
6 Correct 2463 ms 55452 KB Output is correct
7 Correct 2366 ms 55792 KB Output is correct
8 Correct 2286 ms 55584 KB Output is correct