Submission #212385

# Submission time Handle Problem Language Result Execution time Memory
212385 2020-03-22T20:35:01 Z nicolaalexandra Traffickers (RMI18_traffickers) C++14
15 / 100
3500 ms 63576 KB
/// 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 aint[20][21][DIM*4];

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,sol_aint;


void update_aint (int nod, int st, int dr, int poz, int val, int a, int b){
    if (st == dr){
        aint[a][b][nod] += val;
        return;
    }
    int mid = (st+dr)>>1;
    if (poz <= mid)
        update_aint (nod<<1,st,mid,poz,val,a,b);
    else update_aint((nod<<1)|1,mid+1,dr,poz,val,a,b);
    aint[a][b][nod] = aint[a][b][nod<<1] + aint[a][b][(nod<<1)|1];
}

void query_aint (int nod, int st, int dr, int x, int y){
    if (x <= st && dr <= y){

        for (int a=0;a<20;++a){
            if (a > t2)
                break;
            for (int b=a+1;b<=20;++b){
                if (t2 >= a){
                    int nr = (t2 - a) / b + 1;
                    sol_aint += aint[a][b][nod] * nr;
                }
                if (t1-1 >= a){
                    int nr = (t1-1 - a) / b + 1;
                    sol_aint -= aint[a][b][nod] * nr;
                }}}

        return;
    }
    int mid = (st+dr)>>1;
    if (x <= mid)
        query_aint(nod<<1,st,mid,x,y);
    if (y > mid)
        query_aint((nod<<1)|1,mid+1,dr,x,y);
}

long long get_sum (int x, int y){
    sol_aint = 0;
    query_aint (1,1,n,x,y);
    return sol_aint;
}

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_aint (1,1,n,posAib[nod],1,t,lg);

        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_aint (1,1,n,posAib[nod],-1,t,lg);

        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:196: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 6 ms 2816 KB Output is correct
2 Correct 15 ms 4608 KB Output is correct
3 Correct 13 ms 4480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 313 ms 28664 KB Output isn't correct
2 Incorrect 290 ms 24312 KB Output isn't correct
3 Incorrect 262 ms 31096 KB Output isn't correct
4 Incorrect 286 ms 27896 KB Output isn't correct
5 Incorrect 316 ms 25464 KB Output isn't correct
6 Incorrect 296 ms 26744 KB Output isn't correct
7 Incorrect 304 ms 27488 KB Output isn't correct
8 Incorrect 324 ms 30840 KB Output isn't correct
9 Incorrect 385 ms 31204 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3519 ms 55780 KB Time limit exceeded
2 Incorrect 2989 ms 60920 KB Output isn't correct
3 Execution timed out 3588 ms 62196 KB Time limit exceeded
4 Incorrect 3112 ms 52312 KB Output isn't correct
5 Incorrect 2527 ms 54136 KB Output isn't correct
6 Incorrect 3239 ms 61948 KB Output isn't correct
7 Execution timed out 3586 ms 63576 KB Time limit exceeded
8 Execution timed out 3564 ms 62952 KB Time limit exceeded