#include<iostream> #include<algorithm> #include<queue> #include<list> #include<stdio.h> #include<vector> #include<map> #include<set> using namespace std; #define ull long long int #define FOR(i,n) for(int i=0;i<n;++i) #define FORD(i,n) for(int i=(n)-1;i>=0;--i) #define dcout 0 && cout int inline min(int a,int b){return a>b?b:a;} int inline max(int a,int b){return a<b?b:a;} class R{ public: int i; ull start; int count; R*next; R*previous; ull mergeTime; }; struct CR { bool operator()(const R* a, const R* b) const { return a->mergeTime >= b->mergeTime; } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin>>n>>m; ull*times=new ull[n+1]; ull*bakers=new ull[m]; ull*bakers2=new ull[m]; FOR(i,n)cin>>times[i+1]; FOR(i,m)cin>>bakers[i]; FOR(i,m)bakers2[i]=bakers[i]; map<int,ull> b; times[0]=0; ++n; sort(times,times+n); sort(bakers,bakers+m); priority_queue<R*,vector<R*>,CR> p; char *alive=new char[n]; FOR(i,n)alive[i]=1; R**rr=new R*[n]; R*next=NULL; FORD(i,n){ R*r=new R(); r->i=i; r->start=times[i]; r->count=1; r->next=next; r->mergeTime=1000000000000000LL; if(i+1<n){ r->mergeTime=times[i+1]-times[i]+1; } rr[i]=r; next=r; p.push(r); } R*previous=NULL; FOR(i,n){rr[i]->previous=previous;previous=rr[i];} ull grow=0; ull totalWaited=0; ull time=0; int i=0; while(i<m){ //while(bakers[i]>p.top()->mergeTime){ //while(!alive[p.top()->i])p.pop(); ull moment=p.top()->mergeTime; dcout<<"Trying to advance clock to "<<moment<<endl; ull beforeMoment=moment-1; while(i<m && bakers[i]<=beforeMoment){ dcout<<"Before advancing a baker is telling sth at "<<bakers[i]<<endl; b[bakers[i]]=(totalWaited+(bakers[i]-time)*grow); dcout<<"He says b["<<bakers[i]<<"]="<<(totalWaited+(bakers[i]-time)*grow)<<endl; //cout<<(totalWaited+(bakers[i]-time)*grow)<<" "; dcout<<endl; ++i; } if(i==m)break; totalWaited+=(beforeMoment-time)*grow; dcout<<"totalWaited is now "<<totalWaited<<endl; time=beforeMoment; dcout<<"Current time is "<<time<<endl; totalWaited+=grow; while(moment>=p.top()->mergeTime){ R *first = p.top(); p.pop();while(!p.empty() && !alive[p.top()->i]){dcout<<"Removing dead range"<<endl;p.pop();} ull currentTime = first->mergeTime; //alive[first->i]=0; R*next=first->next; dcout<<"merging range at "<<first->i<<" and "<<next->i<<endl; alive[next->i]=0; dcout<<"Group "<<next->i<<" is dead"<<endl; R*sum=new R(); sum->start=first->start; sum->count=first->count+next->count; sum->next=next->next; sum->previous=first->previous; if(first->previous!=NULL) first->previous->next=sum; if(next->next!=NULL) next->next->previous=sum; sum->i=first->i; if(sum->next==NULL){ sum->mergeTime=100000000000000010LL; } else { sum->mergeTime=(sum->next->start-sum->start)/sum->count+1; } totalWaited+=next->count*(first->start+(first->count)*moment-next->start); dcout<<"By pushing, totalWaited increased to "<<totalWaited<<endl; p.push(sum); grow+= sum->count*(sum->count-1LL)/2 - first->count*(first->count-1LL)/2 - next->count*(next->count-1LL)/2; dcout<<"Each time it grows it grows by "<<grow<<", because first count:"<<first->count <<", next:"<<next->count<<", sum:"<<sum->count<<endl; } time=moment; dcout<<"Current time is now "<<time<<endl; } FOR(i,m)cout<<b[bakers2[i]]<<endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #include<iostream> #include<algorithm> #include<queue> #include<list> #include<stdio.h> #include<vector> #include<map> #include<set> using namespace std; #define ull long long int #define FOR(i,n) for(int i=0;i<n;++i) #define FORD(i,n) for(int i=(n)-1;i>=0;--i) #define dcout 0 && cout int inline min(int a,int b){return a>b?b:a;} int inline max(int a,int b){return a<b?b:a;} class R{ public: int i; ull start; int count; R*next; R*previous; ull mergeTime; }; struct CR { bool operator()(const R* a, const R* b) const { return a->mergeTime >= b->mergeTime; } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin>>n>>m; ull*times=new ull[n+1]; ull*bakers=new ull[m]; ull*bakers2=new ull[m]; FOR(i,n)cin>>times[i+1]; FOR(i,m)cin>>bakers[i]; FOR(i,m)bakers2[i]=bakers[i]; map<int,ull> b; times[0]=0; ++n; sort(times,times+n); sort(bakers,bakers+m); priority_queue<R*,vector<R*>,CR> p; char *alive=new char[n]; FOR(i,n)alive[i]=1; R**rr=new R*[n]; R*next=NULL; FORD(i,n){ R*r=new R(); r->i=i; r->start=times[i]; r->count=1; r->next=next; r->mergeTime=1000000000000000LL; if(i+1<n){ r->mergeTime=times[i+1]-times[i]+1; } rr[i]=r; next=r; p.push(r); } R*previous=NULL; FOR(i,n){rr[i]->previous=previous;previous=rr[i];} ull grow=0; ull totalWaited=0; ull time=0; int i=0; while(i<m){ //while(bakers[i]>p.top()->mergeTime){ //while(!alive[p.top()->i])p.pop(); ull moment=p.top()->mergeTime; dcout<<"Trying to advance clock to "<<moment<<endl; ull beforeMoment=moment-1; while(i<m && bakers[i]<=beforeMoment){ dcout<<"Before advancing a baker is telling sth at "<<bakers[i]<<endl; b[bakers[i]]=(totalWaited+(bakers[i]-time)*grow); dcout<<"He says b["<<bakers[i]<<"]="<<(totalWaited+(bakers[i]-time)*grow)<<endl; //cout<<(totalWaited+(bakers[i]-time)*grow)<<" "; dcout<<endl; ++i; } if(i==m)break; totalWaited+=(beforeMoment-time)*grow; dcout<<"totalWaited is now "<<totalWaited<<endl; time=beforeMoment; dcout<<"Current time is "<<time<<endl; totalWaited+=grow; while(moment>=p.top()->mergeTime){ R *first = p.top(); p.pop();while(!p.empty() && !alive[p.top()->i]){dcout<<"Removing dead range"<<endl;p.pop();} ull currentTime = first->mergeTime; //alive[first->i]=0; R*next=first->next; dcout<<"merging range at "<<first->i<<" and "<<next->i<<endl; alive[next->i]=0; dcout<<"Group "<<next->i<<" is dead"<<endl; R*sum=new R(); sum->start=first->start; sum->count=first->count+next->count; sum->next=next->next; sum->previous=first->previous; if(first->previous!=NULL) first->previous->next=sum; if(next->next!=NULL) next->next->previous=sum; sum->i=first->i; if(sum->next==NULL){ sum->mergeTime=100000000000000010LL; } else { sum->mergeTime=(sum->next->start-sum->start)/sum->count+1; } totalWaited+=next->count*(first->start+(first->count)*moment-next->start); dcout<<"By pushing, totalWaited increased to "<<totalWaited<<endl; p.push(sum); grow+= sum->count*(sum->count-1LL)/2 - first->count*(first->count-1LL)/2 - next->count*(next->count-1LL)/2; dcout<<"Each time it grows it grows by "<<grow<<", because first count:"<<first->count <<", next:"<<next->count<<", sum:"<<sum->count<<endl; } time=moment; dcout<<"Current time is now "<<time<<endl; } FOR(i,m)cout<<b[bakers2[i]]<<endl; return 0; } |