Submission #284073

#TimeUsernameProblemLanguageResultExecution timeMemory
284073PedroBigManPutovanje (COCI20_putovanje)C++14
0 / 110
440 ms166188 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; 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;} }; SV upval(ll c) //how lazy values modify a seg value inside a node, c=current node { SV X(p[c].a+(range[c].ss-range[c].ff+1)*lazy[c].a); return X; } SV neuts; LV neutl; vector<SV> ar; vector<SV> p; vector<LV> lazy; vector<pl> range; ST() {N=0LL;} ST(vector<ll> arr) { N = (ll) 1<<(ll) ceil(log2(arr.size())); REP(i,0,2*N) {range.pb(mp(0LL,0LL));} REP(i,0,arr.size()) {SV X(arr[i]); ar.pb(X);} REP(i,arr.size(),N) {ar.pb(neuts);} REP(i,0,N) {p.pb(neuts);} REP(i,0,N) {p.pb(ar[i]); range[i+N]=mp(i,i);} ll cur = N-1; while(cur>0) { p[cur]=p[2*cur]&p[2*cur+1]; range[cur]=mp(range[2*cur].ff,range[2*cur+1].ss); cur--; } REP(i,0,2*N) {lazy.pb(neutl);} } void prop(ll c) //how lazy values propagate { p[c]=upval(c); lazy[2*c]=lazy[c]&lazy[2*c]; lazy[2*c+1]=lazy[c]&lazy[2*c+1]; lazy[c]=neutl; if(2*c>=N) { p[2*c]=upval(2*c); p[2*c+1]=upval(2*c+1); ar[2*c-N]=p[2*c]; ar[2*c+1-N]=p[2*c+1]; lazy[2*c]=neutl; lazy[2*c+1]=neutl; } } SV query(ll a,ll b, ll c=1LL) //range [a,b], current node. initially: query(a,b,1) { ll x=range[c].ff; ll y=range[c].ss; if(y<a || x>b) {return neuts;} if(x>=a && y<=b) {return upval(c);} prop(c); SV ans = query(a,b,2*c)&query(a,b,2*c+1); return ans; } 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) { ll x=range[c].ff; ll y=range[c].ss; if(y<a || x>b) {return ;} if(x>=a && y<=b) { lazy[c]=s&lazy[c]; if(c>=N) {p[c]=upval(c); lazy[c]=neutl; ar[c-N]=p[c];} return; } update(s,a,b,2*c); update(s,a,b,2*c+1); p[c]=upval(2*c)&upval(2*c+1); } }; 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; vector<ll> dif_arr; vector<ll> arr; 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];} ll las=0LL; REP(i,0,st_val.size()) {dif_arr.pb(st_val[i]-las); las=st_val[i];} } ll pos(ll x) { return (T->level[low]-T->level[x]); } ll query(ll ind1, ll ind2) { if(arr.size()==0) { ll sum=0LL; REP(i,0,dif_arr.size()) {sum+=dif_arr[i]; arr.pb(sum);} } ll ans=0LL; REP(i,min(ind1,ind2),max(ind1,ind2)+1) {ans+=arr[i];} return ans; } void update(ll X, ll ind1, ll ind2) { if(ind1>ind2) {swap(ind1,ind2);} dif_arr[ind1]+=X; if(ind2<dif_arr.size()-1) {dif_arr[ind2+1]-=X;} } }; vector<HeavyPath *> h_path; //heavy paths unordered_map<ll,HeavyPath *> HP; //m[s] = heavy path including s void HLD() { vector<ll> large; ll c; REP(i,0,N) { ll node=-1; ll ls = -1LL; REP(j,0,sons[i].size()) { c=sons[i][j]; if(sub[c]>=ls) {ls=sub[c]; node=c;} } large.pb(node); } REP(i,0,N) { if(sons[i].size()>0) {continue;} c=i; while(c!=root && c==large[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); } } ll query_ancestor(ll s, ll anc) { ll ans; if(level[s]<level[anc]) {return ans;} ll c = s; while(1>0) { ll 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; } ll query(ll a, ll b) //query along path a->b { if(a==b) {return query_ancestor(a,a);} ll lca = LCA(a,b); ll V1 = query_ancestor(a,lca); ll V2 = query_ancestor(b,lca); ll V3 = query_ancestor(lca,lca); return (V1+V2-V3); } void update_ancestor(ll 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(ll X, ll a, ll b) { ll lca = LCA(a,b); update_ancestor(X,a,lca); update_ancestor(X,b,lca); update_ancestor(-2LL*X,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) { T.update(1LL,i,i+1); } vector<ll> use; REP(i,0,N) {if(i==0) {use.pb(0);} else{use.pb(T.query(i,i));}} Out(use); 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++)
......
  122 |         REP(i,0,arr.size()) {SV X(arr[i]); ar.pb(X);}
      |             ~~~~~~~~~~~~~~       
