This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debug_with_space(x) cerr << #x << " " << x << " "
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <ll, pii> piii;
const ll NMAX = 30001;
const ll INF = (1 << 30);
const ll MOD = 1000000007;
const ll BLOCK = 101;
const ll nr_of_bits = 14;
const ll delta = 0.0000001;
ll n;
vector <ll> v[NMAX];
ll dp[NMAX][nr_of_bits + 1];
ll pz[NMAX];
ll sf[NMAX];
ll stamp;
ll fnw[NMAX][21][21];
void Precalc(){
for(ll j = 1; j <= nr_of_bits; j++){
for(ll i = 1; i <= n; i++){
dp[i][j] = dp[dp[i][j - 1]][j - 1];
}
}
}
void DFS(ll node, ll p){
dp[node][0] = p;
pz[node] = ++stamp;
for(auto x : v[node]){
if(x == p)
continue;
DFS(x, node);
}
sf[node] = stamp;
}
bool isParent(ll a, ll b){
return (pz[a] <= pz[b] && sf[b] <= sf[a]);
}
ll LCA(ll a, ll b){
ll r = a, pas = nr_of_bits;
while(pas >= 0){
if(dp[r][pas] != 0 && !isParent(dp[r][pas], b))
r = dp[r][pas];
pas--;
}
if(!isParent(r, b))
r = dp[r][0];
return r;
}
ll query(ll node, ll t){
ll sum = 0;
for(ll i = 1; i <= 20; i++){
ll rest = t % i + 1;
ll cnt = 0;
for(ll j = pz[node]; j > 0; j -= j&(-j)){
for(ll k = rest; k > 0; k -= k&(-k)){
cnt += fnw[j][i][k];
}
}
sum += cnt * (t / i);
ll cnt2 = 0;
for(ll j = pz[node]; j > 0; j -= j&(-j)){
for(ll k = i; k > 0; k -= k&(-k)){
cnt2 += fnw[j][i][k];
}
}
cnt2 -= cnt;
sum += (t / i - 1) * cnt2;
}
return sum;
}
void baga(ll nod, ll rest, ll imp, ll val){
for(ll i = nod; i <= n; i += i&(-i)){
for(ll j = rest; j <= imp; j += j&(-j))
fnw[i][imp][j] += val;
}
}
void adauga(ll u, ll v, ll c){
vector <ll> up;
while(!isParent(u, v)){
up.push_back(u);
u = dp[u][0];
}
vector <ll> idk;
while(v != u){
idk.push_back(v);
v = dp[v][0];
}
idk.push_back(v);
reverse(idk.begin(), idk.end());
for(auto x : idk)
up.push_back(x);
idk.clear();
for(ll i = 0; i < up.size(); i++){
baga(pz[up[i]], i + 1, up.size(), c);
baga(sf[up[i]] + 1, i + 1, up.size(), -c);
}
}
ll sol(ll u, ll v, ll t){
ll lca = LCA(u, v);
return query(u, t) + query(v, t) - query(lca, t) - query(dp[lca][0], t);
}
int main() {
//ifstream cin("traffickers.in");
//ofstream cout("traffickers.out");
ll i;
cin >> n;
for(i = 1; i <= n - 1; i++){
ll a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
DFS(1, 0);
Precalc();
ll k;
cin >> k;
while(k--){
ll a, b;
cin >> a >> b;
adauga(a, b, 1);
}
ll q;
cin >> q;
while(q--){
ll t;
cin >> t;
if(t == 1){
ll a, b;
cin >> a >> b;
adauga(a, b, 1);
}else if(t == 2){
ll a, b;
cin >> a >> b;
adauga(a, b, -1);
}else{
ll a, b, t1, t2;
cin >> a >> b >> t1 >> t2;
cout << sol(a, b, t2) - sol(a, b, t1 - 1) << "\n";
}
}
return 0;
}
Compilation message (stderr)
traffickers.cpp: In function 'void adauga(ll, ll, ll)':
traffickers.cpp:107:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(ll i = 0; i < up.size(); i++){
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |