# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
212138 | Ruxandra985 | Traffickers (RMI18_traffickers) | C++14 | 3594 ms | 39444 KiB |
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>
#define DIMN 100010
using namespace std;
int eul[2*DIMN] , first[DIMN] , nv[2*DIMN] , tt[DIMN] , nivel[DIMN] , logg[2*DIMN];
int rmq[20][2*DIMN];
vector <int> v[DIMN];
multiset <pair <int,int> > stare[DIMN];
int elem;
void dfs (int nod , int fth , int lvl){
int i , vecin;
eul[++elem] = nod;
first[nod] = elem;
nv[elem] = lvl;
tt[nod] = fth;
nivel[nod] = lvl;
for (i=0;i<v[nod].size();i++){
vecin = v[nod][i];
if (vecin != fth){
dfs(vecin , nod , lvl + 1);
eul[++elem] = nod;
nv[elem] = lvl;
}
}
}
int query (int x, int y){
int px , py , len;
px = first[x];
py = first[y];
if (px > py)
swap(px , py);
len = py - px + 1;
len = logg[len]; /// cea mai mare p2 <= len
if (nv[ rmq[len][px] ] < nv[ rmq[len][py - (1 << len) + 1] ])
return eul[ rmq[len][px] ];
else return eul[ rmq[len][py - (1 << len) + 1] ];
}
void adauga (int x , int y){
int lca , nod , poz , len;
lca = query(x , y); /// dc sa mai faci for cand poti face asta?
len = nivel[x] + nivel[y] - 2 * nivel[lca] + 1;
nod = x;
poz = 0;
while (nod != tt[lca]){
stare[nod].insert(make_pair(poz , len));
poz++;
nod = tt[nod];
}
nod = y;
poz = len - 1;
while (nod != lca){
stare[nod].insert(make_pair(poz , len));
poz--;
nod = tt[nod];
}
}
void sterge (int x , int y){
int lca , nod , poz , len;
multiset <pair <int,int> > :: iterator it;
lca = query(x , y); /// dc sa mai faci for cand poti face asta?
len = nivel[x] + nivel[y] - 2 * nivel[lca] + 1;
nod = x;
poz = 0;
while (nod != tt[lca]){
it = stare[nod].find(make_pair(poz , len));
stare[nod].erase(it);
poz++;
nod = tt[nod];
}
nod = y;
poz = len - 1;
while (nod != lca){
it = stare[nod].find(make_pair(poz , len));
stare[nod].erase(it);
poz--;
nod = tt[nod];
}
}
long long afla (int x , int y , int t1 , int t2){
int lca , nod , len , rest , val1 , val2;
long long sol = 0;
lca = query(x , y); /// dc sa mai faci for cand poti face asta?
nod = x;
while (nod != tt[lca]){
for (multiset <pair <int,int> > :: iterator it = stare[nod].begin() ; it != stare[nod].end() ; it++){
len = it->second;
rest = it->first;
sol += (t2 / len) + (t2 % len >= rest) - ((t1 - 1) / len) - ((t1 - 1) % len >= rest);
}
nod = tt[nod];
}
nod = y;
while (nod != lca){
for (multiset <pair <int,int> > :: iterator it = stare[nod].begin() ; it != stare[nod].end() ; it++){
len = it->second;
rest = it->first;
sol += (t2 / len) + (t2 % len >= rest) - ((t1 - 1) / len) - ((t1 - 1) % len >= rest);
val1 = (t2 / len) + (t2 % len >= rest) - ((t1 - 1) / len) - ((t1 - 1) % len >= rest);
val2 = 0;
for (int i = t1 ; i <= t2 ; i++){
if (i % len == rest)
val2++;
}
}
nod = tt[nod];
}
return sol;
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int n , i , x , y , tip , k , q , powe , t1 , t2;
fscanf (fin,"%d",&n);
for (i = 1 ; i < n ; i++){
fscanf (fin,"%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1 , 0 , 0);
for (i = 2 ; i <= elem ; i++)
logg[i] = 1 + logg[i / 2];
for (i = 1 ; i <= elem ; i++)
rmq[0][i] = i;
for (powe = 1 ; (1 << powe) <= elem ; powe++){
for (i = 1 ; i + (1 << powe) - 1 <= elem ; i++){
if (nv[ rmq[powe-1][i] ] < nv[ rmq[powe-1][i + (1 << (powe - 1) )] ])
rmq[powe][i] = rmq[powe-1][i];
else rmq[powe][i] = rmq[powe-1][i + (1 << (powe - 1) )];
}
}
fscanf (fin,"%d",&k);
for (i = 1 ; i <= k ; i++){
fscanf (fin,"%d%d",&x,&y);
adauga (x , y);
}
fscanf (fin,"%d",&q);
for (i = 1 ; i <= q ; i++){
fscanf (fin,"%d",&tip);
if (tip == 1){
fscanf (fin,"%d%d",&x,&y);
adauga (x , y);
}
else if (tip == 2){
fscanf (fin,"%d%d",&x,&y);
sterge (x , y);
}
else {
fscanf (fin,"%d%d%d%d",&x,&y,&t1,&t2);
fprintf (fout,"%lld\n",afla (x , y , t1 , t2));
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |