#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
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
121936 KB |
Output is correct |
2 |
Correct |
17 ms |
117072 KB |
Output is correct |
3 |
Correct |
20 ms |
118928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
131288 KB |
Output is correct |
2 |
Correct |
79 ms |
135248 KB |
Output is correct |
3 |
Correct |
95 ms |
127396 KB |
Output is correct |
4 |
Correct |
85 ms |
133704 KB |
Output is correct |
5 |
Correct |
88 ms |
121424 KB |
Output is correct |
6 |
Correct |
92 ms |
132936 KB |
Output is correct |
7 |
Correct |
87 ms |
120588 KB |
Output is correct |
8 |
Correct |
81 ms |
122960 KB |
Output is correct |
9 |
Correct |
68 ms |
114760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1147 ms |
140628 KB |
Output is correct |
2 |
Correct |
1094 ms |
139336 KB |
Output is correct |
3 |
Correct |
1042 ms |
141268 KB |
Output is correct |
4 |
Correct |
1295 ms |
139324 KB |
Output is correct |
5 |
Correct |
1342 ms |
133192 KB |
Output is correct |
6 |
Correct |
1097 ms |
131912 KB |
Output is correct |
7 |
Correct |
761 ms |
131012 KB |
Output is correct |
8 |
Correct |
704 ms |
135304 KB |
Output is correct |