#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
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++){
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
6 ms |
4556 KB |
Output is correct |
3 |
Correct |
6 ms |
4556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
37220 KB |
Output is correct |
2 |
Correct |
86 ms |
33456 KB |
Output is correct |
3 |
Correct |
84 ms |
35668 KB |
Output is correct |
4 |
Correct |
94 ms |
37212 KB |
Output is correct |
5 |
Correct |
87 ms |
37060 KB |
Output is correct |
6 |
Correct |
85 ms |
37092 KB |
Output is correct |
7 |
Correct |
91 ms |
36472 KB |
Output is correct |
8 |
Correct |
94 ms |
37444 KB |
Output is correct |
9 |
Correct |
74 ms |
37460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
768 ms |
110580 KB |
Output is correct |
2 |
Correct |
775 ms |
111008 KB |
Output is correct |
3 |
Correct |
807 ms |
110672 KB |
Output is correct |
4 |
Correct |
758 ms |
110132 KB |
Output is correct |
5 |
Correct |
757 ms |
109776 KB |
Output is correct |
6 |
Correct |
776 ms |
110956 KB |
Output is correct |
7 |
Correct |
682 ms |
111088 KB |
Output is correct |
8 |
Correct |
674 ms |
110672 KB |
Output is correct |