putovanje.cpp:122:9: note: in expansion of macro 'REP'
  122 |         REP(i,0,arr.size()) {SV X(arr[i]); ar.pb(X);}
      |         ^~~
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++)
......
  200 |         REP(i,0,DFSarr2.size()) {pos[DFSarr2[i]]=i;}
      |             ~~~~~~~~~~~~~~~~~~   
putovanje.cpp:200:9: note: in expansion of macro 'REP'
  200 |         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++)
......
  201 |         REP(i,0,DFSarr2.size()) {levDFSarr.pb(mp(level[DFSarr2[i]],DFSarr2[i]));}
      |             ~~~~~~~~~~~~~~~~~~   
putovanje.cpp:201:9: note: in expansion of macro 'REP'
  201 |         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++)
......
  218 |         REP(i,0,adj[s].size())
      |             ~~~~~~~~~~~~~~~~~    
putovanje.cpp:218:9: note: in expansion of macro 'REP'
  218 |         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++)
......
  231 |         REP(i,0,adj[s].size())
      |             ~~~~~~~~~~~~~~~~~    
putovanje.cpp:231:9: note: in expansion of macro 'REP'
  231 |         REP(i,0,adj[s].size())
      |         ^~~
putovanje.cpp: In constructor 'Tree::HeavyPath::HeavyPath(ll, ll, Tree*)':
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++)
......
  265 |             REP(i,0,st_val.size()) {dif_arr.pb(st_val[i]-las); las=st_val[i];}
      |                 ~~~~~~~~~~~~~~~~~
putovanje.cpp:265:13: note: in expansion of macro 'REP'
  265 |             REP(i,0,st_val.size()) {dif_arr.pb(st_val[i]-las); las=st_val[i];}
      |             ^~~
putovanje.cpp: In member function 'll Tree::HeavyPath::query(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++)
......
  278 |                 REP(i,0,dif_arr.size()) {sum+=dif_arr[i]; arr.pb(sum);}
      |                     ~~~~~~~~~~~~~~~~~~
putovanje.cpp:278:17: note: in expansion of macro 'REP'
  278 |                 REP(i,0,dif_arr.size()) {sum+=dif_arr[i]; arr.pb(sum);}
      |                 ^~~
putovanje.cpp: In member function 'void Tree::HeavyPath::update(ll, ll, ll)':
putovanje.cpp:289:20: 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]
  289 |             if(ind2<dif_arr.size()-1) {dif_arr[ind2+1]-=X;}
      |                ~~~~^~~~~~~~~~~~~~~~~
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++)
......
  302 |             REP(j,0,sons[i].size())
      |                 ~~~~~~~~~~~~~~~~~~
putovanje.cpp:302:13: note: in expansion of macro 'REP'
  302 |             REP(j,0,sons[i].size())
      |             ^~~
putovanje.cpp: In instantiation of 'void Out(std::vector<_Tp>) [with A = long long int]':
putovanje.cpp:403:12:   required from here
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++)
putovanje.cpp:31:24: note: in expansion of macro 'REP'
   31 | void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}
      |                        ^~~
putovanje.cpp: In function 'int main()':
putovanje.cpp:322:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
  322 |         ll ans;
      |            ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...