/// nu mai ai ce discuta cu mn ruxandra :(
#include <bits/stdc++.h>
#define DIM 30002
#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
/*
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/
using namespace std;
vector <int> L[DIM];
vector <int> chains[DIM];
int aib[20][21][DIM];
int whatChain[DIM],chainFatherNode[DIM],Size[DIM],posAib[DIM];
int level[DIM],fth[DIM],w[22],aux[22];
int n,m,i,j,tip,q,k,t1,t2,nr_chains,el,el2;
int x,y;
long long sum;
void update_aib (int a, int b, int p, int n, int val){
for (;p<=n;p+=(p&-p))
aib[a][b][p] += val;
}
long long query_aib (int p){
if (!p)
return 0;
long long sol = 0;
for (int a=0;a<20;++a){
if (a > t2)
break;
for (int b=a+1;b<=20;++b){
long long sum = 0;
/// sum din aib
for (int i=p;i;i-=(i&-i))
sum += aib[a][b][i];
if (t2 >= a){
int nr = (t2 - a) / b + 1;
sol += sum * nr;
}
if (t1-1 >= a){
int nr = (t1-1 - a) / b + 1;
sol -= sum * nr;
}}}
return sol;
}
long long get_sum (int x, int y){
return query_aib (y) - query_aib (x-1);
}
void dfs (int nod, int tata){
fth[nod] = tata;
level[nod] = 1 + level[tata];
Size[nod] = 1;
int ok = 0, maxim = 0, poz = 0;
for (auto vecin : L[nod]){
if (vecin != tata){
ok = 1;
dfs (vecin,nod);
Size[nod] += Size[vecin];
if (Size[vecin] > maxim)
maxim = Size[vecin], poz = vecin;
}}
if (!ok){
nr_chains++;
chains[nr_chains].push_back(0);
chains[nr_chains].push_back(nod);
whatChain[nod] = nr_chains;
} else {
chains[whatChain[poz]].push_back(nod);
whatChain[nod] = whatChain[poz];
for (auto vecin : L[nod]){
if (vecin == tata || vecin == poz)
continue;
chainFatherNode[whatChain[vecin]] = nod;
}}}
void drum (int x, int y){
el = el2 = 0;
while (x != y){
if (level[x] > level[y]){
w[++el] = x;
x = fth[x];
} else {
aux[++el2] = y;
y = fth[y];
}
}
w[++el] = x;
for (j=el2;j>=1;j--)
w[++el] = aux[j];
}
void adauga (int x, int y){
drum (x,y);
/// in w am lantul de la x la y
int t = 0, lg = el;
for (int i=1;i<=el;i++){
int nod = w[i];
update_aib(t,lg,posAib[nod],n,1);
t++;
}
}
void scoate (int x, int y){
drum (x,y);
int t = 0, lg = el;
for (int i=1;i<=el;i++){
int nod = w[i];
update_aib(t,lg,posAib[nod],n,-1);
t++;
}
}
void query_heavy (int x, int y){
if (whatChain[x] == whatChain[y]){
int posx = posAib[x], posy = posAib[y];
if (posx > posy)
swap (posx,posy);
sum += get_sum (posx,posy);
return;
}
if (level[chainFatherNode[whatChain[x]]] <= level[chainFatherNode[whatChain[y]]])
swap (x,y);
sum += get_sum (posAib[x],posAib[ chains[whatChain[x]][ chains[whatChain[x]].size()-1 ] ]);
int nr = chainFatherNode[whatChain[x]];
query_heavy (nr,y);
}
long long solve (int x, int y, int t1, int t2){
sum = 0;
query_heavy(x,y);
return sum;
}
int main (){
// ifstream cin ("traffickers.in");
// ofstream cout ("traffickers.out");
cin>>n;
for (i=1;i<n;++i){
cin>>x>>y;
L[x].push_back(y);
L[y].push_back(x);
}
dfs (1,0);
int idx = 0;
for (i=1;i<=nr_chains;++i)
for (j=1;j<chains[i].size();++j){
int nod = chains[i][j];
posAib[nod] = ++idx;
}
cin>>m; /// nr inital de traficanti
for (i=1;i<=m;++i){
cin>>x>>y;
adauga (x,y);
}
cin>>q;
for (;q--;){
cin>>tip>>x>>y;
if (tip == 1){
adauga (x,y);
continue;
}
if (tip == 2){
scoate (x,y);
continue;
}
cin>>t1>>t2;
cout<<solve(x,y,t1-1,t2)<<"\n";
}
return 0;
}
Compilation message
traffickers.cpp: In function 'int main()':
traffickers.cpp:193:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j=1;j<chains[i].size();++j){
~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2560 KB |
Output is correct |
2 |
Correct |
10 ms |
3584 KB |
Output is correct |
3 |
Correct |
12 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
11272 KB |
Output is correct |
2 |
Correct |
179 ms |
9344 KB |
Output is correct |
3 |
Correct |
109 ms |
11648 KB |
Output is correct |
4 |
Correct |
157 ms |
11008 KB |
Output is correct |
5 |
Correct |
181 ms |
10744 KB |
Output is correct |
6 |
Correct |
146 ms |
11000 KB |
Output is correct |
7 |
Correct |
156 ms |
11256 KB |
Output is correct |
8 |
Correct |
136 ms |
11904 KB |
Output is correct |
9 |
Correct |
124 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2000 ms |
27944 KB |
Output is correct |
2 |
Correct |
1626 ms |
30712 KB |
Output is correct |
3 |
Correct |
1671 ms |
30328 KB |
Output is correct |
4 |
Correct |
2424 ms |
25612 KB |
Output is correct |
5 |
Correct |
3033 ms |
27000 KB |
Output is correct |
6 |
Correct |
1655 ms |
30736 KB |
Output is correct |
7 |
Correct |
1696 ms |
31036 KB |
Output is correct |
8 |
Correct |
1534 ms |
30456 KB |
Output is correct |