#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 = 40001;
const ll INF = (1 << 30);
const ll MOD = 1000000007;
const ll BLOCK = 101;
const ll nr_of_bits = 15;
const ll delta = 0.0000001;
int n;
vector <int> v[NMAX];
int dp[NMAX][nr_of_bits + 1];
int pz[NMAX];
int sf[NMAX];
int stamp;
int fnw[NMAX][25][25];
void Precalc(){
for(int j = 1; j <= nr_of_bits; j++){
for(int i = 1; i <= n; i++){
dp[i][j] = dp[dp[i][j - 1]][j - 1];
}
}
}
void DFS(int node, int 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(int a, int b){
return (pz[a] <= pz[b] && sf[b] <= sf[a]);
}
int LCA(int a, int b){
int 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;
}
int query(int node, int t){
int sum = 0;
for(int i = 1; i <= 20; i++){
int rest = t % i + 1;
int cnt = 0;
for(int j = pz[node]; j > 0; j -= j&(-j)){
for(int k = rest; k > 0; k -= k&(-k)){
cnt += fnw[j][i][k];
}
}
sum += cnt * (t / i);
int cnt2 = 0;
for(int j = pz[node]; j > 0; j -= j&(-j)){
for(int k = i; k > 0; k -= k&(-k)){
cnt2 += fnw[j][i][k];
}
}
cnt2 -= cnt;
sum += (t / i - 1) * cnt2;
}
return sum;
}
void baga(int nod, int rest, int imp, int val){
for(int i = nod; i <= n; i += i&(-i)){
for(int j = rest; j <= imp; j += j&(-j))
fnw[i][imp][j] += val;
}
}
void adauga(int u, int v, int c){
vector <int> up;
while(!isParent(u, v)){
up.push_back(u);
u = dp[u][0];
}
vector <int> 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(int 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);
}
}
int sol(int u, int v, int t){
int lca = LCA(u, v);
return query(u, t) + query(v, t) - query(lca, t) - query(dp[lca][0], t);
}
int main() {
int i;
cin >> n;
for(i = 1; i <= n - 1; i++){
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
DFS(1, 0);
Precalc();
int k;
cin >> k;
while(k--){
int a, b;
cin >> a >> b;
adauga(a, b, 1);
}
int q;
cin >> q;
while(q--){
int t;
cin >> t;
if(t == 1){
int a, b;
cin >> a >> b;
adauga(a, b, 1);
}else if(t == 2){
int a, b;
cin >> a >> b;
adauga(a, b, -1);
}else{
int 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(int, int, int)':
traffickers.cpp:107:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i = 0; i < up.size(); i++){
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1388 KB |
Output is correct |
2 |
Correct |
8 ms |
3820 KB |
Output is correct |
3 |
Correct |
8 ms |
3820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
88 ms |
26988 KB |
Output isn't correct |
2 |
Incorrect |
98 ms |
24300 KB |
Output isn't correct |
3 |
Incorrect |
114 ms |
25836 KB |
Output isn't correct |
4 |
Incorrect |
92 ms |
26988 KB |
Output isn't correct |
5 |
Incorrect |
90 ms |
26944 KB |
Output isn't correct |
6 |
Incorrect |
90 ms |
26860 KB |
Output isn't correct |
7 |
Incorrect |
90 ms |
26604 KB |
Output isn't correct |
8 |
Incorrect |
89 ms |
27036 KB |
Output isn't correct |
9 |
Incorrect |
90 ms |
27120 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
844 ms |
78572 KB |
Output isn't correct |
2 |
Incorrect |
872 ms |
79084 KB |
Output isn't correct |
3 |
Incorrect |
864 ms |
78952 KB |
Output isn't correct |
4 |
Incorrect |
886 ms |
78316 KB |
Output isn't correct |
5 |
Incorrect |
909 ms |
78272 KB |
Output isn't correct |
6 |
Incorrect |
857 ms |
79084 KB |
Output isn't correct |
7 |
Incorrect |
742 ms |
79440 KB |
Output isn't correct |
8 |
Incorrect |
752 ms |
78956 KB |
Output isn't correct |