Submission #212222

# Submission time Handle Problem Language Result Execution time Memory
212222 2020-03-22T14:30:46 Z Ruxandra985 Traffickers (RMI18_traffickers) C++14
60 / 100
3500 ms 448632 KB
#include <bits/stdc++.h>
#define DIMN 100010
using namespace std;
int tt[DIMN] , lvl[DIMN] , f[DIMN];
int up[DIMN] , l[DIMN] , d[DIMN] , poz[DIMN];
vector < map <short,int> > aint[DIMN];
vector <int> v[DIMN] , w[DIMN];
int elem , lnt , curr;
void dfs_lant(int nod , int fth){
    int i,vecin,ok=0,maxi,fiu;
    tt[nod] = fth;
    d[nod] = 1;
    maxi=0;
    fiu=0;
    for (i=0;i<v[nod].size();i++){
        vecin=v[nod][i];
        if (vecin!=tt[nod]){
            ok=1;
            lvl[vecin] = 1 + lvl[nod];
            dfs_lant(vecin , nod);
            d[nod] += d[vecin];
            if (d[vecin]>maxi){
                maxi=d[vecin];
                fiu=vecin;
            }
        }
    }
    if (!ok){ /// frunza
        lnt++;
        l[nod]=lnt; /// lantul din care apartine
        w[lnt].push_back(nod);
    }else { /// unim cu poz
        w[l[fiu]].push_back(nod);
        l[nod]=l[fiu];
        for (i=0;i<v[nod].size();i++){
            vecin=v[nod][i];
            if (vecin!=tt[nod] && vecin!=fiu)
                up[l[vecin]]=nod; /// din lantul vecinului trecem in lantul nodului
        }
    }
    v[nod].clear();
}
void update_aint (int lant , int p , int st , int dr , int l , int r , int len , int val , int sens){
    int mid , rest;
    if (st == dr){

        aint[lant][p][len * 100 + curr] += val;
       // printf ("%d %d %d\n", w[lant][st] , len , curr);
        curr++;
        return;
    }
    mid=(st+dr)/2;
    if (sens == -1){
        if (l <= mid)
            update_aint(lant , 2 * p , st , mid , l , r , len , val , sens);
        if (mid + 1 <= r)
            update_aint(lant , 2 * p + 1 , mid + 1 , dr , l , r , len , val , sens);
    }
    else {
        if (mid + 1 <= r)
            update_aint(lant , 2 * p + 1 , mid + 1 , dr , l , r , len , val , sens);
        if (l <= mid)
            update_aint(lant , 2 * p , st , mid , l , r , len , val , sens);
    }
    for (rest = 0 ; rest < len ; rest++)
        aint[lant][p][len * 100 + rest] = aint[lant][2 * p][len * 100 + rest] + aint[lant][2 * p + 1][len * 100 + rest];
}
void update (int x , int y , int len , int val){
    if (l[x]!=l[y]){ /// avansam
        if (lvl[up[l[x]]]>lvl[up[l[y]]]){
            update_aint (l[x] , 1 , 0 , w[l[x]].size()-1 , 0 , poz[x] , len , val , 1);
            update ( up[l[x]] , y , len , val);
        }
        else{
            update ( x , up[l[y]] , len , val );

            update_aint( l[y] , 1 , 0 , w[l[y]].size()-1 , 0 , poz[y] , len , val , -1 );
        }
    }
    else {
        if (poz[x] <= poz[y])
            update_aint( l[x] , 1 , 0 , w[l[x]].size()-1 , poz[x] , poz[y] , len , val , -1 );
        else
            update_aint( l[x] , 1 , 0 , w[l[x]].size()-1 , poz[y] , poz[x] , len , val , 1 );
    }
}
long long query_aint (int lant , int p , int st , int dr , int px , int py , int t1 , int t2){
    int mid , len , rest;
    long long sol = 0;
    if (px <= st && dr <= py){

        for (len = 1 ; len <= 20 ; len ++){
            for (rest = 0 ; rest < len ; rest++){
                sol += 1LL * aint[lant][p][len * 100 + rest] * ((t2 / len) + (t2 % len >= rest) - ((t1 - 1) / len) - ((t1 - 1) % len >= rest));
            }
        }

        return sol;
    }
    mid = (st + dr)/2;
    if (px <= mid)
        sol = sol + query_aint(lant , 2 * p , st , mid , px , py , t1 , t2);
    if (mid+1 <= py)
        sol = sol + query_aint(lant , 2 * p + 1 , mid + 1 , dr , px , py , t1 , t2);
    return sol;
}
long long query (int x , int y , int t1 , int t2){
    if (l[x] != l[y]){ /// avansam
        if (lvl[up[l[x]]]>lvl[up[l[y]]])
            return query(up[l[x]] , y , t1 , t2) + query_aint(l[x] , 1 , 0 , w[l[x]].size()-1 , 0 , poz[x] , t1 , t2);
        else{
            return query(x , up[l[y]] , t1 , t2) + query_aint(l[y] , 1 , 0 , w[l[y]].size()-1 , 0 , poz[y] , t1 , t2);
        }
    }
    else{
        return query_aint(l[x] , 1 , 0 , w[l[x]].size()-1 , min(poz[x],poz[y]) , max(poz[x],poz[y]) , t1 , t2);
    }
}
int adauga (int x , int y , int val , int op){
    int lca , nod , poz , len , i;
    nod = x;
    for (i = 0 ; i < 20 ; i++){
        f[nod] = op;
        nod = tt[nod];
    }

    nod = y;
    for (i = 0 ; i < 20 ; i++){
        if (f[nod] == op){
            lca = nod;
            break;
        }
        f[nod] = op;
        nod = tt[nod];
    }
    return lvl[x] + lvl[y] - 2 * lvl[lca] + 1;
}
int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int n , i , x , y , tip , k , q , t1 , t2 , p , vecin , len;
    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);
    }

    lvl[1] = 1;
    dfs_lant(1 , 0);

    /// fiecare nod al catelea e
    for (i=1;i<=lnt;i++){
        reverse(w[i].begin(),w[i].end());
        p=0;
        for (vector <int> :: iterator j=w[i].begin();j!=w[i].end();j++){
            /// pozitia lui din lantul curent
            vecin=*j;
            poz[vecin]=p;
            p++;
        }
        aint[i].resize(w[i].size()*4);
    }


    fscanf (fin,"%d",&k);
    int op = 1;
    for (i = 1 ; i <= k ; i++){
        fscanf (fin,"%d%d",&x,&y);
        len = adauga(x , y , 1 , ++op);
        curr = 0;
        update(x , y , len , 1);

    }
    fscanf (fin,"%d",&q);
    for (i = 1 ; i <= q ; i++){
        fscanf (fin,"%d",&tip);
        if (tip == 1){
            fscanf (fin,"%d%d",&x,&y);
            len = adauga(x , y , 1 , ++op);
            curr = 0;
            update (x , y , len , 1);
        }
        else if (tip == 2){
            fscanf (fin,"%d%d",&x,&y);
            len = adauga(x , y , -1 , ++op);
            curr = 0;
            update (x , y , len , -1);
        }
        else {
            fscanf (fin,"%d%d%d%d",&x,&y,&t1,&t2);
            fprintf (fout,"%lld\n",query (x , y , t1 , t2));
        }
    }
    return 0;
}

