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>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
int t, n, m, k, q;
vector<int> g[30010];
int anc[30010][20];
int ent[30010], ex[30010];
int tmp=0;
bool vg[30010];
void dfs(int s){
vg[s]=true;
tmp++;ent[s]=tmp;
for(int u:g[s]){
if(!vg[u]){
anc[u][0]=s;
dfs(u);
}
}
tmp++; ex[s]=tmp;
return;
}
void binary_lifting(){
for(int i=1; i<=19; i++){
for(int j=1; j<=n; j++){
if(anc[j][i-1]!=0 ){
if(anc[anc[j][i-1]][i-1]!=0 ){
anc[j][i]=anc[anc[j][i-1] ][i-1];
}
}
}
}
}
bool is_ancestor(int u, int v){
return ( (ent[u]<=ent[v]) && (ex[u]>=ex[v] ) );
}
int Lca(int u, int v){
if(is_ancestor(u, v)){return u;}
int cr=u;
for(int j=19; j>=0; j--){
if( (anc[cr][j]!=0) && (!is_ancestor(anc[cr][j], v)) ){
cr=anc[cr][j];
}
}
return anc[cr][0];
}
int fw[2][100010][21][21];
void fw_u(int u, int t1, int t2, int ext, int upd){
int x;
if(ext==1){
x=ex[u];
}
else{
x=ent[u];
}
for(int i=100000-x+1; i<=100000; i+=(i&(-i))){
fw[ext][i][t1][t2]+=upd;
}
return;
}
int fw_q(int u, int t1, int t2, int ext){
//cout<<u<<" "<<ex[u]<<"\n";
int x=ex[u]+1;
int res=0;
for(int i=100000-x+1; i>0; i-=(i&(-i))){
res+=fw[ext][i][t1][t2];
}
return res;
}
int cnt[30010][21][21];
void bandit(int u, int v, int upd){
int lca=Lca(u, v);
int l=1;
int cr=u;
while(cr!=lca){
l++; cr=anc[cr][0];
}
cr=v;
while(cr!=lca){
cr=anc[cr][0]; l++;
}
int t1=0;
cr=u;
while(cr!=lca){
cnt[cr][t1][l]+=upd; fw_u(cr, t1, l, 1, upd);fw_u(cr, t1, l, 0, upd); cr=anc[cr][0]; t1++;
}
cnt[cr][t1][l]+=upd; fw_u(cr, t1, l, 1, upd); fw_u(cr, t1, l, 0, upd);
t1=0;
cr=v;
while(cr!=lca){
cnt[cr][l-t1-1][l]+=upd; fw_u(cr, l-t1-1, l, 1, upd);fw_u(cr, l-t1-1, l, 0, upd); cr=anc[cr][0]; t1++;
}
return;
}
int until_t(int add,int t1, int t2, int t){
return ( ( (t>=t1)?(add):(0) )+max(0ll, add*( (t-t1)/t2 ) ) );
}
int btwn(int add, int i, int j, int t1, int t2){
return until_t(add, i, j, t2)-until_t(add, i, j, t1-1);
}
int direct_branch(int u, int lca, int t1, int t2){
int res=0;
for(int i=0; i<=19; i++){
for(int j=1; j<=20; j++){
int su=fw_q(u, i, j, 1)-fw_q(u, i, j, 0), sl=fw_q(lca, i, j, 1)-fw_q(lca, i, j, 0);
//cout<<(su-sl)<<" "<<u<<" "<<lca<<" "<<t1<<" "<<t2<<" "<<i<<" "<<j<<"\n";
res+=btwn(su-sl, i, j, t1, t2 );
}
}
return res;
}
int query(int u, int v, int t1, int t2){
int res=0;
int lca=Lca(u, v);
//branch u:
res+=direct_branch(u, lca, t1, t2);
//branchul 2:
res+=direct_branch(v, lca, t1, t2);
for(int i=0; i<=19; i++){
for(int j=1; j<=20; j++){
res-=btwn(cnt[lca][i][j], i, j, t1, t2);
res+=btwn(cnt[u][i][j], i, j, t1, t2);
res+=btwn(cnt[v][i][j], i, j, t1, t2);
}
}
return res;
}
int32_t main(){
INIT
cin>>n;
for(int i=1; i<n; i++){
int u, v;cin>>u>>v;g[u].pb(v); g[v].pb(u);
}
dfs(1);
binary_lifting();
cin>>k;
for(int i=1; i<=k; i++){
int u, v; cin>>u>>v;
bandit(u, v, 1);
}
cin>>q;
for(int i=1; i<=q; i++){
int tp, u, v, t1, t2;
cin>>tp>>u>>v;
if(tp==1){
bandit(u, v, 1);
}
if(tp==2){
bandit(u, v, -1);
}
if(tp==3){
cin>>t1>>t2;
cout<<query(u, v, t1, t2)<<"\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |