#include <stdio.h>
#include <stdlib.h>
#define N 30000
#define D 20
int *ej[N], eo[N], n;
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int dd[N], pp[N], qq[N];
int dfs1(int p, int i, int d) {
int o, s, k_, j_;
dd[i] = d, pp[i] = p;
s = 1, k_ = 0, j_ = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
int k = dfs1(i, j, d + 1);
s += k;
if (k_ < k)
k_ = k, j_ = j;
}
}
qq[i] = j_;
return s;
}
int ta[N], tb[N];
void dfs2(int p, int i, int q) {
static int time;
int o, j_;
ta[i] = time++;
j_ = qq[i], qq[i] = q;
if (j_ != -1)
dfs2(i, j_, q);
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p && j != j_)
dfs2(i, j, j);
}
tb[i] = time;
}
int lca(int i, int j) {
while (qq[i] != qq[j])
if (dd[qq[i]] > dd[qq[j]])
i = pp[qq[i]];
else
j = pp[qq[j]];
return dd[i] < dd[j] ? i : j;
}
void update(int ft[][D + 1], int i, int j_, int n, int m, int x) {
int j;
while (i < n) {
j = j_;
while (j < m) {
ft[i][j] += x;
j |= j + 1;
}
i |= i + 1;
}
}
int query(int ft[][D + 1], int i, int j_) {
int j, x;
x = 0;
while (i >= 0) {
j = j_;
while (j >= 0) {
x += ft[i][j];
j &= j + 1, j--;
}
i &= i + 1, i--;
}
return x;
}
int ft[D + 1][N][D + 1];
void update_(int i, int j, int x) {
static int qu[D + 1];
int a, d, u, h;
a = lca(i, j), d = dd[i] + dd[j] - dd[a] * 2;
u = i;
while (u != a)
qu[dd[i] - dd[u]] = u, u = pp[u];
qu[dd[i] - dd[a]] = a;
u = j;
while (u != a)
qu[dd[i] - dd[a] * 2 + dd[u]] = u, u = pp[u];
for (h = 0; h <= d; h++) {
i = qu[h];
update(ft[1], ta[i], h, n, d + 1, x), update(ft[1], tb[i], h, n, d + 1, -x);
}
}
long long query_(int i, int j, int t) {
int a, d, r;
long long x;
a = lca(i, j);
x = 0;
for (d = 0; d <= D; d++) {
x += (long long) (query(ft[d], ta[i], d) + query(ft[d], ta[j], d) - query(ft[d], ta[a], d) - (pp[a] == -1 ? 0 : query(ft[d], ta[pp[a]], d))) * (t / (d + 1));
r = t % (d + 1);
x += (query(ft[d], ta[i], r - 1) + query(ft[d], ta[j], r - 1) - query(ft[d], ta[a], r - 1) - (pp[a] == -1 ? 0 : query(ft[d], ta[pp[a]], r - 1)));
}
return x;
}
int main() {
int k, q, h, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
dfs1(-1, 0, 0);
dfs2(-1, 0, 0);
scanf("%d", &k);
while (k--) {
scanf("%d%d", &i, &j), i--, j--;
update_(i, j, 1);
}
scanf("%d", &q);
while (q--) {
int t, l, r;
scanf("%d%d%d", &t, &i, &j), i--, j--;
if (t == 1)
update_(i, j, 1);
else if (t == 2)
update_(i, j, -1);
else {
scanf("%d%d", &l, &r), r++;
printf("%lld\n", query_(i, j, r) - query_(i, j, l));
}
}
return 0;
}
Compilation message
traffickers.c: In function 'append':
traffickers.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
12 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
traffickers.c: In function 'main':
traffickers.c:132:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
traffickers.c:136:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
traffickers.c:141:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | scanf("%d", &k);
| ^~~~~~~~~~~~~~~
traffickers.c:143:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
traffickers.c:146:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
traffickers.c:150:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | scanf("%d%d%d", &t, &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
traffickers.c:156:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | scanf("%d%d", &l, &r), r++;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
596 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
576 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
2252 KB |
Output isn't correct |
2 |
Incorrect |
35 ms |
1876 KB |
Output isn't correct |
3 |
Incorrect |
38 ms |
2604 KB |
Output isn't correct |
4 |
Incorrect |
36 ms |
2392 KB |
Output isn't correct |
5 |
Incorrect |
36 ms |
2128 KB |
Output isn't correct |
6 |
Incorrect |
35 ms |
2284 KB |
Output isn't correct |
7 |
Incorrect |
35 ms |
2268 KB |
Output isn't correct |
8 |
Incorrect |
35 ms |
2388 KB |
Output isn't correct |
9 |
Incorrect |
26 ms |
2516 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
332 ms |
6196 KB |
Output isn't correct |
2 |
Incorrect |
385 ms |
7316 KB |
Output isn't correct |
3 |
Incorrect |
344 ms |
6532 KB |
Output isn't correct |
4 |
Incorrect |
361 ms |
5580 KB |
Output isn't correct |
5 |
Incorrect |
339 ms |
5420 KB |
Output isn't correct |
6 |
Incorrect |
374 ms |
7052 KB |
Output isn't correct |
7 |
Incorrect |
259 ms |
7316 KB |
Output isn't correct |
8 |
Incorrect |
264 ms |
6792 KB |
Output isn't correct |