#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> getPath(int x , int y)
{
vector<int>v1 , v2 ;
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)
{
vector<int>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:84:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0 ; i < v.size() ; ++i)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
20204 KB |
Output is correct |
2 |
Correct |
214 ms |
18284 KB |
Output is correct |
3 |
Correct |
191 ms |
19564 KB |
Output is correct |
4 |
Correct |
222 ms |
20204 KB |
Output is correct |
5 |
Correct |
234 ms |
19948 KB |
Output is correct |
6 |
Correct |
226 ms |
20204 KB |
Output is correct |
7 |
Correct |
206 ms |
19820 KB |
Output is correct |
8 |
Correct |
180 ms |
20204 KB |
Output is correct |
9 |
Correct |
164 ms |
20332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2605 ms |
57328 KB |
Output is correct |
2 |
Correct |
2655 ms |
57832 KB |
Output is correct |
3 |
Correct |
2460 ms |
57324 KB |
Output is correct |
4 |
Correct |
2729 ms |
56948 KB |
Output is correct |
5 |
Correct |
3074 ms |
56504 KB |
Output is correct |
6 |
Correct |
2485 ms |
57864 KB |
Output is correct |
7 |
Correct |
2304 ms |
58068 KB |
Output is correct |
8 |
Correct |
2289 ms |
57644 KB |
Output is correct |