#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359;
using u128 = __uint128_t;
const int x[4] = {1, 0, -1, 0};
const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7;
const int mxn = 3e4 + 69, mxq = 1e5 + 5, sq = 500, mxv = 2e7 + 5;
//const int base = (1 <<18);
const int inf = 1e9 + 5, neg = -69420;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
//const int x[9] = {0, 1, 1, -1, -1, 2, -2, 2, -2};
//const inty[9] = {0, 2, -2, 2, -2, 1, 1, -1, -1};
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int n, k, q;
vt<int>adj[mxn + 1];
int tin[mxn + 1], tout[mxn + 1], dep[mxn + 1], up[mxn + 1][17], tt = 0;
struct BIT{
int bit[mxn + 1];
void upd(int p, int v){
while(p <= n){
bit[p] += v; p += p & (-p);
}
}
int get(int p){
int ans = 0;
while(p){
ans += bit[p]; p -= p & (-p);
}
return(ans);
}
};
BIT bit[21][21];
void dfs(int s, int pre){
tin[s] = ++tt;
for(auto v: adj[s]){
if(v != pre){
up[v][0] = s; dep[v] = dep[s] + 1;
dfs(v, s);
}
}
tout[s] = tt;
}
int lca(int u, int v){
if(dep[u] < dep[v])swap(u, v);
int k = dep[u] - dep[v];
forr(i, 0, 17){
if(k & (1 << i))u = up[u][i];
}
if(u == v)return(u);
dorr(i, 16, 0){
if(up[u][i] != up[v][i]){
u = up[u][i]; v = up[v][i];
}
}
return(up[u][0]);
}
void upd(int u, int v, int type){
int lc = lca(u, v);
vt<int>pathu, pathv;
int remu = u, remv = v;
while(u != lc){
pathu.pb(u); u = up[u][0];
}
while(v != lc){
pathv.pb(v); v = up[v][0];
}
pathu.pb(lc);
reverse(ALL(pathv));
for(auto i: pathv)pathu.pb(i);
int len = sz(pathu);
assert(len <= 20);
for(int i = 0; i < sz(pathu); i++){
//cout << pathu[i] << " " << tin[pathu[i]] << "\n";
bit[len][i].upd(tin[pathu[i]], 1 * type); bit[len][i].upd(tout[pathu[i]] + 1, -1 * type);
}
}
ll solve(int u, int v, ll t1,ll t2){
int lc = lca(u, v);
ll ans = 0;
for(int i = 1; i <= 20; i++){
for(int j = 0; j < min(t2, i); j++){
ll cntver = bit[i][j].get(tin[u]) + bit[i][j].get(tin[v]) - bit[i][j].get(tin[lc]) - bit[i][j].get(tin[up[lc][0]]);
ll cnt = (t2 - j) / i + 1;
if(t1 - 1 - j >= 0){
cnt -= (t1 - 1 - j) / i + 1;
}
//cout << cnt << " ";
ans += cnt * cntver;
}
}
return(ans);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("COLLEGE.INP", "r", stdin);
//freopen("COLLEGE.OUT", "w", stdout);
cin >> n;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
dfs(1, -1);
for(int i = 1; i < 16; i++){
for(int j = 1; j <= n; j++){
up[j][i] = up[up[j][i - 1]][i - 1];
}
}
cin >> k;
for(int i = 0; i < k; i++){
int u, v; cin >> u >> v;
upd(u, v, 1);
}
cin >> q;
while(q--){
int idq; cin >> idq;
if(idq == 1){
int u, v; cin >> u >> v; upd(u, v, 1);
}else if(idq == 2){
int u, v; cin >> u >> v; upd(u, v, -1);
}else{
int u, v, t1, t2; cin >> u >> v >> t1 >> t2;
cout << solve(u, v, t1, t2) << "\n";
}
}
return(0);
}
Compilation message
traffickers.cpp: In function 'void upd(int, int, int)':
traffickers.cpp:80:9: warning: unused variable 'remu' [-Wunused-variable]
80 | int remu = u, remv = v;
| ^~~~
traffickers.cpp:80:19: warning: unused variable 'remv' [-Wunused-variable]
80 | int remu = u, remv = v;
| ^~~~
traffickers.cpp: In function 'long long int solve(int, int, long long int, long long int)':
traffickers.cpp:108:37: error: no matching function for call to 'min(long long int&, int&)'
108 | for(int j = 0; j < min(t2, i); j++){
| ^
In file included from /usr/include/c++/10/bits/char_traits.h:39,
from /usr/include/c++/10/ios:40,
from /usr/include/c++/10/istream:38,
from /usr/include/c++/10/sstream:38,
from /usr/include/c++/10/complex:45,
from /usr/include/c++/10/ccomplex:39,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
from traffickers.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
230 | min(const _Tp& __a, const _Tp& __b)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: template argument deduction/substitution failed:
traffickers.cpp:108:37: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
108 | for(int j = 0; j < min(t2, i); j++){
| ^
In file included from /usr/include/c++/10/bits/char_traits.h:39,
from /usr/include/c++/10/ios:40,
from /usr/include/c++/10/istream:38,
from /usr/include/c++/10/sstream:38,
from /usr/include/c++/10/complex:45,
from /usr/include/c++/10/ccomplex:39,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
from traffickers.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
278 | min(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: template argument deduction/substitution failed:
traffickers.cpp:108:37: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
108 | for(int j = 0; j < min(t2, i); j++){
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from traffickers.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(std::initializer_list<_Tp>)'
3468 | min(initializer_list<_Tp> __l)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: template argument deduction/substitution failed:
traffickers.cpp:108:37: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
108 | for(int j = 0; j < min(t2, i); j++){
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from traffickers.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(std::initializer_list<_Tp>, _Compare)'
3474 | min(initializer_list<_Tp> __l, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: template argument deduction/substitution failed:
traffickers.cpp:108:37: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
108 | for(int j = 0; j < min(t2, i); j++){
| ^