Submission #279806

#TimeUsernameProblemLanguageResultExecution timeMemory
279806PedroBigManPutovanje (COCI20_putovanje)C++14
20 / 110
1071 ms189980 KiB
#include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <list> #include <iomanip> #include <stdlib.h> #include <time.h> using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; #define REP(i,a,b) for(ll i=a; i<b; i++) #define pb push_back #define mp make_pair #define pl pair<ll,ll> #define ff first #define ss second #define whole(x) x.begin(),x.end() #define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl #define INF 5000000000000000000LL template<class A=ll> void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;} template<class A=ll> void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}} struct hash_pair { template <class T1, class T2> size_t operator() (pair<T1, T2> p) const { size_t hash1 = hash<T1>()(p.first); size_t hash2 = hash<T2>()(p.second); return (hash1 ^ hash2); } }; template<class T=ll> class SparseTable //Range Minimum Queries { public: ll N; vector<T> a; vector<vector<T> > v; SparseTable() {N=0LL;} SparseTable(vector<T> b) { a=b; N=a.size(); ll lo=(ll) floor((double) log2(N)) +1LL; vector<T> xx; REP(i,0,lo) {xx.pb(mp(INF,INF));} REP(i,0,N) {v.pb(xx);} REP(step,0LL,lo) { REP(i,0,N-(1LL<<step)+1LL) { if(step==0) {v[i][0]=a[i];} else {v[i][step]=min(v[i][step-1],v[i+(1LL<<(step-1))][step-1]);} } } } T query(ll l, ll r) { ll step=(ll) floor((double) log2(r-l+1LL)); return min(v[l][step],v[r-(1LL<<step)+1LL][step]); } }; class ST { public: ll N; vector<ll> dif_ar; vector<ll> arrr; class SV //seg value { public: ll a; SV() {a=0LL;} SV(ll x) {a=x;} SV operator & (SV X) {SV ANS(a+X.a); return ANS;} }; class LV //lazy value { public: ll a; LV() {a=0LL;} LV(ll x) {a=x;} LV operator & (LV X) {LV ANS(a+X.a); return ANS;} }; ST() {N=0LL;} ST(vector<ll> arr) { ll las=0LL; REP(i,0,arr.size()) {dif_ar.pb(arr[i]-las); las=arr[i];} } SV query(ll a,ll b, ll c=1LL) //range [a,b], current node. initially: query(a,b,1) { if(arrr.size()==0) {ll sum=0LL; REP(i,0,dif_ar.size()) {sum+=dif_ar[i]; arrr.pb(sum);}} if(a==b && a<arrr.size()) {return arrr[a];} } void update(LV s, ll a, ll b, ll c=1LL) //update LV, range [a,b], current node, current range. initially: update(s,a,b,1,0,S.N-1) { if(c==1) {dif_ar[a]+=s.a; if(b<dif_ar.size()-1) {dif_ar[b]-=s.a;}} } }; class Tree { public: ll N; vector<ll> p; vector<vector<ll> > sons; vector<vector<ll> > adj; ll root; vector<bool> visited; vector<ll> level; //starting in 0 vector<ll> sub; //number of nodes in subtree vector<ll> val; //node values vector<ll> DFSarr1; //DFS Array vector<ll> DFSarr2; //DFS Array for LCA with whole path vector<ll> pos; //inverted DFSArr, only for LCA vector<pl> levDFSarr; //array of levels on DFSarr, only needed for LCA vector<ll> sumto; //weighted graph, length of path root-->i SparseTable<pl> S; //for LCA Tree(vector<vector<ll> > ad, ll r=0LL) { N=ad.size(); root=r; adj=ad; REP(i,0,N) {visited.pb(false);} vector<ll> xx; REP(i,0,N) {sons.pb(xx); p.pb(-1); level.pb(0); sub.pb(1LL); pos.pb(0LL); sumto.pb(0LL);} DFS_Build(r,r); REP(i,0,DFSarr2.size()) {pos[DFSarr2[i]]=i;} REP(i,0,DFSarr2.size()) {levDFSarr.pb(mp(level[DFSarr2[i]],DFSarr2[i]));} SparseTable<pl> X(levDFSarr); S=X; REP(i,0,N) {val.pb(0LL);} } void Reset() { REP(i,0,N) {visited[i]=false;} } void DFS_Build(ll s, ll par) { DFSarr1.pb(s); DFSarr2.pb(s); if(s!=root) {level[s]=level[par]+1LL;} p[s]=par; visited[s]=true; REP(i,0,adj[s].size()) { if(adj[s][i]==par) {continue;} sons[s].pb(adj[s][i]); DFS_Build(adj[s][i],s); sub[s]+=sub[adj[s][i]]; DFSarr2.pb(s); } return; } void DFS(ll s, ll las=-1LL) { REP(i,0,adj[s].size()) { if(adj[s][i]==las) {continue;} DFS(adj[s][i],s); } } ll LCA(ll a, ll b) { a=pos[a]; b=pos[b]; ll l=min(a,b); ll r=max(a,b); pl ans=S.query(l,r); return ans.ss; } class HeavyPath { public: ll N; ll low, high; Tree *T; ST S; HeavyPath() {N=0LL;} HeavyPath(ll x, ll y, Tree *K) { T=K; low=x; high=y; if(T->level[x]<T->level[y]) {swap(high,low);} N = T->level[x]-T->level[y]+1LL; vector<ll> st_val; ll c = low; while(1>0) {st_val.pb(T->val[c]); if(c==high) {break;} c=T->p[c];} ST R(st_val); S=R; } ll pos(ll x) { return (T->level[low]-T->level[x]); } ST::SV query(ll ind1, ll ind2) { return S.query(ind1,ind2); } void update(ST::LV X, ll ind1, ll ind2) { S.update(X,ind1,ind2); } }; vector<HeavyPath *> h_path; //heavy paths unordered_map<ll,HeavyPath *> HP; //m[s] = heavy path including s void HLD() { vector<bool> lowest; ll c; REP(i,0,N) { bool x = true; REP(j,0,sons[i].size()) { c=sons[i][j]; if(2*sub[c]>=sub[i]) {x=false;} } lowest.pb(x); } REP(i,0,N) { if(!lowest[i]) {continue;} c=i; while(c!=root && 2*sub[c]>=sub[p[c]]) {c=p[c];} HeavyPath *P = new HeavyPath(i,c,this); c=i; while(1>0) {HP[c]=P; if(c==P->high) {break;} c=p[c];} h_path.pb(P); } } ST::SV query_ancestor(ll s, ll anc) { ST::SV ans; if(level[s]<level[anc]) {return ans;} ll c = s; while(1>0) { ST::SV thispath; if(level[HP[c]->high]>=level[anc]) { thispath = HP[c]->query(HP[c]->pos(c),HP[c]->N-1); } else { thispath = HP[c]->query(HP[c]->pos(c),HP[c]->pos(anc)); } ans=ans&thispath; c=HP[c]->high; c=p[c]; if(c==root || level[c]<level[anc]) {break;} } return ans; } ST::SV query(ll a, ll b) //query along path a->b { if(a==b) {return query_ancestor(a,a);} ll lca = LCA(a,b); ST::SV V1 = query_ancestor(a,lca); ST::SV V2 = query_ancestor(b,lca); ST::SV V3 = query_ancestor(lca,lca); ST::SV V4(-V3.a); return V1&V2&V4; } void update_ancestor(ST::LV X, ll s, ll anc) { if(level[s]<level[anc]) {return;} ll c = s; while(1>0) { if(level[HP[c]->high]>=level[anc]) { HP[c]->update(X,HP[c]->pos(c),HP[c]->N-1); } else { HP[c]->update(X,HP[c]->pos(c),HP[c]->pos(anc)); } c=HP[c]->high; if(c==root) {break;} c=p[c]; if(level[c]<level[anc]) {break;} } } void update(ST::LV X, ll a, ll b) { ll lca = LCA(a,b); update_ancestor(X,a,lca); update_ancestor(X,b,lca); ST::LV Y(-2LL*X.a); update_ancestor(Y,lca,lca); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll N; cin>>N; vector<ll> xx; vector<vector<ll> > adj; REP(i,0,N) {adj.pb(xx);} pl cur; unordered_map<pl,pl,hash_pair> m; REP(i,0,N-1) { cin>>cur.ff>>cur.ss; cur.ff--; cur.ss--; adj[cur.ff].pb(cur.ss); adj[cur.ss].pb(cur.ff); ll S,M; cin>>S>>M; m[mp(cur.ff,cur.ss)]=mp(S,M); m[mp(cur.ss,cur.ff)]=mp(S,M); } Tree T(adj,0); T.HLD(); REP(i,0,N-1) { ST::LV X(1LL); T.update(X,i,i+1); } vector<ll> use; REP(i,0,N) {if(i==0) {use.pb(0);} else{use.pb(T.query(i,i).a);}} ll ans=0LL; REP(i,1,N) { pl P = m[mp(i,T.p[i])]; ans+=min(use[i]*P.ff,P.ss); } cout<<ans<<endl; return 0; }

