Submission #211912

#TimeUsernameProblemLanguageResultExecution timeMemory
211912nicolaalexandraTraffickers (RMI18_traffickers)C++14
100 / 100
2949 ms30712 KiB
/// yay heavy /// bine ca scap de mle da acum iau tle #include <bits/stdc++.h> #define DIM 30002 #pragma GCC optimize("Ofast") /* #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("O3")*/ using namespace std; vector <short> L[DIM],w,aux; vector <short> chains[DIM]; //short chains[DIM]; int aib[20][21][DIM]; short whatChain[DIM],chainFatherNode[DIM],Size[DIM],posAib[DIM]; short level[DIM],fth[DIM]; int n,m,i,j,x,y,tip,q,k,t1,t2,nr_chains; 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 a, int b, int p){ if (!p) return 0; long long sol = 0; for (;p;p-=(p&-p)) sol += aib[a][b][p]; return sol; } long long get_sum (int a, int b, int x, int y){ return query_aib (a,b,y) - query_aib (a,b,x-1); } void dfs (int nod, int tata){ fth[nod] = tata; level[nod] = 1 + level[tata]; Size[nod] = 1; int ok = 0; for (auto vecin : L[nod]){ if (vecin != tata){ ok = 1; dfs (vecin,nod); Size[nod] += Size[vecin]; }} if (!ok){ nr_chains++; chains[nr_chains].push_back(0); chains[nr_chains].push_back(nod); whatChain[nod] = nr_chains; } else { int maxim = 0, poz = 0; for (auto vecin : L[nod]){ if (vecin == tata) continue; if (Size[vecin] > maxim) maxim = Size[vecin], poz = vecin; } chains[whatChain[poz]].push_back(nod); // positionInChain[nod] = chains[whatChain[poz]].size()-1; 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){ w.clear(); aux.clear(); while (x != y){ if (level[x] > level[y]){ w.push_back(x); x = fth[x]; } else { aux.push_back(y); y = fth[y]; } } w.push_back(x); for (j=aux.size()-1;j>=0;j--) w.push_back(aux[j]); } void adauga (int x, int y){ drum (x,y); /// in w am lantul de la x la y int t = 0, lg = w.size(); for (auto nod : w){ update_aib(t,lg,posAib[nod],n,1); t++; } } void scoate (int x, int y){ drum (x,y); int t = 0, lg = w.size(); for (auto nod : w){ update_aib(t,lg,posAib[nod],n,-1); t++; } } void query_heavy (int x, int y, int a, int b){ if (whatChain[x] == whatChain[y]){ int posx = posAib[x], posy = posAib[y]; if (posx > posy) swap (posx,posy); sum += get_sum (a,b,posx,posy); return; } if (level[chainFatherNode[whatChain[x]]] <= level[chainFatherNode[whatChain[y]]]) swap (x,y); sum += get_sum (a,b,posAib[x],posAib[ chains[whatChain[x]][ chains[whatChain[x]].size()-1 ] ]); int nr = chainFatherNode[whatChain[x]]; query_heavy (nr,y,a,b); } long long solve (int x, int y, int t1, int t2){ long long sol1 = 0, sol2 = 0; for (int a=0;a<20;++a){ for (int b=a+1;b<=20;++b){ sum = 0; query_heavy (x,y,a,b); if (t2 >= a){ int nr = (t2 - a) / b + 1; sol2 += 1LL * nr * sum; } if (t1 >= a){ int nr = (t1 - a) / b + 1; sol1 += 1LL * nr * sum; } } } return sol2 - sol1; } int main (){ 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 (stderr)

traffickers.cpp: In function 'int main()':
traffickers.cpp:185:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j=1;j<chains[i].size();++j){
                  ~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...