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 fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 60010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
struct BIT
{
int t[maxn];
void up(int x,int val)
{
for( ; x < maxn; x += lb(x)) t[x] += val;
}
int get(int x)
{
int ans = 0;
for( ; x ; x -= lb(x)) ans += t[x];
return ans;
}
}t[21][21];
vi ke[maxn];
int n;
int pos[maxn],tail[maxn];
int cnt;
int par[maxn][16];
int h[maxn];
void dfs(int u)
{
pos[u] = ++cnt;
for(int v : ke[u]) if(v != par[u][0])
{
h[v] = h[u] + 1;
par[v][0] = u;
fo(i,1,15) par[v][i] = par[par[v][i - 1]][i - 1];
dfs(v);
}
tail[u] = ++cnt;
}
int lca(int u, int v)
{
if (h[u] < h[v])
swap(u, v);
int del = h[u] - h[v];
fod(i, 15, 0) if (bit(del, i)) u = par[u][i];
if (u == v)
return u;
fod(i, 15, 0) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
return par[u][0];
}
void update(int u,int v,int val)
{
int x = lca(u,v);
vi node;
while(u != x)
{
node.pb(u);
u = par[u][0];
}
node.pb(x);
vi node1;
while(v != x)
{
node1.pb(v);
v = par[v][0];
}
reverse(all(node1));
for(int it : node1) node.pb(it);
int d = ss(node);
fo(i,0,d - 1)
{
int it = node[i];
t[d][i].up(pos[it],val);
t[d][i].up(tail[it] + 1,-val);
}
}
int get(int u,int tt)
{
int ans = 0;
fo(d,1,20)
{
fo(r,0,d - 1)
{
int x = t[d][r].get(pos[u]);
ans += x * (tt / d) + x * (tt % d >= r);
}
}
return ans;
}
int get(int u,int v,int t)
{
int l = lca(u,v);
return get(u,t) + get(v,t) - get(l,t) - get(par[l][0],t);
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
fo(i,1,n - 1)
{
int u,v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
dfs(1);
int m;cin >> m;
fo(i,1,m)
{
int u,v;
cin >> u >> v;
update(u,v,1);
}
int q;cin >> q;
while(q--)
{
int ty;
cin >> ty;
if(ty <= 2)
{
int u,v;
cin >> u >> v;
int val = (ty == 1 ? 1 : -1);
update(u,v,val);
}
else
{
int u,v,l,r;
cin >> u >> v >> l >> r;
cout << get(u,v,r) - get(u,v,l - 1);en;
}
}
}
Compilation message (stderr)
traffickers.cpp:145:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
145 | main()
| ^~~~
traffickers.cpp: In function 'int main()':
traffickers.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
traffickers.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |