Submission #384942

#TimeUsernameProblemLanguageResultExecution timeMemory
384942kshitij_sodaniAncient Books (IOI17_books)C++14
42 / 100
2048 ms629336 KiB
//#pragma GCC optimize("Ofast,unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long llo; #define mp make_pair #define pb push_back #define a first #define b second #define endl '\n' #include "books.h" llo ans=0; llo n; vector<llo> adj[1000001]; llo vis[1000001]; pair<llo,llo> cot[1000001]; llo par[1000001]; llo find(llo no){ if(par[no]==no){ return no; } par[no]=find(par[no]); return par[no]; } vector<llo> ss; void dfs(int no){ vis[no]=1; ss.pb(no); for(auto j:adj[no]){ if(vis[j]==0){ dfs(j); } } } vector<llo> adj2[1000001]; llo ind[1000001]; pair<llo,llo> pp; vector<pair<llo,llo>> tt; vector<pair<llo,llo>> pre2[1000001]; vector<pair<llo,llo>> pre3[1000001]; llo dp[1001][1001]; pair<llo,llo> rr2[1000001]; pair<llo,llo> rr[1000001]; void dfs2(int no,int par=-1){ pp.a=min(pp.a,tt[no].a); pp.b=max(pp.b,tt[no].b); vis[no]=1; for(auto j:adj2[no]){ if(vis[j]==0){ dfs2(j); } } } vector<llo> kk[1000001]; llo tree[4*100001]; void update(llo no,llo l,llo r,llo i,llo j){ if(l==r){ tree[no]=min(tree[no],j); } else{ int mid=(l+r)/2; if(i<=mid){ update(no*2+1,l,mid,i,j); } else{ update(no*2+2,mid+1,r,i,j); } tree[no]=min(tree[no*2+1],tree[no*2+2]); } } llo query(llo no,llo l,llo r,llo aa,llo bb){ if(r<aa or l>bb){ return n; } if(aa<=l and r<=bb){ return tree[no]; } else{ int mid=(l+r)/2; return min(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb)); } } llo tree2[4*100001]; void update2(llo no,llo l,llo r,llo i,llo j){ if(l==r){ tree2[no]=max(tree2[no],j); } else{ int mid=(l+r)/2; if(i<=mid){ update2(no*2+1,l,mid,i,j); } else{ update2(no*2+2,mid+1,r,i,j); } tree2[no]=max(tree2[no*2+1],tree2[no*2+2]); } } llo query2(llo no,llo l,llo r,llo aa,llo bb){ if(r<aa or l>bb){ return 0; } if(aa<=l and r<=bb){ return tree2[no]; } else{ int mid=(l+r)/2; return max(query2(no*2+1,l,mid,aa,bb),query2(no*2+2,mid+1,r,aa,bb)); } } long long minimum_walk(std::vector<int> p, int s) { while(p.size()){ if(p.back()+1==p.size()){ p.pop_back(); } else{ break; } } n=p.size(); //cout<<p.size()<<":"<<endl; if(n==0){ return 0; } for(int i=0;i<n;i++){ adj[i].pb(p[i]); } //llo co=0; llo ans=0; vector<vector<llo>> zz5; for(int i=0;i<n;i++){ if(vis[i]==0){ ss.clear(); dfs(i); llo mi=ss[0]; llo ma=ss[0]; for(auto j:ss){ mi=min(mi,j); ma=max(ma,j); } for(auto j:ss){ cot[j]={mi,ma}; } llo zz=tt.size(); for(auto j:ss){ ind[j]=tt.size(); } rr2[tt.size()]={mi,ma}; tt.pb({mi,ma}); for(int j=0;j+1<ss.size();j++){ ans+=abs(ss[j]-ss[j+1]); } zz5.pb(ss); ans+=(abs(ss[0]-ss.back())); sort(ss.begin(),ss.end()); for(int j=0;j+1<ss.size();j++){ pre2[ss[j+1]].pb({ss[j],zz}); } for(int j=0;j+1<ss.size();j++){ pre3[ss[j]].pb({ss[j+1],zz}); } } } for(int i=0;i<4*n;i++){ tree[i]=4*n; } for(int i=0;i<n;i++){ for(auto j:pre2[i]){ llo x=query(0,0,n-1,j.a,i-1); //cout<<j.a<<"??"<<i-1<<endl; rr2[j.b].a=min(rr2[j.b].a,x); //cout<<i<<"//"<<rr2[j.b].a<<endl; update(0,0,n-1,i,rr2[j.b].a); //cout<<j.a<<">"<<i<<">"<<j.b<<endl; //cout<<x<<endl; } } for(int i=0;i<4*n;i++){ tree2[i]=0; } for(int i=n-1;i>=0;i--){ for(auto j:pre3[i]){ llo x=query2(0,0,n-1,i+1,j.a); rr2[j.b].b=max(rr2[j.b].b,x); update2(0,0,n-1,i,rr2[j.b].b); //cout<<i<<"<"<<j.a<<"<"<<j.b<<endl; //cout<<x<<endl; } } for(int i=0;i<tt.size();i++){ //cout<<rr2[i].a<<":"<<rr2[i].b<<endl; for(auto j:zz5[i]){ //cout<<j<<"."; rr[j]=rr2[i]; } //cout<<endl; } //cout<<ans<<"::"<<endl; //cout<<ans<<".."<<endl; /*for(int i=0;i<tt.size();i++){ for(int j=0;j<n;j++){ if(ind[j]!=i){ if(j>=tt[i].a and j<=tt[i].b){ adj2[i].pb(ind[j]); //cout<<i<<",,"<<ind[j]<<endl; } } } } for(int j=0;j<tt.size();j++){ for(int i=0;i<tt.size();i++){ vis[i]=0; } pp=tt[j]; dfs2(j); for(int i=0;i<n;i++){ if(ind[i]==j){ rr[i]=pp; } } //cout<<pp.a<<","<<pp.b<<endl; }*/ //cout<<ans<<":"<<endl; pair<llo,llo> cur3={rr[s].a,rr[s].b}; while(cur3.a>0 or cur3.b<n-1){ if(cur3.b==n-1){ ans+=2; cur3={rr[cur3.a-1].a,max(cur3.b,rr[cur3.a-1].b)}; continue; } else if(cur3.a==0){ ans+=2; cur3={min(rr[cur3.b+1].a,cur3.a),rr[cur3.b+1].b}; continue; } else{ if(rr[cur3.a-1].b>cur3.b){ ans+=2; cur3={rr[cur3.a-1].a,max(cur3.b,rr[cur3.a-1].b)}; continue; } else if(rr[cur3.b+1].a<cur3.a){ ans+=2; cur3={min(rr[cur3.b+1].a,cur3.a),rr[cur3.b+1].b}; continue; } vector<pair<llo,llo>> aa; vector<pair<llo,llo>> bb; pair<llo,llo> cur4=cur3; while(cur4.a>0){ if(rr[cur4.a-1].b>cur3.b){ break; } cur4={rr[cur4.a-1].a,max(cur4.b,rr[cur4.a-1].b)}; aa.pb(cur4); } cur4=cur3; while(cur4.b<n-1){ if(rr[cur4.b+1].a<cur3.a){ break; } cur4={min(rr[cur4.b+1].a,cur4.a),rr[cur4.b+1].b}; bb.pb(cur4); } if(aa.size()<bb.size()){ cur3=aa.back(); ans+=2*aa.size(); } else{ cur3=bb.back(); ans+=2*bb.size(); } //cur3={rr[cur3.a-1].a,max(cur3.b,rr[cur3.a-1].b)}; } } return ans; }

Compilation message (stderr)

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:122:16: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |   if(p.back()+1==p.size()){
      |      ~~~~~~~~~~^~~~~~~~~~
books.cpp:163:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  163 |    for(int j=0;j+1<ss.size();j++){
      |                ~~~^~~~~~~~~~
books.cpp:170:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |    for(int j=0;j+1<ss.size();j++){
      |                ~~~^~~~~~~~~~
books.cpp:173:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  173 |    for(int j=0;j+1<ss.size();j++){
      |                ~~~^~~~~~~~~~
books.cpp:206:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  206 |  for(int i=0;i<tt.size();i++){
      |              ~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...