Compilation message (stderr)

putovanje.cpp: In constructor 'ST::ST(std::vector<long long int>)':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
  109 |         REP(i,0,arr.size()) {dif_ar.pb(arr[i]-las); las=arr[i];}
      |             ~~~~~~~~~~~~~~       
putovanje.cpp:109:9: note: in expansion of macro 'REP'
  109 |         REP(i,0,arr.size()) {dif_ar.pb(arr[i]-las); las=arr[i];}
      |         ^~~
putovanje.cpp: In member function 'ST::SV ST::query(ll, ll, ll)':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
  114 |         if(arrr.size()==0) {ll sum=0LL; REP(i,0,dif_ar.size()) {sum+=dif_ar[i]; arrr.pb(sum);}}
      |                                             ~~~~~~~~~~~~~~~~~
putovanje.cpp:114:41: note: in expansion of macro 'REP'
  114 |         if(arrr.size()==0) {ll sum=0LL; REP(i,0,dif_ar.size()) {sum+=dif_ar[i]; arrr.pb(sum);}}
      |                                         ^~~
putovanje.cpp:115:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |         if(a==b && a<arrr.size()) {return arrr[a];}
      |                    ~^~~~~~~~~~~~
putovanje.cpp: In member function 'void ST::update(ST::LV, ll, ll, ll)':
putovanje.cpp:120:39: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         if(c==1) {dif_ar[a]+=s.a; if(b<dif_ar.size()-1) {dif_ar[b]-=s.a;}}
      |                                      ~^~~~~~~~~~~~~~~~
