#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<n; ++i) #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<ll,int> pli; typedef vector<pii> vpi; typedef double lf; int n, m; vl t; vpi d; vl res; ll ans, prev_len; priority_queue<pli> q; vi p,s,last_id; vl fst; bool mode; int gr_num; int Find(int x) { //cout<<x<<endl; if(x==p[x]) return x; return p[x]=Find(p[x]); } bool connect(ll len, int id, bool process_mode) { int next=Find(last_id[id]+1); if(t[next]==t[id]) { t[next]++; last_id[id]=next; s[id]+=s[next]; p[next]=id; ++ans; --gr_num; return true; } ll id_end; if(process_mode) { id_end=len*(ll)s[id]; } else id_end=fst[id]+len*((ll)s[id]-1); ll next_beg = fst[next]-len; if(id_end<next_beg) { ll next_len; if(process_mode) { next_len=llround(ceil((lf)fst[next]/(lf)((lf)s[id]+1.0))); } else { next_len=llround(ceil((lf)(fst[next]-fst[id])/(lf)s[id])); } q.push(mp(-next_len,-id)); //cout<<"added event from id="<<id<<" and next_len="<<next_len<<endl; return false; } ans+=(id_end-next_beg)*(ll)s[next]; p[next]=id; last_id[id]=last_id[next]; s[id]+=s[next]; gr_num--; //cout<<"connected "<<id<<" and "<<next<<" with len="<<len<<" and process_mode="<<process_mode<<endl; return true; } void process(ll len, int id) { if(Find(id)!=id) return; bool process_mode = false; if(!mode && id==0 && len==t[0]) { mode=true; } if(mode && id==0) process_mode=true; //cout<<id<<" "<<last_id[id]<<" <--"<<endl; while(last_id[id]+1<n) { //cout<<last_id[id]<<endl; if(!connect(len,id,process_mode)) break; } } int main() { cin>>n>>m; t.resize(n); rep(i,n) cin>>t[i]; d.resize(m); rep(i,m) cin>>d[i].st,d[i].nd=i; sort(d.begin(), d.end()); res.resize(m); ans=prev_len=0; gr_num=n; mode=false; fst=t; p.resize(n); s.resize(n,1); rep(i,n) p[i]=i; last_id=p; q.push(mp(-t[0],0)); ll bef=t[0]; for(int i=1; i<n; ++i) { q.push(mp(-(t[i]-bef),-(i-1))); bef=t[i]; } int idx=0; while(!q.empty()) { pli pr=q.top(); q.pop(); ll len=-pr.st; int id =-pr.nd; //cout<<len<<" "<<id<<endl; while(idx<m && d[idx].st<len) { //cout<<idx<<endl; ll cur=d[idx].st; res[d[idx].nd]=ans+(cur-prev_len)*(n-gr_num+mode); ++idx; } if(idx==m) break; ans+=(len-prev_len)*(n-gr_num+mode); process(len,id); prev_len=len; } while(idx<m) { ll cur=d[idx].st; res[d[idx].nd]=ans+(cur-prev_len)*(n-gr_num+mode); ++idx; } rep(i,m) cout<<res[i]<<"\n"; 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<n; ++i) #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<ll,int> pli; typedef vector<pii> vpi; typedef double lf; int n, m; vl t; vpi d; vl res; ll ans, prev_len; priority_queue<pli> q; vi p,s,last_id; vl fst; bool mode; int gr_num; int Find(int x) { //cout<<x<<endl; if(x==p[x]) return x; return p[x]=Find(p[x]); } bool connect(ll len, int id, bool process_mode) { int next=Find(last_id[id]+1); if(t[next]==t[id]) { t[next]++; last_id[id]=next; s[id]+=s[next]; p[next]=id; ++ans; --gr_num; return true; } ll id_end; if(process_mode) { id_end=len*(ll)s[id]; } else id_end=fst[id]+len*((ll)s[id]-1); ll next_beg = fst[next]-len; if(id_end<next_beg) { ll next_len; if(process_mode) { next_len=llround(ceil((lf)fst[next]/(lf)((lf)s[id]+1.0))); } else { next_len=llround(ceil((lf)(fst[next]-fst[id])/(lf)s[id])); } q.push(mp(-next_len,-id)); //cout<<"added event from id="<<id<<" and next_len="<<next_len<<endl; return false; } ans+=(id_end-next_beg)*(ll)s[next]; p[next]=id; last_id[id]=last_id[next]; s[id]+=s[next]; gr_num--; //cout<<"connected "<<id<<" and "<<next<<" with len="<<len<<" and process_mode="<<process_mode<<endl; return true; } void process(ll len, int id) { if(Find(id)!=id) return; bool process_mode = false; if(!mode && id==0 && len==t[0]) { mode=true; } if(mode && id==0) process_mode=true; //cout<<id<<" "<<last_id[id]<<" <--"<<endl; while(last_id[id]+1<n) { //cout<<last_id[id]<<endl; if(!connect(len,id,process_mode)) break; } } int main() { cin>>n>>m; t.resize(n); rep(i,n) cin>>t[i]; d.resize(m); rep(i,m) cin>>d[i].st,d[i].nd=i; sort(d.begin(), d.end()); res.resize(m); ans=prev_len=0; gr_num=n; mode=false; fst=t; p.resize(n); s.resize(n,1); rep(i,n) p[i]=i; last_id=p; q.push(mp(-t[0],0)); ll bef=t[0]; for(int i=1; i<n; ++i) { q.push(mp(-(t[i]-bef),-(i-1))); bef=t[i]; } int idx=0; while(!q.empty()) { pli pr=q.top(); q.pop(); ll len=-pr.st; int id =-pr.nd; //cout<<len<<" "<<id<<endl; while(idx<m && d[idx].st<len) { //cout<<idx<<endl; ll cur=d[idx].st; res[d[idx].nd]=ans+(cur-prev_len)*(n-gr_num+mode); ++idx; } if(idx==m) break; ans+=(len-prev_len)*(n-gr_num+mode); process(len,id); prev_len=len; } while(idx<m) { ll cur=d[idx].st; res[d[idx].nd]=ans+(cur-prev_len)*(n-gr_num+mode); ++idx; } rep(i,m) cout<<res[i]<<"\n"; return 0; } |