Compilation message

traffickers.cpp: In function 'void dfs_lant(int, int)':
traffickers.cpp:15:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
traffickers.cpp:35:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i=0;i<v[nod].size();i++){
                  ~^~~~~~~~~~~~~~
traffickers.cpp: In function 'int adauga(int, int, int, int)':
traffickers.cpp:120:21: warning: unused variable 'poz' [-Wunused-variable]
     int lca , nod , poz , len , i;
                     ^~~
traffickers.cpp:120:27: warning: unused variable 'len' [-Wunused-variable]
     int lca , nod , poz , len , i;
                           ^~~
traffickers.cpp: In function 'int main()':
traffickers.cpp:143:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d",&n);
     ~~~~~~~^~~~~~~~~~~~~
traffickers.cpp:145:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d%d",&x,&y);
         ~~~~~~~^~~~~~~~~~~~~~~~~~
traffickers.cpp:167:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d",&k);
     ~~~~~~~^~~~~~~~~~~~~
traffickers.cpp:170:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d%d",&x,&y);
         ~~~~~~~^~~~~~~~~~~~~~~~~~
traffickers.cpp:176:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d",&q);
     ~~~~~~~^~~~~~~~~~~~~
traffickers.cpp:178:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d",&tip);
         ~~~~~~~^~~~~~~~~~~~~~~
traffickers.cpp:180:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             fscanf (fin,"%d%d",&x,&y);
             ~~~~~~~^~~~~~~~~~~~~~~~~~
traffickers.cpp:186:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             fscanf (fin,"%d%d",&x,&y);
             ~~~~~~~^~~~~~~~~~~~~~~~~~
traffickers.cpp:192:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             fscanf (fin,"%d%d%d%d",&x,&y,&t1,&t2);
             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
traffickers.cpp: In function 'int adauga(int, int, int, int)':
traffickers.cpp:136:41: warning: 'lca' may be used uninitialized in this function [-Wmaybe-uninitialized]
     return lvl[x] + lvl[y] - 2 * lvl[lca] + 1;
                                  ~~~~~~~^
traffickers.cpp: In function 'int main()':
traffickers.cpp:136:41: warning: 'lca' may be used uninitialized in this function [-Wmaybe-uninitialized]
     return lvl[x] + lvl[y] - 2 * lvl[lca] + 1;
                                  ~~~~~~~^
traffickers.cpp:120:9: note: 'lca' was declared here
     int lca , nod , poz , len , i;
         ^~~
traffickers.cpp:136:41: warning: 'lca' may be used uninitialized in this function [-Wmaybe-uninitialized]
     return lvl[x] + lvl[y] - 2 * lvl[lca] + 1;
                                  ~~~~~~~^
traffickers.cpp:120:9: note: 'lca' was declared here
     int lca , nod , poz , len , i;
         ^~~
traffickers.cpp:136:41: warning: 'lca' may be used uninitialized in this function [-Wmaybe-uninitialized]
     return lvl[x] + lvl[y] - 2 * lvl[lca] + 1;
                                  ~~~~~~~^
traffickers.cpp:120:9: note: 'lca' was declared here
     int lca , nod , poz , len , i;
         ^~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8064 KB Output is correct
2 Correct 85 ms 18168 KB Output is correct
3 Correct 82 ms 18040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1120 ms 128216 KB Output is correct
2 Correct 984 ms 102904 KB Output is correct
3 Correct 1120 ms 133288 KB Output is correct
4 Correct 1094 ms 124072 KB Output is correct
5 Correct 1028 ms 112892 KB Output is correct
6 Correct 1053 ms 118392 KB Output is correct
7 Correct 1068 ms 123256 KB Output is correct
8 Correct 1126 ms 136952 KB Output is correct
9 Correct 1025 ms 113272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3604 ms 350072 KB Time limit exceeded
2 Execution timed out 3601 ms 404664 KB Time limit exceeded
3 Execution timed out 3624 ms 431484 KB Time limit exceeded
4 Execution timed out 3598 ms 260472 KB Time limit exceeded
5 Execution timed out 3597 ms 250504 KB Time limit exceeded
6 Execution timed out 3608 ms 423544 KB Time limit exceeded
7 Execution timed out 3609 ms 447868 KB Time limit exceeded
8 Execution timed out 3617 ms 448632 KB Time limit exceeded