Submission #212138

#TimeUsernameProblemLanguageResultExecution timeMemory
212138Ruxandra985Traffickers (RMI18_traffickers)C++14
60 / 100
3594 ms39444 KiB
#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)

traffickers.cpp: In function 'void dfs(int, int, int)':
traffickers.cpp:18:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
traffickers.cpp: In function 'long long int afla(int, int, int, int)':
traffickers.cpp:91:34: warning: variable 'val1' set but not used [-Wunused-but-set-variable]
     int lca , nod , len , rest , val1 , val2;
                                  ^~~~
traffickers.cpp: In function 'int main()':
traffickers.cpp:134: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:136: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:160: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:163: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:168: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:170: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:172: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:176: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:180: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);
             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...