putovanje.cpp: In constructor 'Tree::Tree(std::vector<std::vector<long long int> >, ll)':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
  149 |         REP(i,0,DFSarr2.size()) {pos[DFSarr2[i]]=i;}
      |             ~~~~~~~~~~~~~~~~~~   
putovanje.cpp:149:9: note: in expansion of macro 'REP'
  149 |         REP(i,0,DFSarr2.size()) {pos[DFSarr2[i]]=i;}
      |         ^~~
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
  150 |         REP(i,0,DFSarr2.size()) {levDFSarr.pb(mp(level[DFSarr2[i]],DFSarr2[i]));}
      |             ~~~~~~~~~~~~~~~~~~   
putovanje.cpp:150:9: note: in expansion of macro 'REP'
  150 |         REP(i,0,DFSarr2.size()) {levDFSarr.pb(mp(level[DFSarr2[i]],DFSarr2[i]));}
      |         ^~~
putovanje.cpp: In member function 'void Tree::DFS_Build(ll, ll)':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
  167 |         REP(i,0,adj[s].size())
      |             ~~~~~~~~~~~~~~~~~    
putovanje.cpp:167:9: note: in expansion of macro 'REP'
  167 |         REP(i,0,adj[s].size())
      |         ^~~
putovanje.cpp: In member function 'void Tree::DFS(ll, ll)':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
  180 |         REP(i,0,adj[s].size())
      |             ~~~~~~~~~~~~~~~~~    
putovanje.cpp:180:9: note: in expansion of macro 'REP'
  180 |         REP(i,0,adj[s].size())
      |         ^~~
putovanje.cpp: In member function 'void Tree::HLD()':
putovanje.cpp:20:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define REP(i,a,b) for(ll i=a; i<b; i++)
......
  240 |             REP(j,0,sons[i].size())
      |                 ~~~~~~~~~~~~~~~~~~
putovanje.cpp:240:13: note: in expansion of macro 'REP'
  240 |             REP(j,0,sons[i].size())
      |             ^~~
putovanje.cpp: In member function 'ST::SV ST::query(ll, ll, ll)':
putovanje.cpp:116:5: warning: control reaches end of non-void function [-Wreturn-type]
  116 |     }
      |     ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...