#include <stdio.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double
const ll maxn = 6e4 + 1, maxm = 1e6 + 1;
const ll mod = 1e9 + 7, inf = 1e9, block = 550, hb = 31, base = 1000050017, biginf = 5e18;
const ld eps = 1e-9;
int n, k, q, timer, lac;
int tin[maxn], tout[maxn], used[maxn], par[maxn], p[maxn], sz[maxn], anc[maxn], zu[maxn], zu2[maxn];
vector<int> g[maxn];
vector<pair<int, int>> zp[maxn];
int type[maxn], uu[maxn], vv[maxn], t[maxn], t2[maxn], lc[maxn];;
int get(int v){
if (v == p[v])
return v;
return p[v] = get(p[v]);
}
void unite(int a, int b, int x){
a = get(a), b = get(b);
if (sz[a] > sz[b])
swap(a, b);
p[a] = b, anc[b] = x;
sz[b] += sz[a];
}
void dfs(int v = 1, int pr = 0){
par[v] = pr, tin[v] = ++timer;
used[v] = 1;
anc[v] = v;
cout << v << '\n';
for (auto to : g[v]){
if (to != pr){
dfs(to, v);
unite(to, v, v);
}
}
for (auto it : zp[v]){
int ver = it.f, x = it.s;
if (used[ver]){
lc[x] = anc[get(ver)];
}
}
tout[v] = ++timer;
}
struct fenwick{
int f[maxn];
void upd(int pos, int val){
for (int i = pos; i <= timer; i |= (i + 1)){
f[i] += val;
}
}
int get(int pos){
int res = 0;
for (int i = pos; i >= 1; i = (i & (i + 1)) - 1){
res += f[i];
}
return res;
}
} rt[21][20];
ll calc(int x, int d, int pac, int y){
ll ky = y / d, k2 = y % d;
ll z = rt[d][pac].get(x);
ll sum = ky * z + (k2 >= pac) * z;
return sum;
}
ll calc2(int u, int v, int x){
if (x < 0)
return 0;
ll res = 0;
ll parik = par[lac];
for (int i = 1; i <= 20; ++i){
for (int j = 0; j < i; ++j){
ll add = calc(tin[u], i, j, x) + calc(tin[v], i, j, x) - calc(tin[lac], i, j, x);
if (parik > 0){
add -= calc(tin[parik], i, j, x);
}
res += add;
}
}
return res;
}
void upd(int u, int v, int x, int is){
vector<int> path;
while (u != x){
path.pb(u);
u = par[u];
}
vector<int> pp;
while (v != x){
pp.pb(v);
v = par[v];
}
pp.pb(x);
reverse(pp.begin(), pp.end());
for (auto it : pp){
path.pb(it);
}
int len = (int)path.size();
if (is == 1){
for (int i = 0; i < len; ++i){
rt[len][i].upd(tin[path[i]], 1);
rt[len][i].upd(tout[path[i]], -1);
}
}
else{
for (int i = 0; i < len; ++i){
rt[len][i].upd(tin[path[i]], -1);
rt[len][i].upd(tout[path[i]], 1);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i){
sz[i] = 1;
p[i] = i;
}
for (int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
int kekich = 0;
cin >> k;
for (int i = 1; i <= k; ++i){
cin >> zu[i] >> zu2[i];
zp[zu[i]].pb(mp(zu2[i], ++kekich));
zp[zu2[i]].pb(mp(zu[i], kekich));
}
cin >> q;
for (int i = 1; i <= q; ++i){
cin >> type[i];
cin >> uu[i] >> vv[i];
zp[uu[i]].pb(mp(vv[i], ++kekich));
zp[vv[i]].pb(mp(uu[i], kekich));
if (type[i] == 3){
cin >> t[i] >> t2[i];
}
}
dfs();
int lkek = 0;
for (int i = 1; i <= k; ++i){
++lkek;
int lca = lc[lkek];
upd(zu[i], zu2[i], lca, 1);
}
for (int i = 1; i <= q; ++i){
++lkek;
if (type[i] == 1){
int lca = lc[lkek];
upd(uu[i], vv[i], lca, 1);
}
else if (type[i] == 2){
int lca = lc[lkek];
upd(uu[i], vv[i], lca, 0);
}
else{
lac = lc[lkek];
ll ans = calc2(uu[i], vv[i], t2[i]) - calc2(uu[i], vv[i], t[i] - 1);
cout << ans << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
2 |
Incorrect |
8 ms |
5972 KB |
Output isn't correct |
3 |
Incorrect |
7 ms |
5972 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
154 ms |
22396 KB |
Output isn't correct |
2 |
Incorrect |
163 ms |
20488 KB |
Output isn't correct |
3 |
Incorrect |
137 ms |
21880 KB |
Output isn't correct |
4 |
Incorrect |
154 ms |
22616 KB |
Output isn't correct |
5 |
Incorrect |
165 ms |
22276 KB |
Output isn't correct |
6 |
Incorrect |
159 ms |
22488 KB |
Output isn't correct |
7 |
Incorrect |
153 ms |
22236 KB |
Output isn't correct |
8 |
Incorrect |
126 ms |
22624 KB |
Output isn't correct |
9 |
Incorrect |
113 ms |
22856 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1889 ms |
63148 KB |
Output isn't correct |
2 |
Incorrect |
1866 ms |
64088 KB |
Output isn't correct |
3 |
Incorrect |
1745 ms |
63636 KB |
Output isn't correct |
4 |
Incorrect |
1979 ms |
62152 KB |
Output isn't correct |
5 |
Incorrect |
2351 ms |
61808 KB |
Output isn't correct |
6 |
Incorrect |
1833 ms |
64276 KB |
Output isn't correct |
7 |
Incorrect |
1623 ms |
64540 KB |
Output isn't correct |
8 |
Incorrect |
1569 ms |
63760 KB |
Output isn't correct |