Submission #211906

# Submission time Handle Problem Language Result Execution time Memory
211906 2020-03-21T16:27:17 Z nicolaalexandra Traffickers (RMI18_traffickers) C++14
75 / 100
3500 ms 33164 KB
/// yay heavy
/// nu mi place acel mle

#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;
        }}}


/*int get_lca (int x, int y){
    int posx = first[x], posy = first[y];
    if (posx > posy)
        swap (posx,posy);
    int nr = p[posy - posx + 1];
    pair <int, int> sol = min (rmq[nr][posx], rmq[nr][posy - (1<<nr) + 1]);
    return E[sol.second];
}*/
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]);

  /*
    int lca = get_lca (x,y);
    int nod = x;
    while (nod != lca){
        w.push_back(nod);
        nod = fth[nod];
    }
    w.push_back(lca);

    nod = y;
    aux.clear();
    while (nod != lca){
        aux.push_back(nod);
        nod = fth[nod];
    }*/

}
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);
        //int last = chains[whatChain[nod]][ chains[whatChain[nod]].size()-1 ];
        //update_aib(t,lg,posAib[ last ] + 1,n,-1);

        //update_aib(t,lg,posAib[nod],chains[whatChain[nod]],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);
        //update_aib(t,lg,posAib[ chains[whatChain[nod]][ chains[whatChain[nod]].size()-1 ] ] + 1,n,1);
        //update_aib(t,lg,whatChain[nod],positionInChain[nod],chains[whatChain[nod]],-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);

    //int posx = positionInChain[x], posy = chains[whatChain[x]];
    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 t){
    if (t < 0)
        return 0;

    long long sol = 0;
    for (int a=0;a<20;a++){
        if (a > t)
            break;
        for (int b=1;b<=20;b++){
            int nr = (t - a) / b + 1;
            sum = 0;
            query_heavy (x,y,a,b);
            sol += 1LL * nr * sum;
        }
    }

    return sol;
}
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;
        }



  /* for (i=1;i<=k;i++)
        rmq[0][i] = make_pair(level[E[i]],i);

    for (i=1;(1<<i)<=k;i++)
        for (j=1;j<=k;j++){
            rmq[i][j] = rmq[i-1][j];
            if (j + (1<<(i-1)) <= k && rmq[i-1][j + (1<<(i-1))].first < rmq[i][j].first)
                rmq[i][j] = rmq[i-1][j + (1<<(i-1))];
        }

    for (i=2;i<=k;i++)
        p[i] = p[i/2] + 1;*/

    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,t2) - solve(x,y,t1-1)<<"\n";
    }

    return 0;
}

Compilation message

traffickers.cpp: In function 'int main()':
traffickers.cpp:209:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j=1;j<chains[i].size();j++){
                  ~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2688 KB Output is correct
2 Correct 11 ms 3584 KB Output is correct
3 Correct 15 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 288 ms 11000 KB Output is correct
2 Correct 343 ms 9216 KB Output is correct
3 Correct 222 ms 11640 KB Output is correct
4 Correct 278 ms 10880 KB Output is correct
5 Correct 372 ms 10620 KB Output is correct
6 Correct 305 ms 10872 KB Output is correct
7 Correct 316 ms 11128 KB Output is correct
8 Correct 255 ms 11768 KB Output is correct
9 Correct 276 ms 12032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3577 ms 29920 KB Time limit exceeded
2 Correct 3160 ms 32760 KB Output is correct
3 Correct 3303 ms 32440 KB Output is correct
4 Execution timed out 3505 ms 27028 KB Time limit exceeded
5 Execution timed out 3579 ms 27864 KB Time limit exceeded
6 Correct 3097 ms 32760 KB Output is correct
7 Execution timed out 3576 ms 33164 KB Time limit exceeded
8 Execution timed out 3576 ms 32124 KB Time limit exceeded